Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 | 1x 9x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 12x 12x 4x 4x 4x 4x 4x 8x 8x 10x 10x 10x 10x 10x 4x 4x 4x 6x 6x 11x 11x 85x 85x 85x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 90x 90x 90x 6x 6x 6x 1x 1x 1x | /**
* Copyright (c) Siemens 2016 - 2026
* SPDX-License-Identifier: MIT
*/
import {
booleanAttribute,
Component,
computed,
inject,
input,
LOCALE_ID,
OnChanges,
SimpleChange,
SimpleChanges
} from '@angular/core';
import { SiChartBaseComponent, GaugeSeriesOption, echarts } from '@siemens/charts-ng/common';
import { SiCustomLegendComponent } from '@siemens/charts-ng/custom-legend';
import { SiChartLoadingSpinnerComponent } from '@siemens/charts-ng/loading-spinner';
import { GaugeChart } from 'echarts/charts';
import { GaugeChartSeries } from './si-chart-gauge.interface';
echarts.use([GaugeChart]);
@Component({
selector: 'si-chart-gauge',
imports: [SiCustomLegendComponent, SiChartLoadingSpinnerComponent],
templateUrl: '../common/si-chart-base.component.html',
styleUrl: '../common/si-chart-base.component.scss'
})
export class SiChartGaugeComponent extends SiChartBaseComponent implements OnChanges {
/** @defaultValue 0 */
readonly minValue = input(0);
/** @defaultValue 100 */
readonly maxValue = input(100);
/** @defaultValue 0 */
readonly value = input(0);
readonly splitSteps = input<number>();
/** @defaultValue true */
readonly responsiveSplitSteps = input(true);
/** @defaultValue '%' */
readonly unit = input('%');
/** @defaultValue false */
readonly unitsOnSplit = input(false);
/**
* Custom formatter for axis labels.
* Takes precedence when specified, i.e. number of decimals, unit etc needs
* to be set by the user using this formatter.
*/
readonly labelFormatter = input<(val: number) => string>();
/**
* Custom formatter for value.
* Takes precedence when specified, i.e. number of decimals needs
* to be set by the user using this formatter.
*/
readonly valueFormatter = input<(val: number) => string>();
/**
* Min number of decimals.
* Possible values are from 0 to 100.
* @defaultValue 0
*/
readonly minNumberOfDecimals = input(0);
/**
* Max number of decimals.
* Possible values are from 0 to 100.
* @defaultValue 0
*/
readonly maxNumberOfDecimals = input(0);
/**
* Number of decimals on the axis.
* Possible values are from 0 to 100.
* @defaultValue 0
*/
readonly axisNumberOfDecimals = input(0);
/** @defaultValue false */
readonly hideAxisLabels = input(false, { transform: booleanAttribute });
/**
* Segments on the arc from 0 (implicit) to 1 (explicit)
* E.g. 0.2 means from 0 to 0.2 of the possible range (maxValue - minValue)
*
* @defaultValue [0.2, 0.8, 1]
*/
readonly segments = input<number[]>([0.2, 0.8, 1]);
/**
* Colors for the defined segments.
* If there are less colors then segments, the colors will be repeated. Defaults to the standard color palette
*/
readonly colors = input<string[]>();
private readonly locale = inject(LOCALE_ID).toString();
private readonly numberFormat = computed(() => {
const minDecimals = this.minNumberOfDecimals();
return new Intl.NumberFormat(this.locale, {
minimumFractionDigits: minDecimals,
maximumFractionDigits: Math.max(minDecimals, this.maxNumberOfDecimals())
});
});
private readonly axisNumberFormat = computed(() => {
const decimals = this.axisNumberOfDecimals();
return new Intl.NumberFormat(this.locale, {
minimumFractionDigits: decimals,
maximumFractionDigits: decimals
});
});
protected override afterChartInit(): void {
super.afterChartInit();
this.applyOptions();
this.updateColors();
this.refreshSeries();
}
private updateColors(): void {
// gauge doesn't respect color palette, it needs a bit convincing
const effectiveOpts: any = this.getOptionNoClone();
const colors: string[] = this.colors() ?? effectiveOpts.color;
const hasIndicator = !!this.segments().length;
let newColors: [number, string][];
if (hasIndicator) {
newColors = this.segments().map((threshold, index) => {
const color = colors[index % colors.length];
return [threshold, color];
});
} else E{
const colorsValue = this.colors();
const dataColor = colorsValue?.length
? colorsValue[0]
: this.getThemeCustomValue(['gauge', 'defaultColor'], colors[0]);
newColors = [[1, dataColor]];
}
Eif (hasIndicator) {
this.setAxisLineColor(newColors, (this.actualOptions.series![0] as any).axisLine);
}
(this.actualOptions.series![0] as any).axisLine.show = hasIndicator;
this.setAxisLineColor(newColors, (this.actualOptions.series![2] as any).axisLine);
this.refreshSeries();
}
private setAxisLineColor(colors: [number, string][], axisLine: any): void {
axisLine.lineStyle = axisLine.lineStyle ?? {};
axisLine.lineStyle.color = colors;
}
private getResponsiveConfig(): {
outerRadius: number;
center: (string | number)[];
progressWidth: number;
indicatorWidth: number;
gap: number;
splits: number;
axisLabelDist: number;
axisFontSize: number;
valueFontSize: number;
unitFontSize: number;
} {
const marginY = 12;
const maxHeight = this.curHeight - marginY;
const dim = Math.min(this.curWidth / 2, maxHeight);
const top = dim < maxHeight ? this.curHeight / 2 + dim / 2 : maxHeight;
if (dim < 150) {
const splits = dim < 100 ? 1 : 4;
const axisLabelDist = dim < 100 ? -6 : 0;
return {
outerRadius: dim - 4,
center: ['50%', top],
progressWidth: 8,
indicatorWidth: 1,
gap: 2,
splits,
axisLabelDist,
axisFontSize: 12,
valueFontSize: 18,
unitFontSize: 12
};
}
Eif (dim < 200) {
return {
outerRadius: dim - 7,
center: ['50%', top],
progressWidth: 14,
indicatorWidth: 3,
gap: 3,
splits: 6,
axisLabelDist: 12,
axisFontSize: 16,
valueFontSize: 30,
unitFontSize: 14
};
}
return {
outerRadius: dim - 12,
center: ['50%', top],
progressWidth: 24,
indicatorWidth: 4,
gap: 5,
splits: 0,
axisLabelDist: 20,
axisFontSize: 20,
valueFontSize: 40,
unitFontSize: 14
};
}
private calcValue(value: number): number {
// workaround for bug in echarts
Iif (value === this.minValue()) {
return value + 0.0001;
}
return value;
}
private readonly axisLabelFormatter = (value: any): string => {
const formatted = this.axisNumberFormat().format(Number(value));
return this.unitsOnSplit() ? `${formatted} ${this.unit()}` : formatted;
};
protected override applyOptions(): void {
const resp = this.getResponsiveConfig();
const hasIndicator = !!this.segments().length;
const splitNumber =
this.responsiveSplitSteps() && resp.splits ? resp.splits : this.splitSteps();
const grey = this.getThemeCustomValue(['gauge', 'grey'], '#F0F2F5');
const axisColor = this.getThemeCustomValue(['gauge', 'unit'], '#656A6F');
const indicator: GaugeSeriesOption = {
type: 'gauge',
radius: resp.outerRadius - resp.progressWidth - resp.gap,
startAngle: 180,
endAngle: 0,
min: this.minValue(),
max: this.maxValue(),
center: resp.center,
axisLine: {
show: hasIndicator,
lineStyle: { width: resp.indicatorWidth }
},
axisTick: {
length: 0,
splitNumber: 1,
lineStyle: {
width: 2,
color: axisColor
}
},
splitLine: {
length: 0,
lineStyle: {
width: 1,
color: axisColor
}
},
axisLabel: { show: false }
};
const labelDistance = resp.axisLabelDist + (hasIndicator ? resp.indicatorWidth + resp.gap : 0);
const shade: GaugeSeriesOption = {
type: 'gauge',
radius: resp.outerRadius,
startAngle: 180,
endAngle: 0,
min: this.minValue(),
max: this.maxValue(),
splitNumber,
axisTick: { show: false },
splitLine: { show: false },
title: { show: false },
center: resp.center,
axisLine: {
lineStyle: {
width: resp.progressWidth,
opacity: 0.6,
color: [[1, grey]]
}
},
axisLabel: {
show: !this.hideAxisLabels(),
distance: labelDistance,
color: axisColor,
fontSize: resp.axisFontSize,
formatter: this.labelFormatter() ?? this.axisLabelFormatter
}
};
const data: GaugeSeriesOption = {
type: 'gauge',
silent: false,
radius: resp.outerRadius,
startAngle: 180,
endAngle: 0,
min: this.minValue(),
max: this.maxValue(),
splitNumber,
progress: {
show: true,
itemStyle: { color: 'auto' },
width: resp.progressWidth
},
emphasis: {
// This code is added to avoid flickering when the progress is hovered and
// when mouse effects are enabled with `silent: false` flag. The issue occurs
// because of the progress.itemStyle.color is set to `auto` which tries to
// calculate the color dynamically and it conflicts with the mouse hover action
// which tries to apply the hover style
itemStyle: { color: 'auto' }
},
pointer: {
show: true,
length: '0%',
itemStyle: {
color: 'auto',
opacity: 0.001
}
},
center: resp.center,
axisLine: {
show: false,
lineStyle: { opacity: 0.6 }
},
axisTick: { show: false },
splitLine: { show: false },
axisLabel: { show: false },
title: { show: false },
detail: {
offsetCenter: [0, -0.5 * resp.valueFontSize],
valueAnimation: true,
formatter: this.valueFormatterInternal(),
rich: {
value: {
fontSize: resp.valueFontSize,
lineHeight: resp.valueFontSize,
fontWeight: 600
},
unit: {
fontSize: resp.unitFontSize,
lineHeight: resp.unitFontSize,
padding: [0, 0, (resp.unitFontSize - resp.valueFontSize) / 2, 0]
}
}
},
data: [{ value: this.calcValue(this.value()) }]
};
const series: GaugeSeriesOption[] = [indicator, shade, data];
this.showLegend.set(false);
this.actualOptions.series = series;
this.applyTitles();
}
private valueFormatterInternal(): (val: number) => string {
const unit = this.unit();
const valueFormatter = this.valueFormatter();
const labelFormatter = this.labelFormatter();
const formattedUnit = unit ? (unit.length > 5 ? `\n{unit|${unit}}` : ` {unit|${unit}}`) : '';
return (value: number): string => {
Iif (valueFormatter) {
return `{value|${valueFormatter(value)}}${formattedUnit}`;
}
// DEPRECATED: Use the new input `valueFormatter` to format the value.
// labelFormatter should be removed from here in future versions.
Iif (labelFormatter) {
return labelFormatter(value);
}
return `{value|${this.numberFormat().format(value)}}${formattedUnit}`;
};
}
override ngOnChanges(changes: SimpleChanges<unknown>): void {
Iif (this.chart && (changes.palette || changes.colors || changes.segments)) {
this.updateColors();
}
changes.forceAll = new SimpleChange(false, true, false);
super.ngOnChanges(changes);
}
protected override applyDataZoom(): void {}
protected override afterChartResize(): void {
this.applyOptions();
this.refreshSeries();
}
/**
* Update gauge chart value.
*/
setValue(value: number): void {
const optionSeries = this.actualOptions.series as GaugeChartSeries[];
optionSeries[2].data[0].value = this.calcValue(value);
this.refreshSeries();
}
}
|