Skip to content

Commit 48f62eb

Browse files
committed
feat: support standalone directive
1 parent 80a1b70 commit 48f62eb

File tree

7 files changed

+211
-141
lines changed

7 files changed

+211
-141
lines changed

README.md

+164-128
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,19 @@ Angular directive for [Apache ECharts (incubating)](https://github.com/apache/in
2323
- [Installation](#installation)
2424
- [Upgrade from v4.x](#upgrade-from-v4x)
2525
- [Usage](#usage)
26+
- [Standalone](#standalone)
27+
- [NgModule](#ngmodule)
28+
- [Directive](#directive)
2629
- [API](#api)
27-
- [Directive](#directive)
30+
- [Directive](#directive-1)
2831
- [ECharts API](#echarts-api)
2932
- [ECharts Instance](#echarts-instance)
3033
- [ECharts Extensions](#echarts-extensions)
3134
- [Service](#service)
3235
- [Events](#events)
3336
- [Custom Build](#custom-build)
34-
- [Legacy Custom Build](#legacy-custom-build)
3537
- [Treeshaking Custom Build](#treeshaking-custom-build)
38+
- [Legacy Custom Build](#legacy-custom-build)
3639
- [Custom Locale](#custom-locale)
3740
- [Demo](#demo)
3841

@@ -42,7 +45,7 @@ Angular directive for [Apache ECharts (incubating)](https://github.com/apache/in
4245

4346
Latest version @npm:
4447

45-
- `v16.1.2` for Angular 16
48+
- `v16.2.0` for Angular 16
4649
- `v15.0.3` for Angular 15
4750
- `v14.0.0` for Angular 14
4851
- `v8.0.1` for Angular 13
@@ -55,6 +58,10 @@ A starter project on Github: https://github.com/xieziyu/ngx-echarts-starter
5558

5659
# Latest Update
5760

61+
- 2023.10.11: v16.2.0:
62+
63+
- Feat: Exported standalone `NgxEchartsDirective`, `provideEcharts` and `provideEchartsCore`
64+
5865
- 2023.10.11: v16.1.0:
5966

6067
- Feat: Add types to `chartXXX` EventEmitters. Support new events such as: `'selectchanged'`
@@ -93,10 +100,6 @@ A starter project on Github: https://github.com/xieziyu/ngx-echarts-starter
93100
- Fix: remove @juggle/resize-observer from the peer dependencies
94101
- Perf: fix performance issue [#330](https://github.com/xieziyu/ngx-echarts/issues/330)
95102

96-
- 2021.08.05: v7.0.2:
97-
98-
- [PR #322](https://github.com/xieziyu/ngx-echarts/pull/322): Add initOpts locale property (by [augustoicaro](https://github.com/augustoicaro))
99-
100103
- 2021.05.17: v7.0.0:
101104

102105
- Feat: support Angular v11, ECharts v5
@@ -167,107 +170,105 @@ A starter project on Github: https://github.com/xieziyu/ngx-echarts-starter
167170

168171
Please refer to the [demo](https://xieziyu.github.io/ngx-echarts) page.
169172

170-
1. Firstly, import `NgxEchartsModule` in your app module (or any other proper angular module):
171-
172-
```typescript
173-
import { NgxEchartsModule } from 'ngx-echarts';
174-
175-
@NgModule({
176-
imports: [
177-
NgxEchartsModule.forRoot({
178-
/**
179-
* This will import all modules from echarts.
180-
* If you only need custom modules,
181-
* please refer to [Custom Build] section.
182-
*/
183-
echarts: () => import('echarts'), // or import('./path-to-my-custom-echarts')
184-
}),
185-
],
186-
})
187-
export class AppModule {}
188-
```
173+
## Standalone
189174

190-
The echarts library will be imported **only when it gets called the first time** thanks to the function that uses the native import.
175+
import `NgxEchartsDirective` and `provideEcharts`. Or you can use `provideEchartsCore` to do treeshaking custom build.
191176

192-
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.
177+
```typescript
178+
import { NgxEchartsDirective, provideEcharts } from 'ngx-echarts';
179+
180+
@Component({
181+
selector: 'app-root',
182+
standalone: true,
183+
imports: [CommonModule, NgxEchartsDirective],
184+
templateUrl: './app.component.html',
185+
styleUrls: ['./app.component.scss'],
186+
providers: [
187+
provideEcharts(),
188+
]
189+
})
190+
export class AppComponent {}
191+
```
193192

194-
```typescript
195-
import * as echarts from 'echarts';
196-
import { NgxEchartsModule } from 'ngx-echarts';
193+
## NgModule
197194

198-
@NgModule({
199-
imports: [
200-
NgxEchartsModule.forRoot({ echarts }),
201-
],
202-
})
203-
export class AppModule {}
204-
```
195+
import `NgxEchartsModule`:
205196

206-
When using NgxEchartsModule in a **standalone component**, we can use token `NGX_ECHARTS_CONFIG` to provide echarts
207-
208-
```typescript
209-
import { NgxEchartsModule, NGX_ECHARTS_CONFIG } from 'ngx-echarts';
210-
211-
@Component({
212-
standalone: true,
213-
selector: 'my-chart',
214-
template: `
215-
<div echarts [options]="chartOptions" class="demo-chart"></div>
216-
`,
217-
imports: [NgxEchartsModule],
218-
providers: [
219-
{
220-
provide: NGX_ECHARTS_CONFIG,
221-
useFactory: () => ({ echarts: () => import('echarts') })
222-
},
223-
]
224-
})
225-
export class MyChartComponent {
226-
// logic
227-
}
228-
```
197+
```typescript
198+
import { NgxEchartsModule } from 'ngx-echarts';
229199

230-
2. Then: use `echarts` directive in a div which has **pre-defined height**. (From v2.0, it has default height: 400px)
200+
@NgModule({
201+
imports: [
202+
NgxEchartsModule.forRoot({
203+
/**
204+
* This will import all modules from echarts.
205+
* If you only need custom modules,
206+
* please refer to [Custom Build] section.
207+
*/
208+
echarts: () => import('echarts'), // or import('./path-to-my-custom-echarts')
209+
}),
210+
],
211+
})
212+
export class AppModule {}
213+
```
214+
215+
The echarts library will be imported **only when it gets called the first time** thanks to the function that uses the native import.
216+
217+
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.
218+
219+
```typescript
220+
import * as echarts from 'echarts';
221+
import { NgxEchartsModule } from 'ngx-echarts';
231222

232-
- Simple example:
223+
@NgModule({
224+
imports: [
225+
NgxEchartsModule.forRoot({ echarts }),
226+
],
227+
})
228+
export class AppModule {}
229+
```
233230

234-
- html:
231+
## Directive
235232

236-
```html
237-
<div echarts [options]="chartOption" class="demo-chart"></div>
238-
```
233+
use `echarts` directive in a div which has **pre-defined height**. (From v2.0, it has default height: 400px)
239234

240-
- scss:
235+
- html:
241236

242-
```css
243-
.demo-chart {
244-
height: 400px;
245-
}
246-
```
237+
```html
238+
<div echarts [options]="chartOption" class="demo-chart"></div>
239+
```
247240

248-
- component:
241+
- css:
249242

250-
```typescript
251-
import { EChartsOption } from 'echarts';
243+
```css
244+
.demo-chart {
245+
height: 400px;
246+
}
247+
```
252248

253-
// ...
249+
- component:
254250

255-
chartOption: EChartsOption = {
256-
xAxis: {
257-
type: 'category',
258-
data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'],
259-
},
260-
yAxis: {
261-
type: 'value',
262-
},
263-
series: [
264-
{
265-
data: [820, 932, 901, 934, 1290, 1330, 1320],
266-
type: 'line',
267-
},
268-
],
269-
};
270-
```
251+
```typescript
252+
import { EChartsOption } from 'echarts';
253+
254+
// ...
255+
256+
chartOption: EChartsOption = {
257+
xAxis: {
258+
type: 'category',
259+
data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'],
260+
},
261+
yAxis: {
262+
type: 'value',
263+
},
264+
series: [
265+
{
266+
data: [820, 932, 901, 934, 1290, 1330, 1320],
267+
type: 'line',
268+
},
269+
],
270+
};
271+
```
271272

272273
# API
273274

@@ -408,6 +409,76 @@ You can refer to the ECharts tutorial: [Events and Actions in ECharts](https://e
408409

409410
# Custom Build
410411

412+
## Treeshaking Custom Build
413+
414+
> Since version 5.0.1 ECharts supports [Treeshaking with NPM](https://echarts.apache.org/en/tutorial.html#Use%20ECharts%20with%20bundler%20and%20NPM).
415+
416+
The `app.modules.ts` should look like this:
417+
418+
```typescript
419+
import { BrowserModule } from '@angular/platform-browser';
420+
import { NgModule } from '@angular/core';
421+
import { HttpClientModule } from '@angular/common/http';
422+
423+
import { NgxEchartsDirective, provideEchartsCore } from 'ngx-echarts';
424+
425+
import { AppComponent } from './app.component';
426+
427+
// Import the echarts core module, which provides the necessary interfaces for using echarts.
428+
import * as echarts from 'echarts/core';
429+
430+
// Import bar charts, all suffixed with Chart
431+
import { BarChart } from 'echarts/charts';
432+
433+
// Import the tooltip, title, rectangular coordinate system, dataset and transform components
434+
import {
435+
TitleComponent,
436+
TooltipComponent,
437+
GridComponent,
438+
DatasetComponent,
439+
TransformComponent
440+
} from 'echarts/components';
441+
442+
// Features like Universal Transition and Label Layout
443+
import { LabelLayout, UniversalTransition } from 'echarts/features';
444+
445+
// Import the Canvas renderer
446+
// Note that including the CanvasRenderer or SVGRenderer is a required step
447+
import { CanvasRenderer } from 'echarts/renderers';
448+
449+
// Import the theme
450+
import 'echarts/theme/macarons.js';
451+
452+
// Register the required components
453+
echarts.use([
454+
BarChart,
455+
TitleComponent,
456+
TooltipComponent,
457+
GridComponent,
458+
DatasetComponent,
459+
TransformComponent,
460+
LabelLayout,
461+
UniversalTransition,
462+
CanvasRenderer
463+
]);
464+
465+
@NgModule({
466+
declarations: [AppComponent],
467+
imports: [
468+
BrowserModule,
469+
HttpClientModule,
470+
// import standalone directive:
471+
NgxEchartsDirective,
472+
],
473+
providers: [{
474+
// Provide custom builded ECharts core:
475+
provideEchartsCore({ echarts })
476+
}],
477+
bootstrap: [AppComponent],
478+
})
479+
export class AppModule {}
480+
```
481+
411482
## Legacy Custom Build
412483

413484
> Please refer to [ECharts Documentation](https://echarts.apache.org/en/tutorial.html#Create%20Custom%20Build%20of%20ECharts) for more details.
@@ -466,41 +537,6 @@ function (root, factory) {
466537
}
467538
```
468539

469-
## Treeshaking Custom Build
470-
471-
> Since version 5.0.1 ECharts supports [Treeshaking with NPM](https://echarts.apache.org/en/tutorial.html#Use%20ECharts%20with%20bundler%20and%20NPM).
472-
473-
There is no need for the `custom-echarts.ts` file anymore. The `app.modules.ts` should look like this:
474-
475-
```typescript
476-
import { BrowserModule } from '@angular/platform-browser';
477-
import { NgModule } from '@angular/core';
478-
import { HttpClientModule } from '@angular/common/http';
479-
480-
import { NgxEchartsModule } from 'ngx-echarts';
481-
482-
import { AppComponent } from './app.component';
483-
484-
// Import the echarts core module, which provides the necessary interfaces for using echarts.
485-
import * as echarts from 'echarts/core';
486-
// Import bar charts, all with Chart suffix
487-
import { BarChart } from 'echarts/charts';
488-
import { TitleComponent, TooltipComponent, GridComponent } from 'echarts/components';
489-
// Import the Canvas renderer, note that introducing the CanvasRenderer or SVGRenderer is a required step
490-
import { CanvasRenderer } from 'echarts/renderers';
491-
import 'echarts/theme/macarons.js';
492-
493-
echarts.use([TitleComponent, TooltipComponent, GridComponent, BarChart, CanvasRenderer]);
494-
495-
@NgModule({
496-
declarations: [AppComponent],
497-
imports: [BrowserModule, NgxEchartsModule.forRoot({ echarts }), HttpClientModule],
498-
providers: [],
499-
bootstrap: [AppComponent],
500-
})
501-
export class AppModule {}
502-
```
503-
504540
# Custom Locale
505541

506542
You 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`).

projects/ngx-echarts/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ngx-echarts",
3-
"version": "16.1.2",
3+
"version": "16.2.0",
44
"author": "Xie, Ziyu",
55
"license": "MIT",
66
"keywords": [

projects/ngx-echarts/src/lib/ngx-echarts.directive.ts

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export type ThemeOption = Record<string, any>;
2727
export const NGX_ECHARTS_CONFIG = new InjectionToken<NgxEchartsConfig>('NGX_ECHARTS_CONFIG');
2828

2929
@Directive({
30+
standalone: true,
3031
selector: 'echarts, [echarts]',
3132
exportAs: 'echarts',
3233
})

0 commit comments

Comments
 (0)