forked from coreui/coreui-angular
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchartjs.component.ts
223 lines (181 loc) · 5.66 KB
/
chartjs.component.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
import {
AfterViewInit,
Component,
ElementRef,
EventEmitter,
HostBinding,
Input,
NgZone,
OnChanges,
OnDestroy,
Output,
Renderer2,
SimpleChanges,
ViewChild
} from '@angular/core';
import { BooleanInput, coerceBooleanProperty, coerceNumberProperty, NumberInput } from '@angular/cdk/coercion';
import { assign, find, merge } from 'lodash';
import Chart, { ChartData, ChartOptions, ChartType, Plugin } from 'chart.js/auto';
import * as chartjs from 'chart.js';
import { customTooltips as cuiCustomTooltips } from '@coreui/chartjs';
import { IChartjs } from './chartjs.interface';
let nextId = 0;
@Component({
selector: 'c-chart',
templateUrl: './chartjs.component.html',
styleUrls: ['./chartjs.component.scss']
})
export class ChartjsComponent implements IChartjs, AfterViewInit, OnDestroy, OnChanges {
static ngAcceptInputType_height: NumberInput;
static ngAcceptInputType_width: NumberInput;
static ngAcceptInputType_redraw: BooleanInput;
@Input() customTooltips = true;
@Input() data: ChartData | ((canvas: HTMLCanvasElement) => ChartData) | undefined;
@HostBinding('style.height.px')
@Input()
set height(value: number) {
this._height = coerceNumberProperty(value);
}
get height() {
return this._height;
}
private _height = 150;
@Input() id = `c-chartjs-${nextId++}`;
@Input() options?: ChartOptions;
@Input() plugins?: Plugin[];
@Input()
set redraw(value: boolean) {
this._redraw = coerceBooleanProperty(value);
}
get redraw(): boolean {
return this._redraw;
}
private _redraw = false;
@Input() type: ChartType = 'bar';
@HostBinding('style.width.px')
@Input()
set width(value: number | undefined) {
this._width = coerceNumberProperty(value);
}
get width() {
return this._width;
}
private _width: number | undefined;
@Input() wrapper = true;
@Output() getDatasetAtEvent = new EventEmitter<any>();
@Output() getElementAtEvent = new EventEmitter<any>();
@Output() getElementsAtEvent = new EventEmitter<any>();
@ViewChild('canvasElement') canvasElement!: ElementRef;
chart: Chart | undefined;
@HostBinding('class')
get hostClasses() {
return {
'chart-wrapper': this.wrapper
};
}
get computedData() {
const canvasRef = this.canvasElement.nativeElement;
return typeof this.data === 'function'
? canvasRef.value
? this.data(canvasRef.value)
: { datasets: [] }
: merge({}, this.data);
}
constructor(
private elementRef: ElementRef,
private ngZone: NgZone,
private renderer: Renderer2
) {}
ngAfterViewInit(): void {
this.setupChart();
this.updateChart();
}
ngOnChanges(changes: SimpleChanges): void {
this.updateChart();
}
ngOnDestroy(): void {
this.destroyChart();
}
handleOnClick($event: MouseEvent) {
if (!this.chart) return;
const datasetAtEvent = this.chart.getElementsAtEventForMode($event, 'dataset', { intersect: true }, false);
this.getDatasetAtEvent.emit(datasetAtEvent);
const elementAtEvent = this.chart.getElementsAtEventForMode($event, 'nearest', { intersect: true }, false);
this.getElementAtEvent.emit(elementAtEvent);
const elementsAtEvent = this.chart.getElementsAtEventForMode($event, 'index', { intersect: true }, false);
this.getElementsAtEvent.emit(elementsAtEvent);
}
destroyChart() {
this.chart?.destroy();
}
setupChart() {
if (!this.canvasElement) return;
if (this.customTooltips) {
chartjs.defaults.plugins.tooltip.enabled = false;
chartjs.defaults.plugins.tooltip.mode = 'index';
chartjs.defaults.plugins.tooltip.position = 'nearest';
chartjs.defaults.plugins.tooltip.external = cuiCustomTooltips;
}
const ctx: CanvasRenderingContext2D = this.canvasElement.nativeElement.getContext('2d');
return this.ngZone.runOutsideAngular(() => {
this.chart = new Chart(ctx, {
type: this.type,
data: this.computedData,
options: this.options as ChartOptions,
plugins: this.plugins
});
setTimeout(() => {
this.renderer.setStyle(this.canvasElement.nativeElement, 'display', 'block');
});
});
}
updateChart() {
if (!this.chart) return;
if (this.redraw) {
this.destroyChart();
setTimeout(() => {
this.setupChart();
});
return;
}
if (this.options) {
this.chart.options = { ...this.options };
}
if (!this.chart.config.data) {
this.chart.config.data = this.computedData;
this.ngZone.runOutsideAngular(() => {this.chart?.update();});
return;
}
const { datasets: newDataSets = [], ...newChartData } = this.computedData;
const { datasets: currentDataSets = [] } = this.chart.config.data;
// copy values
assign(this.chart.config.data, newChartData);
this.chart.config.data.datasets = newDataSets.map((newDataSet: any) => {
// given the new set, find it's current match
const currentDataSet = find(
currentDataSets,
(d: any) => d.label === newDataSet.label && d.type === newDataSet.type
);
// There is no original to update, so simply add new one
if (!currentDataSet || !newDataSet.data) return newDataSet;
if (!currentDataSet.data) {
currentDataSet.data = [];
} else {
currentDataSet.data.length = newDataSet.data.length;
}
// copy in values
assign(currentDataSet.data, newDataSet.data);
// apply dataset changes, but keep copied data
return {
...currentDataSet,
...newDataSet,
data: currentDataSet.data
};
});
this.ngZone.runOutsideAngular(() => {
setTimeout(() => {
this.chart?.update();
});
});
}
}