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

How import a module in Ionic 2 App

Open fernandommota opened this issue 8 years ago • 24 comments

Hello guys,

I would like to import the module 'highcharts-more', i need use a polar chart (http://www.highcharts.com/demo/polar) in my Ionic 2 App.

declare var require: any;

const Highcharts = require('../../node_modules/highcharts/highcharts.src');
const HighchartsMore = require('../../node_modules/highcharts/highcharts-more.js');

HighchartsMore(Highcharts);

I have been try this code above, but not work's.

Regards

fernandommota avatar Jan 26 '17 18:01 fernandommota

Are you using StockChart? I have a error when try to use the StockChart type.

raugaral avatar Feb 03 '17 15:02 raugaral

hello @raugaral for now not, but i probably will need this in future too.

fernandommota avatar Feb 03 '17 16:02 fernandommota

Hi @fernandommota, following this example https://github.com/gevgeny/angular2-webpack-starter-and-angular2-highcharts/blob/master/src/app/home/home.component.ts I have tried that:

import * as Highcharts from 'highcharts'; import * as HighchartsMore from 'highcharts/highcharts-more.js'; HighchartsMore(Highcharts);

but doesn't work for me ... a have this error http://www.highcharts.com/errors/17

raugaral avatar Feb 06 '17 09:02 raugaral

Hi @raugaral i can't reproduce your example...

Only works the charts for me if a import CharModule in app.module.ts.

import { ChartModule } from 'angular2-highcharts';

fernandommota avatar Feb 06 '17 11:02 fernandommota

@fernandommota did you try new v0.5.5 ? check out the readme

gevgeny avatar Feb 13 '17 12:02 gevgeny

The confusion here is that the ionic2 build config doesn't use 'require'. To do the same as the 'require' from the README (suggest adding an example section for Ionic 2):

app.module.ts:

import { ChartModule } from 'angular2-highcharts';
import * as highcharts from 'highcharts';
...
...
imports: [
    IonicModule.forRoot(MyApp),
    ChartModule.forRoot(highcharts)
  ]
...
..

webprofusion-chrisc avatar Feb 15 '17 01:02 webprofusion-chrisc

@soundshed how does modules syntax looks like after compilation ES6 to ES5 in the ionic2 ?

gevgeny avatar Feb 15 '17 05:02 gevgeny

@gevgeny Ionic 2 uses TypeScript almost exclusively, then transpiles to es5 using es2015 modules. The ionic build process then uses Webpack with UglifyJS, so the modules are finally managed by webpack, they get translated to webpack_require in the output JS.

webprofusion-chrisc avatar Feb 15 '17 08:02 webprofusion-chrisc

@soundshed Hm, so looks like you have require but TypeScript does not know about this. Try this:

  import { ChartModule } from 'angular2-highcharts';
+ declare var require: any;

  ...
  ...
  imports: [
      IonicModule.forRoot(MyApp),
      ChartModule.forRoot(require('highcharts'))
    ]
  ...

gevgeny avatar Feb 15 '17 08:02 gevgeny

@gevgeny I see, yes that works fine. Would the 'import' syntax work unmodified for all users then (not using 'require' at all)? Using 'require' in the middle of the code seems unusual.

webprofusion-chrisc avatar Feb 16 '17 01:02 webprofusion-chrisc

agree about unusual. but it would not. There are some inconsistencies between webpack/systemjs/js/ts combinations of env and imports so i decided to leave the require syntax

gevgeny avatar Feb 16 '17 06:02 gevgeny

can i close the issue ?

gevgeny avatar Feb 16 '17 06:02 gevgeny

@soundshed @peterpeterparker does it work also for modules https://github.com/gevgeny/angular2-highcharts#add-highcharts-modules ?

gevgeny avatar Feb 16 '17 07:02 gevgeny

@gevgeny @soundshed I withdraw my comment. Seems it works while debugging (ionic serve) but not when I want to build a proper app using aot

ionic build iOS --prod =>

[11:23:01] build prod failed: Error encountered resolving symbol values statically. Function calls are not supported. Consider replacing the function or lambda with a reference to an exported function, resolving symbol AppModule in /project/src/app/app.module.ts, resolving symbol AppModule in /project/src/app/app.module.ts [11:23:01] ionic-app-script task: "build"

peterpeterparker avatar Feb 16 '17 10:02 peterpeterparker

I'm sure I've done a prod release already that worked. What version of app-scripts and ionic cli are you on (ionic info)?

webprofusion-chrisc avatar Feb 16 '17 14:02 webprofusion-chrisc

@soundshed here the informations.

Your system information:

Cordova CLI: 6.5.0 Ionic Framework Version: 2.0.1 Ionic CLI Version: 2.2.1 Ionic App Lib Version: 2.2.0 Ionic App Scripts Version: 1.0.0 ios-deploy version: 1.9.0 ios-sim version: 5.0.13 OS: macOS Sierra Node Version: v7.2.1

peterpeterparker avatar Feb 16 '17 14:02 peterpeterparker

@gevgeny @soundshed I had a try, again, but could find a way. I've the feeling that I should provide my own HighchartsStatic new object because the prod compiler doesn't accept the static reference, but didn't find a way. I think I should achieve something like

Pseudo-code:

export function myExportHighchartsStaticLoader() {
    return HighchartsStatic());
 }

