将数据集循环到 chart.js?

Loop datasets into chart.js?

如何将这两个数据集添加到循环数组的图表中? 我已经添加了一个数据集,但是在尝试添加一个循环来添加两个数据集时,我不知道如何让它工作。

<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.4/Chart.js"></script>

<canvas id="myChart" style="width:100%;max-width:700px"></canvas>

<script>
const datasetValue = []

    datasetValue[0] = {
      data: [860,1140,1060,1060,1070,1110,1330,2210,7830,2478],
      borderColor: "red",
      fill: false
    }

    datasetValue[1] = {
      data: [1600,1700,1700,1900,2000,2700,4000,5000,6000,7000],
      borderColor: "green",
      fill: false
    }

var xValues = [100,200,300,400,500,600,700,800,900,1000];

new Chart("myChart", {
  type: "line",
  data: {
    labels: xValues,
    datasets: [{
      data: datasetValue[0].data,
      borderColor: datasetValue[0].borderColor,
      fill: datasetValue[0].fill        
    }]
  },
  options: {
    legend: {display: false}
  }
});

</script>

只需将 datasetValues 变量直接传递到数据集字段即可:

const datasetValue = []

datasetValue[0] = {
  data: [860, 1140, 1060, 1060, 1070, 1110, 1330, 2210, 7830, 2478],
  borderColor: "red",
  fill: false
}

datasetValue[1] = {
  data: [1600, 1700, 1700, 1900, 2000, 2700, 4000, 5000, 6000, 7000],
  borderColor: "green",
  fill: false
}

var xValues = [100, 200, 300, 400, 500, 600, 700, 800, 900, 1000];

new Chart("myChart", {
  type: "line",
  data: {
    labels: xValues,
    datasets: datasetValue
  },
  options: {
    legend: {
      display: false
    }
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.4/Chart.js"></script>

<canvas id="myChart" style="width:100%;max-width:700px"></canvas>