循环数据集ChartJS Javascript

时间:2016-07-17 13:06:00

标签: javascript jquery arrays chart.js

我从API中获取一组数据集,返回以下内容:

[
"[0,0,0,0,0,0,0,0,0,0,0,0,0,58,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]",
"[0,0,53,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]",
"[0,0,381,0,0,649,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]"
]

我需要在Chart JS Linechart中迭代上面的每一个,如下所示:

 var lineChartData = {
labels: ['1', '2', '3', '4', '5', '6', '7','8', '9', '10', '11', '12', '13', '14','15', '16', '17', '18', '19', '20', '21','22', '23', '24', '25', '26', '27','28','29','30','31'],
datasets: [{
  label: 'My First dataset',
  fillColor: 'rgba(220,220,220,0.2)',
  strokeColor: 'rgba(220,220,220,1)',
  pointColor: 'rgba(220,220,220,1)',
  pointStrokeColor: '#fff',
  pointHighlightFill: '#fff',
  pointHighlightStroke: 'rgba(220,220,220,1)',
  data: dataset[0]
}, {
  label: 'My Second dataset',
  fillColor: 'rgba(151,187,205,0.2)',
  strokeColor: 'rgba(151,187,205,1)',
  pointColor: 'rgba(151,187,205,1)',
  pointStrokeColor: '#fff',
  pointHighlightFill: '#fff',
  pointHighlightStroke: 'rgba(151,187,205,1)',
  data: dataset[1]
},
{
  label: 'My Third dataset',
  fillColor: 'rgba(151,187,205,0.2)',
  strokeColor: 'rgba(151,187,205,1)',
  pointColor: 'rgba(151,187,205,1)',
  pointStrokeColor: '#fff',
  pointHighlightFill: '#fff',
  pointHighlightStroke: 'rgba(151,187,205,1)',
  data: dataset[2]
}]

从API获取的数据集数量是可变的,因此我可能需要在折线图上使用x个数据集。

如何在数据集中循环数组?

我想到的逻辑:

var lineChartData = {
labels : [1,2,3..]
for(i=0;i<array.length;i++)
{
     datasets : [{
     data : array[i]}]
}

Nina帮助后的新更新代码:

var xmlhttp = new XMLHttpRequest();
var url = "http://www.autosys.xyz/api/v1/getAllEmpDailyData?clientid=132617";

xmlhttp.onreadystatechange = function() {
    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
        var myArr = JSON.parse(xmlhttp.responseText);
        myFunction(myArr);
    }
};
xmlhttp.open("GET", url, true);
xmlhttp.send();

function myFunction(arr) {

  var lineChartData = { 
    labels: ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30', '31'], 
    datasets: [{ 
      label: 'My First dataset', 
      fillColor: 'rgba(220,220,220,0.2)', 
      strokeColor: 'rgba(220,220,220,1)', 
      pointColor: 'rgba(220,220,220,1)', 
      pointStrokeColor: '#fff', 
      pointHighlightFill: '#fff', 
      pointHighlightStroke: 'rgba(220,220,220,1)', 
      data: null }, 
      { 
        label: 'My Second dataset', 
        fillColor: 'rgba(151,187,205,0.2)', 
        strokeColor: 'rgba(151,187,205,1)', 
        pointColor: 'rgba(151,187,205,1)', 
        pointStrokeColor: '#fff', 
        pointHighlightFill: '#fff', 
        pointHighlightStroke: 'rgba(151,187,205,1)', 
        data: null }, 
        { 
          label: 'My Third dataset', 
          fillColor: 'rgba(151,187,205,0.2)', 
          strokeColor: 'rgba(151,187,205,1)', 
          pointColor: 'rgba(151,187,205,1)', 
          pointStrokeColor: '#fff', 
          pointHighlightFill: '#fff', 
          pointHighlightStroke: 'rgba(151,187,205,1)', 
          data: null }] },
    array = arr;

array.forEach(function (a, i) {
    lineChartData.datasets[i].data = JSON.parse(a);
});

console.log(lineChartData);

2 个答案:

答案 0 :(得分:4)

您可以使用循环(Array#forEach)并分配数组的已解析项(JSON.parse)。

var lineChartData = { labels: ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30', '31'], datasets: [] },
    array = ["[0,0,0,0,0,0,0,0,0,0,0,0,0,58,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]", "[0,0,53,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]", "[0,0,381,0,0,649,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]"];

array.forEach(function (a, i) {
    lineChartData.datasets.push({
        label: 'Label ' + i,
        fillColor: 'rgba(220,220,220,0.2)',
        strokeColor: 'rgba(220,220,220,1)',
        pointColor: 'rgba(220,220,220,1)',
        pointStrokeColor: '#fff',
        pointHighlightFill: '#fff',
        pointHighlightStroke:
        'rgba(220,220,220,1)',
        data: JSON.parse(a)
    });
});

console.log(lineChartData);

答案 1 :(得分:0)

我在Nina Scholz的帮助下得到了答案。

function getDataset(index, data) { 
return { 
label: 'Label '+ index, 
fillColor: 'rgba(220,220,220,0.2)', 
strokeColor: 'rgba(220,220,220,1)', 
pointColor: 'rgba(220,220,220,1)', 
pointStrokeColor: '#fff', 
pointHighlightFill: '#fff', 
pointHighlightStroke: 'rgba(220,220,220,1)', 
data: data 
}; 
}


array.forEach(function (a, i) { 
lineChartData.datasets.push(getDataset(i,JSON.parse(a))); 
});

console.log(lineChartData);

在父函数中创建了一个函数getDataset,并使用array.forEach函数对其进行迭代。

感谢Nina的帮助。