You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@superset.apache.org by jl...@apache.org on 2026/08/13 20:57:24 UTC
(superset) 02/03: fix(echarts): refine bar label fit handling
This is an automated email from the ASF dual-hosted git repository.
sadpandajoe pushed a commit to branch fix-bar-chart-label-placement
in repository https://gitbox.apache.org/repos/asf/superset.git
commit 8432b616954fa3ed8ed99d81ea5377ce1a77a1ca
Author: sadpandajoe <jc...@gmail.com>
AuthorDate: Thu Aug 13 20:23:22 2026 +0000
fix(echarts): refine bar label fit handling
---
.../src/MixedTimeseries/transformProps.ts | 3 +
.../src/Timeseries/transformers.ts | 14 ++-
.../test/MixedTimeseries/transformProps.test.ts | 24 +++++
.../test/Timeseries/transformers.test.ts | 106 ++++++++++++++++++++-
4 files changed, 138 insertions(+), 9 deletions(-)
diff --git a/superset-frontend/plugins/plugin-chart-echarts/src/MixedTimeseries/transformProps.ts b/superset-frontend/plugins/plugin-chart-echarts/src/MixedTimeseries/transformProps.ts
index 019e2300ef5..81ffa263745 100644
--- a/superset-frontend/plugins/plugin-chart-echarts/src/MixedTimeseries/transformProps.ts
+++ b/superset-frontend/plugins/plugin-chart-echarts/src/MixedTimeseries/transformProps.ts
@@ -58,6 +58,7 @@ import {
LegendOrientation,
Refs,
} from '../types';
+import { BarValueLabelPosition } from '../Timeseries/types';
import { parseAxisBound } from '../utils/controls';
import { safeParseEChartOptions } from '../utils/safeEChartOptionsParser';
import {
@@ -510,6 +511,7 @@ export default function transformProps(
areaOpacity: opacity,
seriesType,
showValue,
+ valueLabelPosition: BarValueLabelPosition.OutsideEnd,
onlyTotal,
stack: Boolean(stack),
stackIdSuffix: '\na',
@@ -598,6 +600,7 @@ export default function transformProps(
areaOpacity: opacityB,
seriesType: seriesTypeB,
showValue: showValueB,
+ valueLabelPosition: BarValueLabelPosition.OutsideEnd,
onlyTotal: onlyTotalB,
stack: Boolean(stackB),
stackIdSuffix: '\nb',
diff --git a/superset-frontend/plugins/plugin-chart-echarts/src/Timeseries/transformers.ts b/superset-frontend/plugins/plugin-chart-echarts/src/Timeseries/transformers.ts
index 8eee349b73b..d2a6c2500c1 100644
--- a/superset-frontend/plugins/plugin-chart-echarts/src/Timeseries/transformers.ts
+++ b/superset-frontend/plugins/plugin-chart-echarts/src/Timeseries/transformers.ts
@@ -142,10 +142,13 @@ export function getAutoBarLabelLayout(
isHorizontal: boolean,
isNegative = false,
): LabelLayoutOption {
- const barLength = Math.abs(
- isHorizontal ? params.rect.width : params.rect.height,
- );
- if (params.labelRect.width <= barLength * AUTO_LABEL_FIT_RATIO) return {};
+ const fitsWidth =
+ params.labelRect.width <=
+ Math.abs(params.rect.width) * AUTO_LABEL_FIT_RATIO;
+ const fitsHeight =
+ params.labelRect.height <=
+ Math.abs(params.rect.height) * AUTO_LABEL_FIT_RATIO;
+ if (fitsWidth && fitsHeight) return {};
return isHorizontal
? getHorizontalOutsideLayout(params, isNegative)
: getVerticalOutsideLayout(params, isNegative);
@@ -583,7 +586,8 @@ export function transformSeries(
: isHorizontal
? 'right'
: 'top',
- // ECharts derives contrast-aware colors from the bar fill when unset.
+ // ECharts derives contrast from the bar fill while the label is inside.
+ // Returning x/y clears the attached position, selecting its outside fill.
...(isAutoBarLabel ? {} : { color: theme?.colorText }),
textBorderWidth: 0,
formatter: (params: any) => {
diff --git a/superset-frontend/plugins/plugin-chart-echarts/test/MixedTimeseries/transformProps.test.ts b/superset-frontend/plugins/plugin-chart-echarts/test/MixedTimeseries/transformProps.test.ts
index a670286470c..e921fdf560c 100644
--- a/superset-frontend/plugins/plugin-chart-echarts/test/MixedTimeseries/transformProps.test.ts
+++ b/superset-frontend/plugins/plugin-chart-echarts/test/MixedTimeseries/transformProps.test.ts
@@ -42,6 +42,7 @@ import {
} from '../../src/MixedTimeseries/types';
import { createEchartsTimeseriesTestChartProps } from '../helpers';
import type { SeriesOption } from 'echarts';
+import type { BarSeriesOption } from 'echarts/charts';
type LabelFormatterParams = {
value: [number, number];
@@ -192,6 +193,29 @@ function formatSeriesLabel(
});
}
+test('bar value labels retain their legacy outside position', () => {
+ const chartProps = createEchartsTimeseriesTestChartProps<
+ EchartsMixedTimeseriesFormData,
+ EchartsMixedTimeseriesProps
+ >({
+ ...MIXED_TIMESERIES_CHART_PROPS_DEFAULTS,
+ defaultQueriesData: queriesData,
+ formData: { ...formData, showValueB: true },
+ queriesData,
+ });
+
+ const transformed = transformProps(chartProps);
+ const barSeries = (transformed.echartOptions.series as SeriesOption[]).filter(
+ (series): series is BarSeriesOption => series.type === 'bar',
+ );
+
+ expect(barSeries).not.toHaveLength(0);
+ barSeries.forEach(series => {
+ expect(series.label).toMatchObject({ show: true, position: 'top' });
+ expect(series.labelLayout).toBeUndefined();
+ });
+});
+
test('should transform chart props for viz with showQueryIdentifiers=false', () => {
const chartProps = createEchartsTimeseriesTestChartProps<
EchartsMixedTimeseriesFormData,
diff --git a/superset-frontend/plugins/plugin-chart-echarts/test/Timeseries/transformers.test.ts b/superset-frontend/plugins/plugin-chart-echarts/test/Timeseries/transformers.test.ts
index 902ca5f02f4..81584494c2b 100644
--- a/superset-frontend/plugins/plugin-chart-echarts/test/Timeseries/transformers.test.ts
+++ b/superset-frontend/plugins/plugin-chart-echarts/test/Timeseries/transformers.test.ts
@@ -19,11 +19,12 @@
import {
CategoricalColorScale,
ChartProps,
+ getNumberFormatter,
TimeGranularity,
} from '@superset-ui/core';
import { GenericDataType } from '@apache-superset/core/common';
import { supersetTheme } from '@apache-superset/core/theme';
-import type { SeriesOption } from 'echarts';
+import { init, type SeriesOption } from 'echarts';
import type {
BarSeriesOption,
LineSeriesOption,
@@ -203,7 +204,7 @@ test('Auto bar labels move outside narrow stacked segments', () => {
});
});
-test('Auto bar labels stay inside when they use at most 80% of bar length', () => {
+test('Auto labels stay inside when both dimensions fit within 80% of the bar', () => {
const result = transformSeries(
{ name: 'test-series', type: 'bar', data: [[2026, 1]] },
mockColorScale,
@@ -222,12 +223,75 @@ test('Auto bar labels stay inside when they use at most 80% of bar length', () =
text: '1,000',
align: 'center',
verticalAlign: 'top',
- rect: { x: 10, y: 20, width: 12, height: 40 },
- labelRect: { x: 0, y: 25, width: 32, height: 14 },
+ rect: { x: 10, y: 20, width: 50, height: 40 },
+ labelRect: { x: 19, y: 25, width: 32, height: 14 },
}),
).toEqual({});
});
+test('Auto moves wide labels outside tall narrow vertical bars', () => {
+ const result = transformSeries(
+ { name: 'test-series', type: 'bar', data: [[2026, 100]] },
+ mockColorScale,
+ 'test-key',
+ { seriesType: EchartsTimeseriesSeriesType.Bar },
+ ) as BarSeriesOption;
+ const { labelLayout } = result;
+
+ expect(typeof labelLayout).toBe('function');
+ if (typeof labelLayout !== 'function') return;
+
+ expect(
+ labelLayout({
+ dataIndex: 0,
+ seriesIndex: 0,
+ text: '1,000',
+ align: 'center',
+ verticalAlign: 'top',
+ rect: { x: 10, y: 20, width: 12, height: 200 },
+ labelRect: { x: 1, y: 25, width: 30, height: 14 },
+ }),
+ ).toEqual({
+ x: 16,
+ y: 15,
+ align: 'center',
+ verticalAlign: 'bottom',
+ });
+});
+
+test('Auto overflow uses ECharts outside-label text color', () => {
+ const darkBarColorScale = jest.fn(() => '#111111');
+ const series = transformSeries(
+ { name: 'test-series', type: 'bar', data: [[0, 123456789]] },
+ darkBarColorScale as unknown as CategoricalColorScale,
+ 'test-key',
+ {
+ formatter: getNumberFormatter('d'),
+ seriesType: EchartsTimeseriesSeriesType.Bar,
+ showValue: true,
+ },
+ ) as BarSeriesOption;
+ const chart = init(null, null, {
+ renderer: 'svg',
+ ssr: true,
+ width: 300,
+ height: 220,
+ });
+
+ chart.setOption({
+ animation: false,
+ darkMode: false,
+ xAxis: { type: 'category', data: ['A'], show: false },
+ yAxis: { type: 'value', max: 1_000_000_000, show: false },
+ series: [series],
+ });
+
+ expect(chart.renderToSVGString()).toMatch(
+ /fill="#333"[^>]*>123456789<\/text>/,
+ );
+ chart.dispose();
+});
+
test('Auto bar labels use horizontal bar length and move to the value end', () => {
const result = transformSeries(
{ name: 'test-series', type: 'bar', data: [[1, 2026]] },
@@ -322,6 +386,40 @@ test('Auto positions negative stacked segments at their inside end', () => {
});
});
+test('Auto moves horizontal negative labels beyond their value end', () => {
+ const result = transformSeries(
+ { name: 'test-series', type: 'bar', data: [[-1, 2026]] },
+ mockColorScale,
+ 'test-key',
+ { seriesType: EchartsTimeseriesSeriesType.Bar, isHorizontal: true },
+ ) as BarSeriesOption;
+
+ expect(result.data).toEqual([
+ {
+ value: [-1, 2026],
+ label: { position: 'insideLeft' },
+ },
+ ]);
+ expect(typeof result.labelLayout).toBe('function');
+ if (typeof result.labelLayout !== 'function') return;
+ expect(
+ result.labelLayout({
+ dataIndex: 0,
+ seriesIndex: 0,
+ text: '-1,000',
+ align: 'left',
+ verticalAlign: 'middle',
+ rect: { x: 10, y: 20, width: 20, height: 12 },
+ labelRect: { x: 10, y: 19, width: 30, height: 14 },
+ }),
+ ).toEqual({
+ x: 5,
+ y: 26,
+ align: 'right',
+ verticalAlign: 'middle',
+ });
+});
+
test('Auto label layout does not change non-Bar series', () => {
const result = transformSeries(
{ name: 'test-series', type: 'line', data: [[2026, 1]] },