/
chart-heatfield.ts
282 lines (251 loc) · 8.44 KB
/
chart-heatfield.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/**
* @license
* Copyright 2020 Dynatrace LLC
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { coerceBooleanProperty } from '@angular/cdk/coercion';
import { ESCAPE } from '@angular/cdk/keycodes';
import { CdkConnectedOverlay, ConnectedPosition } from '@angular/cdk/overlay';
import {
ChangeDetectionStrategy,
ChangeDetectorRef,
Component,
ElementRef,
EventEmitter,
Input,
OnDestroy,
Output,
ViewChild,
ViewEncapsulation,
} from '@angular/core';
import {
_readKeyCode,
CanColor,
Constructor,
dtFadeAnimation,
isDefined,
mixinColor,
} from '@dynatrace/barista-components/core';
import { clamp, round } from 'lodash-es';
import { Subject } from 'rxjs';
import { PlotBackgroundInfo } from '../utils';
import {
DT_HEATFIELD_OVERLAY_POSITIONS,
DT_HEATFIELD_TOP_OFFSET,
} from './chart-heatfield-config';
/** Event object emitted by DtOption when selected or deselected. */
export class DtChartHeatfieldActiveChange {
constructor(
/** Reference to the heatfield that emitted the event. */
public source: DtChartHeatfield,
) {}
}
export type DtChartHeatfieldThemePalette = 'main' | 'error';
// Boilerplate for applying mixins to DtHeatfield.
export class DtHeatfieldBase {
constructor(public _elementRef: ElementRef) {}
}
export const _DtHeatfieldMixinBase = mixinColor<
Constructor<DtHeatfieldBase>,
DtChartHeatfieldThemePalette
>(DtHeatfieldBase, 'error');
@Component({
selector: 'dt-chart-heatfield',
templateUrl: 'chart-heatfield.html',
styleUrls: ['chart-heatfield.scss'],
exportAs: 'dtChartHeatfield',
changeDetection: ChangeDetectionStrategy.OnPush,
preserveWhitespaces: false,
encapsulation: ViewEncapsulation.Emulated,
inputs: ['color'],
animations: [dtFadeAnimation],
})
export class DtChartHeatfield extends _DtHeatfieldMixinBase
implements CanColor<DtChartHeatfieldThemePalette>, OnDestroy {
/** Event emitted when the option is selected or deselected. */
@Output() readonly activeChange = new EventEmitter<
DtChartHeatfieldActiveChange
>();
/** @internal The current state of the animation. */
_overlayAnimationState: 'void' | 'fadeIn' = 'void';
private _start: number;
/** Start on the xAxis of the chart for the heatfield */
@Input()
get start(): number {
return this._start;
}
set start(val: number) {
if (this._start !== val) {
this._start = val;
this._updatePosition();
}
}
private _end: number;
/** End on the xAxis of the chart for the heatfield */
@Input()
get end(): number {
return this._end;
}
set end(val: number) {
if (this._end !== val) {
this._end = val;
this._updatePosition();
}
}
private _active = false;
/** Wether the heatfield is currently active */
@Input()
get active(): boolean {
return this._active;
}
set active(val: boolean) {
const coercedValue = coerceBooleanProperty(val);
if (this._active !== coercedValue) {
this._active = coercedValue;
this._overlayAnimationState = coercedValue ? 'fadeIn' : 'void';
this.activeChange.next(new DtChartHeatfieldActiveChange(this));
this._changeDetectorRef.markForCheck();
}
}
/** Provide an aria-label for better accessibility. */
@Input('aria-label') ariaLabel: string;
/** Aria reference to a label for better accessibility. */
@Input('aria-labelledby') ariaLabelledBy: string;
/**
* @internal
* BoundingBox of the Highcharts plotBackground is
* set by the chart via the `_initHeatfield` function that
* is called by the chart.
* Is used to set the height of the heatfield backdrop.
*/
_boundingBox: PlotBackgroundInfo;
/**
* Positions for the overlay that gets created
* @internal
*/
_positions: ConnectedPosition[] = DT_HEATFIELD_OVERLAY_POSITIONS;
/**
* Wether the input is a valid range. Input range is valid as long there is a
* start value given. If there is a distinct end value given, it needs to be
* larger than the start.
* @internal
*/
get _isValidStartEndRange(): boolean {
const isValidEnd = isDefined(this.end) ? this.start < this.end : true;
return isDefined(this.start) && isValidEnd;
}
/** @internal The backdrop that is positioned behind the chart */
@ViewChild('backdrop', { read: ElementRef, static: true })
_backdrop: ElementRef;
/** @internal The marker button used to activate the heatfield */
@ViewChild('marker', { read: ElementRef, static: true }) _marker: ElementRef;
/** @internal The reference to the overlay. */
@ViewChild(CdkConnectedOverlay)
_overlay: CdkConnectedOverlay;
private _destroy$ = new Subject<void>();
private _relativeBoundingBox: { top: number; left: number; width: number };
/**
* Highcharts charts object is needed for the xAxis is
* set by the chart via the `_initHeatfield` function that
* is called by the chart.
*/
private _chartObject: Highcharts.Chart;
constructor(
elementRef: ElementRef,
private _changeDetectorRef: ChangeDetectorRef,
) {
super(elementRef);
}
ngOnDestroy(): void {
this._destroy$.next();
this._destroy$.complete();
}
/**
* @internal
* Initializes the heatfield with the provided bounding box of the chart
* and the Highcharts chartObject that is needed to calculate the position
*/
_initHeatfield(
boundingBox: PlotBackgroundInfo,
chartObject: Highcharts.Chart,
): void {
this._boundingBox = boundingBox;
this._chartObject = chartObject;
this._updatePosition();
if (this._overlay.overlayRef) {
this._overlay.overlayRef.updatePosition();
}
}
/**
* Toggles the active state
* @internal
*/
_toggleActive(): void {
this.active = !this.active;
}
/**
* Keydown handler
* @internal
*/
_handleKeydown(event: KeyboardEvent): void {
if (_readKeyCode(event) === ESCAPE) {
this.active = false;
}
}
/** Calculates and updates the position for the heatfield */
private _updatePosition(): void {
if (this._isValidStartEndRange && this._chartObject && this._boundingBox) {
const xAxis = this._chartObject.xAxis;
if (xAxis && xAxis.length > 0) {
// NOTE that there can be multiple xAxis in highcharts but our charts don't have multiple xAxis
// so we use the first one for the extremes
const extremes = xAxis[0].getExtremes();
const pxPerUnit =
this._boundingBox.width / (extremes.max - extremes.min);
const left =
(clamp(this._start, extremes.min, extremes.max) - extremes.min) *
pxPerUnit;
// calculate the width based on start/end values or from start to the edge of the chart
const width = isDefined(this._end)
? (clamp(this._end, extremes.min, extremes.max) - extremes.min) *
pxPerUnit -
left
: (extremes.max - extremes.min) * pxPerUnit - left;
// tslint:disable:no-magic-numbers
this._relativeBoundingBox = {
left: round(left + this._boundingBox.left, 2),
width: round(width, 2),
top: round(this._boundingBox.top - DT_HEATFIELD_TOP_OFFSET, 2),
};
// tslint:enable:no-magic-numbers
// It is not possible to set this on the ref nativelement and not in the marker and the backdrop,
// because we need to have the backdrop behind the chart and the marker in front.
this._updateMarkerPosition();
this._updateBackdropPosition();
}
}
}
/** Update marker position */
private _updateMarkerPosition(): void {
this._marker.nativeElement.style.left = `${this._relativeBoundingBox.left}px`;
this._marker.nativeElement.style.width = `${this._relativeBoundingBox.width}px`;
}
/** Update backdrop position */
private _updateBackdropPosition(): void {
if (this._backdrop) {
this._backdrop.nativeElement.style.left = `${this._relativeBoundingBox.left}px`;
this._backdrop.nativeElement.style.width = `${this._relativeBoundingBox.width}px`;
this._backdrop.nativeElement.style.top = `${DT_HEATFIELD_TOP_OFFSET}px`;
}
}
}