Skip to content
This repository has been archived by the owner on Dec 10, 2021. It is now read-only.

feat(plugin-chart-echarts): [wip] gauge chart enhancements and fixes #1070

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions plugins/plugin-chart-echarts/src/Gauge/controlPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* under the License.
*/
import React from 'react';
import { t, validateNonEmpty, validateInteger } from '@superset-ui/core';
import { t } from '@superset-ui/core';
import {
sharedControls,
ControlPanelConfig,
Expand Down Expand Up @@ -68,8 +68,7 @@ const config: ControlPanelConfig = {
config: {
type: 'TextControl',
isInt: true,
default: String(DEFAULT_FORM_DATA.minVal),
validators: [validateNonEmpty, validateInteger],
default: DEFAULT_FORM_DATA.minVal,
renderTrigger: true,
label: t('Min'),
description: t('Minimum value on the gauge axis'),
Expand All @@ -81,7 +80,6 @@ const config: ControlPanelConfig = {
type: 'TextControl',
isInt: true,
default: DEFAULT_FORM_DATA.maxVal,
validators: [validateNonEmpty, validateInteger],
renderTrigger: true,
label: t('Max'),
description: t('Maximum value on the gauge axis'),
Expand Down
45 changes: 31 additions & 14 deletions plugins/plugin-chart-echarts/src/Gauge/transformProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
import { EChartsOption, GaugeSeriesOption } from 'echarts';
import { GaugeDataItemOption } from 'echarts/types/src/chart/gauge/GaugeSeries';
import range from 'lodash/range';
import { CallbackDataParams } from 'echarts/types/src/util/types';
import { parseNumbersList } from '../utils/controls';
import {
DEFAULT_FORM_DATA as DEFAULT_GAUGE_FORM_DATA,
Expand Down Expand Up @@ -71,6 +72,12 @@ const setIntervalBoundsAndColors = (
const calculateAxisLineWidth = (data: DataRecord[], fontSize: number, overlap: boolean): number =>
overlap ? fontSize : data.length * fontSize;

const calculateMin = (data: GaugeDataItemOption[]) =>
2 * Math.min(...data.map(d => d.value as number).concat([0]));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why here must be 2 ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does not have to be, I was looking at how other BI tools do this and came up with 2. We could have 1.5, 1.2 or similar. The point is to ensure that the max value fits into the chart, but that there is a bit of additional "space" left on the gauge.


const calculateMax = (data: GaugeDataItemOption[]) =>
2 * Math.max(...data.map(d => d.value as number).concat([0]));

export default function transformProps(chartProps: ChartProps) {
const { width, height, formData, queriesData } = chartProps;
const {
Expand Down Expand Up @@ -98,26 +105,15 @@ export default function transformProps(chartProps: ChartProps) {
const data = (queriesData[0]?.data || []) as DataRecord[];
const numberFormatter = getNumberFormatter(numberFormat);
const colorFn = CategoricalColorNamespace.getScale(colorScheme as string);
const normalizer = maxVal;
const axisLineWidth = calculateAxisLineWidth(data, fontSize, overlap);
const axisLabels = range(minVal, maxVal, (maxVal - minVal) / splitNumber);
const axisLabelLength = Math.max(
...axisLabels.map(label => numberFormatter(label).length).concat([1]),
);
const formatValue = (value: number) => valueFormatter.replace('{value}', numberFormatter(value));
const axisTickLength = FONT_SIZE_MULTIPLIERS.axisTickLength * fontSize;
const splitLineLength = FONT_SIZE_MULTIPLIERS.splitLineLength * fontSize;
const titleOffsetFromTitle = FONT_SIZE_MULTIPLIERS.titleOffsetFromTitle * fontSize;
const detailOffsetFromTitle = FONT_SIZE_MULTIPLIERS.detailOffsetFromTitle * fontSize;
const intervalBoundsAndColors = setIntervalBoundsAndColors(
intervals,
intervalColorIndices,
colorFn,
normalizer,
);
const transformedData: GaugeDataItemOption[] = data.map((data_point, index) => ({
value: data_point[getMetricLabel(metric as QueryFormMetric)] as number,
name: groupby.map(column => `${column}: ${data_point[column]}`).join(', '),
name: groupby.map(column => `${column} = ${data_point[column]}`).join(', '),
itemStyle: {
color: colorFn(index),
},
Expand All @@ -133,6 +129,19 @@ export default function transformProps(chartProps: ChartProps) {
fontSize: FONT_SIZE_MULTIPLIERS.detailFontSize * fontSize,
},
}));
const min = minVal ?? calculateMin(transformedData);
const max = maxVal ?? calculateMax(transformedData);
const axisLabels = range(min, max, (max - min) / splitNumber);
const axisLabelLength = Math.max(
...axisLabels.map(label => numberFormatter(label).length).concat([1]),
);
const normalizer = max;
const intervalBoundsAndColors = setIntervalBoundsAndColors(
intervals,
intervalColorIndices,
colorFn,
normalizer,
);

const progress = {
show: showProgress,
Expand Down Expand Up @@ -181,6 +190,12 @@ export default function transformProps(chartProps: ChartProps) {
formatter: (value: number) => formatValue(value),
color: DEFAULT_GAUGE_SERIES_OPTION.detail?.color,
};
const tooltip = {
formatter: (params: CallbackDataParams) => {
const { name, value } = params;
return `${name} : ${formatValue(value as number)}`;
},
};
let pointer;

if (intervalBoundsAndColors.length) {
Expand All @@ -203,8 +218,8 @@ export default function transformProps(chartProps: ChartProps) {
type: 'gauge',
startAngle,
endAngle,
min: minVal,
max: maxVal,
min,
max,
progress,
animation,
axisLine: axisLine as GaugeSeriesOption['axisLine'],
Expand All @@ -214,11 +229,13 @@ export default function transformProps(chartProps: ChartProps) {
axisTick,
pointer,
detail,
tooltip,
data: transformedData,
},
];

const echartOptions: EChartsOption = {
tooltip: { trigger: 'item' },
series,
};

Expand Down
8 changes: 4 additions & 4 deletions plugins/plugin-chart-echarts/src/Gauge/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ export type EchartsGaugeFormData = {
groupby: string[];
metric?: object;
rowLimit: number;
minVal: number;
maxVal: number;
minVal: number | null;
maxVal: number | null;
fontSize: number;
numberFormat: string;
animation: boolean;
Expand All @@ -51,8 +51,8 @@ export const DEFAULT_FORM_DATA: EchartsGaugeFormData = {
...DEFAULT_LEGEND_FORM_DATA,
groupby: [],
rowLimit: 10,
minVal: 0,
maxVal: 100,
minVal: null,
maxVal: null,
fontSize: 15,
numberFormat: 'SMART_NUMBER',
animation: true,
Expand Down
12 changes: 6 additions & 6 deletions plugins/plugin-chart-echarts/test/Gauge/transformProps.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ describe('Echarts Gauge transformProps', () => {
data: [
{
value: 15,
name: 'year: 1988',
name: 'year = 1988',
itemStyle: {
color: '#1f77b4',
},
Expand All @@ -148,7 +148,7 @@ describe('Echarts Gauge transformProps', () => {
},
{
value: 219,
name: 'year: 1995',
name: 'year = 1995',
itemStyle: {
color: '#ff7f0e',
},
Expand Down Expand Up @@ -207,7 +207,7 @@ describe('Echarts Gauge transformProps', () => {
data: [
{
value: 140,
name: 'year: 2011, platform: PC',
name: 'year = 2011, platform = PC',
itemStyle: {
color: '#1f77b4',
},
Expand All @@ -222,7 +222,7 @@ describe('Echarts Gauge transformProps', () => {
},
{
value: 76,
name: 'year: 2008, platform: PC',
name: 'year = 2008, platform = PC',
itemStyle: {
color: '#ff7f0e',
},
Expand Down Expand Up @@ -296,7 +296,7 @@ describe('Echarts Gauge transformProps', () => {
data: [
{
value: 140,
name: 'year: 2011, platform: PC',
name: 'year = 2011, platform = PC',
itemStyle: {
color: '#1f77b4',
},
Expand All @@ -311,7 +311,7 @@ describe('Echarts Gauge transformProps', () => {
},
{
value: 76,
name: 'year: 2008, platform: PC',
name: 'year = 2008, platform = PC',
itemStyle: {
color: '#ff7f0e',
},
Expand Down