You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@superset.apache.org by ru...@apache.org on 2025/08/05 06:16:51 UTC
(superset) 01/01: fix(heatmap): preserve axis order when data combinations are missing
This is an automated email from the ASF dual-hosted git repository.
rusackas pushed a commit to branch fix-33245-heatmap-axis-sorting
in repository https://gitbox.apache.org/repos/asf/superset.git
commit 2d62110208627bc28d1baf96a5c10c28e8d44969
Author: Evan Rusackas <ev...@rusackas.com>
AuthorDate: Mon Aug 4 23:16:16 2025 -0700
fix(heatmap): preserve axis order when data combinations are missing
Fixes #33245
The heatmap chart was not preserving the proper axis order when some data
combinations were missing. This occurred because ECharts automatically
creates axis categories from the data it receives, ignoring the SQL ORDER BY
applied on the backend.
Solution: Explicitly set the axis categories in the correct order by:
1. Extracting unique values for each axis from the data
2. Sorting them according to the user's sorting configuration
3. Explicitly setting the axis data in the ECharts configuration
This ensures both axes maintain their proper order regardless of missing data.
🤖 Generated with [Claude Code](https://claude.ai/code)
Co-Authored-By: Claude <no...@anthropic.com>
---
.../src/Heatmap/transformProps.ts | 75 ++++++
.../test/Heatmap/transformProps.test.ts | 271 +++++++++++++++++++++
2 files changed, 346 insertions(+)
diff --git a/superset-frontend/plugins/plugin-chart-echarts/src/Heatmap/transformProps.ts b/superset-frontend/plugins/plugin-chart-echarts/src/Heatmap/transformProps.ts
index 3167834a88..2133157214 100644
--- a/superset-frontend/plugins/plugin-chart-echarts/src/Heatmap/transformProps.ts
+++ b/superset-frontend/plugins/plugin-chart-echarts/src/Heatmap/transformProps.ts
@@ -100,6 +100,8 @@ export default function transformProps(
yAxisFormat,
xAxisTimeFormat,
currencyFormat,
+ sort_x_axis: sortXAxis,
+ sort_y_axis: sortYAxis,
} = formData;
const metricLabel = getMetricLabel(metric);
const xAxisLabel = getColumnLabel(xAxis);
@@ -143,6 +145,73 @@ export default function transformProps(
DEFAULT_ECHARTS_BOUNDS[1];
}
+ // Extract unique values for each axis
+ const xValues = Array.from(new Set(data.map(row => row[xAxisLabel])));
+ const yValues = Array.from(new Set(data.map(row => row[yAxisLabel])));
+
+ // Sort axis values based on configuration
+ const sortAxisValues = (
+ values: any[],
+ sortConfig: string | undefined,
+ axisLabel: string,
+ ) => {
+ if (!sortConfig) {
+ return values;
+ }
+
+ const isMetricSort = sortConfig.includes('value');
+ const isAscending = sortConfig.includes('asc');
+
+ if (isMetricSort) {
+ // Create a map of axis value to metric sum for sorting by metric
+ const metricSums: Record<string, number> = {};
+ data.forEach(row => {
+ const axisValue = row[axisLabel];
+ const metricValue = row[metricLabel];
+ if (typeof metricValue === 'number' && axisValue != null) {
+ const key = String(axisValue);
+ metricSums[key] = (metricSums[key] || 0) + metricValue;
+ }
+ });
+
+ values.sort((a, b) => {
+ const keyA = String(a);
+ const keyB = String(b);
+ const sumA = metricSums[keyA] || 0;
+ const sumB = metricSums[keyB] || 0;
+ return isAscending ? sumA - sumB : sumB - sumA;
+ });
+ } else {
+ // Sort alphabetically/numerically
+ values.sort((a, b) => {
+ // Handle null/undefined values
+ if (a === null || a === undefined) return isAscending ? -1 : 1;
+ if (b === null || b === undefined) return isAscending ? 1 : -1;
+
+ // Convert to strings for comparison
+ const strA = String(a);
+ const strB = String(b);
+
+ // Try numeric comparison first
+ const numA = Number(strA);
+ const numB = Number(strB);
+ if (!Number.isNaN(numA) && !Number.isNaN(numB)) {
+ return isAscending ? numA - numB : numB - numA;
+ }
+
+ // Fall back to string comparison
+ return isAscending
+ ? strA.localeCompare(strB)
+ : strB.localeCompare(strA);
+ });
+ }
+
+ return values;
+ };
+
+ const sortedXValues = sortAxisValues(xValues, sortXAxis, xAxisLabel);
+ const sortedYValues = sortAxisValues(yValues, sortYAxis, yAxisLabel);
+
const series: HeatmapSeriesOption[] = [
{
name: metricLabel,
@@ -248,6 +317,9 @@ export default function transformProps(
},
xAxis: {
type: 'category',
+ data: sortedXValues.map(v =>
+ v === null || v === undefined ? NULL_STRING : v,
+ ),
axisLabel: {
formatter: xAxisFormatter,
interval: xscaleInterval === -1 ? 'auto' : xscaleInterval - 1,
@@ -255,6 +327,9 @@ export default function transformProps(
},
yAxis: {
type: 'category',
+ data: sortedYValues.map(v =>
+ v === null || v === undefined ? NULL_STRING : v,
+ ),
axisLabel: {
formatter: yAxisFormatter,
interval: yscaleInterval === -1 ? 'auto' : yscaleInterval - 1,
diff --git a/superset-frontend/plugins/plugin-chart-echarts/test/Heatmap/transformProps.test.ts b/superset-frontend/plugins/plugin-chart-echarts/test/Heatmap/transformProps.test.ts
new file mode 100644
index 0000000000..a4a029387b
--- /dev/null
+++ b/superset-frontend/plugins/plugin-chart-echarts/test/Heatmap/transformProps.test.ts
@@ -0,0 +1,271 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+import {
+ ChartProps,
+ ChartPropsConfig,
+ SqlaFormData,
+ supersetTheme,
+} from '@superset-ui/core';
+import { EChartsOption } from 'echarts';
+import transformProps from '../../src/Heatmap/transformProps';
+import { HeatmapChartProps } from '../../src/Heatmap/types';
+
+describe('Heatmap transformProps', () => {
+ const defaultFormData = {
+ datasource: '1__table',
+ viz_type: 'heatmap_v2',
+ x_axis: 'day',
+ groupby: ['hour'],
+ metric: 'count',
+ sort_x_axis: 'alpha_asc',
+ sort_y_axis: 'alpha_asc',
+ } as SqlaFormData;
+
+ const chartConfig: ChartPropsConfig = {
+ formData: defaultFormData,
+ height: 800,
+ width: 800,
+ queriesData: [
+ {
+ data: [
+ { day: 'Monday', hour: '08', count: 10 },
+ { day: 'Monday', hour: '09', count: 20 },
+ { day: 'Tuesday', hour: '08', count: 15 },
+ { day: 'Wednesday', hour: '09', count: 25 },
+ ],
+ colnames: ['day', 'hour', 'count'],
+ coltypes: [0, 0, 2], // string, string, numeric
+ },
+ ],
+ datasource: {
+ datasource_type: 'table',
+ description: 'test datasource',
+ id: 1,
+ columns: [],
+ metrics: [],
+ column_formats: {},
+ currency_formats: {},
+ },
+ theme: supersetTheme,
+ };
+
+ it('should maintain proper axis order when data is missing', () => {
+ const chartProps = new ChartProps(chartConfig);
+ const transformedProps = transformProps(
+ chartProps as unknown as HeatmapChartProps,
+ );
+ const echartOptions = transformedProps.echartOptions as EChartsOption;
+
+ // Check that x-axis has all days in alphabetical order
+ expect(echartOptions.xAxis).toEqual(
+ expect.objectContaining({
+ type: 'category',
+ data: ['Monday', 'Tuesday', 'Wednesday'],
+ }),
+ );
+
+ // Check that y-axis has all hours in alphabetical order
+ expect(echartOptions.yAxis).toEqual(
+ expect.objectContaining({
+ type: 'category',
+ data: ['08', '09'],
+ }),
+ );
+ });
+
+ it('should sort axes in descending order when configured', () => {
+ const descFormData = {
+ ...defaultFormData,
+ sort_x_axis: 'alpha_desc',
+ sort_y_axis: 'alpha_desc',
+ };
+ const chartProps = new ChartProps({
+ ...chartConfig,
+ formData: descFormData,
+ });
+ const transformedProps = transformProps(
+ chartProps as unknown as HeatmapChartProps,
+ );
+ const echartOptions = transformedProps.echartOptions as EChartsOption;
+
+ // Check descending alphabetical order
+ expect(echartOptions.xAxis).toEqual(
+ expect.objectContaining({
+ type: 'category',
+ data: ['Wednesday', 'Tuesday', 'Monday'],
+ }),
+ );
+
+ expect(echartOptions.yAxis).toEqual(
+ expect.objectContaining({
+ type: 'category',
+ data: ['09', '08'],
+ }),
+ );
+ });
+
+ it('should sort by metric value when configured', () => {
+ const valueFormData = {
+ ...defaultFormData,
+ sort_x_axis: 'value_asc',
+ sort_y_axis: 'value_desc',
+ };
+ const chartProps = new ChartProps({
+ ...chartConfig,
+ formData: valueFormData,
+ });
+ const transformedProps = transformProps(
+ chartProps as unknown as HeatmapChartProps,
+ );
+ const echartOptions = transformedProps.echartOptions as EChartsOption;
+
+ // Monday: 10+20=30, Tuesday: 15, Wednesday: 25
+ // Ascending order by value: Tuesday(15), Wednesday(25), Monday(30)
+ expect(echartOptions.xAxis).toEqual(
+ expect.objectContaining({
+ type: 'category',
+ data: ['Tuesday', 'Wednesday', 'Monday'],
+ }),
+ );
+
+ // Hour 08: 10+15=25, Hour 09: 20+25=45
+ // Descending order by value: 09(45), 08(25)
+ expect(echartOptions.yAxis).toEqual(
+ expect.objectContaining({
+ type: 'category',
+ data: ['09', '08'],
+ }),
+ );
+ });
+
+ it('should handle numeric axis values properly', () => {
+ const numericData = {
+ ...chartConfig,
+ queriesData: [
+ {
+ data: [
+ { year: 2020, quarter: 1, sales: 100 },
+ { year: 2021, quarter: 2, sales: 150 },
+ { year: 2022, quarter: 1, sales: 200 },
+ { year: 2021, quarter: 3, sales: 175 },
+ ],
+ colnames: ['year', 'quarter', 'sales'],
+ coltypes: [2, 2, 2], // all numeric
+ },
+ ],
+ formData: {
+ ...defaultFormData,
+ x_axis: 'year',
+ groupby: ['quarter'],
+ metric: 'sales',
+ },
+ };
+
+ const chartProps = new ChartProps(numericData);
+ const transformedProps = transformProps(
+ chartProps as unknown as HeatmapChartProps,
+ );
+ const echartOptions = transformedProps.echartOptions as EChartsOption;
+
+ // Numeric sorting should work correctly
+ expect(echartOptions.xAxis).toEqual(
+ expect.objectContaining({
+ type: 'category',
+ data: [2020, 2021, 2022],
+ }),
+ );
+
+ expect(echartOptions.yAxis).toEqual(
+ expect.objectContaining({
+ type: 'category',
+ data: [1, 2, 3],
+ }),
+ );
+ });
+
+ it('should preserve original order when no sorting is specified', () => {
+ const noSortFormData = {
+ ...defaultFormData,
+ sort_x_axis: undefined,
+ sort_y_axis: undefined,
+ };
+ const chartProps = new ChartProps({
+ ...chartConfig,
+ formData: noSortFormData,
+ });
+ const transformedProps = transformProps(
+ chartProps as unknown as HeatmapChartProps,
+ );
+ const echartOptions = transformedProps.echartOptions as EChartsOption;
+
+ // Should maintain the order as they appear in data
+ expect(echartOptions.xAxis).toEqual(
+ expect.objectContaining({
+ type: 'category',
+ data: ['Monday', 'Tuesday', 'Wednesday'],
+ }),
+ );
+
+ expect(echartOptions.yAxis).toEqual(
+ expect.objectContaining({
+ type: 'category',
+ data: ['08', '09'],
+ }),
+ );
+ });
+
+ it('should handle null/undefined values correctly', () => {
+ const nullData = {
+ ...chartConfig,
+ queriesData: [
+ {
+ data: [
+ { day: 'Monday', hour: '08', count: 10 },
+ { day: null, hour: '09', count: 20 },
+ { day: 'Tuesday', hour: null, count: 15 },
+ { day: undefined, hour: '10', count: 25 },
+ ],
+ colnames: ['day', 'hour', 'count'],
+ coltypes: [0, 0, 2],
+ },
+ ],
+ };
+
+ const chartProps = new ChartProps(nullData);
+ const transformedProps = transformProps(
+ chartProps as unknown as HeatmapChartProps,
+ );
+ const echartOptions = transformedProps.echartOptions as EChartsOption;
+
+ // Null values should be converted to NULL_STRING
+ expect(echartOptions.xAxis).toEqual(
+ expect.objectContaining({
+ type: 'category',
+ data: expect.arrayContaining(['<NULL>', 'Monday', 'Tuesday']),
+ }),
+ );
+
+ expect(echartOptions.yAxis).toEqual(
+ expect.objectContaining({
+ type: 'category',
+ data: expect.arrayContaining(['<NULL>', '08', '09', '10']),
+ }),
+ );
+ });
+});