javascript - How can i iterate or get all object out of array with object key in Chart.js -


i working on making graph in chart.js, problem have , array objects 0: {y: 17854.69, y2: 14283.75, x: thu aug 17 2017 02:00:00 gmt+0200 (romance daylight time)} 1: {y: 41750.08, y2: 33400.06, x: wed aug 16 2017 02:00:00 gmt+0200 (romance daylight time)}

my graph have 2 y axes different data , x axes dates. chardata looks this.

var chartdata = {                 labels: dates,                 datasets: [{                         yaxisid: "funding",                         label: "credorax dk funding",                         data: [{                                 y: credoraxdk[i].y,                                 x: credoraxdk[i].x                          }],                         position: "left",                     },                     {                         yaxisid: "release",                         label: "credorax dk release",                         data: [{                                 y: credoraxdk[i].y2,                                 x: credoraxdk[i].x                          }],                         position: "right",                     }, 

when try this, first object key out in array. there way can iterate through array given keys ?

my new chart looks this

                var ctx = document.getelementbyid("likviditetchart").getcontext("2d");             var chart = new chart(ctx, {                 type: "line",                 data: chartdata,                  options: {                     stacked: false,                     responsive: true,                     onhover: [],                     tooltips: {                         mode: "index",                         intersect: false                     },                     scales: {                         yaxes: [{                                 ticks: {                                     max: 100000,                                     min: 0,                                 },                                 type: "linear",                                 display: true,                                 title: "funding",                                 id: "funding",                                 position: "left",                              }, {                                 ticks: {                                     max: 100000,                                     min: 0,                                 },                                 type: "linear",                                 display: true,                                 title: "release",                                 id: "release",                                 position: "right",                             }]                     }                 }             }); 

picture of graph. can see there 2 points on graph, , have 6 objects in array, when write chart data this, chart.js puts out first object in array

hope problem can solved somehow.

if understood correctly, mean;

if have created chartdata, can acces dataset follows:

chartdata = {     datasets: [{               yaxisid: "funding",               label: "credorax dk funding",               data: [],               position: "left",         },         {               yaxisid: "release",               label: "credorax dk release",               data: [],               position: "right",         }    ,//other config, etc...   };  //init chart config var ctx = document.getelementbyid("likviditetchart").getcontext("2d"); var chart = new chart(ctx, chartdata);  //itterate data , add datasets (var = 0; < credoraxdk.length;i++){     chartdata.datasets[0].data.push({                             y: credoraxdk[i].y,                             x: credoraxdk[i].x});     chartdata.datasets[1].data.push({                             y: credoraxdk[i].y2,                             x: credoraxdk[i].x}); }  //update  new data. chart.update(); 

so basically, set chart config first, , add chart-data data array later.

don't forget call chart.update(); function when done.


Comments

Popular posts from this blog

python - Operations inside variables -

Generic Map Parameter java -

arrays - What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it? -