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 e12cdfe6f8101..e44059e780bb9 100644 --- a/superset-frontend/plugins/plugin-chart-echarts/src/MixedTimeseries/transformProps.ts +++ b/superset-frontend/plugins/plugin-chart-echarts/src/MixedTimeseries/transformProps.ts @@ -77,6 +77,7 @@ import { extractForecastValuesFromTooltipParams, formatForecastTooltipSeries, rebaseForecastDatum, + reorderForecastSeries, } from '../utils/forecast'; import { convertInteger } from '../utils/convertInteger'; import { defaultGrid, defaultYAxis } from '../defaults'; @@ -654,7 +655,7 @@ export default function transformProps( .map(entry => entry.name || '') .concat(extractAnnotationLabels(annotationLayers, annotationData)), }, - series: dedupSeries(series), + series: dedupSeries(reorderForecastSeries(series)), toolbox: { show: zoomable, top: TIMESERIES_CONSTANTS.toolboxTop, diff --git a/superset-frontend/plugins/plugin-chart-echarts/src/Timeseries/transformProps.ts b/superset-frontend/plugins/plugin-chart-echarts/src/Timeseries/transformProps.ts index c0da444bfe802..87f34ab86f6e4 100644 --- a/superset-frontend/plugins/plugin-chart-echarts/src/Timeseries/transformProps.ts +++ b/superset-frontend/plugins/plugin-chart-echarts/src/Timeseries/transformProps.ts @@ -80,6 +80,7 @@ import { extractForecastValuesFromTooltipParams, formatForecastTooltipSeries, rebaseForecastDatum, + reorderForecastSeries, } from '../utils/forecast'; import { convertInteger } from '../utils/convertInteger'; import { defaultGrid, defaultYAxis } from '../defaults'; @@ -615,7 +616,7 @@ export default function transformProps( ), data: legendData as string[], }, - series: dedupSeries(series), + series: dedupSeries(reorderForecastSeries(series)), toolbox: { show: zoomable, top: TIMESERIES_CONSTANTS.toolboxTop, diff --git a/superset-frontend/plugins/plugin-chart-echarts/src/utils/forecast.ts b/superset-frontend/plugins/plugin-chart-echarts/src/utils/forecast.ts index c7244baf48d1b..aaf6ebcf3d704 100644 --- a/superset-frontend/plugins/plugin-chart-echarts/src/utils/forecast.ts +++ b/superset-frontend/plugins/plugin-chart-echarts/src/utils/forecast.ts @@ -150,3 +150,35 @@ export function rebaseForecastDatum( return newRow; }); } + + +// For Confidence Bands, forecast series on mixed charts require the series sent in the following sortOrder: +export function reorderForecastSeries(row: any[]): any[] { + // Define the order for sorting using ForecastSeriesEnum + const sortOrder = { + [ForecastSeriesEnum.ForecastLower]: 1, + [ForecastSeriesEnum.ForecastUpper]: 2, + [ForecastSeriesEnum.ForecastTrend]: 3, + [ForecastSeriesEnum.Observation]: 4, + }; + + if ( + !row.some(item => { + const context = extractForecastSeriesContext(item.id); + return context && sortOrder.hasOwnProperty(context.type); + }) + ) { + return row; + } + + return row.sort((a, b) => { + // Extract the forecast series context from the id to determine the order + const aContext = extractForecastSeriesContext(a.id); + const bContext = extractForecastSeriesContext(b.id); + + const aOrder = sortOrder[aContext.type] || Number.MAX_SAFE_INTEGER; // Put other metrics at the end + const bOrder = sortOrder[bContext.type] || Number.MAX_SAFE_INTEGER; // Put other metrics at the end + + return aOrder - bOrder; + }); +} \ No newline at end of file