Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] creating chart when target element not on DOM yet #3790

Closed
pyalot opened this issue Jan 13, 2017 · 3 comments · Fixed by #4591
Closed

[BUG] creating chart when target element not on DOM yet #3790

pyalot opened this issue Jan 13, 2017 · 3 comments · Fixed by #4591
Assignees
Milestone

Comments

@pyalot
Copy link

pyalot commented Jan 13, 2017

Expected Behavior

It should work.

Current Behavior

An exception is thrown:

Uncaught TypeError: Cannot set property '_chartjs' of null

Possible Solution

Do not depend on parent element for watching resizes.

Steps to Reproduce (for bugs)

  1. canvas = document.createElement('canvas');
  2. chart = new Chart(canvas, ...);

Context

Trying to display a chart.

Environment

  • Chart.js version: 2.4.0 (latest)
  • Browser name and version: Any
@simonbrunel
Copy link
Member

Chart.js doesn't support creating a responsive chart from an orphan canvas, and I don't think this will be fixed anytime soon since it would require to monitor extra DOM activity to know when the canvas is added to the tree (which may be beyond the library responsibility, /cc @chartjs/maintainers).

Depending on your use case, immediate alternatives are:

  • if you don't need responsiveness, then turn it off via options: options: { responsive: false }
  • else, make sure to insert your canvas in the tree before creating the chart

Note that this case should work fine when the new context acquisition method from a div node will be implemented (proposal).

@etanb
Copy link

etanb commented Feb 2, 2017

+1 in having this issue.

@simonbrunel
Copy link
Member

@pyalot It took some time but it should work now (#4591) :) Are you able to test this development build and confirm that it works as you expect?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants