Skip to content

Commit

Permalink
feat(plugin-chart-echarts): add only_total control to ts chart (#1313)
Browse files Browse the repository at this point in the history
* feat(plugin-chart-echarts): add only_total control to ts chart

* fix

* fix: ci
  • Loading branch information
stephenLYZ authored and zhaoyongjie committed Nov 26, 2021
1 parent 1e76ac6 commit 471f245
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 81 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import {
EchartsTimeseriesContributionType,
EchartsTimeseriesSeriesType,
} from '../types';
import { legendSection, showValueControl } from '../../controls';
import { legendSection, showValueSection } from '../../controls';

const {
contributionMode,
Expand All @@ -43,7 +43,6 @@ const {
opacity,
rowLimit,
seriesType,
stack,
tooltipTimeFormat,
truncateYAxis,
yAxisBounds,
Expand Down Expand Up @@ -135,19 +134,7 @@ const config: ControlPanelConfig = {
},
},
],
[showValueControl],
[
{
name: 'stack',
config: {
type: 'CheckboxControl',
label: t('Stack series'),
renderTrigger: true,
default: stack,
description: t('Stack series on top of each other'),
},
},
],
...showValueSection,
[
{
name: 'markerEnabled',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import {
} from '@superset-ui/chart-controls';

import { DEFAULT_FORM_DATA, EchartsTimeseriesContributionType } from '../types';
import { legendSection, showValueControl } from '../../controls';
import { legendSection, showValueSection } from '../../controls';

const {
contributionMode,
Expand All @@ -37,7 +37,6 @@ const {
markerSize,
minorSplitLine,
rowLimit,
stack,
tooltipTimeFormat,
truncateYAxis,
yAxisBounds,
Expand Down Expand Up @@ -95,19 +94,7 @@ const config: ControlPanelConfig = {
expanded: true,
controlSetRows: [
['color_scheme', 'label_colors'],
[showValueControl],
[
{
name: 'stack',
config: {
type: 'CheckboxControl',
label: t('Stack series'),
renderTrigger: true,
default: stack,
description: t('Stack series on top of each other'),
},
},
],
...showValueSection,
[
{
name: 'markerEnabled',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import {
EchartsTimeseriesContributionType,
EchartsTimeseriesSeriesType,
} from '../types';
import { legendSection, showValueControl } from '../../controls';
import { legendSection, showValueSection } from '../../controls';

const {
area,
Expand All @@ -43,7 +43,6 @@ const {
minorSplitLine,
opacity,
rowLimit,
stack,
tooltipTimeFormat,
truncateYAxis,
yAxisBounds,
Expand Down Expand Up @@ -120,19 +119,7 @@ const config: ControlPanelConfig = {
},
},
],
[showValueControl],
[
{
name: 'stack',
config: {
type: 'CheckboxControl',
label: t('Stack series'),
renderTrigger: true,
default: stack,
description: t('Stack series on top of each other'),
},
},
],
...showValueSection,
[
{
name: 'area',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import {
EchartsTimeseriesContributionType,
EchartsTimeseriesSeriesType,
} from './types';
import { legendSection, showValueControl } from '../controls';
import { legendSection, showValueSection } from '../controls';

const {
area,
Expand All @@ -44,7 +44,6 @@ const {
opacity,
rowLimit,
seriesType,
stack,
tooltipTimeFormat,
truncateYAxis,
yAxisBounds,
Expand Down Expand Up @@ -123,19 +122,7 @@ const config: ControlPanelConfig = {
},
},
],
[showValueControl],
[
{
name: 'stack',
config: {
type: 'CheckboxControl',
label: t('Stack series'),
renderTrigger: true,
default: stack,
description: t('Stack series on top of each other'),
},
},
],
...showValueSection,
[
{
name: 'area',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ export default function transformProps(
emitFilter,
groupby,
showValue,
onlyTotal,
}: EchartsTimeseriesFormData = { ...DEFAULT_FORM_DATA, ...formData };

const colorScale = CategoricalColorNamespace.getScale(colorScheme as string);
Expand All @@ -114,18 +115,18 @@ export default function transformProps(
const totalStackedValues: number[] = [];
const showValueIndexes: number[] = [];

if (stack) {
rebasedData.forEach(data => {
const values = Object.keys(data).reduce((prev, curr) => {
if (curr === '__timestamp') {
return prev;
}
const value = data[curr] || 0;
return prev + (value as number);
}, 0);
totalStackedValues.push(values);
});
rebasedData.forEach(data => {
const values = Object.keys(data).reduce((prev, curr) => {
if (curr === '__timestamp') {
return prev;
}
const value = data[curr] || 0;
return prev + (value as number);
}, 0);
totalStackedValues.push(values);
});

if (stack) {
rawSeries.forEach((entry, seriesIndex) => {
const { data = [] } = entry;
(data as [Date, number][]).forEach((datum, dataIndex) => {
Expand All @@ -148,6 +149,7 @@ export default function transformProps(
stack,
formatter,
showValue,
onlyTotal,
totalStackedValues,
showValueIndexes,
richTooltip,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ export function transformSeries(
stack?: boolean;
yAxisIndex?: number;
showValue?: boolean;
onlyTotal?: boolean;
formatter?: NumberFormatter;
totalStackedValues?: number[];
showValueIndexes?: number[];
Expand All @@ -93,6 +94,7 @@ export function transformSeries(
stack,
yAxisIndex = 0,
showValue,
onlyTotal,
formatter,
totalStackedValues = [],
showValueIndexes = [],
Expand Down Expand Up @@ -188,16 +190,14 @@ export function transformSeries(
dataIndex,
seriesIndex,
} = params;
if (formatter) {
if (!stack) {
return formatter(numericValue);
}
if (seriesIndex === showValueIndexes[dataIndex]) {
return formatter(totalStackedValues[dataIndex]);
}
return '';
if (!formatter) return numericValue;
if (!stack || !onlyTotal) {
return formatter(numericValue);
}
return numericValue;
if (seriesIndex === showValueIndexes[dataIndex]) {
return formatter(totalStackedValues[dataIndex]);
}
return '';
},
},
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ export type EchartsTimeseriesFormData = QueryFormData & {
emitFilter: boolean;
groupby: string[];
showValue: boolean;
onlyTotal: boolean;
} & EchartsLegendFormData;

// @ts-ignore
Expand Down Expand Up @@ -108,6 +109,7 @@ export const DEFAULT_FORM_DATA: EchartsTimeseriesFormData = {
groupby: [],
yAxisTitle: '',
showValue: false,
onlyTotal: false,
};

export interface EchartsTimeseriesChartProps extends ChartProps {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ export const legendSection = [
[legendMarginControl],
];

export const showValueControl = {
const showValueControl = {
name: 'show_value',
config: {
type: 'CheckboxControl',
Expand All @@ -104,3 +104,29 @@ export const showValueControl = {
description: t('Show series values on the chart'),
},
};

const stackControl = {
name: 'stack',
config: {
type: 'CheckboxControl',
label: t('Stack series'),
renderTrigger: true,
default: false,
description: t('Stack series on top of each other'),
},
};

const onlyTotalControl = {
name: 'only_total',
config: {
type: 'CheckboxControl',
label: t('Only Total'),
default: true,
renderTrigger: true,
description: t('Only show the total value on the stacked chart'),
visibility: ({ controls }: ControlPanelsContainerProps) =>
Boolean(controls?.show_value?.value) && Boolean(controls?.stack?.value),
},
};

export const showValueSection = [[showValueControl], [stackControl], [onlyTotalControl]];

0 comments on commit 471f245

Please sign in to comment.