Skip to content

Commit 85c4978

Browse files
committed
refactor: fixing width and first render
1 parent 539beb1 commit 85c4978

File tree

2 files changed

+32
-21
lines changed

2 files changed

+32
-21
lines changed

projects/components/src/gauge/gauge.component.scss

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
@import 'font';
22
@import 'color-palette';
33

4+
.gauge-container {
5+
width: 100%;
6+
height: 100%;
7+
}
8+
49
.gauge {
510
width: 100%;
611
height: 100%;

projects/components/src/gauge/gauge.component.ts

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,28 @@ import { Arc, arc, DefaultArcObject } from 'd3-shape';
55
@Component({
66
selector: 'ht-gauge',
77
template: `
8-
<svg #chartContainer class="gauge" (htLayoutChange)="this.onLayoutChange()" *ngIf="this.rendererData">
9-
<g attr.transform="translate({{ rendererData.origin.x }}, {{ rendererData.origin.y }})">
10-
<path class="gauge-ring" [attr.d]="rendererData.backgroundArc" />
11-
<g
12-
class="input-data"
13-
*ngIf="rendererData.data"
14-
htTooltip="{{ rendererData.data.value }} of {{ rendererData.data.maxValue }}"
15-
>
16-
<path
17-
class="value-ring"
18-
[attr.d]="rendererData.data.valueArc"
19-
[attr.fill]="rendererData.data.threshold.color"
20-
/>
21-
<text x="0" y="0" class="value-display" [attr.fill]="rendererData.data.threshold.color">
22-
{{ rendererData.data.value }}
23-
</text>
24-
<text x="0" y="24" class="label-display">{{ rendererData.data.threshold.label }}</text>
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>
2527
</g>
26-
</g>
27-
</svg>
28+
</svg>
29+
</div>
2830
`,
2931
styleUrls: ['./gauge.component.scss'],
3032
changeDetection: ChangeDetectionStrategy.OnPush
@@ -44,14 +46,18 @@ export class GaugeComponent implements OnChanges {
4446
public thresholds: GaugeThreshold[] = [];
4547

4648
public rendererData?: GaugeSvgRendererData;
49+
public hasLayoutChangeReceived: boolean = false;
4750

4851
public constructor(public readonly elementRef: ElementRef) {}
4952

5053
public ngOnChanges(): void {
51-
this.rendererData = this.buildRendererData();
54+
if (this.hasLayoutChangeReceived) {
55+
this.rendererData = this.buildRendererData();
56+
}
5257
}
5358

5459
public onLayoutChange(): void {
60+
this.hasLayoutChangeReceived = true;
5561
this.rendererData = this.buildRendererData();
5662
}
5763

@@ -107,7 +113,7 @@ export class GaugeComponent implements OnChanges {
107113
private buildRadius(boundingBox: ClientRect): number {
108114
return Math.min(
109115
boundingBox.height - GaugeComponent.GAUGE_AXIS_PADDING,
110-
boundingBox.width / 2 - GaugeComponent.GAUGE_AXIS_PADDING / 2
116+
boundingBox.height / 2 + Math.min(boundingBox.height, boundingBox.width) / 2
111117
);
112118
}
113119

0 commit comments

Comments
 (0)