question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Provide a way to lookup chart instance given canvas element

See original GitHub issue

Feature Proposal

Upon instantiation of the Chart, it would be great if a reference to the Chart instance could be tagged to the HTMLCanvasElement for later usage, rather than having to use hacky ways to access them.

Feature Use Case

I’m looking for a way to reference the instance of one of my charts which is defined in a different JS module to the one I want to reference it in. Exporting the instance doesn’t seem to work, and I took a look at @vijayj’s implementation, and it would’ve worked fine, but I push the Chart instances to the window.charts AFTER importing my other JS module, so when I came to access window.charts, it’s empty until my other JS module is loaded and executed.

ChartJS already adds a $chartjs property onto the HTMLCanvasElement once instantiated, so it would make sense to add the instance to that property.

Possible Implementation

index.html

<canvas id="chart" width="400" height="200"></canvas>

module1.js

const chartEl = document.querySelector('#chart');
new Chart(chartEl, {
  type: 'pie',
  data: {
    labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
    datasets: [{
      data: [15, 1, 1, 1, 45, 1],
      backgroundColor: [
        'rgba(255, 99, 132, 0.2)',
        'rgba(54, 162, 235, 0.2)',
        'rgba(255, 206, 86, 0.2)',
        'rgba(75, 192, 192, 0.2)',
        'rgba(153, 102, 255, 0.2)',
        'rgba(255, 159, 64, 0.2)'
      ],
      borderColor: [
        'rgba(255,99,132,1)',
        'rgba(54, 162, 235, 1)',
        'rgba(255, 206, 86, 1)',
        'rgba(75, 192, 192, 1)',
        'rgba(153, 102, 255, 1)',
        'rgba(255, 159, 64, 1)'
      ],
      borderWidth: 1
    }]
  }
});

module2.js

const chartInstance = document.querySelector('#chart').$chartjs.instance;
chartInstance.getDatasetMeta(0).hidden = true;
chartInstance.update();

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
kurklecommented, Mar 30, 2020

Object.values(Chart.instances).filter((c) => c.canvas.id == 'chart').pop()

1reaction
benmccanncommented, Mar 30, 2020

We’d provide some API like Chart.getInstance('chart') which would return the chart instance ('chart' in this case is the DOM ID on your element)

Read more comments on GitHub >

github_iconTop Results From Across the Web

Chart.js: get chart data from canvas - javascript - Stack Overflow
var updateChart = $('#chart-2'); // This is where I'd need to get the chart data from the canvas element updateChart.data.datasets[0].data = ...
Read more >
The Graphics Canvas element - HTML - MDN Web Docs
Use the HTML element with either the canvas scripting API or the WebGL API to draw graphics and animations.
Read more >
API | Chart.js
Finds the chart instance from the given key. If the key is a string , it is interpreted as the ID of the...
Read more >
HTML DOM Canvas Object - W3Schools
Create a Canvas Object. You can create a <canvas> element by using the document.createElement() method: Example. var x = ...
Read more >
Understand tables and records in canvas apps - Power Apps
Table elements. Records. Each record contains at least one category of information for a person, a place, or a thing. The example above...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found