-1

I can get my chart to display with 1 dataset no problem, but adding in the second one to the syntax below gives me an error of

Uncaught Syntax Error: Unexpected Token }

This is my syntax - what is causing the issue?

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;
                    }
                }]
            }
        }]  
    );
IcyPopTarts
  • 494
  • 1
  • 12
  • 25

1 Answers1

1

The second last line of your code }] is backwards. It should be ]}

reconfigured structure:

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;
    }
  }]
});
ever.wakeful
  • 422
  • 3
  • 10