diff --git a/ui-ngx/src/app/modules/home/components/widget/lib/analogue-gauge.models.ts b/ui-ngx/src/app/modules/home/components/widget/lib/analogue-gauge.models.ts index 8f7c64ab74..3383f28df6 100644 --- a/ui-ngx/src/app/modules/home/components/widget/lib/analogue-gauge.models.ts +++ b/ui-ngx/src/app/modules/home/components/widget/lib/analogue-gauge.models.ts @@ -4,13 +4,14 @@ import * as CanvasGauges from 'canvas-gauges'; import { FontSettings, getFontFamily } from '@home/components/widget/lib/settings.models'; import { WidgetContext } from '@home/models/widget-component.models'; -import { isDefined, isDefinedAndNotNull } from '@core/utils'; +import { isDefined, isDefinedAndNotNull, isNumeric } from '@core/utils'; import tinycolor from 'tinycolor2'; import { TbUnit } from '@shared/models/unit.models'; import { ValueFormatProcessor } from '@shared/models/widget-settings.models'; import { UnitService } from '@core/services/unit.service'; import { DataKey } from '@shared/models/widget.models'; import { CustomTranslatePipe } from '@shared/pipe/custom-translate.pipe'; +import { NOT_SUPPORTED } from '@shared/models/telemetry/telemetry.models'; import Highlight = CanvasGauges.Highlight; import BaseGauge = CanvasGauges.BaseGauge; import GenericOptions = CanvasGauges.GenericOptions; @@ -61,6 +62,9 @@ interface BaseGaugeModel extends BaseGauge { _value?: number; } +export const getGaugeValueText = (value: any): string => + !isDefinedAndNotNull(value) || value === '' ? 'N/A' : !isNumeric(value) ? NOT_SUPPORTED : ''; + export abstract class TbBaseGauge { private gauge: BaseGaugeModel; @@ -80,20 +84,23 @@ export abstract class TbBaseGauge { update() { if (this.ctx.data.length > 0) { const cellData = this.ctx.data[0]; - if (cellData.data.length > 0) { - const tvPair = cellData.data[cellData.data.length - 1]; - let value = parseFloat(tvPair[1]); - if (this.formatValue) { - value = parseFloat(this.formatValue.format(value)); - } - if (value !== this.gauge.value) { - if (!this.gauge.options.animation) { - this.gauge._value = value; - } else { - delete this.gauge._value; - } - this.gauge.value = value; + const origValue = cellData.data.length > 0 ? cellData.data[cellData.data.length - 1][1] : null; + let value = parseFloat(origValue); + if (this.formatValue) { + value = parseFloat(this.formatValue.format(value)); + } + const valueText = getGaugeValueText(origValue); + if (this.gauge.options.valueText !== valueText) { + this.gauge.update({valueText} as GenericOptions); + } + const gaugeValue = valueText ? this.gauge.options.minValue : value; + if (!isNaN(gaugeValue) && gaugeValue !== this.gauge.value) { + if (!this.gauge.options.animation) { + this.gauge._value = gaugeValue; + } else { + delete this.gauge._value; } + this.gauge.value = gaugeValue; } } } diff --git a/ui-ngx/src/app/modules/home/components/widget/lib/canvas-digital-gauge.ts b/ui-ngx/src/app/modules/home/components/widget/lib/canvas-digital-gauge.ts index 9c864a578e..29b617e619 100644 --- a/ui-ngx/src/app/modules/home/components/widget/lib/canvas-digital-gauge.ts +++ b/ui-ngx/src/app/modules/home/components/widget/lib/canvas-digital-gauge.ts @@ -1,5 +1,6 @@ // SPDX-FileCopyrightText: Copyright The ThingsBoard Authors -// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Modifications Copyright ThingsBoard, Inc. +// SPDX-License-Identifier: Apache-2.0 AND BUSL-1.1 import * as CanvasGauges from 'canvas-gauges'; import { FontStyle, FontWeight } from '@home/components/widget/lib/settings.models'; import tinycolor from 'tinycolor2'; @@ -750,19 +751,26 @@ function drawDigitalValue(context: DigitalGaugeCanvasRenderingContext2D, options return; } - const {valueY, baseX, width, fontSizeFactor, fontValueBaseline} = + const {valueY, baseX, width, Ri, fontSizeFactor, fontValueBaseline} = context.barDimensions; const textX = Math.round(baseX + width / 2); const textY = valueY; - let text = options.valueText || padValue(options.valueFormat.format(value), options.valueDec); - text += options.symbol; + const text = options.valueText ? options.valueText : padValue(options.valueFormat.format(value), options.valueDec) + options.symbol; context.save(); context.textAlign = 'center'; context.textBaseline = fontValueBaseline; context.font = Drawings.font(options, 'Value', fontSizeFactor); + if (options.valueText) { + const isRound = options.gaugeType === 'donut' || options.gaugeType === 'arc'; + const maxTextWidth = (isRound ? Ri * 2 : width) * 0.9; + const textWidth = context.measureText(text).width; + if (textWidth > maxTextWidth) { + context.font = Drawings.font(options, 'Value', fontSizeFactor * maxTextWidth / textWidth); + } + } context.lineWidth = 0; drawText(context, options, 'Value', text, textX, textY); context.restore(); diff --git a/ui-ngx/src/app/modules/home/components/widget/lib/digital-gauge.ts b/ui-ngx/src/app/modules/home/components/widget/lib/digital-gauge.ts index 09a207752d..ffb8ecbf86 100644 --- a/ui-ngx/src/app/modules/home/components/widget/lib/digital-gauge.ts +++ b/ui-ngx/src/app/modules/home/components/widget/lib/digital-gauge.ts @@ -23,6 +23,7 @@ import { UnitService } from '@core/services/unit.service'; import { isNotEmptyTbUnits } from '@shared/models/unit.models'; import { CustomTranslatePipe } from '@shared/pipe/custom-translate.pipe'; import GenericOptions = CanvasGauges.GenericOptions; +import { getGaugeValueText } from '@home/components/widget/lib/analogue-gauge.models'; // @dynamic export class TbCanvasDigitalGauge { @@ -247,27 +248,32 @@ export class TbCanvasDigitalGauge { update() { if (this.ctx.data.length > 0) { const cellData = this.ctx.data[0]; - if (cellData.data.length > 0) { - const tvPair = cellData.data[cellData.data.length - - 1]; - let timestamp: number; - if (this.localSettings.showTimestamp) { - timestamp = tvPair[0]; - const filter = this.ctx.$injector.get(DatePipe); - (this.gauge.options as CanvasDigitalGaugeOptions).labelTimestamp = - filter.transform(timestamp, this.localSettings.timestampFormat); - } - const value = parseFloat(tvPair[1]); - if (value !== this.gauge.value) { - if (!this.gauge.options.animation) { - this.gauge._value = value; - } else { - delete this.gauge._value; - } - this.gauge.value = value; - } else if (this.localSettings.showTimestamp && this.gauge.timestamp !== timestamp) { - this.gauge.timestamp = timestamp; + const tvPair = cellData.data.length > 0 ? cellData.data[cellData.data.length - 1] : null; + let timestamp: number; + if (this.localSettings.showTimestamp && tvPair) { + timestamp = tvPair[0]; + const filter = this.ctx.$injector.get(DatePipe); + (this.gauge.options as CanvasDigitalGaugeOptions).labelTimestamp = + filter.transform(timestamp, this.localSettings.timestampFormat); + } + const origValue = tvPair ? tvPair[1] : null; + const value = parseFloat(origValue); + const options = this.gauge.options as CanvasDigitalGaugeOptions; + const valueText = getGaugeValueText(origValue); + if (options.valueText !== valueText) { + options.valueText = valueText; + this.gauge.update({} as CanvasDigitalGaugeOptions); + } + const gaugeValue = valueText ? options.minValue : value; + if (!isNaN(gaugeValue) && gaugeValue !== this.gauge.value) { + if (!this.gauge.options.animation) { + this.gauge._value = gaugeValue; + } else { + delete this.gauge._value; } + this.gauge.value = gaugeValue; + } else if (this.localSettings.showTimestamp && tvPair && this.gauge.timestamp !== timestamp) { + this.gauge.timestamp = timestamp; } } }