How to bind data to line chart in highcharts in MV

2020-04-21 06:06发布

Hi all i have storedprocedure which where i get the output data like this

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

and i call this storedprocedure in my MVC application and return this data as Json like this

    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 return me record count two here...so now what i want to do is ...this data should be binded to linechart where LoggedBugsCount should have a different line and ClosedBugs should have a different line... and weeks should be on Xaxis and y axis should have the count....

can any one help me here in how to bind this data line chart in highcharts..this is what i am trying for now but there is no result

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

1条回答
Deceive 欺骗
2楼-- · 2020-04-21 06:35

See here

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

First, You are adding data to series before creating Chart and even defining chart variable.

Second, You can set data in series using:

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

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

So, You event don't need

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

Here is the example: http://jsfiddle.net/mhardik/JRq7Z/

EDIT: I dont know asp.net MVC3

Check whether you are getting data. Print response in console using console.log() if you are using FF.

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

 }
 // Check
 console.log(loggedbugs); console.log(closedbugs);
查看更多
登录 后发表回答