如何将数据绑定到线图在MVC3 highcharts?(How to bind data to li

2019-07-31 06:35发布

大家好我有StoredProcedure的这哪里获得这样的输出数据

是loggedbugs

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

是loggedbugs

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

我把这个StoredProcedure的在我的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);
    }  

模型返回我的记录数的双在这里......所以现在我想要做的是......这个数据应该绑定到线型图,其中LoggedBugsCount应该有不同的线路和ClosedBugs应该有不同的行...和周应该是x轴和y轴应该具有计数....

任何一个可以帮助我在这里如何highcharts..this这个数据线图表绑定是什么,我想现在,但没有结果

   <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>

Answer 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

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

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

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


文章来源: How to bind data to line chart in highcharts in MVC3?