Chart.js无法显示数据

时间:2017-03-28 10:52:34

标签: javascript arrays codeigniter arraylist chart.js

我这里有我的图表的代码,我试图显示一个图表但是我从另一个函数获取我的标签值(该函数的输出是var label3,如果我将label3用作我的标签图表它不起作用,但如果我使用像label2这样的格式,它将起作用。 我的问题是如何使label3成为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

});

我已经制作了一个代码,您可以查看here

2 个答案:

答案 0 :(得分:0)

您的data1,label3是字符串,您必须使用数组。尝试将数据推送到数组中。

答案 1 :(得分:0)

您犯了一个小错误,您的数据变量应该是一个数组。这样的事情。

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];