
Security News
npm Adopts OIDC for Trusted Publishing in CI/CD Workflows
npm now supports Trusted Publishing with OIDC, enabling secure package publishing directly from CI/CD workflows without relying on long-lived tokens.
ng2-charts
Advanced tools
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.
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}"}
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 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 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.
Beautiful charts for Angular based on Chart.js
Samples using ng2-charts
https://valor-software.com/ng2-charts/
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.
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 directive in your standalone component:
import { BaseChartDirective } from 'ng2-charts';
@Component({
standalone: true,
imports: [BaseChartDirective],
})
export class MyComponent {}
Provide a configuration, typically in your main.ts
:
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:
provideCharts({ registerables: [BarController, Legend, Colors] });
Or in your AppModule:
import { provideCharts, withDefaultRegisterables } from 'ng2-charts';
@NgModule({
providers: [provideCharts(withDefaultRegisterables())],
bootstrap: [AppComponent],
})
export class AppModule {}
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 | ✓ | ✓ |
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>
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.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
labelsBy 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.
The ThemeService
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.
Please follow this guidelines when reporting bugs and feature requests:
Thanks for understanding!
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!
FAQs
Reactive, responsive, beautiful charts for Angular based on Chart.js
The npm package ng2-charts receives a total of 235,861 weekly downloads. As such, ng2-charts popularity was classified as popular.
We found that ng2-charts demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 4 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
npm now supports Trusted Publishing with OIDC, enabling secure package publishing directly from CI/CD workflows without relying on long-lived tokens.
Research
/Security News
A RubyGems malware campaign used 60 malicious packages posing as automation tools to steal credentials from social media and marketing tool users.
Security News
The CNA Scorecard ranks CVE issuers by data completeness, revealing major gaps in patch info and software identifiers across thousands of vulnerabilities.