Skip to content

Commit

Permalink
fix(plugin-chart-echarts): add series deduplication (#1046)
Browse files Browse the repository at this point in the history
* fix(plugin-chart-echarts): add series deduplication

* refactor function
  • Loading branch information
villebro authored and zhaoyongjie committed Nov 26, 2021
1 parent 53df21d commit 6377933
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,12 @@ import { EChartsOption, SeriesOption } from 'echarts';
import { DEFAULT_FORM_DATA, EchartsTimeseriesFormData } from './types';
import { EchartsProps, ForecastSeriesEnum, ProphetValue, LegendOrientation } from '../types';
import { parseYAxisBound } from '../utils/controls';
import { extractTimeseriesSeries, getChartPadding, getLegendProps } from '../utils/series';
import {
dedupSeries,
extractTimeseriesSeries,
getChartPadding,
getLegendProps,
} from '../utils/series';
import { extractAnnotationLabels } from '../utils/annotation';
import {
extractForecastSeriesContext,
Expand Down Expand Up @@ -214,7 +219,7 @@ export default function transformProps(chartProps: ChartProps): EchartsProps {
.map(entry => entry.name || '')
.concat(extractAnnotationLabels(annotationLayers, annotationData)),
},
series,
series: dedupSeries(series),
toolbox: {
show: zoomable,
top: TIMESERIES_CONSTANTS.toolboxTop,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,3 +184,19 @@ export function getChartPadding(
bottom: bottom + (orientation === LegendOrientation.Bottom ? legendMargin : 0),
};
}

export function dedupSeries(series: SeriesOption[]): SeriesOption[] {
const counter = new Map<string, number>();
return series.map(row => {
let { id } = row;
if (id === undefined) return row;
id = String(id);
const count = counter.get(id) || 0;
const suffix = count > 0 ? ` (${count})` : '';
counter.set(id, count + 1);
return {
...row,
id: `${id}${suffix}`,
};
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/
import { getNumberFormatter, getTimeFormatter } from '@superset-ui/core';
import {
dedupSeries,
extractGroupbyLabel,
extractTimeseriesSeries,
formatSeriesName,
Expand Down Expand Up @@ -328,4 +329,25 @@ describe('formatSeriesName', () => {
});
});
});

describe('dedupSeries', () => {
it('should deduplicate ids in series', () => {
expect(
dedupSeries([
{
id: 'foo',
},
{
id: 'bar',
},
{
id: 'foo',
},
{
id: 'foo',
},
]),
).toEqual([{ id: 'foo' }, { id: 'bar' }, { id: 'foo (1)' }, { id: 'foo (2)' }]);
});
});
});

0 comments on commit 6377933

Please sign in to comment.