0

I am trying to use CanvasJS in my angular2 application but whenever i run my app i keep getting the console error that 'CanvasJS is not defined'.

i have included the following in my index.html page:

<script src="http://canvasjs.com/assets/script/canvasjs.min.js"></script>

but i belief i might need to add some kind of reference in the systemjs.config file or the packages.json file but not sure how to resolve this problem.

2
  • Please look at this: stackoverflow.com/questions/45003413/… Different library same problem. Commented Jul 10, 2017 at 9:29
  • @Ludwig i tried that answer but i am still getting a CanvasJS is not defined error in my console. Commented Jul 10, 2017 at 16:13

1 Answer 1

2

While you technically do not need to map it in your SystemJS configuration file, as the script tag will suffice to load the polyfill, by adding it to your configuration you can load it on demand ensuring it is loaded only when needed and in an appropriate order relative to your application.

Your config might look like

SystemJS.config({
  map: {
    "canvas-polyfill": "https://canvasjs.com/assets/script/canvasjs.min.js"
  },
  meta: {
    "https://canvasjs.com/assets/script/canvasjs.min.js": {
      "format": "global",
      "scriptLoad": true,
      "build": false
    }
  }
});

You can then add an import to any modules that employ the canvas API to ensure it is loaded when needed without mucking around with script tags and implicit dependencies.

import 'canvas-polyfill';

import {Component} from '@angular/core';

// etc.

EDIT: I realized this isn't a polyfill exactly so it would be better style to write

SystemJS.config({
  map: {
    "canvasjs": "https://canvasjs.com/assets/script/canvasjs.min.js"
  },
  meta: {
    "https://canvasjs.com/assets/script/canvasjs.min.js": {
      "format": "global",
      "exports": ["CanvasJS"],
      "scriptLoad": true,
      "build": false
    }
  }
});

And import it in your application code via

import CanvasJS from 'canvasjs';

import {Component} from '@angular/core';

// etc.

However, both approaches work fine.

Sign up to request clarification or add additional context in comments.

8 Comments

After trying to implement this solution - i got this error in my console: __zone_symbol__error: Error: (SystemJS) https://canvasjs.com/assets/script/canvasjs.min.js did not call System.register or…
would the actual process of downloading canvasjs be of any help rather than referencing a url?
@bluePearl what version of SystemJS are you using? The point of the format: "global" metadata is that it doesn't need to call register or define.
is it format: "global" or "format": "global". I am not really sure how to get the right info to answer your question but i am running on windows 10 and i am working out of the angular 2 quickstart folder.
Yes it did work but after all the trouble i didn't find canvasjs doing everything i wanted so i ended up switching the whole thing to using Highcharts.js. Thank you though and hope others will find this helpful.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.