@@ -775,6 +775,24 @@ export const QueryResultsChart = memo(function QueryResultsChart({
775775 return sortData ( unsortedData , sortByColumn , sortDirection , xDataKey ) ;
776776 } , [ unsortedData , sortByColumn , sortDirection , isDateBased , xDataKey ] ) ;
777777
778+ // Sort series by descending total sum so largest appears at bottom of
779+ // stacked charts and first in the legend
780+ const sortedSeries = useMemo ( ( ) => {
781+ if ( series . length <= 1 ) return series ;
782+ const totals = new Map < string , number > ( ) ;
783+ for ( const s of series ) {
784+ let total = 0 ;
785+ for ( const point of data ) {
786+ const val = point [ s ] ;
787+ if ( typeof val === "number" && isFinite ( val ) ) {
788+ total += Math . abs ( val ) ;
789+ }
790+ }
791+ totals . set ( s , total ) ;
792+ }
793+ return [ ...series ] . sort ( ( a , b ) => ( totals . get ( b ) ?? 0 ) - ( totals . get ( a ) ?? 0 ) ) ;
794+ } , [ series , data ] ) ;
795+
778796 // Detect time granularity — use the full time range when available so tick
779797 // labels are appropriate for the period (e.g. "Jan 5" for a 7-day range
780798 // instead of just "16:00:00" when data is sparse)
@@ -809,15 +827,15 @@ export const QueryResultsChart = memo(function QueryResultsChart({
809827 // Build chart config for colors/labels
810828 const chartConfig = useMemo ( ( ) => {
811829 const cfg : ChartConfig = { } ;
812- series . forEach ( ( s , i ) => {
830+ sortedSeries . forEach ( ( s , i ) => {
813831 const statusColor = groupByIsRunStatus ? getRunStatusHexColor ( s ) : undefined ;
814832 cfg [ s ] = {
815833 label : s ,
816834 color : statusColor ?? config . seriesColors ?. [ s ] ?? getSeriesColor ( i ) ,
817835 } ;
818836 } ) ;
819837 return cfg ;
820- } , [ series , groupByIsRunStatus , config . seriesColors ] ) ;
838+ } , [ sortedSeries , groupByIsRunStatus , config . seriesColors ] ) ;
821839
822840 // Custom tooltip label formatter for better date display
823841 const tooltipLabelFormatter = useMemo ( ( ) => {
@@ -1002,15 +1020,15 @@ export const QueryResultsChart = memo(function QueryResultsChart({
10021020 domain : yAxisDomain ,
10031021 } ;
10041022
1005- const showLegend = series . length > 0 ;
1023+ const showLegend = sortedSeries . length > 0 ;
10061024
10071025 if ( chartType === "bar" ) {
10081026 return (
10091027 < Chart . Root
10101028 config = { chartConfig }
10111029 data = { data }
10121030 dataKey = { xDataKey }
1013- series = { series }
1031+ series = { sortedSeries }
10141032 labelFormatter = { legendLabelFormatter }
10151033 showLegend = { showLegend }
10161034 maxLegendItems = { fullLegend ? Infinity : 5 }
@@ -1036,7 +1054,7 @@ export const QueryResultsChart = memo(function QueryResultsChart({
10361054 config = { chartConfig }
10371055 data = { data }
10381056 dataKey = { xDataKey }
1039- series = { series }
1057+ series = { sortedSeries }
10401058 labelFormatter = { legendLabelFormatter }
10411059 showLegend = { showLegend }
10421060 maxLegendItems = { fullLegend ? Infinity : 5 }
@@ -1049,7 +1067,7 @@ export const QueryResultsChart = memo(function QueryResultsChart({
10491067 < Chart . Line
10501068 xAxisProps = { xAxisPropsForLine }
10511069 yAxisProps = { yAxisProps }
1052- stacked = { stacked && series . length > 1 }
1070+ stacked = { stacked && sortedSeries . length > 1 }
10531071 tooltipLabelFormatter = { tooltipLabelFormatter }
10541072 lineType = "linear"
10551073 />
0 commit comments