1
votes

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

3
could you create a working example of your issue ?Deep
the fiddle is not working , you will have to provide the value of Result to make it workingDeep

3 Answers

1
votes
 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...

0
votes

Your start depends on year, so:

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

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/