Skip to content

Commit da5b2b3

Browse files
committed
BREAKING Tree shakable standalone library
The library is standalone and tree-shakable, so it introduces the following breaking changes: - All usage of `NgChartsModule` must be replaced by `provideNgCharts()` (and `withDefaultRegisterables()` and optionally `withColorGenerator()`)
1 parent b945381 commit da5b2b3

File tree

48 files changed

+368
-429
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+368
-429
lines changed

README.md

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,16 +49,35 @@ or with yarn:
4949
yarn add chart.js --save
5050
```
5151

52-
3. Import the `NgChartsModule` in your app main module:
52+
3. Import the directive in your standalone component:
5353

5454
```typescript
55-
import { NgChartsModule } from 'ng2-charts';
55+
import { BaseChartDirective } from 'ng2-charts';
5656

57-
// In your App's module:
58-
imports: [NgChartsModule];
57+
@Component({
58+
standalone: true,
59+
imports: [BaseChartDirective],
60+
})
61+
export class MyComponent { }
5962
```
6063

61-
### Angular version compability table
64+
4. Provide a configuration, typically in your `main.ts`:
65+
66+
```typescript
67+
import {
68+
provideCharts,
69+
withColorGenerator,
70+
withDefaultRegisterables,
71+
} from 'ng2-charts';
72+
73+
bootstrapApplication(AppComponent, {
74+
providers: [
75+
provideCharts(withDefaultRegisterables(), withColorGenerator()),
76+
],
77+
}).catch((err) => console.error(err));
78+
```
79+
80+
### Angular version compatibility table
6281

6382
<table role="table">
6483
<tbody><tr>

apps/ng2-charts-demo/src/app/app.component.html

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -65,38 +65,37 @@ <h2>Installation</h2>
6565
<h2>API</h2>
6666
<markdown ngPreserveWhitespaces>
6767
### Usage
68-
In order to use ***ng2-charts*** you need to import `NgChartsModule`:
68+
In order to use ***ng2-charts*** you need to import the directive in your standalone component:
6969

7070
```typescript
71-
import {{ '{' }} NgChartsModule &#125; from 'ng2-charts';
71+
import &#123; BaseChartDirective &#125; from 'ng2-charts';
7272

