Skip to content

Adding a button to pan left/right #11320

Closed Answered by stockiNail
effofxprime asked this question in Q&A
Discussion options

You must be logged in to vote

@effofxprime Chart.js has got an object where is storing statically all chart instances created (and still active, no destroyed).

To access to that object, you can use Chart.instances. To retrieve the right chart instance, you can check the canvas element related to the chart that it's interesting you.

Sample of event handler:

document.getElementById("bu").addEventListener('click', function() {
  for (const k of Object.keys(Chart.instances)) {
    const chart = Chart.instances[k];
    if (chart.canvas === document.getElementById("myChart2")){ // <-- get the canvas element by id
      console.log(chart); // <--- chart is found
    }
  }
});

See Codepen: https://codepen.io/stockinail/pen/bG…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@effofxprime
Comment options

Answer selected by effofxprime
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants