8

I am trying to implement EChart Baidu in Angular 2 application (typescript).

I am following the start guide on their website https://ecomfe.github.io/echarts/doc/start-en.html but have no idea how I am supposed to init the chart having no clue about the function parameter of this line of code:

function (ec) {
            var myChart = ec.init(document.getElementById('main')); 

Using Angular 2 I have ngOnInit() function that can be used as jquery's $(document).ready().

I've tries to implement ECharts in a separate page using pure javasript and is working just fine. I even have HTML theme Limitless.

The problem is that I don't know how to get this 'ec' parameter from the code above in Angular2.

Any help would be appreciated! Thank you

Anton Cholakov
  • 163
  • 1
  • 1
  • 7

6 Answers6

10
npm install --save echarts
npm install --save-dev @types/echarts

code:

import {
  Directive, ElementRef, Input, OnInit, HostBinding, OnChanges, OnDestroy
} from '@angular/core';

import {Subject, Subscription} from "rxjs";

import * as echarts from 'echarts';
import ECharts = echarts.ECharts;
import EChartOption = echarts.EChartOption;


@Directive({
  selector: '[ts-chart]',
})
export class echartsDirective implements OnChanges,OnInit,OnDestroy {
  private chart: ECharts;
  private sizeCheckInterval = null;
  private reSize$ = new Subject<string>();
  private onResize: Subscription;

  @Input('ts-chart') options: EChartOption;

  @HostBinding('style.height.px')
  elHeight: number;

  constructor(private el: ElementRef) {
    this.chart = echarts.init(this.el.nativeElement, 'vintage');
  }


  ngOnChanges(changes) {
    if (this.options) {
      this.chart.setOption(this.options);
    }
  }

  ngOnInit() {
    this.sizeCheckInterval = setInterval(() => {
      this.reSize$.next(`${this.el.nativeElement.offsetWidth}:${this.el.nativeElement.offsetHeight}`)
    }, 100);
    this.onResize = this.reSize$
      .distinctUntilChanged()
      .subscribe((_) => this.chart.resize());

    this.elHeight = this.el.nativeElement.offsetHeight;
    if (this.elHeight < 300) {
      this.elHeight = 300;
    }
  }


  ngOnDestroy() {
    if (this.sizeCheckInterval) {
      clearInterval(this.sizeCheckInterval);
    }
    this.reSize$.complete();
    if (this.onResize) {
      this.onResize.unsubscribe();
    }
  }
}

luck :)

TossPig
  • 511
  • 5
  • 9
6

So, I have found a solution!

First, you should use echarts.js library which is NOT common js. I am using this one which I have found here. Notice that the first two libraries are common.js and common.min.js. They use require as a function but typescript has its own require. It is not okay to mess up like that so for clearer solution just use non common js library of echarts.

In the directory where echarts.js file is located, I've created echarts.d.ts which has only one line of code:

export function init (elem: any);

Then, in my component.ts file I import like this:

import * as echarts from 'path/to/echarts/jsfile'

You should import without the .js extention!

After that in my onInit function I just do:

let basic_lines = echarts.init(document.getElementById(this.chartId));

where this.chartId is just the id of my DOM element holding the chart and basic_lines is an object which I fill with options later on (just like in the example given in the question!

I hope this would help someone!

Anton Cholakov
  • 163
  • 1
  • 1
  • 7
2

I have used:

import * as echarts from 'path/to/echarts/jsfile'

but that has a error error TS2307: Cannot find module 'echarts'

In the end, simple and crude,I have require as a property for the chass:

1.install whith npm module for echarts:

npm install echarts

2.defined a property for the class:

export class AdvertReportComponent implements OnInit {
     echarts: any = require('echarts')
     ngOnInit(){
       //set options;
       var options= {...};
       var chartView = this.echarts.init(this._chartElement.nativeElement);
       chartView.setOption(options);
    }
}
Haifeng Zhang
  • 30,077
  • 19
  • 81
  • 125
shaddock
  • 21
  • 1
1

first, install the echart via npm

npm install --save echarts

second, require the echart in your component

const echarts = require('echarts');

then, in ngInit function, apply your code

const selectDom = this.renderer.selectRootElement('#chart');
const myChart = charts.init(selectDom);
razor1895
  • 56
  • 5
1

npm install -S echarts
npm install -D @types/echarts
npm install -S ngx-echart --save (the new version of angular2-echarts)

-D = --save-dev -S = --save

Reference with @Ng, @Component/@Directive in your .ts files. Call into your pages - Don't forget to check your selector!

import * as echarts from 'echarts'; (TS(Typescript) not JS filetype)
import { AngularModuleEcharts } from 'ngx-charts';

@TossPig's code works fine.

Am using Ionic3 with TS devDependencies with ngx-echarts. Had some problems with converting the Angular Directive to an Ionic Module for reuse. Check out https://github.com/xieziyu/ngx-echarts and https://xieziyu.github.io/#/ngx-echarts/demo for code structure and implementation as Angular components.

NexusInk
  • 41
  • 3
0

Take a look at angular2-echarts library. To install:

npm install echarts --save

npm install angular2-echarts --save

Hope it helps!

jos
  • 1,070
  • 12
  • 22