Beautiful charts for Angular based on ng2-chartjs.
This library is updated for usage with Angular 9 and removes chart.js as a peer dependency.
npm install @rinminase/ng-charts
import { ChartsModule } from '@rinminase/ng-charts';
// In your App's module:
imports: [
ChartsModule
]
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
.
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
(ChartDatasets[]
) - data
see about, the label
for the dataset which appears in the legend and tooltipslabels
(ChartLabel[]
) - 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 | DatasetColor[]
) - data colors per dataset 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 shownNote Pie charts only contain one dataset unlike other charts, as such colors
are formatted as such:
data = [10, 20, 30];
colors = [{
backgroundColor: ["red", "#0F0", "rgba(41, 182, 246,0.75)"],
borderColor: ["rgb(250,120,100)", "green", "#0086c3"],
}];
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 labelsThere 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.
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.
Please follow this guidelines when reporting bugs and feature requests:
Thanks for understanding!
The library also doesn’t support Angular 9 as of now.
The library doesn’t seem to remove chart.js as its peer dependency, but needs it a lot. I seem to see the need to remove the additional library installed for your project and have this library install it instead
Instead of:
npm install ng2-chartjs chart.js
I wanted to do it this way:
npm install @rinminase/ng-charts
Which only adds one dependency to your project.
Author: RinMinase
Live Demo: https://ng-charts.netlify.app/
GitHub: https://github.com/RinMinase/ng-charts
#angular #javascript #angularjs