73-
// In your App's module:
74-
imports: [
75-
NgChartsModule
76-
]
73+
&#64;Component(&#123;
74+
standalone: true,
75+
imports: [BaseChartDirective],
76+
&#125;)
77+
export class MyComponent &#123; &#125;
7778
```
7879
### Global configuration
79-
When you import `NgChartsModule` you can pass a global configuration object to it:
80+
You also need to provide a global configuration in your `main.ts` (or wherever that makes sense if you are lazy-loading things):
8081

8182
```typescript
82-
import {{ '{' }} NgChartsModule &#125; from 'ng2-charts';
83-
84-
// In your App's module:
85-
imports: [
86-
NgChartsModule.forRoot({{ '{' }} defaults: {{ '{' }} ... &#125; &#125;)
87-
]
83+
import &#123;
84+
provideCharts,
85+
withColorGenerator,
86+
withDefaultRegisterables,
87+
&#125; from 'ng2-charts';
88+
89+
bootstrapApplication(AppComponent, &#123;
90+
providers: [
91+
provideCharts(withDefaultRegisterables(), withColorGenerator()),
92+
],
93+
&#125;).catch((err) => console.error(err));
8894
```
89-
Alternatively, include a provider in your module, or one of the parent modules:
95+
Alternatively, include a minimal configuration to reduce the bundle size, eg:
9096

9197
```typescript
92-
import {{ '{' }} NgChartsModule, NgChartsConfiguration &#125; from 'ng2-charts';
93-
94-
imports: [
95-
NgChartsModule
96-
],
97-
providers: [
98-
{{ '{' }} provide: NgChartsConfiguration, useValue: {{ '{' }} generateColors: false &#125;&#125;
99-
]
98+
provideCharts(&#123; registerables: [BarController, Legend, Colors] &#125;)
10099
```
101100

102101
### Chart types
@@ -148,7 +147,7 @@ <h2>API</h2>
148147

149148
### Dynamic Theming
150149

151-
The `NgChartsModule` provides a service called `ThemeService` which allows clients to set a structure
150+
The `ThemeService` allows clients to set a structure
152151
specifying colors override settings. This service may be called when the dynamic theme changes, with colors
153152
which fit the theme. The structure is interpreted as an override, so in order to reset any existing option
154153
or customization you will have to define `undefined` properties explicitly. For example:

apps/ng2-charts-demo/src/app/app.component.spec.ts

Lines changed: 2 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,22 @@
11
import { TestBed, waitForAsync } from '@angular/core/testing';
22
import { RouterTestingModule } from '@angular/router/testing';
33
import { AppComponent } from './app.component';
4-
import { MaterialModule } from './material/material.module';
54
import { MarkdownModule } from 'ngx-markdown';
65
import { HttpClient, HttpClientModule } from '@angular/common/http';
7-
import { LineChartComponent } from './line-chart/line-chart.component';
8-
import { BarChartComponent } from './bar-chart/bar-chart.component';
9-
import { PieChartComponent } from './pie-chart/pie-chart.component';
10-
import { PolarAreaChartComponent } from './polar-area-chart/polar-area-chart.component';
11-
import { RadarChartComponent } from './radar-chart/radar-chart.component';
12-
import { DynamicChartComponent } from './dynamic-chart/dynamic-chart.component';
13-
import { ChartHostComponent } from './chart-host/chart-host.component';
14-
import { DoughnutChartComponent } from './doughnut-chart/doughnut-chart.component';
15-
import { NgChartsModule } from 'ng2-charts';
16-
176
import 'highlight.js';
18-
import { HIGHLIGHT_OPTIONS, HighlightModule } from 'ngx-highlightjs';
19-
207
import { NoopAnimationsModule } from '@angular/platform-browser/animations';
21-
import { BubbleChartComponent } from './bubble-chart/bubble-chart.component';
22-
import { ScatterChartComponent } from './scatter-chart/scatter-chart.component';
23-
import { FinancialChartComponent } from './financial-chart/financial-chart.component';
24-
25-
export function hljsLanguages(): { [name: string]: () => Promise<unknown> } {
26-
return {
27-
typescript: () => import('highlight.js/lib/languages/typescript'),
28-
// html: import('highlight.js/lib/languages/html'),
29-
// scss: import('highlight.js/lib/languages/scss'),
30-
xml: () => import('highlight.js/lib/languages/xml'),
31-
};
32-
}
8+
import { highlightProvider } from '../main';
339

3410
describe('AppComponent', () => {
3511
beforeEach(waitForAsync(() => {
3612
TestBed.configureTestingModule({
37-
declarations: [
38-
AppComponent,
39-
LineChartComponent,
40-
BarChartComponent,
41-
PieChartComponent,
42-
PolarAreaChartComponent,
43-
RadarChartComponent,
44-
DynamicChartComponent,
45-
DoughnutChartComponent,
46-
BubbleChartComponent,
47-
ScatterChartComponent,
48-
ChartHostComponent,
49-
FinancialChartComponent,
50-
],
5113
imports: [
5214
NoopAnimationsModule,
5315
RouterTestingModule,
54-
NgChartsModule,
55-
MaterialModule,
5616
HttpClientModule,
5717
MarkdownModule.forRoot({ loader: HttpClient }),
58-
HighlightModule,
59-
],
60-
providers: [
61-
{
62-
provide: HIGHLIGHT_OPTIONS,
63-
useValue: {
64-
coreLibraryLoader: () => import('highlight.js/lib/core'),
65-
languages: hljsLanguages(),
66-
},
67-
},
6818
],
19+
providers: [highlightProvider],
6920
}).compileComponents();
7021
}));
7122

apps/ng2-charts-demo/src/app/app.component.ts

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,53 @@ import {
1212
} from '@angular/core';
1313
import { DOCUMENT } from '@angular/common';
1414
import { ActivatedRoute, Router } from '@angular/router';
15-
import { MatTabGroup } from '@angular/material/tabs';
16-
import { Subscription, filter } from 'rxjs';
15+
import { MatTabGroup, MatTabsModule } from '@angular/material/tabs';
16+
import { filter, Subscription } from 'rxjs';
1717
import { Chart, ChartOptions } from 'chart.js';
1818
import { ThemeService } from 'ng2-charts';
19+
import { ChartHostComponent } from './chart-host/chart-host.component';
20+
import { BarChartComponent } from './bar-chart/bar-chart.component';
21+
import { LineChartComponent } from './line-chart/line-chart.component';
22+
import { DoughnutChartComponent } from './doughnut-chart/doughnut-chart.component';
23+
import { RadarChartComponent } from './radar-chart/radar-chart.component';
24+
import { PieChartComponent } from './pie-chart/pie-chart.component';
25+
import { PolarAreaChartComponent } from './polar-area-chart/polar-area-chart.component';
26+
import { BubbleChartComponent } from './bubble-chart/bubble-chart.component';
27+
import { FinancialChartComponent } from './financial-chart/financial-chart.component';
28+
import { DynamicChartComponent } from './dynamic-chart/dynamic-chart.component';
29+
import { ScatterChartComponent } from './scatter-chart/scatter-chart.component';
30+
import { MatToolbar } from '@angular/material/toolbar';
31+
import { MatSlideToggle } from '@angular/material/slide-toggle';
32+
import { FormsModule } from '@angular/forms';
33+
import { MatAnchor } from '@angular/material/button';
34+
import { MarkdownComponent } from 'ngx-markdown';
1935

2036
const darkThemeClass = 'dark-theme';
2137

2238
@Component({
2339
selector: 'app-root',
2440
templateUrl: './app.component.html',
2541
styleUrls: ['./app.component.scss'],
42+
standalone: true,
43+
imports: [
44+
BarChartComponent,
45+
BubbleChartComponent,
46+
ChartHostComponent,
47+
DoughnutChartComponent,
48+
DynamicChartComponent,
49+
FinancialChartComponent,
50+
FormsModule,
51+
LineChartComponent,
52+
MarkdownComponent,
53+
MatAnchor,
54+
MatSlideToggle,
55+
MatTabsModule,
56+
MatToolbar,
57+
PieChartComponent,
58+
PolarAreaChartComponent,
59+
RadarChartComponent,
60+
ScatterChartComponent,
61+
],
2662
})
2763
export class AppComponent implements OnInit, OnDestroy, AfterViewInit {
2864
public isDarkTheme = false;
@@ -88,7 +124,7 @@ export class AppComponent implements OnInit, OnDestroy, AfterViewInit {
88124
private renderer: Renderer2,
89125
private themeService: ThemeService,
90126
private router: Router,
91-
private route: ActivatedRoute
127+
private route: ActivatedRoute,
92128
) {
93129
// For consistent rendering across CI and local envs
94130
Chart.defaults.set('font', { family: 'Arial' });
@@ -105,14 +141,14 @@ export class AppComponent implements OnInit, OnDestroy, AfterViewInit {
105141
this.tabGroup.selectedIndex = index;
106142
}
107143
}
108-
})
144+
}),
109145
);
110146
}
111147

112148
ngAfterViewInit(): void {
113149
if (this.tabElements) {
114150
this.tabLabels = this.tabElements.map((r) =>
115-
r.nativeElement.getAttribute('label').replace(/ /g, '')
151+
r.nativeElement.getAttribute('label').replace(/ /g, ''),
116152
);
117153
}
118154
}

apps/ng2-charts-demo/src/app/app.module.ts

Lines changed: 0 additions & 77 deletions
This file was deleted.

apps/ng2-charts-demo/src/app/bar-chart/bar-chart.component.spec.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing';
2-
32
import { BarChartComponent } from './bar-chart.component';
4-
import { NgChartsModule } from 'ng2-charts';
3+
import {provideCharts, withDefaultRegisterables} from "ng2-charts";
54

65
describe('BarChartComponent', () => {
76
let component: BarChartComponent;
87
let fixture: ComponentFixture<BarChartComponent>;
98

109
beforeEach(waitForAsync(() => {
1110
TestBed.configureTestingModule({
12-
declarations: [BarChartComponent],
13-
imports: [NgChartsModule],
11+
providers: [provideCharts(withDefaultRegisterables())],
1412
}).compileComponents();
1513
}));
1614

apps/ng2-charts-demo/src/app/bar-chart/bar-chart.component.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
import { Component, ViewChild } from '@angular/core';
22
import { Chart, ChartConfiguration, ChartData, ChartEvent, ChartType } from "chart.js";
33
import { BaseChartDirective } from 'ng2-charts';
4-
54
import DataLabelsPlugin from 'chartjs-plugin-datalabels';
5+
import { MatButton } from '@angular/material/button';
66

77
@Component({
8-
selector: 'app-bar-chart',
9-
templateUrl: './bar-chart.component.html',
10-
styleUrls: ['./bar-chart.component.scss'],
8+
selector: 'app-bar-chart',
9+
templateUrl: './bar-chart.component.html',
10+
styleUrls: ['./bar-chart.component.scss'],
11+
standalone: true,
12+
imports: [MatButton, BaseChartDirective],
1113
})
1214
export class BarChartComponent {
1315
@ViewChild(BaseChartDirective) chart: BaseChartDirective | undefined;

0 commit comments

Comments
 (0)