@@ -6,7 +6,7 @@ import { getFieldTypeFromValue } from '../dataframe/processDataFrame';
66import { toUtc , dateTimeParse } from '../datetime' ;
77import { GrafanaTheme2 } from '../themes/types' ;
88import { KeyValue , TimeZone } from '../types' ;
9- import { EnumFieldConfig , Field , FieldType } from '../types/dataFrame' ;
9+ import { Field , FieldType } from '../types/dataFrame' ;
1010import { DecimalCount , DisplayProcessor , DisplayValue } from '../types/displayValue' ;
1111import { anyToNumber } from '../utils/anyToNumber' ;
1212import { getValueMappingResult } from '../utils/valueMappings' ;
@@ -44,6 +44,7 @@ export function getDisplayProcessor(options?: DisplayProcessorOptions): DisplayP
4444
4545 const field = options . field as Field ;
4646 const config = field . config ?? { } ;
47+ const { palette } = options . theme . visualization ;
4748
4849 let unit = config . unit ;
4950 let hasDateUnit = unit && ( timeFormats [ unit ] || unit . startsWith ( 'time:' ) ) ;
@@ -70,8 +71,6 @@ export function getDisplayProcessor(options?: DisplayProcessorOptions): DisplayP
7071 }
7172 } else if ( ! unit && field . type === FieldType . string ) {
7273 unit = 'string' ;
73- } else if ( field . type === FieldType . enum ) {
74- return getEnumDisplayProcessor ( options . theme , config . type ?. enum ) ;
7574 }
7675
7776 const hasCurrencyUnit = unit ?. startsWith ( 'currency' ) ;
@@ -116,6 +115,28 @@ export function getDisplayProcessor(options?: DisplayProcessorOptions): DisplayP
116115 icon = mappingResult . icon ;
117116 }
118117 }
118+ } else if ( field . type === FieldType . enum ) {
119+ // Apply enum display handling if field is enum type and no mappings are specified
120+ if ( value == null ) {
121+ return {
122+ text : '' ,
123+ numeric : NaN ,
124+ } ;
125+ }
126+
127+ const enumIndex = + value ;
128+ if ( config && config . type && config . type . enum ) {
129+ const { text : enumText , color : enumColor } = config . type . enum ;
130+
131+ text = enumText ? enumText [ enumIndex ] : `${ value } ` ;
132+ // If no color specified in enum field config we will fallback to iterating through the theme palette
133+ color = enumColor ? enumColor [ enumIndex ] : undefined ;
134+
135+ if ( color == null ) {
136+ const namedColor = palette [ enumIndex % palette . length ] ;
137+ color = options . theme . visualization . getColorByName ( namedColor ) ;
138+ }
139+ }
119140 }
120141
121142 if ( ! Number . isNaN ( numeric ) ) {
@@ -192,41 +213,6 @@ function toStringProcessor(value: unknown): DisplayValue {
192213 return { text : toString ( value ) , numeric : anyToNumber ( value ) } ;
193214}
194215
195- export function getEnumDisplayProcessor ( theme : GrafanaTheme2 , cfg ?: EnumFieldConfig ) : DisplayProcessor {
196- const config = {
197- text : cfg ?. text ?? [ ] ,
198- color : cfg ?. color ?? [ ] ,
199- } ;
200- // use the theme specific color values
201- config . color = config . color . map ( ( v ) => theme . visualization . getColorByName ( v ) ) ;
202-
203- return ( value : unknown ) => {
204- if ( value == null ) {
205- return {
206- text : '' ,
207- numeric : NaN ,
208- } ;
209- }
210- const idx = + value ;
211- let text = config . text [ idx ] ;
212- if ( text == null ) {
213- text = `${ value } ` ; // the original value
214- }
215- let color = config . color [ idx ] ;
216- if ( color == null ) {
217- // constant color for index
218- const { palette } = theme . visualization ;
219- color = palette [ idx % palette . length ] ;
220- config . color [ idx ] = color ;
221- }
222- return {
223- text,
224- numeric : idx ,
225- color,
226- } ;
227- } ;
228- }
229-
230216export function getRawDisplayProcessor ( ) : DisplayProcessor {
231217 return ( value : unknown ) => ( {
232218 text : getFieldTypeFromValue ( value ) === 'other' ? `${ JSON . stringify ( value , getCircularReplacer ( ) ) } ` : `${ value } ` ,
0 commit comments