From 50cd8d995b8fef2d34ec2c921d649e39d21929ae Mon Sep 17 00:00:00 2001 From: Peter Holmberg Date: Wed, 12 Dec 2018 11:04:34 +0100 Subject: [PATCH] display value map or range map --- public/app/viz/Gauge.tsx | 39 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 36 insertions(+), 3 deletions(-) diff --git a/public/app/viz/Gauge.tsx b/public/app/viz/Gauge.tsx index 34d657b0503..d918752f287 100644 --- a/public/app/viz/Gauge.tsx +++ b/public/app/viz/Gauge.tsx @@ -1,6 +1,6 @@ import React, { PureComponent } from 'react'; import $ from 'jquery'; -import { RangeMap, Threshold, TimeSeriesVMs, ValueMap } from 'app/types'; +import { MappingType, RangeMap, Threshold, TimeSeriesVMs, ValueMap } from 'app/types'; import config from '../core/config'; import kbn from '../core/utils/kbn'; @@ -47,16 +47,49 @@ export class Gauge extends PureComponent { this.draw(); } + formatWithMappings(mappings, value) { + const valueMaps = mappings.filter(m => m.type === MappingType.ValueToText); + const rangeMaps = mappings.filter(m => m.type === MappingType.RangeToText); + + const valueMap = valueMaps.map(mapping => { + if (mapping.value && value === mapping.value) { + return mapping.text; + } + })[0]; + + const rangeMap = rangeMaps.map(mapping => { + if (mapping.from && mapping.to && value > mapping.from && value < mapping.to) { + return mapping.text; + } + })[0]; + + return { + rangeMap, + valueMap, + }; + } + formatValue(value) { - const { decimals, prefix, suffix, unit } = this.props; + const { decimals, mappings, prefix, suffix, unit } = this.props; const formatFunc = kbn.valueFormats[unit]; + const formattedValue = formatFunc(value, decimals); + + if (mappings.length > 0) { + const { rangeMap, valueMap } = this.formatWithMappings(mappings, formattedValue); + + if (valueMap) { + return valueMap; + } else if (rangeMap) { + return rangeMap; + } + } if (isNaN(value)) { return '-'; } - return `${prefix} ${formatFunc(value, decimals)} ${suffix}`; + return `${prefix} ${formattedValue} ${suffix}`; } draw() {