chart-color.service.ts
import { Injectable } from '@angular/core';
import { ChartOptions } from 'chart.js';
import { ThemeService } from 'ng2-charts';
type Theme = 'light-theme' | 'dark-theme';
@Injectable({
providedIn: 'root'
})
export class ChartColorService {
onstructor(private themeService: ThemeService) { }
private _selectedTheme: Theme = 'light-theme';
setCurrentTheme(theme: Theme): void {
this.selectedTheme = theme;
}
public get selectedTheme() {
return this._selectedTheme;
}
public set selectedTheme(value) {
this._selectedTheme = value;
let overrides: ChartOptions;
if (this.selectedTheme === 'dark-theme') {
overrides = {
title: {
fontColor: 'yellow'
},
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 = {
title: {
fontColor: 'blue'
},
legend: {
labels: { fontColor: 'red' }
},
scales: {
xAxes: [{
ticks: { fontColor: 'black' },
gridLines: { color: 'rgba(255,255,255,0.1)' },
scaleLabel: { fontColor: 'green' }
}],
yAxes: [{
ticks: { fontColor: 'red' },
gridLines: { color: 'rgba(255,255,255,0.1)' }
}]
}
};
}
this.themeService.setColorschemesOptions(overrides);
}
}
app.component.ts
constructor(
private chartColorService: ChartColorService) {
}
this.chartColorService.selectedTheme = 'light-theme';