jeudi 16 juin 2016

How to create datasets dynamically for chart.js Line chart?


I want to create the Line chart with multiple datasets dynamically in the chart.js library.

I can able to assign tha data dynamically. But I want to create the datasets itself dynamically. I saw the below link

How to add the elements dynamically from the html table for chart.js

and tried like

var datasetValue = [];
for (var j = 0; j < count; j++) {
datasetValue[j] = [
{
fillColor: 'rgba(220,220,220,0.5)',
strokeColor :'rgba(220,220,220,1)' ,
title :'2013',
data : [Math.round(Math.random() * 100),Math.round(Math.random() * 100)-10]
}]
}

var mydata = {
datasets : datasetValue
}

Here the count value is 3 and i want to display the 3 lines in the chart and the count value will get vary. Even though the chart line color and title will be the same i want to display the line first i can change that as dynamic once i solved this.

I tried to access the data like

alert("Datasets:"+mydata.datasets[0].data);

It should show the data of first datasets but its showing undefined.

but In the below case

var mydata1 = {
labels : ["January","February","March","April","May","June","July"],
datasets : [
    {
        fillColor : "rgba(220,220,220,0.5)",
        strokeColor : "rgba(220,220,220,1)",
        data : [95,53,99,73,27,82,40],
        title : "2014"
    },
    {
        fillColor : "rgba(151,187,205,0.5)",
        strokeColor : "rgba(151,187,205,1)",
        data : [35,43,59,31,50,66,55],
        title : "2013"
    }
 ]
}

alert("Datasets:"+mydata1.datasets[0].data);

I can able to get the data of first dataset. Could you please anyone give me the solution?


Aucun commentaire:

Enregistrer un commentaire