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

Angular (2), Chart.JS and Redux freeze exceptions #4157

Closed
philjones88 opened this issue Apr 19, 2017 · 7 comments
Closed

Angular (2), Chart.JS and Redux freeze exceptions #4157

philjones88 opened this issue Apr 19, 2017 · 7 comments
Milestone

Comments

@philjones88
Copy link

When trying to combine Angular (2) Redux and Chart.JS you end up with an exception as the arrays of data / labels are frozen.

i.e. https://github.com/buunguyen/redux-freeze

I traced it back to an error in zone.js Cannot read property 'toString' of null

    at Function.Object.defineProperty (http://localhost:8080/static/ng2/js/polyfills.bundle.js:17391:16)
    at listenArrayEvents (http://localhost:8080/static/ng2/js/vendor.bundle.js:109092:10)
    at ChartElement.buildOrUpdateElements (http://localhost:8080/static/ng2/js/vendor.bundle.js:109272:5)
    at Chart.Controller.<anonymous> (http://localhost:8080/static/ng2/js/vendor.bundle.js:108638:48)
    at Object.helpers.each (http://localhost:8080/static/ng2/js/vendor.bundle.js:109514:15)
    at Chart.Controller.update (http://localhost:8080/static/ng2/js/vendor.bundle.js:108637:12)
    at new Chart.Controller (http://localhost:8080/static/ng2/js/vendor.bundle.js:108418:6)
    at new Chart (http://localhost:8080/static/ng2/js/vendor.bundle.js:110793:21)

Stepping through the methods:

buildOrUpdateElements -> listenArrayEvents

which tries to do add listeners and it blows up

Expected Behavior

It should handle failing to add listeners to a frozen array

Current Behavior

Exception

Possible Solution

Detect frozen arrays

Steps to Reproduce (for bugs)

Context

I can't use Chart.JS in my Angular (2.4.10) application on pages with Redux data.

Environment

  • Chart.js version: 2.5.0
  • Browser name and version: Chrome latest
  • Link to your project: commercial
@etimberg
Copy link
Member

I think we could support this. It just means that live data updates will not work.

@etimberg
Copy link
Member

@simonbrunel thoughts?

@simonbrunel
Copy link
Member

@philjones88 can you provide a live test case that reproduce this issue?

@philjones88
Copy link
Author

A NG (2) + NG2-Charts + Chart.js example

http://plnkr.co/edit/WFpWrrD7FPtl1dmUuuVS?p=preview

I used the demo from:

http://valor-software.com/ng2-charts/

and simply wrapped one of the datasets in Object.freeze

See custom-chart.component.ts line 12:

 public lineChartData:Array<any> = [
    {data: Object.freeze([65, 59, 80, 81, 56, 55, 40]), label: 'Series A'},
    {data: [28, 48, 40, 19, 86, 27, 90], label: 'Series B'},
    {data: [18, 48, 77, 9, 100, 27, 40], label: 'Series C'}
  ];

And boom. Same trace.

@simonbrunel
Copy link
Member

Checking that Object.isFrozen in the dataset controller may workaround this issue?

    // ...
    if (me._data !== data) {
        if (me._data) {
            // This case happens when the user replaced the data array instance.
            unlistenArrayEvents(me._data, me);
        }
        if (data && !Object.isFrozen(data)) {
            listenArrayEvents(data, me);
        }
        me._data = data;
    }
    // ...

@etimberg
Copy link
Member

Good idea! I like that, keeps it nice and simple 😄

@Ant59
Copy link

Ant59 commented Dec 18, 2017

This is still an issue with Angular 5.0, Chart.js 2.7.1, Ngrx 4.1.1.

The above fix suggested by @simonbrunel works.

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

No branches or pull requests

4 participants