Beautiful charts for Angular based on Chart.js
https://valor-software.com/ng2-charts/
The required packages will be automatically installed, and your
The
If you like this library and want to say thanks, consider buying me a coffee!
Usage & Demo
Samples using ng2-charts
https://valor-software.com/ng2-charts/
Installation
You can install ng2-charts by using the Angular CLI:ng add ng2-charts
The required packages will be automatically installed, and your
app.config.ts
will be updated with the required
changes to start using the library right away.Manual install through package managers
- You can install ng2-charts using npm:
```bash
npm install ng2-charts --save
```
or yarn:
```bash
yarn add ng2-charts --save
```
- You will also need to install and include
Chart.js
library in your application (it is a peer dependency of this
library, more info can be found in the
official `chart.js` [documentation](http://www.chartjs.org/docs/#getting-started))
```bash
npm install chart.js --save
```
or with yarn:
```bash
yarn add chart.js --save
```
- Import the directive in your standalone component:
```typescript
import { BaseChartDirective } from 'ng2-charts';
@Component({
standalone: true,
imports: [BaseChartDirective],
})
export class MyComponent {}
```
- Provide a configuration, typically in your
main.ts
:
```typescript
import { provideCharts, withDefaultRegisterables } from 'ng2-charts';
bootstrapApplication(AppComponent, {
providers: [provideCharts(withDefaultRegisterables())],
}).catch((err) => console.error(err));
```
Alternatively, include a minimal configuration to reduce the bundle size, eg:
```typescript
provideCharts({ registerables: [BarController, Legend, Colors] });
```
Or in your AppModule:
```typescript
import { provideCharts, withDefaultRegisterables } from 'ng2-charts';
@NgModule({
providers: [provideCharts(withDefaultRegisterables())],
bootstrap: [AppComponent],
})
export class AppModule {}
```
Angular version compatibility table
ng2-chart version | ||||||
Angular version | v1.x | v2.x | v3.x | v4.x | v5.x | v6.x |
2 - 9 | ✓ | |||||
10 | ✓ | |||||
11 | ✓ | |||||
12 | ✓ | |||||
13 | ✓ | |||||
14 | ✓ | ✓ | ||||
15 | ✓ | ✓ | ||||
16 | ✓ | |||||
17 | ✓ | ✓ |
Stackblitz Starting Templates
- Line Chart - https://stackblitz.com/github/santam85/ng2-charts-line-template?preset=node
- Pie Chart - https://stackblitz.com/github/santam85/ng2-charts-pie-template?preset=node
- Bar Chart - https://stackblitz.com/github/santam85/ng2-charts-bar-template?preset=node
- Doughnut Chart - https://stackblitz.com/github/santam85/ng2-charts-doughnut-template?preset=node
- Radar Chart - https://stackblitz.com/github/santam85/ng2-charts-radar-template?preset=node
- Polar Area Chart - https://stackblitz.com/github/santam85/ng2-charts-polar-area-template?preset=node
- Bubble Chart - https://stackblitz.com/github/santam85/ng2-charts-bubble-template?preset=node
- Scatter Chart - https://stackblitz.com/github/santam85/ng2-charts-scatter-template?preset=node
API
Chart types
There is one directive for all chart types:baseChart
, and there are 8 types of charts: line
, bar
, radar
, pie
, polarArea
, doughnut
, bubble
and scatter
. You can use the directive on a canvas
element as follows:<canvas baseChart [data]="barChartData" [options]="barChartOptions" [type]="'bar'"> </canvas>
Properties
Note: For more information about possible options please refer to original chart.js documentationtype
: (ChartType
) - indicates the type of chart, it can be:line
,bar
,radar
,pie
,polarArea
,doughnut
data
: (ChartData<TType, TData, TLabel>
) - the whole data structure to be rendered in the chart. Support different
type
of your chart, you can use the labels
and datasets
properties to specify individual options.labels
: (TLabel[]
) - Datasets labels. It's necessary for charts:line
,bar
andradar
. And just labels (on
polarArea
, pie
and doughnut
. Labels are matched in order with the datasets
array.datasets
: (ChartDataset<TType, TData>[]
) - Same as thedatasets
property of thedata
input.
options
: (ChartOptions<TType>
) - chart options (as
legend
: (boolean = false
) - if true, chart legend is shown.
Events
chartClick
: fires when click on a chart has occurred, returns information regarding active points and labelschartHover
: fires when mousemove (hover) on a chart has occurred, returns information regarding active points and
Colors
By default, this library uses the colors as defined by Chart.js. If you need more control on colors, eg: generating colors on the fly, see the advanced color palettes.Dynamic Theming
TheThemeService
allows clients to set a structure specifying colors
override settings. This service may be called when the dynamic theme changes, with colors which fit the theme. The
structure is interpreted as an override, with special functionality when dealing with arrays. Example:type Theme = 'light-theme' | 'dark-theme';
private _selectedTheme: Theme = 'light-theme';
public get selectedTheme(){
return this._selectedTheme;
}
public set selectedTheme(value){
this._selectedTheme = value;
let overrides: ChartOptions;
if (this.selectedTheme === 'dark-theme') {
overrides = {
legend: {
labels: { fontColor: 'white' }
},
scales: {
xAxes: [ {
ticks: { fontColor: 'white' },
gridLines: { color: 'rgba(255,255,255,0.1)' }
} ],
yAxes: [ {
ticks: { fontColor: 'white' },
gridLines: { color: 'rgba(255,255,255,0.1)' }
} ]
}
};
} else {
overrides = {};
}
this.themeService.setColorschemesOptions(overrides);
}
constructor(private themeService: ThemeService<AppChartMetaConfig>){
}
setCurrentTheme(theme: Theme){
this.selectedTheme = theme;
}
The
overrides
object has the same type as the chart options object ChartOptions
, and wherever a simple field is
encountered it replaces the matching field in the options
object. When an array is encountered (as in the xAxes
and yAxes
fields above), the single object inside the array is used as a template to override all array elements in
the matching field in the options
object. So in the case above, every axis will have its ticks and gridline colors
changed.Troubleshooting
Please follow this guidelines when reporting bugs and feature requests:- Use GitHub Issues board to report bugs and feature requests (
not our email address)
- Please always write steps to reproduce the error. That way we can focus on fixing the bug, not scratching our
heads trying to reproduce it.
Thanks for understanding!License
The MIT License (see the LICENSE file for the full text)If you like this library and want to say thanks, consider buying me a coffee!