...
   ChartModule.forRoot(myExportHighchartsStaticLoader)

I'm using es2015

peterpeterparker avatar Feb 17 '17 13:02 peterpeterparker

@peterpeterparker ah, so you must have your own custom build process (Ionic defaults to typescript)? Are you using rollup or webpack? Rollup generally needs added config to tell it about classic js libraries where the exports aren't clear. Webpack probably does too, but using typescript means you get the type definition for the modules (and the export definitions).

webprofusion-chrisc avatar Feb 18 '17 00:02 webprofusion-chrisc

@soundshed I don't have a custom build process, just standard Ionic rollup typescript build.

I just tried following solution described by @fr4ngus https://github.com/gevgeny/angular2-highcharts/issues/156 and it worked just fine, bot "ionic serve" and "ionic build ios --prod" + debug in the simulator where successful with a pie chart

peterpeterparker avatar Feb 18 '17 09:02 peterpeterparker

@peterpeterparker thank, yes I hadn't done a prod build after all! The hybrid approach (avoiding the 'require' var) I ended up with was:

..
..
import { HighchartsStatic } from 'angular2-highcharts/dist/HighchartsService';
import { ChartModule } from 'angular2-highcharts';
import * as highcharts from 'highcharts';
..
..
export function highchartsFactory() {
  return highcharts;
}
..
..

@NgModule({
..
...
  imports: [
    IonicModule.forRoot(MyApp),
    ChartModule
  ],
..
..
  providers: [
  ...
    {
      provide: HighchartsStatic,
      useFactory: highchartsFactory
    },

  ]
})

webprofusion-chrisc avatar Feb 20 '17 06:02 webprofusion-chrisc

@gevgeny based in your description, my final solution:

import { ChartModule } from 'angular2-highcharts';
declare var require: any;

imports: [
    ChartModule.forRoot(
            require('highcharts')
           , require('../../node_modules/highcharts/highcharts-more.js')
    )

Thanks

fernandommota avatar Feb 22 '17 14:02 fernandommota

Hi guys. Even following the examples above, I can not get the activity gauge to work. Help me please!

Uncaught ReferenceError: Highcharts is not defined at highcharts-more.js:8 at highcharts-more.js:8

julioaze avatar Jun 02 '17 13:06 julioaze

Solve..

This is my solution

ChartModule.forRoot( require('highcharts'), require('../../node_modules/highcharts/highcharts-more.js'), require('../../node_modules/highcharts/solid-gauge.js') )

Thanks guys

julioaze avatar Jun 02 '17 18:06 julioaze

For me the following solution worked:

import {NgModule} from '@angular/core'; import {IonicPageModule} from 'ionic-angular'; import {MapPage} from './map'; import {ChartModule} from 'angular2-highcharts'; import * as highcharts from 'highcharts'; declare var require:any;

@NgModule({ declarations: [ MapPage, ], imports: [ ChartModule.forRoot( highcharts, require('highcharts/modules/map') ), IonicPageModule.forChild(MapPage) ], exports: [ MapPage ] }) export class MapPageModule { }

lujakob avatar Jul 06 '17 15:07 lujakob