fix(echarts): suppress phantom x-axis label at axis edge when no time grain (#39972)

This commit is contained in:
jesperct
2026-05-18 13:52:48 -03:00
committed by GitHub
parent 054aeb3bae
commit 5393fdfabf
4 changed files with 36 additions and 4 deletions

View File

@@ -589,7 +589,8 @@ export default function transformProps(
? getXAxisFormatter(xAxisTimeFormat, timeGrainSqla)
: String;
const showMaxLabel = xAxisType === AxisType.Time && xAxisLabelRotation === 0;
const showMaxLabel =
xAxisType === AxisType.Time && xAxisLabelRotation === 0 && !!timeGrainSqla;
const deduplicatedFormatter = showMaxLabel
? (() => {
let lastLabel: string | undefined;

View File

@@ -861,7 +861,8 @@ export default function transformProps(
// boundary that formats identically to the last data-point tick (e.g.
// "2005" appears twice with Year grain). Wrap the formatter to suppress
// consecutive duplicate labels.
const showMaxLabel = xAxisType === AxisType.Time && xAxisLabelRotation === 0;
const showMaxLabel =
xAxisType === AxisType.Time && xAxisLabelRotation === 0 && !!timeGrainSqla;
const deduplicatedFormatter = showMaxLabel
? (() => {
let lastLabel: string | undefined;

View File

@@ -1444,7 +1444,7 @@ test('x-axis formatter deduplicates consecutive identical labels for coarse time
const chartProps = createTestChartProps({
formData: {
granularity_sqla: 'ds',
time_grain_sqla: TimeGranularity.YEAR,
timeGrainSqla: TimeGranularity.YEAR,
xAxisTimeFormat: '%Y',
},
queriesData: [
@@ -1473,6 +1473,30 @@ test('x-axis formatter deduplicates consecutive identical labels for coarse time
expect(label4).toBe('');
});
test('x-axis does not force showMaxLabel when no time grain is set', () => {
const data = [
{ __timestamp: Date.UTC(2003, 0, 6), sales: 100 },
{ __timestamp: Date.UTC(2004, 5, 15), sales: 200 },
{ __timestamp: Date.UTC(2005, 4, 31), sales: 300 },
];
const chartProps = createTestChartProps({
formData: {
granularity_sqla: 'ds',
timeGrainSqla: undefined,
},
queriesData: [
createTestQueryData(data, {
colnames: ['__timestamp', 'sales'],
coltypes: [GenericDataType.Temporal, GenericDataType.Numeric],
}),
],
});
const xAxisResult = transformProps(chartProps).echartOptions.xAxis as any;
expect(xAxisResult.axisLabel.showMaxLabel).not.toBe(true);
});
test('numeric x coltype routes through the number formatter (not the time formatter)', () => {
// Regression guard for echarts-timeseries-epoch-x-axis-labels investigation.
// When the query reports a Numeric x-axis coltype (including epoch-ms-like

View File

@@ -16,7 +16,11 @@
* specific language governing permissions and limitations
* under the License.
*/
import { CategoricalColorScale, ChartProps } from '@superset-ui/core';
import {
CategoricalColorScale,
ChartProps,
TimeGranularity,
} from '@superset-ui/core';
import { GenericDataType } from '@apache-superset/core/common';
import { supersetTheme } from '@apache-superset/core/theme';
import type { SeriesOption } from 'echarts';
@@ -235,6 +239,7 @@ function buildTimeseriesChartProps(
colorScheme: 'bnbColors',
datasource: '3__table',
granularity_sqla: 'ds',
timeGrainSqla: TimeGranularity.MONTH,
metric: 'sum__num',
viz_type: 'my_viz',
...overrides,
@@ -263,6 +268,7 @@ test('should configure time axis labels to show max label for last month visibil
colorScheme: 'bnbColors',
datasource: '3__table',
granularity_sqla: 'ds',
timeGrainSqla: TimeGranularity.MONTH,
metric: 'sum__num',
viz_type: 'my_viz',
};