【问题标题】:How to bind data to line chart in highcharts in MVC3?如何将数据绑定到 MVC3 中的 highcharts 中的折线图?
【发布时间】:2012-08-29 11:46:46
【问题描述】:

大家好,我已经存储了这样的输出数据

var loggedbugs

    projectName ProjectYear ProjectMonth    Week1   Week2   Week3   Week4 Week5
    Ecommerce       2012           8          0      1       4       3    0

var loggedbugs

    projectName ProjectYear ProjectMonth    Week1   Week2   Week3   Week4 Week5 
    Ecommerce       2012           8          2      2       8       3    0

我在我的 MVC 应用程序中调用此存储过程并将此数据作为 Json 像这样返回

    public ActionResult Index()
    {
        return View();
    }
    public JsonResult CreatedBugs()
    {
        int year;
        int month;
        int projectid;
        year = 2012;
        month = 8;
        projectid = 16;       
        var loggedbugs = db.ExecuteStoreQuery<LoggedBugs>("LoggedBugs @Year,@Month,@ProjectID", new SqlParameter("@Year", year), new SqlParameter("@Month", month), new SqlParameter("@ProjectID", projectid)).ToList();
       var ClosedBugs = db.ExecuteStoreQuery<LoggedBugs>("ClosedBugs @Year,@Month,@ProjectID", new SqlParameter("@Year", year), new SqlParameter("@Month", month), new SqlParameter("@ProjectID", projectid)).ToList();
        var model = new LoggedBugs
        {
            LoggedBugsCount = loggedbugs,
           ClosedBugs = ClosedBugs
        };
        return Json(model, JsonRequestBehavior.AllowGet);
    }  

model 在这里向我返回记录计数两个...所以现在我要做的是...此数据应该绑定到线图,其中 LoggedBugsCount 应该有不同的行,而 ClosedBugs 应该有不同的行... 周数应该在 X 轴上,y 轴应该有计数....

任何人都可以在这里帮助我如何在 highcharts 中绑定此数据折线图。这是我现在正在尝试的,但没有结果

   <script type="text/javascript">
    $(document).ready(function () {
        alert(1);
        $.getJSON('<%= Url.Action("CreatedBugs","WeeklyLoggedBugs") %>', {}, function (data) {
            var json = data;
            alert(data);
            var loggedbugs = [];
            var closedbugs = [];
            for (var i in json) {
                // var serie = new Array(json[i].Projects, json[i].Bugs);
                //jsondata.push([json[i].projectName, json[i].ProjectYear, json[i].ProjectMonth, json[i].Week1, json[i].Week2, json[i].Week3, json[i].Week4, json[i].Week5]);
                loggedbugs.push([json[i].LoggedBugsCount]);
                closedbugs.push([json[i].ClosedBugs]);

            }
            chart.series[0].data = loggedbugs;
            chart.series[1].data = closedbugs;
            var chart;
            chart = new Highcharts.Chart({
                chart: {
                    renderTo: 'container',
                    type: 'line'
                },
                title: {
                    text: 'Daily Reports'
                },
                subtitle: {
                    text: 'Logged Bugs'
                },
                xAxis: {
                    categories: ['Week1', 'Week2', 'Week3', 'Week4', 'Week5']
                },
                yAxis: {
                    title: {
                        text: 'Temperature (°C)'
                    }
                },
                tooltip: {
                    enabled: false,
                    formatter: function () {
                        return '<b>' + this.series.name + '</b><br/>' +
                    this.x + ': ' + this.y + '°C';
                    }
                },
                plotOptions: {
                    line: {
                        dataLabels: {
                            enabled: true
                        },
                        enableMouseTracking: false
                    }
                },
                series: [{
                    type: 'line',
                    name: 'Logged Bugs'

                },
                    {
                        type: 'line',
                        name: 'ClosedBugs'

                    }]
            });
        });
    });       
</script>

【问题讨论】:

  • 你盲目复制了例子。它仍然在您的 y 轴标题中显示“温度 (°C)”。其次,你得到什么错误?可能带有示例数据的小提琴(js.fiddle.net)会更有帮助?
  • @HardikMishra 当我构建应用程序时,我的页面上没有任何内容...是的,我必须更改那里的文本
  • :下面的回答对你有帮助吗?

标签: asp.net-mvc-3 highcharts linechart


【解决方案1】:

看这里

chart.series[0].data = loggedbugs;
chart.series[1].data = closedbugs;
var chart;
chart = new Highcharts.Chart({
      ........
});

首先,您在创建图表甚至定义图表变量之前向系列添加数据。

其次,您可以使用以下方法串行设置数据:

series: [{
       name: 'Logged Bugs',
       data: loggedbugs

},
{
      name: 'ClosedBugs',
      data: closedbugs
}]

所以,你的活动不需要

chart.series[0].data = loggedbugs;
chart.series[1].data = closedbugs;

示例如下:http://jsfiddle.net/mhardik/JRq7Z/

编辑: 我不知道 asp.net MVC3

检查您是否正在获取数据。如果您使用的是 FF,请使用 console.log() 在控制台中打印响应。

for (var i in json) {
         loggedbugs.push([json[i].LoggedBugsCount]);
         closedbugs.push([json[i].ClosedBugs]);

 }
 // Check
 console.log(loggedbugs); console.log(closedbugs);

【讨论】:

  • 那么我如何在 var loggedbugs = [] 中获取我的数据; var closedbugs= [];我需要将我的数据填充到这个变量中
  • @Anil:与您在问题中提供的方式相同。我不知道 asp.net mvc3
猜你喜欢
  • 1970-01-01
  • 2012-07-25
  • 1970-01-01
  • 2015-08-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多