Chart.js的标签和数据集表中的编码

问题描述 投票:0回答:1

嗨,我想在标签内使用while循环来显示小时,我想在数据集中使用while循环显示我的数组,

我的问题是我无法在标签和我尝试过的数据集内进行编码

label: [{CODE}]
datasets: [{CODE}]

但是它确实能使Charts.js上的文档无法工作

        var myChartObject = document.getElementById('myChartHistory');
        var chart = new Chart(myChartObject,{
            type: 'line',
            data: {
                labels: [//WANT TO CODE HERE],
                datasets: [{
                    label: "GATEWAY1",
                    fill: true,
                    backgroundColor: 'rgba(255, 99, 132, 0.2)',
                    borderColor: 'rgba(255, 99, 132, 1)',
                    data: [//WANT TO CODE HERE], 
  }

JavaScript代码

javascript chart.js
1个回答
0
投票

似乎您想在将labels传递给chart.js之前对其进行格式设置>

您可以按照以下方式进行操作:

//Below are the original Labels
var labels = ['Red', 'Blue', 'Yellow', 'Green', 'Purple', 'Orange'];


//Run a while loop and format them as you want.
index = 0;
while (index < labels.length) { 
    labels[index] += index; 
    index++; 
}


       var myChartObject = document.getElementById('myChartHistory');
        var chart = new Chart(myChartObject,{
            type: 'line',
            data: {
                labels: labels,  //pass the formatted labels
                datasets: [{
                    label: "GATEWAY1",
                    fill: true,
                    backgroundColor: 'rgba(255, 99, 132, 0.2)',
                    borderColor: 'rgba(255, 99, 132, 1)',
                    data: [//WANT TO CODE HERE], 
© www.soinside.com 2019 - 2024. All rights reserved.