@@ -35,12 +35,14 @@ import {
3535 BuilderSavedChartConfig ,
3636 ChartConfigWithDateRange ,
3737 ChartConfigWithOptDateRange ,
38+ ChartPaletteToken ,
3839 DateRange ,
3940 DisplayType ,
4041 Filter ,
4142 isSearchableSource ,
4243 MetricsDataType as MetricsDataTypeV2 ,
4344 PromqlChartConfig ,
45+ resolveChartPaletteToken ,
4446 SourceKind ,
4547 SQLInterval ,
4648 TMetricSource ,
@@ -62,7 +64,12 @@ import {
6264import { getMetricNameSql } from './otelSemanticConventions' ;
6365import { AggFn , TableChartSeries , TimeChartSeries } from './types' ;
6466import { NumberFormat } from './types' ;
65- import { getColorProps , getLogLevelColorOrder , logLevelColor } from './utils' ;
67+ import {
68+ getColorFromCSSToken ,
69+ getColorProps ,
70+ getLogLevelColorOrder ,
71+ logLevelColor ,
72+ } from './utils' ;
6673
6774type SortOrder = 'asc' | 'desc' ;
6875
@@ -634,8 +641,33 @@ interface LineDataWithOptionalColor extends Omit<LineData, 'color'> {
634641 color ?: string ;
635642}
636643
644+ /**
645+ * Series colors set on a chart's select items, keyed by result-column name
646+ * (the item's alias), in the shape formatResponseForTimeChart's
647+ * `seriesColors` takes. Tokens go through resolveChartPaletteToken, as on
648+ * table and number tiles, so a legacy token maps to its hue and an
649+ * unrecognized one is skipped: that series falls back to the positional
650+ * palette instead of failing the whole chart.
651+ */
652+ export function getSelectSeriesColors (
653+ config : ChartConfigWithDateRange ,
654+ ) : Record < string , ChartPaletteToken > | undefined {
655+ if ( ! isBuilderChartConfig ( config ) || ! Array . isArray ( config . select ) ) {
656+ return undefined ;
657+ }
658+ const entries : [ string , ChartPaletteToken ] [ ] = [ ] ;
659+ for ( const selectItem of config . select ) {
660+ const token = resolveChartPaletteToken ( selectItem . color ) ;
661+ if ( selectItem . alias && token ) {
662+ entries . push ( [ selectItem . alias , token ] ) ;
663+ }
664+ }
665+ return entries . length > 0 ? Object . fromEntries ( entries ) : undefined ;
666+ }
667+
637668function setLineColors (
638669 sortedLineData : LineDataWithOptionalColor [ ] ,
670+ seriesColors ?: Record < string , ChartPaletteToken > ,
639671) : LineData [ ] {
640672 // Ensure that the current and previous period lines are the same color
641673 const lineColorByCurrentPeriodKey = new Map < string , string > ( ) ;
@@ -646,10 +678,15 @@ function setLineColors(
646678 if ( lineColorByCurrentPeriodKey . has ( currentPeriodKey ) ) {
647679 line . color = lineColorByCurrentPeriodKey . get ( currentPeriodKey ) ;
648680 } else if ( ! line . color ) {
649- line . color = getColorProps (
650- colorIndex ++ ,
651- line . displayName ?? line . dataKey ,
652- ) ;
681+ const seriesName = line . displayName ?? line . dataKey ;
682+ // A color set on the series' select item (keyed by its result-column name)
683+ // wins over the positional palette, so a series keeps the same hue no
684+ // matter how the data sorts it. A pinned series does not consume a
685+ // positional slot, so unpinned series keep their natural palette order.
686+ const pinnedToken = seriesColors ?. [ line . valueColumnName ] ;
687+ line . color = pinnedToken
688+ ? getColorFromCSSToken ( pinnedToken )
689+ : getColorProps ( colorIndex ++ , seriesName ) ;
653690 lineColorByCurrentPeriodKey . set ( currentPeriodKey , line . color ) ;
654691 } else {
655692 lineColorByCurrentPeriodKey . set ( currentPeriodKey , line . color ) ;
@@ -801,6 +838,7 @@ export function formatResponseForTimeChart({
801838 hiddenSeries = [ ] ,
802839 previousPeriodOffsetSeconds = 0 ,
803840 maxSeries = MAX_RENDERED_TIME_CHART_SERIES ,
841+ seriesColors,
804842} : {
805843 dateRange : [ Date , Date ] ;
806844 granularity ?: SQLInterval ;
@@ -816,6 +854,14 @@ export function formatResponseForTimeChart({
816854 * every series (the "load all" escape hatch behind the hidden-series notice).
817855 */
818856 maxSeries ?: number ;
857+ /**
858+ * Pin specific series (keyed by result-column name) to a palette token instead
859+ * of the positional palette, so their color is stable across sessions and can
860+ * avoid hues reserved for other meanings (e.g. keeping latency off error red).
861+ * DBTimeChart derives this from each select item's `color`
862+ * (getSelectSeriesColors) when its caller opts in with `applySelectColors`.
863+ */
864+ seriesColors ?: Record < string , ChartPaletteToken > ;
819865} ) {
820866 const meta = currentPeriodResponse . meta ;
821867
@@ -1058,7 +1104,7 @@ export function formatResponseForTimeChart({
10581104 ( a , b ) => a [ timestampColumn . name ] - b [ timestampColumn . name ] ,
10591105 ) ;
10601106
1061- const sortedLineDataWithColors = setLineColors ( sortedLineData ) ;
1107+ const sortedLineDataWithColors = setLineColors ( sortedLineData , seriesColors ) ;
10621108
10631109 // Count of LOGICAL groups actually rendered, in the same unit as
10641110 // hiddenSeriesCount — so a comparison chart (current + previous entries per
0 commit comments