angular2-highcharts icon indicating copy to clipboard operation
angular2-highcharts copied to clipboard

How to import latest angular2-highcharts using AOT compiler

Open mannb1 opened this issue 7 years ago • 21 comments

Using angular2-highcharts with webpack and AOT compiler. The app module does not like the ChartModule.forRoot(require('highcharts')). I get the following error when I build:

ERROR in Error encountered resolving symbol values statically. Calling function 'ChartModule', function calls are not supported. Consider replacing the function or lambda with a reference to an exported function, resolving symbol AppModule in ... /app/app.module.ts, resolving symbol AppModule in ... /app/app.module.ts

It worked fine before upgrading to "angular2-highcharts": "0.5.5", "@types/highcharts": "4.2.47",

Is there a workaround or how should I configure this?

I have tried using a function to return highcharts like this:

export function highchartsFactory() { return highcharts; }

ChartModule.forRoot(highchartsFactory()),

but I get the same error.

mannb1 avatar Feb 20 '17 18:02 mannb1

You can view a temporary work around here: #160 #156

Looks like a more permanent solution is being investigated by the author.

xouqoa avatar Feb 21 '17 17:02 xouqoa

Yes, it is known issue

gevgeny avatar Feb 22 '17 09:02 gevgeny

May be some of the steps suggested here could help solve this issue - https://medium.com/@isaacplmann/getting-your-angular-2-library-ready-for-aot-90d1347bcad#.c04a7rngi

ajitpatil avatar Feb 28 '17 03:02 ajitpatil

Any updates on this issue?

jvendryes avatar Mar 03 '17 15:03 jvendryes

I used the workaround provided above (#160) to move on for now:

mannb1 avatar Mar 03 '17 15:03 mannb1

@mannb1 When you did the workaround, did you run into: Cannot find name 'require'.)? Might have missed something from the #160 solution...

Solution npm install highcharts --save ... doh... heh

jvendryes avatar Mar 03 '17 15:03 jvendryes

I don't recall having any issues of that nature. Just make sure you import your statics and your export function defined

mannb1 avatar Mar 03 '17 17:03 mannb1

Yeah I keep getting that same error, it's almost like a race condition...

jvendryes avatar Mar 03 '17 20:03 jvendryes

Just a note on the #160 workaround. It seems like this fix has a huge performance degradation associated with it.

mwraitt avatar Mar 15 '17 12:03 mwraitt

Used following code in my app module and its running fine

import { BrowserModule } from '@angular/platform-browser';
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { MaterialModule } from '@angular/material';
import 'hammerjs';
import { ChartModule } from 'angular2-highcharts';
import * as highcharts from 'highcharts';
import { HighchartsStatic } from 'angular2-highcharts/dist/HighchartsService';

import { AppRouting } from './app.routing';
import { AppComponent } from './app.component';

declare var require: any;


export function highchartsFactory() {
      const hc = require('highcharts');
      const dd = require('highcharts/modules/drilldown');
      dd(hc);

      return hc;
}

@NgModule({
  declarations: [
    AppComponent,
  ],
  imports: [
    BrowserModule,
    FormsModule,
    HttpModule,
    AppRouting,
    BrowserAnimationsModule,
    MaterialModule,
    ChartModule
  ],
  providers: [{
      provide: HighchartsStatic,
      useFactory: highchartsFactory
    }],
  bootstrap: [AppComponent]
})
export class AppModule { }

Gsmalhotra1 avatar May 18 '17 11:05 Gsmalhotra1

@Gsmalhotra1

did you try running rollup afterwards?

I'm getting 'Uncaught (in promise): ReferenceError: require is not defined' error

alexus85 avatar Jun 08 '17 14:06 alexus85

I am getting the same error

core.es5.js:1084 ERROR Error: Uncaught (in promise): Error: Template parse errors: Can't bind to 'options' since it isn't a known property of 'chart'

hsHemant avatar Jun 22 '17 21:06 hsHemant

Any update on whether this will be fixed without requiring a workaround?

gamebenchjake avatar Jul 20 '17 16:07 gamebenchjake

Any updates on this issue?

caioc avatar Jul 24 '17 14:07 caioc

having the same issue

dani0805 avatar Aug 30 '17 14:08 dani0805

bump

eddyoc avatar Oct 18 '17 10:10 eddyoc

Also curious about a solution that does not require a workaround

egervari avatar Nov 16 '17 09:11 egervari

Please use following setting in app.module.ts

import { ChartModule } from 'angular2-highcharts'; import * as highcharts from 'highcharts'; import { HighchartsStatic } from 'angular2-highcharts/dist/HighchartsService';

declare var require: any;

export function highchartsFactory() { const hc = require('highcharts'); const dd = require('highcharts/modules/drilldown'); dd(hc); return hc; } @NgModule({ declarations: [

], imports: [
ChartModule ], providers: [ { provide: HighchartsStatic, useFactory: highchartsFactory }, ], bootstrap: [AppComponent] }) export class AppModule { }

I hope issue will be resolved at both end deceleration time and options using time. I am using type following in package.json "rxjs": "^5.0.1",

"@angular/compiler-cli": "^2.3.1",
"typescript": "2.4.0" 

atulrathoreg avatar Jan 04 '18 10:01 atulrathoreg

I used a different approach to come around the issue.

  1. Load highcharts via .angular-cli.json through scripts field,
scripts: [
......                
{
                    "input": "../node_modules/highcharts/highcharts.js",
                    "output": "highcharts.js"
 },
......
]
  1. Declare highcharts in typings.d.ts (or any other definitions file)

declare var Highcharts: any;

  1. Use the following configuration for HighchartsStatic in your module declaration
    providers: [
        {provide: HighchartsStatic, useValue: Highcharts}
    ]

That's it. Now your code should compile on aot mode.

Hope this helps.

catchshyam avatar Apr 20 '18 12:04 catchshyam

We use the regular approach, and it works with aot. Just import and apply the modules.

// For: regular highcharts.
import * as highcharts from 'highcharts/highcharts';
import * as highchartsMore from 'highcharts/highcharts-more';
import * as bullet from 'highcharts/modules/bullet';

// Or for: styled mode. Remember to import the provided css file from the highcharts package.
import * as highcharts from 'highcharts/js/highcharts';
import * as highchartsMore from 'highcharts/js/highcharts-more';
import * as bullet from 'highcharts/js/modules/bullet';

@NgModule({
  imports: [
    ChartModule,
  ],
  providers: [
    {
      provide: HighchartsStatic,
      useFactory: highchartsFactory
    },
  ],
})
export class AppModule { }

export function highchartsFactory() {
  // Default options.
  highcharts.setOptions({
    global: {
      useUTC: false
    }
  });

  // Initialize addons.
  highchartsMore(highcharts);
  bullet(highcharts);

  return highcharts;
}

Works like a charm.

Jrubzjeknf avatar Apr 24 '18 08:04 Jrubzjeknf

Thanks @Jrubzjeknf, it worked perfectly.

emmanueldaher avatar May 07 '18 17:05 emmanueldaher