151 lines
19 KiB
JavaScript
151 lines
19 KiB
JavaScript
|
import { Directive, ElementRef, EventEmitter, Input, NgZone, Output, } from '@angular/core';
|
||
|
import { Chart, } from 'chart.js';
|
||
|
import { ThemeService } from './theme.service';
|
||
|
import { distinctUntilChanged } from 'rxjs/operators';
|
||
|
import { merge } from 'lodash-es';
|
||
|
import * as i0 from "@angular/core";
|
||
|
import * as i1 from "./theme.service";
|
||
|
export class BaseChartDirective {
|
||
|
constructor(element, zone, themeService) {
|
||
|
this.zone = zone;
|
||
|
this.themeService = themeService;
|
||
|
this.type = 'bar';
|
||
|
this.plugins = [];
|
||
|
this.chartClick = new EventEmitter();
|
||
|
this.chartHover = new EventEmitter();
|
||
|
this.subs = [];
|
||
|
this.themeOverrides = {};
|
||
|
this.ctx = element.nativeElement.getContext('2d');
|
||
|
this.subs.push(this.themeService.colorschemesOptions
|
||
|
.pipe(distinctUntilChanged())
|
||
|
.subscribe((r) => this.themeChanged(r)));
|
||
|
}
|
||
|
ngOnChanges(changes) {
|
||
|
const requireRender = ['type'];
|
||
|
const propertyNames = Object.getOwnPropertyNames(changes);
|
||
|
if (propertyNames.some((key) => requireRender.includes(key)) ||
|
||
|
propertyNames.every((key) => changes[key].isFirstChange())) {
|
||
|
this.render();
|
||
|
}
|
||
|
else {
|
||
|
const config = this.getChartConfiguration();
|
||
|
// Using assign to avoid changing the original object reference
|
||
|
if (this.chart) {
|
||
|
Object.assign(this.chart.config.data, config.data);
|
||
|
if (this.chart.config.plugins) {
|
||
|
Object.assign(this.chart.config.plugins, config.plugins);
|
||
|
}
|
||
|
if (this.chart.config.options) {
|
||
|
Object.assign(this.chart.config.options, config.options);
|
||
|
}
|
||
|
}
|
||
|
this.update();
|
||
|
}
|
||
|
}
|
||
|
ngOnDestroy() {
|
||
|
if (this.chart) {
|
||
|
this.chart.destroy();
|
||
|
this.chart = void 0;
|
||
|
}
|
||
|
this.subs.forEach((s) => s.unsubscribe());
|
||
|
}
|
||
|
render() {
|
||
|
if (this.chart) {
|
||
|
this.chart.destroy();
|
||
|
}
|
||
|
return this.zone.runOutsideAngular(() => (this.chart = new Chart(this.ctx, this.getChartConfiguration())));
|
||
|
}
|
||
|
update(duration) {
|
||
|
if (this.chart) {
|
||
|
this.zone.runOutsideAngular(() => this.chart?.update(duration));
|
||
|
}
|
||
|
}
|
||
|
hideDataset(index, hidden) {
|
||
|
if (this.chart) {
|
||
|
this.chart.getDatasetMeta(index).hidden = hidden;
|
||
|
this.update();
|
||
|
}
|
||
|
}
|
||
|
isDatasetHidden(index) {
|
||
|
return this.chart?.getDatasetMeta(index)?.hidden;
|
||
|
}
|
||
|
toBase64Image() {
|
||
|
return this.chart?.toBase64Image();
|
||
|
}
|
||
|
themeChanged(options) {
|
||
|
this.themeOverrides = options;
|
||
|
if (this.chart) {
|
||
|
if (this.chart.config.options) {
|
||
|
Object.assign(this.chart.config.options, this.getChartOptions());
|
||
|
}
|
||
|
this.update();
|
||
|
}
|
||
|
}
|
||
|
getChartOptions() {
|
||
|
return merge({
|
||
|
onHover: (event, active) => {
|
||
|
if (!this.chartHover.observed && !this.chartHover.observers?.length) {
|
||
|
return;
|
||
|
}
|
||
|
this.zone.run(() => this.chartHover.emit({ event, active }));
|
||
|
},
|
||
|
onClick: (event, active) => {
|
||
|
if (!this.chartClick.observed && !this.chartClick.observers?.length) {
|
||
|
return;
|
||
|
}
|
||
|
this.zone.run(() => this.chartClick.emit({ event, active }));
|
||
|
},
|
||
|
}, this.themeOverrides, this.options, {
|
||
|
plugins: {
|
||
|
legend: {
|
||
|
display: this.legend,
|
||
|
},
|
||
|
},
|
||
|
});
|
||
|
}
|
||
|
getChartConfiguration() {
|
||
|
return {
|
||
|
type: this.type,
|
||
|
data: this.getChartData(),
|
||
|
options: this.getChartOptions(),
|
||
|
plugins: this.plugins,
|
||
|
};
|
||
|
}
|
||
|
getChartData() {
|
||
|
return this.data
|
||
|
? this.data
|
||
|
: {
|
||
|
labels: this.labels || [],
|
||
|
datasets: this.datasets || [],
|
||
|
};
|
||
|
}
|
||
|
static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "16.2.3", ngImport: i0, type: BaseChartDirective, deps: [{ token: i0.ElementRef }, { token: i0.NgZone }, { token: i1.ThemeService }], target: i0.ɵɵFactoryTarget.Directive }); }
|
||
|
static { this.ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "16.2.3", type: BaseChartDirective, selector: "canvas[baseChart]", inputs: { type: "type", legend: "legend", data: "data", options: "options", plugins: "plugins", labels: "labels", datasets: "datasets" }, outputs: { chartClick: "chartClick", chartHover: "chartHover" }, exportAs: ["base-chart"], usesOnChanges: true, ngImport: i0 }); }
|
||
|
}
|
||
|
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "16.2.3", ngImport: i0, type: BaseChartDirective, decorators: [{
|
||
|
type: Directive,
|
||
|
args: [{
|
||
|
// eslint-disable-next-line @angular-eslint/directive-selector
|
||
|
selector: 'canvas[baseChart]',
|
||
|
exportAs: 'base-chart',
|
||
|
}]
|
||
|
}], ctorParameters: function () { return [{ type: i0.ElementRef }, { type: i0.NgZone }, { type: i1.ThemeService }]; }, propDecorators: { type: [{
|
||
|
type: Input
|
||
|
}], legend: [{
|
||
|
type: Input
|
||
|
}], data: [{
|
||
|
type: Input
|
||
|
}], options: [{
|
||
|
type: Input
|
||
|
}], plugins: [{
|
||
|
type: Input
|
||
|
}], labels: [{
|
||
|
type: Input
|
||
|
}], datasets: [{
|
||
|
type: Input
|
||
|
}], chartClick: [{
|
||
|
type: Output
|
||
|
}], chartHover: [{
|
||
|
type: Output
|
||
|
}] } });
|
||
|
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiYmFzZS1jaGFydC5kaXJlY3RpdmUuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi8uLi8uLi8uLi9saWJzL25nMi1jaGFydHMvc3JjL2xpYi9iYXNlLWNoYXJ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