Skip to content

Commit

Permalink
fix(plugin-chart-echarts): fix null labels on pie and funnel charts (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
villebro authored and zhaoyongjie committed Nov 26, 2021
1 parent 7441009 commit 5ed2ba1
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,15 @@ export function formatFunnelLabel({
params,
labelType,
numberFormatter,
sanitizeName = false,
}: {
params: CallbackDataParams;
params: Pick<CallbackDataParams, 'name' | 'value' | 'percent'>;
labelType: EchartsFunnelLabelTypeType;
numberFormatter: NumberFormatter;
sanitizeName?: boolean;
}): string {
const { name: rawName = '', value, percent } = params;
const name = sanitizeHtml(rawName);
const name = sanitizeName ? sanitizeHtml(rawName) : rawName;
const formattedValue = numberFormatter(value as number);
const formattedPercent = percentFormatter((percent as number) / 100);
switch (labelType) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,15 @@ export function formatPieLabel({
params,
labelType,
numberFormatter,
sanitizeName = false,
}: {
params: CallbackDataParams;
params: Pick<CallbackDataParams, 'name' | 'value' | 'percent'>;
labelType: EchartsPieLabelType;
numberFormatter: NumberFormatter;
sanitizeName?: boolean;
}): string {
const { name: rawName = '', value, percent } = params;
const name = sanitizeHtml(rawName);
const name = sanitizeName ? sanitizeHtml(rawName) : rawName;
const formattedValue = numberFormatter(value as number);
const formattedPercent = percentFormatter((percent as number) / 100);

Expand Down Expand Up @@ -221,6 +223,7 @@ export default function transformProps(chartProps: EchartsPieChartProps): PieCha
params,
numberFormatter,
labelType: EchartsPieLabelType.KeyValuePercent,
sanitizeName: true,
}),
},
legend: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,5 +102,20 @@ describe('formatFunnelLabel', () => {
labelType: EchartsFunnelLabelTypeType.KeyValuePercent,
}),
).toEqual('My Label: 1.23k (12.34%)');
expect(
formatFunnelLabel({
params: { ...params, name: '<NULL>' },
numberFormatter,
labelType: EchartsFunnelLabelTypeType.Key,
}),
).toEqual('<NULL>');
expect(
formatFunnelLabel({
params: { ...params, name: '<NULL>' },
numberFormatter,
labelType: EchartsFunnelLabelTypeType.Key,
sanitizeName: true,
}),
).toEqual('&lt;NULL&gt;');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,18 @@
* specific language governing permissions and limitations
* under the License.
*/
import { ChartProps, getNumberFormatter } from '@superset-ui/core';
import { ChartProps, getNumberFormatter, SqlaFormData } from '@superset-ui/core';
import transformProps, { formatPieLabel } from '../../src/Pie/transformProps';
import { EchartsPieLabelType } from '../../src/Pie/types';

describe('Pie tranformProps', () => {
const formData = {
const formData: SqlaFormData = {
colorScheme: 'bnbColors',
datasource: '3__table',
granularity_sqla: 'ds',
metric: 'sum__num',
groupby: ['foo', 'bar'],
viz_type: 'my_viz',
};
const chartProps = new ChartProps({
formData,
Expand Down Expand Up @@ -91,5 +92,20 @@ describe('formatPieLabel', () => {
expect(
formatPieLabel({ params, numberFormatter, labelType: EchartsPieLabelType.KeyValuePercent }),
).toEqual('My Label: 1.23k (12.34%)');
expect(
formatPieLabel({
params: { ...params, name: '<NULL>' },
numberFormatter,
labelType: EchartsPieLabelType.Key,
}),
).toEqual('<NULL>');
expect(
formatPieLabel({
params: { ...params, name: '<NULL>' },
numberFormatter,
labelType: EchartsPieLabelType.Key,
sanitizeName: true,
}),
).toEqual('&lt;NULL&gt;');
});
});

0 comments on commit 5ed2ba1

Please sign in to comment.