Chart.js Bar chart load data based on Label

2019-07-26 05:49发布

问题:

I'm using Chart.js for bar chart. I need to display 12 months data in bar chart. So in 2016 year i have only feb,mar and apr i have data. In label im loading all 12 months with year example Jan2016 to Dec2016.

var monthNames = ["Jan", "Feb", "Mar", "Apr", "May", "Jun",
 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
    ];
    var startmonth = ["Dec", "Nov", "Oct", "Sep", "Aug", "Jul",
"Jun", "May", "Apr", "Mar", "Feb", "Jan"
    ];
var itemMonths = [];
 var start;
        var end = 11;
        var month;
        var year;

        var date = new Date();

        month = date.getMonth();
        year = parseInt(Result[0].yearfromdb); //getting createddate year from db
        start = 0;
        for (var i = 0; i < 12; i++) {
            var months = monthNames[start];
            itemMonths.push(months + year);
            start = start + 1;
            if (start == 12) {
                start = 0;
                year = year + 1;
            }
        }

 for (var i in Result)
        {


            itemCountList.push( Result[i].Counts );// Result is List from Db contains Feb,mar and apr total counts..


        }

 var mybarChart = null;
        var ctx = document.getElementById("mybarChart");

         mybarChart = new Chart(ctx, {
            type: 'bar',
            data: {
                labels: itemMonths
                ,
                datasets: [{
                    label: 'Total Count',
                    backgroundColor: "#26B99A",
                    data: itemCountList
                }]
            },

            options: {
                scales: {
                    yAxes: [{
                        ticks: {
                            beginAtZero: true,
                            steps: 10,
                            stepValue: 5,
                            max: 100

                        }
                    }],
                    xAxes: [{

                        steps: 10,
                        stepValue: 5,
                        max: 12
                    }]

                }
            }
         });

My problem is , data is loading from jan,feb,march instead of feb,mar,apr.... How to do this? please help me...

回答1:

 var Result=[];
        Result.push({Months:"Feb",Years:2016,Counts:6});
        Result.push({Months:"Mar",Years:2016,Counts:1});
        Result.push({Months:"Apr",Years:2016,Counts:1});
        for(var j in itemMonths)
        {
         for(var i in Result)
        {
        if(itemMonths[j]==Result[i].Months+Result[i].Years)
        {

     itemCountList[j]=Result[i].Counts;
        //itemCountList.push( Result[i].Counts )
        }

        }
        }

Try this...



回答2:

Your start depends on year, so:

year = parseInt(Result[0].yearfromdb); //getting createddate year from db
start = (year === 2016? 1 : 0);


回答3:

The issue with your code is that the count of the data points is not equal to the count of the labels. Due to this chart is plotting the available 3 data points at right from the start of the x axis labels.

To fix this you can make your data points count same as the data labels count.

Try this , here i am checking if the data point is available for a given month in the Result array id yes then insert that value in the data point array else insert a 0 value.

for (var i = 0; i < 12; i++) {
   var months = monthNames[start];
   var monthValue = 0;
   itemMonths.push(months + year);
   start = start + 1;
   if (start == 12) {
     start = 0;
     year = year + 1;
   }

   var dataObj = $.grep(Result, function(a) {
     return a.Months == months
   })[0];
   var monthValue = dataObj !== undefined ? dataObj.Counts : 0;
   itemCountList.push(monthValue)
 }

https://jsfiddle.net/6u7a1dm4/