简体   繁体   中英

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.

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.

        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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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