From 74fec89ffd344a90e22762698820865d68832fc7 Mon Sep 17 00:00:00 2001 From: Yongjie Zhao Date: Tue, 10 Aug 2021 12:15:12 +0100 Subject: [PATCH] fix: 0 indicator on radar viz (#1282) * fix: 0 indicator on radar viz * fix lint --- .../src/Radar/transformProps.ts | 35 ++++++++++++++++--- 1 file changed, 31 insertions(+), 4 deletions(-) diff --git a/superset-frontend/temporary_superset_ui/superset-ui/plugins/plugin-chart-echarts/src/Radar/transformProps.ts b/superset-frontend/temporary_superset_ui/superset-ui/plugins/plugin-chart-echarts/src/Radar/transformProps.ts index 3c1581d4e5153..b83e76725a16a 100644 --- a/superset-frontend/temporary_superset_ui/superset-ui/plugins/plugin-chart-echarts/src/Radar/transformProps.ts +++ b/superset-frontend/temporary_superset_ui/superset-ui/plugins/plugin-chart-echarts/src/Radar/transformProps.ts @@ -19,6 +19,7 @@ import { CategoricalColorNamespace, DataRecordValue, + ensureIsInt, getMetricLabel, getNumberFormatter, getTimeFormatter, @@ -100,6 +101,7 @@ export default function transformProps( const metricsLabel = metrics.map(metric => getMetricLabel(metric)); + const metricLabelAndMaxValueMap = new Map(); const columnsLabelMap = new Map(); const transformedData: RadarSeriesDataItemOption[] = []; data.forEach(datum => { @@ -115,6 +117,22 @@ export default function transformProps( groupby.map(col => datum[col]), ); + // put max value of series into metricLabelAndMaxValueMap + // eslint-disable-next-line no-restricted-syntax + for (const [metricLabel, value] of Object.entries(datum)) { + if (metricLabelAndMaxValueMap.has(metricLabel)) { + metricLabelAndMaxValueMap.set( + metricLabel, + Math.max( + value as number, + ensureIsInt(metricLabelAndMaxValueMap.get(metricLabel), Number.MIN_SAFE_INTEGER), + ), + ); + } else { + metricLabelAndMaxValueMap.set(metricLabel, value as number); + } + } + const isFiltered = filterState.selectedValues && !filterState.selectedValues.includes(joinedName); @@ -148,10 +166,19 @@ export default function transformProps( {}, ); - const indicator = metricsLabel.map(metricLabel => ({ - name: metricLabel, - max: columnConfig?.[metricLabel]?.radarMetricMaxValue, - })); + const indicator = metricsLabel.map(metricLabel => { + const maxValueInControl = columnConfig?.[metricLabel]?.radarMetricMaxValue; + // Ensure that 0 is at the center of the polar coordinates + const metricValueAsMax = + metricLabelAndMaxValueMap.get(metricLabel) === 0 + ? Number.MAX_SAFE_INTEGER + : metricLabelAndMaxValueMap.get(metricLabel); + const max = maxValueInControl === null ? metricValueAsMax : maxValueInControl; + return { + name: metricLabel, + max, + }; + }); const series: RadarSeriesOption[] = [ {