Chartjs-Chart.js – How do I dynamically change bar's stack

0πŸ‘

βœ…

I’m not sure why the stack isn’t updating or why changing the type helps in your example.

The way I do this is to completely remove the dataset and then add a new dataset as I’ve never had a need to just change the stack, usually all of the dataset gets updated:-

Removing a dataset:-

function removeDataset(chart, label) {
  chart.data.datasets = chart.data.datasets.filter(function(obj) {
    return (obj.label != label);
  });

  chart.update();
}

Adding a dataset:-

function addDataset(chart, dataset) {
  chart.data.datasets.push(dataset);

  chart.update();
}

I have added a fork to your fiddle to demonstrate this working https://jsfiddle.net/u21vw9sh/

Leave a comment