|
| 1 | +import { ChangeDetectionStrategy, Component, ElementRef, Input, OnChanges } from '@angular/core'; |
| 2 | +import { Color, Point } from '@hypertrace/common'; |
| 3 | +import { Arc, arc, DefaultArcObject } from 'd3-shape'; |
| 4 | + |
| 5 | +@Component({ |
| 6 | + selector: 'ht-gauge', |
| 7 | + template: ` |
| 8 | + <div class="gauge-container" (htLayoutChange)="this.onLayoutChange()"> |
| 9 | + <svg class="gauge" *ngIf="this.rendererData"> |
| 10 | + <g attr.transform="translate({{ rendererData.origin.x }}, {{ rendererData.origin.y }})"> |
| 11 | + <path class="gauge-ring" [attr.d]="rendererData.backgroundArc" /> |
| 12 | + <g |
| 13 | + class="input-data" |
| 14 | + *ngIf="rendererData.data" |
| 15 | + htTooltip="{{ rendererData.data.value }} of {{ rendererData.data.maxValue }}" |
| 16 | + > |
| 17 | + <path |
| 18 | + class="value-ring" |
| 19 | + [attr.d]="rendererData.data.valueArc" |
| 20 | + [attr.fill]="rendererData.data.threshold.color" |
| 21 | + /> |
| 22 | + <text x="0" y="0" class="value-display" [attr.fill]="rendererData.data.threshold.color"> |
| 23 | + {{ rendererData.data.value }} |
| 24 | + </text> |
| 25 | + <text x="0" y="24" class="label-display">{{ rendererData.data.threshold.label }}</text> |
| 26 | + </g> |
| 27 | + </g> |
| 28 | + </svg> |
| 29 | + </div> |
| 30 | + `, |
| 31 | + styleUrls: ['./gauge.component.scss'], |
| 32 | + changeDetection: ChangeDetectionStrategy.OnPush |
| 33 | +}) |
| 34 | +export class GaugeComponent implements OnChanges { |
| 35 | + private static readonly GAUGE_RING_WIDTH: number = 20; |
| 36 | + private static readonly GAUGE_ARC_CORNER_RADIUS: number = 10; |
| 37 | + private static readonly GAUGE_AXIS_PADDING: number = 30; |
| 38 | + |
| 39 | + @Input() |
| 40 | + public value?: number; |
| 41 | + |
| 42 | + @Input() |
| 43 | + public maxValue?: number; |
| 44 | + |
| 45 | + @Input() |
| 46 | + public thresholds: GaugeThreshold[] = []; |
| 47 | + |
| 48 | + public rendererData?: GaugeSvgRendererData; |
| 49 | + |
| 50 | + public constructor(public readonly elementRef: ElementRef) {} |
| 51 | + |
| 52 | + public ngOnChanges(): void { |
| 53 | + this.rendererData = this.buildRendererData(); |
| 54 | + } |
| 55 | + |
| 56 | + public onLayoutChange(): void { |
| 57 | + this.rendererData = this.buildRendererData(); |
| 58 | + } |
| 59 | + |
| 60 | + private buildRendererData(): GaugeSvgRendererData | undefined { |
| 61 | + const inputData = this.calculateInputData(); |
| 62 | + if (!inputData) { |
| 63 | + return undefined; |
| 64 | + } |
| 65 | + |
| 66 | + const boundingBox = this.elementRef.nativeElement.getBoundingClientRect(); |
| 67 | + const radius = this.buildRadius(boundingBox); |
| 68 | + |
| 69 | + return { |
| 70 | + origin: this.buildOrigin(boundingBox, radius), |
| 71 | + backgroundArc: this.buildBackgroundArc(radius), |
| 72 | + data: this.buildGaugeData(radius, inputData) |
| 73 | + }; |
| 74 | + } |
| 75 | + |
| 76 | + private buildBackgroundArc(radius: number): string { |
| 77 | + return this.buildArcGenerator()({ |
| 78 | + innerRadius: radius - GaugeComponent.GAUGE_RING_WIDTH, |
| 79 | + outerRadius: radius, |
| 80 | + startAngle: -Math.PI / 2, |
| 81 | + endAngle: Math.PI / 2 |
| 82 | + })!; |
| 83 | + } |
| 84 | + |
| 85 | + private buildGaugeData(radius: number, inputData?: GaugeInputData): GaugeData | undefined { |
| 86 | + if (inputData === undefined) { |
| 87 | + return undefined; |
| 88 | + } |
| 89 | + |
| 90 | + return { |
| 91 | + valueArc: this.buildValueArc(radius, inputData), |
| 92 | + ...inputData |
| 93 | + }; |
| 94 | + } |
| 95 | + |
| 96 | + private buildValueArc(radius: number, inputData: GaugeInputData): string { |
| 97 | + return this.buildArcGenerator()({ |
| 98 | + innerRadius: radius - GaugeComponent.GAUGE_RING_WIDTH, |
| 99 | + outerRadius: radius, |
| 100 | + startAngle: -Math.PI / 2, |
| 101 | + endAngle: -Math.PI / 2 + (inputData.value / inputData.maxValue) * Math.PI |
| 102 | + })!; |
| 103 | + } |
| 104 | + |
| 105 | + private buildArcGenerator(): Arc<unknown, DefaultArcObject> { |
| 106 | + return arc().cornerRadius(GaugeComponent.GAUGE_ARC_CORNER_RADIUS); |
| 107 | + } |
| 108 | + |
| 109 | + private buildRadius(boundingBox: ClientRect): number { |
| 110 | + return Math.min( |
| 111 | + boundingBox.height - GaugeComponent.GAUGE_AXIS_PADDING, |
| 112 | + boundingBox.height / 2 + Math.min(boundingBox.height, boundingBox.width) / 2 |
| 113 | + ); |
| 114 | + } |
| 115 | + |
| 116 | + private buildOrigin(boundingBox: ClientRect, radius: number): Point { |
| 117 | + return { |
| 118 | + x: boundingBox.width / 2, |
| 119 | + y: radius |
| 120 | + }; |
| 121 | + } |
| 122 | + |
| 123 | + private calculateInputData(): GaugeInputData | undefined { |
| 124 | + if (this.value !== undefined && this.maxValue !== undefined && this.maxValue > 0 && this.thresholds.length > 0) { |
| 125 | + const currentThreshold = this.thresholds.find( |
| 126 | + threshold => this.value! >= threshold.start && this.value! < threshold.end |
| 127 | + ); |
| 128 | + |
| 129 | + if (currentThreshold) { |
| 130 | + return { |
| 131 | + value: this.value, |
| 132 | + maxValue: this.maxValue, |
| 133 | + threshold: currentThreshold |
| 134 | + }; |
| 135 | + } |
| 136 | + } |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +export interface GaugeThreshold { |
| 141 | + label: string; |
| 142 | + start: number; |
| 143 | + end: number; |
| 144 | + color: Color; |
| 145 | +} |
| 146 | + |
| 147 | +interface GaugeSvgRendererData { |
| 148 | + origin: Point; |
| 149 | + backgroundArc: string; |
| 150 | + data?: GaugeData; |
| 151 | +} |
| 152 | + |
| 153 | +interface GaugeData { |
| 154 | + valueArc: string; |
| 155 | + value: number; |
| 156 | + maxValue: number; |
| 157 | + threshold: GaugeThreshold; |
| 158 | +} |
| 159 | + |
| 160 | +interface GaugeInputData { |
| 161 | + value: number; |
| 162 | + maxValue: number; |
| 163 | + threshold: GaugeThreshold; |
| 164 | +} |
0 commit comments