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 Angular2 based on Chart.js
Library updated for Angular 7
![NPM](https://nodei.co/npm-dl/ng2-charts.png?height=3&months=9)
Usage & Demo
Sample using ng2-charts@2.3.3
https://valor-software.com/ng2-charts/
Installation
- You can install ng2-charts using npm
npm install ng2-charts@2.3.3 --save
- You 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
Stackblitz Starting Templates
API
Import
import { ChartsModule } from 'ng2-charts';
imports: [
ChartsModule
]
Chart types
There are one directive for all chart types: baseChart
, and there are 8 types of charts: line
, bar
, radar
, pie
, polarArea
, doughnut
, bubble
and scatter
.
Properties
Note: For more information about possible options please refer to original chart.js documentation
data
(SingleOrMultiDataSet
) - set of points of the chart, it should be MultiDataSet
only for line
, bar
, radar
and doughnut
, otherwise SingleDataSet
datasets
({ data: SingleDataSet, label: string }[]
) - data
see about, the label
for the dataset which appears in the legend and tooltipslabels
(Label[]
) - x axis labels. It's necessary for charts: line
, bar
and radar
. And just labels (on hover) for charts: polarArea
, pie
and doughnut
. Label
is either a single string
, or it may be a string[]
representing a multi-line label where each array element is on a new line.chartType
(ChartType
) - indicates the type of charts, it can be: line
, bar
, radar
, pie
, polarArea
, doughnut
options
(ChartOptions
) - chart options (as from Chart.js documentation)colors
(Color[]
) - data colors, will use default and|or random colors if not specified (see below)legend
: (boolean = false
) - if true show legend below the chart, otherwise not be 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
There are a set several default colors. Colors can be replaced using the colors
attribute. If there is more data than colors, colors are generated randomly.
Dynamic Theming
The ChartsModule
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) { }
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 generate chart components using Angular CLI. The components are defined in package ng2-charts-schematics
.
Installation of Schematics Package
npm install --save-dev ng2-charts-schematics
Example of Generating a Line Chart using Angular CLI
ng generate ng2-charts-schematics: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. This schematics will also add the ChartsModule
as an imported module in the main app module (or another module as specified in the --module
command switch).
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)