UNPKG

19.9 kBMarkdownView Raw
1# ngx-echarts
2
3<!-- Badges section here. -->
4
5[![npm](https://img.shields.io/npm/v/ngx-echarts.svg)][npm-badge-url]
6[![npm](https://img.shields.io/npm/dm/ngx-echarts.svg)][npm-badge-url]
7[![Build Status](https://github.com/xieziyu/ngx-echarts/actions/workflows/ci.yaml/badge.svg)][ci-url]
8
9Angular directive for [Apache ECharts (incubating)](https://github.com/apache/incubator-echarts)
10(version >= 3.x) (The project is renamed from **angular2-echarts**)
11
12- [Online Demo](https://xieziyu.github.io/ngx-echarts)
13- [Online Docs](https://xieziyu.github.io/ngx-echarts/api-doc)
14- [Starter Project](https://github.com/xieziyu/ngx-echarts-starter)
15- [Changelog](https://xieziyu.github.io/ngx-echarts/#/changelogs)
16
17## Table of contents
18
19- [ngx-echarts](#ngx-echarts)
20 - [Table of contents](#table-of-contents)
21- [Getting Started](#getting-started)
22- [Latest Update](#latest-update)
23- [Installation](#installation)
24 - [Upgrade from v4.x](#upgrade-from-v4x)
25- [Usage](#usage)
26- [API](#api)
27 - [Directive](#directive)
28 - [ECharts API](#echarts-api)
29 - [ECharts Instance](#echarts-instance)
30 - [ECharts Extensions](#echarts-extensions)
31 - [Service](#service)
32- [Events](#events)
33- [Custom Build](#custom-build)
34 - [Legacy Custom Build](#legacy-custom-build)
35 - [Treeshaking Custom Build](#treeshaking-custom-build)
36- [Custom Locale](#custom-locale)
37- [Demo](#demo)
38
39# Getting Started
40
41`ngx-echarts` is an Angular (ver >= 2.x) directive for ECharts (ver >= 3.x).
42
43Latest version @npm:
44
45- `v16.x` for Angular >= 16
46- `v15.x` for Angular >= 15
47- `v14.x` for Angular >= 14
48- `v8.x` for Angular >= 13
49- `v7.x` for Angular >= 11
50- `v6.x` for Angular >= 10, < 11
51- `v5.x` for Angular >= 6, < 10
52- `v2.3.1` for Angular < 6 (Please refer to https://github.com/xieziyu/ngx-echarts/blob/v2.x/README.md)
53
54A starter project on Github: https://github.com/xieziyu/ngx-echarts-starter
55
56# Latest Update
57
58- 2023.05.10: v16.0.0:
59
60 - Feat: upgrade to Angular 16
61 - Chore: replaced tslint with eslint.
62
63- 2023.03.27: v15.0.3:
64
65 - Fix: `[loading]=true` is not triggered when chart initialized.
66
67- 2023.03.20: v15.0.2:
68
69 - Fix: unsubscribe ChangeFilter subscriptions onDestroy.
70
71- 2022.12.01: v15.0.1:
72
73 - Feat: Support nullable @Inputs. Issue [#378](https://github.com/xieziyu/ngx-echarts/issues/378)
74
75- 2022.11.23: v15.0.0:
76
77 - Feat: upgrade to Angular 15
78
79- 2022.06.21: v14.0.0:
80
81 - Feat: upgrade to Angular 14
82
83- 2021.12.07: v8.0.1:
84
85 - Fix: NgxEchartsModule.forChild() issue [#334](https://github.com/xieziyu/ngx-echarts/issues/334)
86
87- 2021.11.08: v8.0.0 / v7.1.0:
88
89 - Fix: remove @juggle/resize-observer from the peer dependencies
90 - Perf: fix performance issue [#330](https://github.com/xieziyu/ngx-echarts/issues/330)
91
92- 2021.08.05: v7.0.2:
93
94 - [PR #322](https://github.com/xieziyu/ngx-echarts/pull/322): Add initOpts locale property (by [augustoicaro](https://github.com/augustoicaro))
95
96- 2021.05.17: v7.0.0:
97
98 - Feat: support Angular v11, ECharts v5
99 - Feat: support echart theme object
100 - Perf: resize animation
101
102- 2021.01.10: v6.0.1:
103
104 - [PR #295](https://github.com/xieziyu/ngx-echarts/pull/295): Guard dispose (by [taipeiwu](https://github.com/taipeiwu))
105
106- 2021.01.10: v6.0.0:
107
108 - [PR #285](https://github.com/xieziyu/ngx-echarts/pull/285): Guard dispose (by [gjsmith66](https://github.com/gjsmith66))
109 - update demo to use echarts v5.0
110 - [PR #282](https://github.com/xieziyu/ngx-echarts/pull/282): fix avoid "ResizeObserver loop limit exceeded" error (by [parkdihoon](https://github.com/parkdihoon))
111 - [PR #272](https://github.com/xieziyu/ngx-echarts/pull/272): Angular 10 support (by [Ghostbird](https://github.com/Ghostbird))
112
113- 2020.11.07: v5.2.1:
114
115 - Required `resize-observer-polyfill`
116 - [PR #271](https://github.com/xieziyu/ngx-echarts/pull/271): Fix autoResize functionality (by [ThomasBower](https://github.com/ThomasBower))
117 - Exposed methods: `refreshChart()` and `resize()`
118
119- 2020.07.24: v5.1.0:
120
121 - [PR #240](https://github.com/xieziyu/ngx-echarts/pull/240): Added output 'optionsError' (by [trajnisz](https://github.com/trajnisz))
122 - [PR #242](https://github.com/xieziyu/ngx-echarts/pull/242): Add output for brushEnd event (by [Uular](https://github.com/Uular))
123 - [PR #246](https://github.com/xieziyu/ngx-echarts/pull/246): Allow loading echarts library lazily via native import (by [smnbbrv](https://github.com/smnbbrv))
124
125- 2020.05.19: v5.0.0
126 - **BREAKING CHANGES**:
127 - `NgxEchartsModule` provides `.forRoot()` method to inject `echarts` core.
128 - Due to `.forRoot` method, we can do custom build without `NgxEchartsCoreModule`. Just import the `echarts` core from `echarts/src/echarts`, and other necessary charts.
129 - `NgxEchartsCoreModule` is removed.
130 - `[detectEventChanges]` is removed.
131
132# Installation
133
134- Since v5.0
135
136 ```bash
137 # if you use npm
138 npm install echarts -S
139 npm install ngx-echarts -S
140
141 # or if you use yarn
142 yarn add echarts
143 yarn add ngx-echarts
144 ```
145
146- If you need ECharts GL support, please install it first:
147
148 ```bash
149 npm install echarts-gl -S
150
151 # or
152 yarn add echarts-gl
153 ```
154
155- Import other extensions such as themes or `echarts-gl` in your `main.ts`: [ECharts Extensions](#echarts-extensions)
156
157## Upgrade from v4.x
158
1591. import `echarts` and provide it in `NgxEchartsModule.forRoot({ echarts })`.
1602. `NgxEchartsCoreModule` is removed.
161
162# Usage
163
164Please refer to the [demo](https://xieziyu.github.io/ngx-echarts) page.
165
1661. Firstly, import `NgxEchartsModule` in your app module (or any other proper angular module):
167
168 ```typescript
169 import { NgxEchartsModule } from 'ngx-echarts';
170
171 @NgModule({
172 imports: [
173 NgxEchartsModule.forRoot({
174 /**
175 * This will import all modules from echarts.
176 * If you only need custom modules,
177 * please refer to [Custom Build] section.
178 */
179 echarts: () => import('echarts'), // or import('./path-to-my-custom-echarts')
180 }),
181 ],
182 })
183 export class AppModule {}
184 ```
185
186 The echarts library will be imported **only when it gets called the first time** thanks to the function that uses the native import.
187
188 You can also directly pass the echarts instead which will slow down initial rendering because it will load the whole echarts into your main bundle.
189
190 ```typescript
191 import * as echarts from 'echarts';
192 import { NgxEchartsModule } from 'ngx-echarts';
193
194 @NgModule({
195 imports: [
196 NgxEchartsModule.forRoot({ echarts }),
197 ],
198 })
199 export class AppModule {}
200 ```
201
202 When using NgxEchartsModule in a **standalone component**, we can use token `NGX_ECHARTS_CONFIG` to provide echarts
203
204 ```typescript
205 import { NgxEchartsModule, NGX_ECHARTS_CONFIG } from 'ngx-echarts';
206
207 @Component({
208 standalone: true,
209 selector: 'my-chart',
210 template: `
211 <div echarts [options]="chartOptions" class="demo-chart"></div>
212 `,
213 imports: [NgxEchartsModule],
214 providers: [
215 {
216 provide: NGX_ECHARTS_CONFIG,
217 useFactory: () => ({ echarts: () => import('echarts') })
218 },
219 ]
220 })
221 export class MyChartComponent {
222 // logic
223 }
224 ```
225
2262. Then: use `echarts` directive in a div which has **pre-defined height**. (From v2.0, it has default height: 400px)
227
228 - Simple example:
229
230 - html:
231
232 ```html
233 <div echarts [options]="chartOption" class="demo-chart"></div>
234 ```
235
236 - scss:
237
238 ```css
239 .demo-chart {
240 height: 400px;
241 }
242 ```
243
244 - component:
245
246 ```typescript
247 import { EChartsOption } from 'echarts';
248
249 // ...
250
251 chartOption: EChartsOption = {
252 xAxis: {
253 type: 'category',
254 data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'],
255 },
256 yAxis: {
257 type: 'value',
258 },
259 series: [
260 {
261 data: [820, 932, 901, 934, 1290, 1330, 1320],
262 type: 'line',
263 },
264 ],
265 };
266 ```
267
268# API
269
270### Directive
271
272`echarts` directive support following input properties:
273
274| Input | Type | Default | Description |
275| --------------- | ------- | ------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
276| `[options]` | object | null | The same as the options on the official demo site. |
277| `[merge]` | object | null | Used to update a 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`. Refer to [ECharts documentation](https://echarts.apache.org/en/api.html#echartsInstance.setOption) for details. |
278| `[loading]` | boolean | false | Used to toggle the echarts loading animation when your data is not ready. |
279| `[autoResize]` | boolean | true | If set to `true`, the chart will be automatically resized when the window's width is changed. |
280| `[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](https://echarts.apache.org/en/api.html#echarts.init) for details. |
281| `[theme]` | string | null | Used it to initialize echarts with theme. The theme file must also be imported in `main.ts`. |
282| `[loadingOpts]` | object | null | Input an object to customize the loading style. Refer to [ECharts documentation](https://echarts.apache.org/en/api.html#echartsInstance.showLoading) for details. |
283
284By default, `loadingOpts` is:
285
286```javascript
287{
288 text: 'loading',
289 color: '#c23531',
290 textColor: '#000',
291 maskColor: 'rgba(255, 255, 255, 0.8)',
292 zlevel: 0
293}
294```
295
296### ECharts API
297
298If you need to access parts of the ECharts API such as `echarts.graphic`, please import it from echarts. For example:
299
300```typescript
301import { graphic } from 'echarts';
302
303new graphic.LinearGradient(/* ... */);
304```
305
306### ECharts Instance
307
308`echartsInstance` is exposed (since v1.1.6) in the `(chartInit)` event, enabling you to directly call functions like: `resize()`, `showLoading()`, etc. For example:
309
310- html:
311
312```html
313<div echarts class="demo-chart" [options]="chartOptions" (chartInit)="onChartInit($event)"></div>
314```
315
316- component:
317
318```typescript
319onChartInit(ec) {
320 this.echartsInstance = ec;
321}
322
323resizeChart() {
324 if (this.echartsInstance) {
325 this.echartsInstance.resize();
326 }
327}
328```
329
330### ECharts Extensions
331
332Import echarts theme files or other extension files after you have imported `echarts` core. For example:
333
334```typescript
335import * as echarts from 'echarts';
336
337/** echarts extensions: */
338import 'echarts-gl';
339import 'echarts/theme/macarons.js';
340import 'echarts/dist/extension/bmap.min.js';
341```
342
343### Service
344
345`NgxEchartsService` has been obsolete since v4.0
346
347# Events
348
349As ECharts supports the `'click'`, `'dblclick'`, `'mousedown'`, `'mouseup'`, `'mouseover'`, `'mouseout'`, and `'globalout'` mouse events, our `ngx-echarts` directive also supports the same mouse events but with an additional `chart` prefix. For example:
350
351- html:
352
353```html
354<div echarts class="demo-chart" [options]="chartOptions" (chartClick)="onChartClick($event)"></div>
355```
356
357- The '\$event' is same with the 'params' that ECharts dispatches.
358
359It supports following event outputs:
360
361| @Output | Event |
362| ------------------------- | --------------------------------------- |
363| chartInit | Emitted when the chart is initialized |
364| chartClick | echarts event: `'click'` |
365| chartDblClick | echarts event: `'dblclick'` |
366| chartMouseDown | echarts event: `'mousedown'` |
367| chartMouseMove | echarts event: `'mousemove'` |
368| chartMouseUp | echarts event: `'mouseup'` |
369| chartMouseOver | echarts event: `'mouseover'` |
370| chartMouseOut | echarts event: `'mouseout'` |
371| chartGlobalOut | echarts event: `'globalout'` |
372| chartContextMenu | echarts event: `'contextmenu'` |
373| chartLegendSelectChanged | echarts event: `'legendselectchanged'` |
374| chartLegendSelected | echarts event: `'legendselected'` |
375| chartLegendUnselected | echarts event: `'legendunselected'` |
376| chartLegendScroll | echarts event: `'legendscroll'` |
377| chartDataZoom | echarts event: `'datazoom'` |
378| chartDataRangeSelected | echarts event: `'datarangeselected'` |
379| chartTimelineChanged | echarts event: `'timelinechanged'` |
380| chartTimelinePlayChanged | echarts event: `'timelineplaychanged'` |
381| chartRestore | echarts event: `'restore'` |
382| chartDataViewChanged | echarts event: `'dataviewchanged'` |
383| chartMagicTypeChanged | echarts event: `'magictypechanged'` |
384| chartPieSelectChanged | echarts event: `'pieselectchanged'` |
385| chartPieSelected | echarts event: `'pieselected'` |
386| chartPieUnselected | echarts event: `'pieunselected'` |
387| chartMapSelectChanged | echarts event: `'mapselectchanged'` |
388| chartMapSelected | echarts event: `'mapselected'` |
389| chartMapUnselected | echarts event: `'mapunselected'` |
390| chartAxisAreaSelected | echarts event: `'axisareaselected'` |
391| chartFocusNodeAdjacency | echarts event: `'focusnodeadjacency'` |
392| chartUnfocusNodeAdjacency | echarts event: `'unfocusnodeadjacency'` |
393| chartBrush | echarts event: `'brush'` |
394| chartBrushEnd | echarts event: `'brushend'` |
395| chartBrushSelected | echarts event: `'brushselected'` |
396| chartRendered | echarts event: `'rendered'` |
397| chartFinished | echarts event: `'finished'` |
398
399You can refer to the ECharts tutorial: [Events and Actions in ECharts](https://echarts.apache.org/en/tutorial.html#Events%20and%20Actions%20in%20ECharts) for more details of the event params. You can also refer to the [demo](https://xieziyu.github.io/#/ngx-echarts/demo) page for a detailed example.
400
401# Custom Build
402
403## Legacy Custom Build
404
405> Please refer to [ECharts Documentation](https://echarts.apache.org/en/tutorial.html#Create%20Custom%20Build%20of%20ECharts) for more details.
406
407If you want to produce a custom build of ECharts, prepare a file like `custom-echarts.ts`:
408
409```ts
410// custom-echarts.ts
411export * from 'echarts/src/echarts';
412
413import 'echarts/src/chart/line';
414import 'echarts/src/chart/bar';
415// component examples:
416import 'echarts/src/component/tooltip';
417import 'echarts/src/component/title';
418import 'echarts/src/component/toolbox';
419```
420
421And then inject it in your `NgxEchartsModule`:
422
423```ts
424import { NgxEchartsModule } from 'ngx-echarts';
425import * as echarts from './custom-echarts';
426
427@NgModule({
428 imports: [
429 NgxEchartsModule.forRoot({
430 echarts,
431 }),
432 ],
433})
434export class AppModule {}
435```
436
437And if you want to use the global `echarts` object, please import it from `lib` or `src` instead:
438
439```ts
440import * as echarts from 'echarts/lib/echarts';
441```
442
443If you need to import theme files, remember to change the `'echarts'` path to `'echarts/lib/echarts'`, for example:
444
445```ts
446// ... part of echarts/theme/dark.js:
447function (root, factory) {
448 if (typeof define === 'function' && define.amd) {
449 // AMD. Register as an anonymous module.
450 define(['exports', 'echarts/lib/echarts'], factory);
451 } else if (typeof exports === 'object' && typeof exports.nodeName !== 'string') {
452 // CommonJS
453 factory(exports, require('echarts/lib/echarts'));
454 } else {
455 // Browser globals
456 factory({}, root.echarts);
457 }
458}
459```
460
461## Treeshaking Custom Build
462
463> Since version 5.0.1 ECharts supports [Treeshaking with NPM](https://echarts.apache.org/en/tutorial.html#Use%20ECharts%20with%20bundler%20and%20NPM).
464
465There is no need for the `custom-echarts.ts` file anymore. The `app.modules.ts` should look like this:
466
467```typescript
468import { BrowserModule } from '@angular/platform-browser';
469import { NgModule } from '@angular/core';
470import { HttpClientModule } from '@angular/common/http';
471
472import { NgxEchartsModule } from 'ngx-echarts';
473
474import { AppComponent } from './app.component';
475
476// Import the echarts core module, which provides the necessary interfaces for using echarts.
477import * as echarts from 'echarts/core';
478// Import bar charts, all with Chart suffix
479import { BarChart } from 'echarts/charts';
480import { TitleComponent, TooltipComponent, GridComponent } from 'echarts/components';
481// Import the Canvas renderer, note that introducing the CanvasRenderer or SVGRenderer is a required step
482import { CanvasRenderer } from 'echarts/renderers';
483import 'echarts/theme/macarons.js';
484
485echarts.use([TitleComponent, TooltipComponent, GridComponent, BarChart, CanvasRenderer]);
486
487@NgModule({
488 declarations: [AppComponent],
489 imports: [BrowserModule, NgxEchartsModule.forRoot({ echarts }), HttpClientModule],
490 providers: [],
491 bootstrap: [AppComponent],
492})
493export class AppModule {}
494```
495
496# Custom Locale
497
498You can change the chart locale registering a built-in locale (located in `node_modules/echarts/lib/i18n/`) or a custom locale object. To register a locale, you will need to change the module that echart is being imported (usually `app.module.ts`).
499
500```ts
501import {NgxEchartsModule} from "ngx-echarts";
502import * as echarts from 'echarts/core';
503import langCZ from 'echarts/lib/i18n/langCZ';
504
505echarts.registerLocale("CZ", langCZ)
506
507@NgModule({
508 imports: [NgxEchartsModule.forRoot({echarts})],
509 declarations: [],
510 providers: [],
511 bootstrap: [AppComponent]
512})
513```
514
515and in your HTML file use:
516
517```html
518<div echarts [initOpts]="{ locale: 'CZ' }" [options]="options" class="demo-chart"></div>
519```
520
521# Demo
522
523You can clone this repo to your working copy and then launch the demo page in your local machine:
524
525```bash
526npm install
527npm run demo
528
529# or
530yarn install
531yarn demo
532```
533
534The demo page server is listening on: http://localhost:4202
535
536[npm-badge-url]: https://www.npmjs.com/package/ngx-echarts
537[ci-url]: https://github.com/xieziyu/ngx-echarts/actions/workflows/ci.yaml