Skip to content

Error when canvas is null using Chart.js in Angular 2 #3874

@andreas-aeschlimann

Description

@andreas-aeschlimann

Using TypeScript/Angular 2, I have an issue with Chart.js when the data of the chart is changing.

To be more precise, Chart.js does not like the fact that I temporarily hide the canvas while data is loading. You might wonder why I do that. Well, in Angular 1/2 it is quite "normal" to use conditionals as follows:

<canvas *ngIf="chart.hasValidData" ...></canvas>

I found out that Chart.js has problems with that because Angular 2 is suddenly removing the canvas from the DOM. Here is one of the errors that I see:

Uncaught TypeError: Cannot set property 'width' of null
    at Chart.Controller.resize (eval at webpackJsonp.1314.module.exports (addScript.js:9), <anonymous>:8398:30)
    at eval (eval at webpackJsonp.1314.module.exports (addScript.js:9), <anonymous>:8332:24)
    at eval (eval at webpackJsonp.1314.module.exports (addScript.js:9), <anonymous>:10308:36)

The error was raised, but the chart was still working even later. I was able to mute the errors by the following workarounds:

resize: function(silent) {
    [...]
    if (chart.width === newWidth && chart.height === newHeight) {
        return;
    } else if (canvas === null) { // new check to prevent problems
        return;
    }
helpers.getMaximumWidth = function(domNode) {
    if (domNode === null) return 0; // new check to prevent problems
    [...]
};
helpers.getMaximumHeight = function(domNode) {
    if (domNode === null) return 0; // new check to prevent problems
    [...]
};

It would be nice if you considered including this fix in a later release.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions