ngx-echarts
Angular directive for echarts (version >= 3.x) (The project is renamed from angular2-echarts)
Table of contents
- Getting Started
- Latest Update
- Installation
- Usage
- API
- Events
- Demo
Getting Started
ngx-echarts
is an Angular (ver >= 2.x) directive for ECharts (ver >= 3.x).
Latest version @npm:
Github branches:
master
for Angular >= 6v2.x
for Angular < 6
A starter project on Github: https://github.com/xieziyu/ngx-echarts-starter
Latest Update
-
2018.08.10: v4.0.0-beta.1
- New: [detectEventChanges] Use it to avoid unwanted change detections.
- If you set it false,
chartClick
and some other event-emitters become silent. demo
- Change: [theme] now detect changes at runtime.
- When you change theme at runtime, the chart would be destroyed first and then initialized again. demo
-
2018.08.03: v4.0.0-beta.0 (thanks to smnbbrv)
-
2018.07.24: v3.2.0 & v2.3.1:
- New: [autoResize] now detects its container element's offset height.
- Change: Resizing detection is now debounced.
-
2018.06.13: v3.1.0 & v2.2.0:
- New: [autoResize] now detects its container element's offset width. Especially useful for charts inside
<ng-template>
such as NG-ZORRO components.
-
2018.06.12: v3.0.1 & v2.1.1:
- Bugfix: Line chart is not animated on init. issue#102
-
2018.05.08: v3.0.0:
- Change: Support Angular 6
- Docs: generate demo app by @angular/cli-6.0.0
-
2018.03.14: v2.1.0
- New:
[loadingOpts]
to customize loading style. Refer to API - Bugfix: double check
dom.style.height
before setting default value.
-
2018.02.07: v2.0.2. New: [autoResize]
input (default: true). PR #73 by arethore-actility
-
2017.12.11: v2.0.1. Fix issue: No change detection involved in event handler. Refer to issue #26, #28
-
2017.12.04: v2.0.0. Provide an echarts wrapper service: NgxEchartsService.
-
2017.11.25: v2.0.0-beta.0. It has some BREAKING CHANGES you should know.
Installation
-
Since v4.0
npm install echarts -S
npm install ngx-echarts -S
npm install @types/echarts -D
yarn add echarts
yarn add ngx-echarts
yarn add @types/echarts -D
-
tsconfig.json:
You need to map the echarts path to minified version of echarts in the compilerOptions of "tsconfig.json" in your project's root (this is important for AoT build):
{
...,
"compilerOptions": {
...,
+ "paths": {
+ "echarts": ["node_modules/echarts/dist/echarts.min.js"]
+ }
}
}
Usage
Please refer to the demo page.
-
Firstly, import NgxEchartsModule
in your app module (or any other proper angular module):
import { NgxEchartsModule } from 'ngx-echarts';
@NgModule({
imports: [
...,
NgxEchartsModule
],
...
})
export class AppModule { }
-
Then: use echarts
directive in a div which has pre-defined height. (From v2.0, it has default height: 400px)
-
Simple example:
<div echarts [options]="chartOption" class="demo-chart"></div>
.demo-chart {
height: 400px;
}
import { EChartOption } from 'echarts';
chartOption: EChartOption = {
xAxis: {
type: 'category',
data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
},
yAxis: {
type: 'value'
},
series: [{
data: [820, 932, 901, 934, 1290, 1330, 1320],
type: 'line'
}]
}
API
Directive
echarts
directive support following input porperties:
Input | Type | Default | Description |
---|
[options] | object | null | It's the same with the options in official demo site. |
[merge] | object | null | You can use it to update part of the options , especially helpful when you need to update the chart data. In fact, the value of merge will be used in echartsInstance.setOption() with notMerge = false . So you can refer to ECharts documentation for details |
[loading] | boolean | false | Use it to toggle the echarts loading animation when your data is not ready. |
[autoResize] | boolean | true | Charts will be automatically resized when container's width changed. |
[initOpts] | object | null | The value of [initOpts] will be used in echarts.init() . It may contain devicePixelRatio , renderer , width or height properties. Refer to ECharts documentation for details |
[theme] | string | null | Use it to init echarts with theme. You need to import the theme file in main.ts . |
[loadingOpts] | object | null | Input an object to customize loading style. Refer to ECharts documentation for details. |
[detectEventChanges] | boolean | true | Whether to register mouse event handlers on echartInstance. Use it to avoid unwanted change detections. |
By default, loadingOpts
is:
{
text: 'loading',
color: '#c23531',
textColor: '#000',
maskColor: 'rgba(255, 255, 255, 0.8)',
zlevel: 0
}
ECharts API
If you need echarts API such as echarts.graphic
, please import it from echarts. For example:
import * as echarts from 'echarts';
new echarts.graphic.LinearGradient();
import { graphic } from 'echarts';
new graphic.LinearGradient();
ECharts Instance
echartsInstance
is exposed (since v1.1.6) in (chartInit)
event. So you can directly call the APIs just like: resize()
, showLoading()
, etc. For example:
<div echarts class="demo-chart" [options]="chartOptions" (chartInit)="onChartInit($event)"></div>
onChartInit(ec) {
this.echartsIntance = ec;
}
resizeChart() {
if (this.echartsIntance) {
this.echartsIntance.resize();
}
}
ECharts Extensions
Import echarts theme files or other extension files in main.ts
. For example:
import { enableProdMode } from '@angular/core';
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { AppModule } from './app/app.module';
import { environment } from './environments/environment';
import 'echarts/theme/macarons.js';
import 'echarts/dist/extension/bmap.min.js';
if (environment.production) {
enableProdMode();
}
platformBrowserDynamic().bootstrapModule(AppModule)
.catch(err => console.log(err));
Service
NgxEchartsService
has been obsoleted since v4.0
Events
As echarts support the 'click'
, 'dblclick'
, 'mousedown'
, 'mouseup'
, 'mouseover'
, 'mouseout'
, 'globalout'
mouse events, our ngx-echarts
directive also support the same mouse events but with additional chart
prefix.
<div echarts class="demo-chart" [options]="chartOptions" (chartClick)="onChartClick($event)"></div>
- The '$event' is same with the 'params' that Echarts dispatches
It supports following event outputs:
chartClick
: It emits the same params
of 'click'
eventchartDblClick
: It emits the same params
of 'dblclick'
eventchartMouseDown
: It emits the same params
of 'mousedown'
eventchartMouseUp
: It emits the same params
of 'mouseup'
eventchartMouseOver
: It emits the same params
of 'mouseover'
eventchartMouseOut
: It emits the same params
of 'mouseout'
eventchartGlobalOut
: It emits the same params
of 'globalout'
eventchartContextMenu
: It emits the same params
of 'contextmenu'
event (since v1.2.1)chartDataZoom
: It emits the same params
of 'dataZoom'
event (thanks to averhaegen)
You can refer to the echarts tutorial: Events and Actions in ECharts for more details of the event params. You can also refer to the demo page for the detailed example.
Demo
You can clone this repo to your working copy and then launch the demo page in your local machine:
npm install
npm run demo
yarn install
yarn demo
The demo page server is listening to: http://localhost:4202