[Chartjs]-Add DataSet Bar Chart – Chart.JS

21👍

Since you store your chart data in a variable (called data in your code), you can do it with a simple function on a button :

$('button').click(function() {
    // You create the new dataset `Vendas` with new data and color to differentiate
    var newDataset = {
        label: "Vendas",
        backgroundColor: 'rgba(99, 255, 132, 0.2)',
        borderColor: 'rgba(99, 255, 132, 1)',
        borderWidth: 1,
        data: [10, 20, 30, 40, 50, 60, 70],
    }

    // You add the newly created dataset to the list of `data`
    data.datasets.push(newDataset);

    // You update the chart to take into account the new dataset
    myBarChart.update();
});

You can see the full code on this jsFiddle and here is its result after a click :

enter image description here

0👍

based on tektiv answer only, it didn’t work for me.
I constructed the newDataSet just like he suggested, but I had a problem while pushing the Data.
to fix this, i had to do:

data.config.data.datasets.push(newDataSet);

instead of:

data.datasets.push(newDataSet);

Leave a comment