After creating a chart like this (with Chart.js, latest version):
var ctx = $('#graph');
var graph = new Chart(ctx, {
type: 'line',
data: someData,
});
I would like to retrieve the Chart object in another function in order to add some data point to it:
var graph = ???
graph.data.labels.push(1)
graph.data.datasets[0].data.push(10);
How do I get the Chart object?
Thank you in advance!
You can just do this:
var graph = Chart.getChart('graph')
Works in v3 - not sure when it was introduced.
Perhaps:
var ctx = $('#graph');
var graph = new Chart(ctx, {
type: 'line',
data: someData,
});
ctx.data('graph', graph);
and later:
var graph = $('#graph').data('graph');
graph.data.labels.push(1)
graph.data.datasets[0].data.push(10);
graph.update();
// //
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With