Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to push datasets dynamically for chart.js (bar chart)?

Im using chartjs (bar chart) to display some data. Im trying to dynamically add data to datasets array but its not working.

for example, lets say I have 2 objects in datasets array, and I dynamically creating this object and trying to push him into datasets (from Chrome console) after the page loaded and chart is already up.

var e = {
                fillColor : "#efefef",
                strokeColor : "#efefef",
                highlightFill: "#efefef",
                highlightStroke: "#efefef",
                data : [randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor()]
            }

and then

barChartData.datasets.push(e)

I also tried to do window.myBar.update() but again nothing happend.

Do you know this issue? Thanks,

like image 844
NextGen123 Avatar asked Jun 25 '15 19:06

NextGen123


People also ask

What are plugins in chart js?

Plugins are the most efficient way to customize or change the default behavior of a chart. They have been introduced at version 2.1. 0 (global plugins only) and extended at version 2.5. 0 (per chart plugins and options).

Does chart js use canvas?

Chart. js charts are rendered on user provided canvas elements. Thus, it is up to the user to create the canvas element in a way that is accessible. The canvas element has support in all browsers and will render on screen but the canvas content will not be accessible to screen readers.


1 Answers

I don't think you can use addData to add a series - it's for adding points / bars to existing series.

However you can insert your new series directly into the chart object. With the chart object and new dataset like so

var ctx = document.getElementById("chart").getContext("2d");
var myBarChart = new Chart(ctx).Bar(data);

var myNewDataset = {
    label: "My Second dataset",
    fillColor: "rgba(187,205,151,0.5)",
    strokeColor: "rgba(187,205,151,0.8)",
    highlightFill: "rgba(187,205,151,0.75)",
    highlightStroke: "rgba(187,205,151,1)",
    data: [48, 40, 19, 86, 27, 90, 28]
}

the code to insert a new dataset would be

var bars = []
myNewDataset.data.forEach(function (value, i) {
    bars.push(new myBarChart.BarClass({
        value: value,
        label: myBarChart.datasets[0].bars[i].label,
        x: myBarChart.scale.calculateBarX(myBarChart.datasets.length + 1, myBarChart.datasets.length, i),
        y: myBarChart.scale.endPoint,
        width: myBarChart.scale.calculateBarWidth(myBarChart.datasets.length + 1),
        base: myBarChart.scale.endPoint,
        strokeColor: myNewDataset.strokeColor,
        fillColor: myNewDataset.fillColor
    }))
})

myBarChart.datasets.push({
    bars: bars
})

myBarChart.update();

Fiddle - http://jsfiddle.net/pvak6rkx/ (inserts the new dataset after 3 seconds)

like image 141
potatopeelings Avatar answered Oct 07 '22 11:10

potatopeelings