1

I have here the code of my chart, I am trying to display a chart however I am getting my values on label from another function (the output of that function is the var label3, if I will use label3 as my labels on the chart it will not work, but if I use the format like label2 it will work. My question is how can I make label3 be like the format of label2?

var ctx = document.getElementById('myChart').getContext('2d');

var label2 = ['28 Dec','29 Dec','30 Dec'];
var label3 = '28 Dec,29 Dec,30 Dec';

var data1 = '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,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,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';

var lineChartData = {
    labels: label2,
    datasets: [{
        label: ' No. of clicks',
        backgroundColor: "lightblue",
        data: data1
    }]
}
var myChart = new Chart(ctx, {
      type: 'line',
      data: lineChartData

});

I have made a code you could check it here

2 Answers 2

0

There is a small mistake that you made, your data variable should be an array. Something like this.

var data1 = [10,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,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,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];
2
  • with 28 Dec,29 Dec,30 Dec values how can I make it as an array ?
    – shifu
    Mar 28, 2017 at 11:09
  • currently you have var data1 = '0,0,0'; to make this an array you have to change it to be var data1 = [0,0,0]
    – Spharah
    Mar 28, 2017 at 11:11
0

Your data1, label3 are strings, you have to use an array for that. Try to push you data in the array.

1
  • 1
    how can I push the data of label3, can you show me? i am quite new to this
    – shifu
    Mar 28, 2017 at 11:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.