使用2个数据集生成图表Charts.js

时间:2017-07-27 00:35:31

标签: javascript php jquery chart.js chart.js2

我可以让我的图表显示1个数据集没有问题,但在第二个添加到下面的语法给我一个错误

  

未捕获的语法错误:意外的令牌}

这是我的语法 - 导致问题的原因是什么?

var ctx = document.getElementById('canvas').getContext('2d');
var chart = new Chart(ctx, {
        datasets: [{
                type: 'bar',
                labels: labelsarr,
                label: 'Red Team',
                backgroundColor: 'rgba(0, 129, 214, 0.8)',
                data: [values]
            }, {
                type: 'line', 
                label: 'Green Team',
                backgroundColor: 'rgba(0,129, 218, 0.8)',
                data: [values1]
            }, {
            options: {
                tooltips: {
                    callbacks: {
                        label: function (t, d) {
                            var xLabel = d.datasets[t.datasetIndex].label;
                            var yLabel = t.yLabel >= 1000 ? '$' + t.yLabel.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",") : '$' + t.yLabel;
                            return xLabel + ': ' + yLabel;
                        }
                    }
                },
                legend: {
                    display: false,
                    position: 'top',
                },
                scales: {
                    yAxes: [{
                            ticks: {
                                beginAtZero: true,
                                callback: function (value, index, values) {
                                    if (parseInt(value) >= 1000) {
                                        return '$' + value.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
                                    } else { return '$' + value; }
                                }
                            }
                        }]
                }
            },
            plugins: [{
                    beforeDraw: function (chart) {
                        var labels = chart.data.labels;
                    }
                }]
            }
        }]  
    );

1 个答案:

答案 0 :(得分:1)

代码}]的倒数第二行是向后的。它应该是]}

重新配置结构:

var ctx = document.getElementById('canvas').getContext('2d');
var chart = new Chart(ctx, {
  type: 'bar',
  data: {
    datasets: [
      {
        label: 'Red Team',
        data: values,
        backgroundColor: 'rgba(0, 129, 214, 0.8)',
      },
      {
        label: 'Green Team',
        data: values1,
        type: 'line',
        backgroundColor: 'rgba(0,129, 218, 0.8)',
      }
    ],
    labels: labelsarr
  },
  options: {
    callbacks: {
      label: function (t, d) {
        var xLabel = d.datasets[t.datasetIndex].label;
        var yLabel = t.yLabel >= 1000 ? '$' + t.yLabel.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",") : '$' + t.yLabel;
        return xLabel + ': ' + yLabel;
      }
    },
    legend: {
      display: false,
      position: 'top'
    },
    scales: {
      yAxes: [{
        ticks: {
          beginAtZero: true,
          callback: function (value, index, values) {
            if (parseInt(value) >= 1000) {
              return '$' + value.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
            } else { return '$' + value; }
          }
        }
      }]
    }
  },
  plugins: [{
    beforeDraw: function (chart) {
      var labels = chart.data.labels;
    }
  }]
});