简体   繁体   English

在 chart.js 的标签和数据集表内编码

[英]Coding within labels and datasets table from chart.js

Hi I would like to use a while loop within the labels to display the hours and I would like to use a while loop within the datasets to display my array.嗨,我想在标签中使用 while 循环来显示小时数,我想在数据集中使用 while 循环来显示我的数组。

My problem is i cant code within the labels and the dataset.我的问题是我无法在标签和数据集中编码。 i tried我试过了

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

But it doesn't work & the documentation on charts.js didnt help me.但它不起作用,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], 
  }

It looks like you are looking to format labels before passing them to the chart.js看起来您希望在将labels传递给chart.js之前对其进行格式化

You can do it like below:你可以这样做:

//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], 

You can check the Demo at JSFiddle您可以在JSFiddle查看演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM