14

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!

4 Answers 4

11

You can just do this:

var graph = Chart.getChart('graph')

Works in v3 - not sure when it was introduced.

Sign up to request clarification or add additional context in comments.

Comments

10

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();

 

// //

8 Comments

I like this idea, but far as I can see, modifying that data object has no effect on the rendered chart. For instance, I can read .config.type form it, but changing it does nothing. I wish chart.js would provide some supported way to access the chart object from its DOM element. Seems basic.
"modifying that data object has no effect on the rendered chart" .. I do believe it does ! you might need to update the chart after modifications
"I wish chart.js would provide some supported way to access the chart object from its DOM element" ... true ! but this is exactly what I was doing in the above snippet: attaching the chart object as a property to the DOM element, for retrieving it later to work with it
Doh! You're right, works fine with update(), thanks.
Did you forget <canvas id="graph"></canvas> in your template ?
|
3

You could directly use the graph object without assigning it to another variable ( considering graph is a global variable ) to add any sort of data from another function.

Here is a quick example of that ...

var ctx = $('#graph');
var graph = new Chart(ctx, {
    type: 'line',
    data: {
        labels: ["January", "February", "March", "April", "May"],
        datasets: [{
            label: "Sales",
            data: [1, 2, 3, 4, 5],
            fill: false,
            borderColor: '#07C',
        }]
    }
});

function addData() {
    graph.data.labels.push('June')
    graph.data.datasets[0].data.push(6);
    graph.update();
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.5.0/Chart.min.js"></script>
<button onclick="addData()">Add Data</button>
<canvas id="graph"></canvas>

in case: the graph variable is inside a function then, you'd have to make the variable global to use it from another function

2 Comments

The code where I create the graph is in a document.ready function, so it is not global. I thought it was bad practice to define a variable in global scope, but I was able to solve it this way. Thank you!
How can we access the chart object if in case we don't save it in a variable or we don't have access to that variable as it might be overridden by some other chart object.
2
//get object
const chartJS: Chart = $("#myChart").data('chart');

//change something
chartJS.config.options.scales.yAxes[0].ticks.min = minVal <= 0 ? minVal : minVal - 

//update
chartJS.update();

1 Comment

For me, $(<appropriateQuerySelector>).data('chart') returns undefined.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.