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

Question: using static methods of Highcharts using angular/cli #69

Open
ASK83 opened this issue Apr 11, 2017 · 6 comments
Open

Question: using static methods of Highcharts using angular/cli #69

ASK83 opened this issue Apr 11, 2017 · 6 comments

Comments

@ASK83
Copy link

ASK83 commented Apr 11, 2017

Hi @Bigous,
First of all thanks for the great work on updating the documentations and releasing the new version compatible with ng 4.
Here is my situation:

I just updated my app to use angular/cli. Thanks to your wiki for the most parts it was a smooth process. I have a script which successfully imports all those needed libraries:

"scripts": [ "../node_modules/highcharts/highcharts.js", "../node_modules/highcharts/highcharts-more.js", "../node_modules/highcharts/modules/exporting.js" ],

This in itself works and creates the charts. However, I need to access the Highchart class itself to set some global options using static methods on the Highcharts. For example, setOptions or applying dateFormat for labeling. Previously, I was using webpack itself and per your recommendation I used
import * as Highcharts from 'highcharts'; window['Highcharts'] = Highcharts;
to attach the objet to the global window. That was working perfectly. But with my current setup with CLI addition of the above codes to attach the Highcharts will produce the following error:
Highcharts Error #16
Which is Highcharts already defined in the page.

I was wondering if there is good way to access the Highcharts object for its static methods inside my chart module to be able to set globalOptions.

Thanks for your help.

My Env:
Angular: 4.0.1,
ng2-highcharts: 1.0.0
highcharts: 4.2.5
@angular/cli: 1.0.0

@ASK83
Copy link
Author

ASK83 commented Apr 13, 2017

I solved my issue by referencing the global window, like window['Highcharts'] and setting the global option of Highcharts this way. But I am not closing my question to see if there is a better way to access the Highcharts class for its static methods.

Thanks

@Bigous
Copy link
Owner

Bigous commented Apr 13, 2017

Hi @ASK83 , sorry about the delay.

Yes, if you load the highcharts with the script, you cannot use the import statement, because the highcharts is already loaded, but the systemjs or webpack doesn't know about it.

The best way would be with the import and everything working like magic :) but, sadly, it doesn't works...

In theory, putting the script, and not using the import should work - but it doesn't because the script encapsulate the global definitions and does not sends it to the browser.

Because of this we use the trick to import and set the global variable ( window['Highcharts] = hc;` ).

I think the best solution is your solution for now... I'll keep it opened until I can figure out a better solution!

Tks

@ddanurwenda
Copy link

Hi @ASK83 ,

I have removed the highcharts line from the .angular-cli.json and also put import + set global variable. The code compiles but still I get hc error 16. Can you please provide the snippet of a working example?

Thank you,

@ASK83
Copy link
Author

ASK83 commented Jun 2, 2017

Hi @Danurwenda,

I think you still need to have highcharts in the script section of your ...cli.json file.
"scripts": [ "../node_modules/highcharts/highcharts.js", "../node_modules/highcharts/modules/exporting.js" ]
But to be able to set static members of highcharts you just need to access the Highcharts object from window and set either global options or access static methods, as follows:

` window['Highcharts'].setOptions(this.globalOptions);

window['Highcharts'].dateFormat('%b %e, %Y', this.value)
`

Hope this helps!

@ddanurwenda
Copy link

Hi @ASK83 , thank you for your swift reply.

I want to imitate the charts as in https://www.highcharts.com/demo/arearange-line, which includes calls to static method getOptions().

I have tried 2 approaches, each gives me different hc error :

  1. Import HC in component file (import * as Highcharts from 'highcharts';) and use Highcharts.getOptions()... The code compiles but gives me error HC 16.

  2. Remove the import statement and instead use window['Highcharts'] . This code compiles but gives me error HC 17.

Please advise.

@ASK83
Copy link
Author

ASK83 commented Jun 2, 2017

@Danurwenda I think you gotta go with the second method and access highcharts object through the window to make it work, except adding highcharts in scripts tag in cli.json you don't need to import anything just importing the Ng2HighchartsModule at your module should do it.

The only thing that I can think of is that you might not have the library for range plots in cli.json
I think adding below line to your cli.json would solve the problem:
"../node_modules/highcharts/highcharts-more.js",

Hope this solves your problem.

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

No branches or pull requests

3 participants