Chartjs-How to get variable datasets in Charts JS?

0👍

You can create self invoking function and which will return array of datasets like this.

const options = {
  type: 'line',
  data: {
    labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
    datasets: (() => {
      return [{
        label: '# of Votes',
        data: [12, 19, 3, 5, 2, 3],
        borderWidth: 1
      }];
    })()
  },
  options: {
    scales: {
      yAxes: [{
        ticks: {
          reverse: false
        }
      }]
    }
  }
};

var ctx = document.getElementById('chartJSContainer').getContext('2d');
new Chart(ctx, options);

or you can create a function outside which will return datasets array and call it like below

datasets: getDataset()

Leave a comment