-
Notifications
You must be signed in to change notification settings - Fork 11
Gauge with layout service #339
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
20bbc93
feat: adding a new gauge widget
anandtiwary 1da0e38
refactor: updating package json
anandtiwary 244869d
refactor: remove subs life cycle service
anandtiwary 0691f9c
refactor: fixing radius and origin logic
anandtiwary a512fdc
refactor: updating type
anandtiwary f9b35e6
refactor: removing resize polyfill
anandtiwary 8769e6a
refactor: adding layout service
anandtiwary 2c9aea0
refactor: adding delay
anandtiwary File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
@import 'font'; | ||
@import 'color-palette'; | ||
|
||
.gauge { | ||
width: 100%; | ||
height: 100%; | ||
|
||
.gauge-ring { | ||
fill: $gray-2; | ||
} | ||
|
||
.input-data { | ||
cursor: default; | ||
} | ||
|
||
.value-ring { | ||
transition: transform 0.2s ease-out; | ||
} | ||
|
||
.value-display { | ||
font-style: normal; | ||
font-weight: bold; | ||
font-size: 56px; | ||
text-anchor: middle; | ||
} | ||
|
||
.label-display { | ||
@include body-1-semibold($gray-7); | ||
font-family: $font-family; | ||
text-anchor: middle; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { Color } from '@hypertrace/common'; | ||
import { runFakeRxjs } from '@hypertrace/test-utils'; | ||
import { createHostFactory, Spectator } from '@ngneat/spectator/jest'; | ||
import { MockDirective } from 'ng-mocks'; | ||
import { LayoutChangeDirective } from '../layout/layout-change.directive'; | ||
import { GaugeComponent } from './gauge.component'; | ||
import { GaugeModule } from './gauge.module'; | ||
|
||
describe('Gauge component', () => { | ||
let spectator: Spectator<GaugeComponent>; | ||
|
||
const createHost = createHostFactory({ | ||
component: GaugeComponent, | ||
declareComponent: false, | ||
declarations:[MockDirective(LayoutChangeDirective)], | ||
imports: [GaugeModule] | ||
}); | ||
|
||
test('render all data', () => { | ||
spectator = createHost(`<ht-gauge [value]="value" [maxValue]="maxValue" [thresholds]="thresholds"></ht-gauge>`, { | ||
hostProps: { | ||
value: 80, | ||
maxValue: 100, | ||
thresholds: [ | ||
{ | ||
label: 'Medium', | ||
start: 60, | ||
end: 90, | ||
color: Color.Brown1 | ||
}, | ||
{ | ||
label: 'High', | ||
start: 90, | ||
end: 100, | ||
color: Color.Red5 | ||
} | ||
] | ||
} | ||
}); | ||
spectator.component.onLayoutChange(); | ||
|
||
runFakeRxjs(({ expectObservable }) => { | ||
expectObservable(spectator.component.gaugeRendererData$).toBe('200ms (x)', { | ||
x: { | ||
backgroundArc: 'M0,0Z', | ||
origin: { | ||
x: 0, | ||
y: 0 | ||
}, | ||
data: { | ||
value: 80, | ||
maxValue: 100, | ||
valueArc: 'M0,0Z', | ||
threshold: { | ||
color: '#9e4c41', | ||
end: 90, | ||
label: 'Medium', | ||
start: 60 | ||
} | ||
} | ||
} | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,176 @@ | ||
import { ChangeDetectionStrategy, Component, ElementRef, Input, OnChanges } from '@angular/core'; | ||
import { Color, LayoutChangeService, Point } from '@hypertrace/common'; | ||
import { Arc, arc, DefaultArcObject } from 'd3-shape'; | ||
import { BehaviorSubject, combineLatest, Observable, Subject } from 'rxjs'; | ||
import { map, debounceTime } from 'rxjs/operators'; | ||
|
||
@Component({ | ||
selector: 'ht-gauge', | ||
template: ` | ||
<svg #chartContainer class="gauge" (htLayoutChange)="this.onLayoutChange()" *ngIf="this.gaugeRendererData$ | async as rendererData"> | ||
<g attr.transform="translate({{ rendererData.origin.x }}, {{ rendererData.origin.y }})"> | ||
<path class="gauge-ring" [attr.d]="rendererData.backgroundArc" /> | ||
<g | ||
class="input-data" | ||
*ngIf="rendererData.data" | ||
htTooltip="{{ rendererData.data.value }} of {{ rendererData.data.maxValue }}" | ||
> | ||
<path | ||
class="value-ring" | ||
[attr.d]="rendererData.data.valueArc" | ||
[attr.fill]="rendererData.data.threshold.color" | ||
/> | ||
<text x="0" y="0" class="value-display" [attr.fill]="rendererData.data.threshold.color"> | ||
{{ rendererData.data.value }} | ||
</text> | ||
<text x="0" y="24" class="label-display">{{ rendererData.data.threshold.label }}</text> | ||
</g> | ||
</g> | ||
</svg> | ||
`, | ||
styleUrls: ['./gauge.component.scss'], | ||
providers: [LayoutChangeService], | ||
changeDetection: ChangeDetectionStrategy.OnPush | ||
}) | ||
export class GaugeComponent implements OnChanges { | ||
private static readonly GAUGE_RING_WIDTH: number = 20; | ||
private static readonly GAUGE_ARC_CORNER_RADIUS: number = 10; | ||
private static readonly GAUGE_AXIS_PADDING: number = 30; | ||
|
||
@Input() | ||
public value?: number; | ||
|
||
@Input() | ||
public maxValue?: number; | ||
|
||
@Input() | ||
public thresholds: GaugeThreshold[] = []; | ||
|
||
public readonly gaugeRendererData$: Observable<GaugeSvgRendererData>; | ||
|
||
private readonly inputDataSubject: Subject<GaugeInputData | undefined> = new BehaviorSubject< | ||
GaugeInputData | undefined | ||
>(undefined); | ||
private readonly inputData$: Observable<GaugeInputData | undefined> = this.inputDataSubject.asObservable(); | ||
|
||
private readonly redrawSubject: Subject<true> = new BehaviorSubject(true); | ||
private readonly redraw$: Observable<true> = this.redrawSubject.pipe(debounceTime(100)); | ||
|
||
public constructor(public readonly elementRef: ElementRef) { | ||
this.gaugeRendererData$ = this.buildGaugeRendererDataObservable(); | ||
} | ||
|
||
public ngOnChanges(): void { | ||
this.emitInputData(); | ||
} | ||
|
||
public onLayoutChange(): void { | ||
this.redrawSubject.next(true); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not wrong, but a bit strange to do this with a subject. Could just as easily remove the subject + combineLatest and do |
||
} | ||
|
||
private buildGaugeRendererDataObservable(): Observable<GaugeSvgRendererData> { | ||
return combineLatest([this.inputData$, this.redraw$ ]).pipe( | ||
map(([inputData]) => { | ||
const boundingBox = this.elementRef.nativeElement.getBoundingClientRect(); | ||
const radius = this.buildRadius(boundingBox); | ||
|
||
return { | ||
origin: this.buildOrigin(boundingBox, radius), | ||
backgroundArc: this.buildBackgroundArc(radius), | ||
data: this.buildGaugeData(radius, inputData) | ||
}; | ||
}) | ||
); | ||
} | ||
|
||
private buildBackgroundArc(radius: number): string { | ||
return this.buildArcGenerator()({ | ||
innerRadius: radius - GaugeComponent.GAUGE_RING_WIDTH, | ||
outerRadius: radius, | ||
startAngle: -Math.PI / 2, | ||
endAngle: Math.PI / 2 | ||
})!; | ||
} | ||
|
||
private buildGaugeData(radius: number, inputData?: GaugeInputData): GaugeData | undefined { | ||
if (inputData === undefined) { | ||
return undefined; | ||
} | ||
|
||
return { | ||
valueArc: this.buildValueArc(radius, inputData), | ||
...inputData | ||
}; | ||
} | ||
|
||
private buildValueArc(radius: number, inputData: GaugeInputData): string { | ||
return this.buildArcGenerator()({ | ||
innerRadius: radius - GaugeComponent.GAUGE_RING_WIDTH, | ||
outerRadius: radius, | ||
startAngle: -Math.PI / 2, | ||
endAngle: -Math.PI / 2 + (inputData.value / inputData.maxValue) * Math.PI | ||
})!; | ||
} | ||
|
||
private buildArcGenerator(): Arc<unknown, DefaultArcObject> { | ||
return arc().cornerRadius(GaugeComponent.GAUGE_ARC_CORNER_RADIUS); | ||
} | ||
|
||
private buildRadius(boundingBox: ClientRect): number { | ||
return Math.min( | ||
boundingBox.height - GaugeComponent.GAUGE_AXIS_PADDING, | ||
boundingBox.height / 2 + Math.min(boundingBox.height, boundingBox.width) / 2 | ||
); | ||
} | ||
|
||
private buildOrigin(boundingBox: ClientRect, radius: number): Point { | ||
return { | ||
x: boundingBox.width / 2, | ||
y: radius | ||
}; | ||
} | ||
|
||
private emitInputData(): void { | ||
let inputData; | ||
if (this.value !== undefined && this.maxValue !== undefined && this.maxValue > 0 && this.thresholds.length > 0) { | ||
const currentThreshold = this.thresholds.find( | ||
threshold => this.value! >= threshold.start && this.value! < threshold.end | ||
); | ||
|
||
if (currentThreshold) { | ||
inputData = { | ||
value: this.value, | ||
maxValue: this.maxValue, | ||
threshold: currentThreshold | ||
}; | ||
} | ||
} | ||
this.inputDataSubject.next(inputData); | ||
} | ||
} | ||
|
||
export interface GaugeThreshold { | ||
label: string; | ||
start: number; | ||
end: number; | ||
color: Color; | ||
} | ||
|
||
interface GaugeSvgRendererData { | ||
origin: Point; | ||
backgroundArc: string; | ||
data?: GaugeData; | ||
} | ||
|
||
interface GaugeData { | ||
valueArc: string; | ||
value: number; | ||
maxValue: number; | ||
threshold: GaugeThreshold; | ||
} | ||
|
||
interface GaugeInputData { | ||
value: number; | ||
maxValue: number; | ||
threshold: GaugeThreshold; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { CommonModule } from '@angular/common'; | ||
import { NgModule } from '@angular/core'; | ||
import { FormattingModule } from '@hypertrace/common'; | ||
import { LayoutChangeModule } from '../layout/layout-change.module'; | ||
import { TooltipModule } from './../tooltip/tooltip.module'; | ||
import { GaugeComponent } from './gauge.component'; | ||
|
||
@NgModule({ | ||
declarations: [GaugeComponent], | ||
exports: [GaugeComponent], | ||
imports: [CommonModule, FormattingModule, TooltipModule, LayoutChangeModule] | ||
}) | ||
export class GaugeModule {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not needed - that's why we have the directive.