What is ng2-charts?
ng2-charts is an Angular wrapper for Chart.js, providing a way to create and manage various types of charts in Angular applications. It simplifies the integration of Chart.js into Angular projects by offering Angular components and directives.
What are ng2-charts's main functionalities?
Line Chart
This feature allows you to create a line chart using ng2-charts. The code sample demonstrates how to set up the chart data, labels, options, colors, and type in an Angular component.
{"template":"<canvas baseChart [datasets]=\"lineChartData\" [labels]=\"lineChartLabels\" [options]=\"lineChartOptions\" [colors]=\"lineChartColors\" [legend]=\"lineChartLegend\" [chartType]=\"lineChartType\"></canvas>","component":"import { Component } from '@angular/core';\n\n@Component({\n selector: 'app-line-chart',\n templateUrl: './line-chart.component.html'\n})\nexport class LineChartComponent {\n public lineChartData: Array<any> = [\n { data: [65, 59, 80, 81, 56, 55, 40], label: 'Series A' },\n { data: [28, 48, 40, 19, 86, 27, 90], label: 'Series B' }\n ];\n public lineChartLabels: Array<any> = ['January', 'February', 'March', 'April', 'May', 'June', 'July'];\n public lineChartOptions: any = {\n responsive: true\n };\n public lineChartColors: Array<any> = [\n { // grey\n backgroundColor: 'rgba(148,159,177,0.2)',\n borderColor: 'rgba(148,159,177,1)',\n pointBackgroundColor: 'rgba(148,159,177,1)',\n pointBorderColor: '#fff',\n pointHoverBackgroundColor: '#fff',\n pointHoverBorderColor: 'rgba(148,159,177,0.8)'\n },\n { // dark grey\n backgroundColor: 'rgba(77,83,96,0.2)',\n borderColor: 'rgba(77,83,96,1)',\n pointBackgroundColor: 'rgba(77,83,96,1)',\n pointBorderColor: '#fff',\n pointHoverBackgroundColor: '#fff',\n pointHoverBorderColor: 'rgba(77,83,96,1)'\n }\n ];\n public lineChartLegend: boolean = true;\n public lineChartType: string = 'line';\n}"}
Bar Chart
This feature allows you to create a bar chart using ng2-charts. The code sample demonstrates how to set up the chart data, labels, options, and type in an Angular component.
{"template":"<canvas baseChart [datasets]=\"barChartData\" [labels]=\"barChartLabels\" [options]=\"barChartOptions\" [colors]=\"barChartColors\" [legend]=\"barChartLegend\" [chartType]=\"barChartType\"></canvas>","component":"import { Component } from '@angular/core';\n\n@Component({\n selector: 'app-bar-chart',\n templateUrl: './bar-chart.component.html'\n})\nexport class BarChartComponent {\n public barChartOptions: any = {\n scaleShowVerticalLines: false,\n responsive: true\n };\n public barChartLabels: string[] = ['2006', '2007', '2008', '2009', '2010', '2011', '2012'];\n public barChartType: string = 'bar';\n public barChartLegend: boolean = true;\n public barChartData: any[] = [\n { data: [65, 59, 80, 81, 56, 55, 40], label: 'Series A' },\n { data: [28, 48, 40, 19, 86, 27, 90], label: 'Series B' }\n ];\n}"}
Pie Chart
This feature allows you to create a pie chart using ng2-charts. The code sample demonstrates how to set up the chart data, labels, and type in an Angular component.
{"template":"<canvas baseChart [data]=\"pieChartData\" [labels]=\"pieChartLabels\" [chartType]=\"pieChartType\"></canvas>","component":"import { Component } from '@angular/core';\n\n@Component({\n selector: 'app-pie-chart',\n templateUrl: './pie-chart.component.html'\n})\nexport class PieChartComponent {\n public pieChartLabels: string[] = ['Download Sales', 'In-Store Sales', 'Mail Sales'];\n public pieChartData: number[] = [300, 500, 100];\n public pieChartType: string = 'pie';\n}"}
Other packages similar to ng2-charts
ngx-charts
ngx-charts is a declarative charting framework for Angular2 and beyond. It uses D3.js under the hood and provides a variety of chart types. Compared to ng2-charts, ngx-charts offers more customization options and is built specifically for Angular, whereas ng2-charts is a wrapper around Chart.js.
angular-google-charts
angular-google-charts is an Angular wrapper for the Google Charts library. It allows you to create a wide range of charts using Google's charting tools. Compared to ng2-charts, angular-google-charts provides access to Google's extensive charting capabilities and is ideal for users who prefer Google Charts.
ng-apexcharts
ng-apexcharts is an Angular wrapper for ApexCharts, a modern charting library that offers a variety of chart types and extensive customization options. Compared to ng2-charts, ng-apexcharts provides more modern and interactive charting features.
ng2-charts slack
Beautiful charts for Angular based on Chart.js
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.module.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
npm install ng2-charts --save
or yarn
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)
npm install chart.js --save
or with yarn:
yarn add chart.js --save
- Import the
NgChartsModule
in your app main module:
import { NgChartsModule } from 'ng2-charts';
imports: [
NgChartsModule
]
Stackblitz Starting Templates
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
documentation
type
: (ChartType
) - indicates the type of chart, it can be: line
, bar
, radar
, pie
, polarArea
, doughnut
or any custom type added to Chart.jsdata
: (ChartData<TType, TData, TLabel>
) - the whole data structure to be rendered in the chart. Support different
flexible formats and parsing options,
see here. In alternative, and depending on
the 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
and radar
. And just labels (on
hover) for charts: polarArea
, pie
and doughnut
. Labels are matched in order with the datasets
array.datasets
: ( ChartDataset<TType, TData>[]
) - Same as the datasets
property of the data
input.
See here for details.options
: (ChartOptions<TType>
) - chart options (as
per chart.js documentation).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
labels
Colors
The library comes with a set of predefined default colors (which are exported as baseColors
). If there are more
datasets than colors, colors are generated randomly. You can specify custom colors by following
these instructions.
Dynamic Theming
The NgChartsModule
provides a service called ThemeService
which 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.
Schematics
There are schematics that may be used to add this library to your project and generate chart components using Angular
CLI.
Installation of library through ng-add schematics
ng add ng2-charts
This schematics will add the NgChartsModule
as an imported module in the main app module (or another module as specified
in the --module
command option).
Example of Generating a Line Chart using Angular CLI
ng generate ng2-charts:line my-line-chart
This calls angular's component schematics and then modifies the result, so all the options for the component schematic
are also usable here.
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)