diff --git a/docs/sources/panels-visualizations/configure-standard-options/index.md b/docs/sources/panels-visualizations/configure-standard-options/index.md index e9e1080739e..d82cc74574d 100644 --- a/docs/sources/panels-visualizations/configure-standard-options/index.md +++ b/docs/sources/panels-visualizations/configure-standard-options/index.md @@ -116,18 +116,19 @@ Select one of the following palettes:
-| Color mode | Description | -| ------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------- | -| **Single color** | Specify a single color, useful in an override rule | -| **Shades of a color** | Selects shades of a single color, useful in an override rule | -| **From thresholds** | Informs Grafana to take the color from the matching threshold | -| **Classic palette** | Grafana will assign color by looking up a color in a palette by series index. Useful for Graphs and pie charts and other categorical data visualizations | -| **Green-Yellow-Red (by value)** | Continuous color scheme | -| **Blue-Yellow-Red (by value)** | Continuous color scheme | -| **Blues (by value)** | Continuous color scheme (panel background to blue) | -| **Reds (by value)** | Continuous color scheme (panel background color to blue) | -| **Greens (by value)** | Continuous color scheme (panel background color to blue) | -| **Purple (by value)** | Continuous color scheme (panel background color to blue) | +| Color mode | Description | +| ------------------------------------ | -------------------------------------------------------------------------------------------------------------------------------------------------------- | +| **Single color** | Specify a single color, useful in an override rule | +| **Shades of a color** | Selects shades of a single color, useful in an override rule | +| **From thresholds** | Informs Grafana to take the color from the matching threshold | +| **Classic palette** | Grafana will assign color by looking up a color in a palette by series index. Useful for Graphs and pie charts and other categorical data visualizations | +| **Classic palette (by series name)** | Grafana will assign color based on the name of the series. Useful when the series names to be visualized depend on the available data. | +| **Green-Yellow-Red (by value)** | Continuous color scheme | +| **Blue-Yellow-Red (by value)** | Continuous color scheme | +| **Blues (by value)** | Continuous color scheme (panel background to blue) | +| **Reds (by value)** | Continuous color scheme (panel background color to blue) | +| **Greens (by value)** | Continuous color scheme (panel background color to blue) | +| **Purple (by value)** | Continuous color scheme (panel background color to blue) | {{< figure src="/static/img/docs/v73/color_scheme_dropdown.png" max-width="350px" caption="Color scheme" >}} diff --git a/packages/grafana-data/package.json b/packages/grafana-data/package.json index 7032827909b..30c421f565b 100644 --- a/packages/grafana-data/package.json +++ b/packages/grafana-data/package.json @@ -38,6 +38,7 @@ "@braintree/sanitize-url": "6.0.2", "@grafana/schema": "10.0.0-pre", "@types/d3-interpolate": "^3.0.0", + "@types/string-hash": "1.1.1", "d3-interpolate": "3.0.1", "date-fns": "2.29.3", "dompurify": "^2.4.3", @@ -53,6 +54,7 @@ "react-use": "17.4.0", "regenerator-runtime": "0.13.11", "rxjs": "7.8.0", + "string-hash": "^1.1.3", "tinycolor2": "1.6.0", "tslib": "2.5.0", "uplot": "1.6.24", diff --git a/packages/grafana-data/src/field/fieldColor.test.ts b/packages/grafana-data/src/field/fieldColor.test.ts index a279429f997..43813f1f10a 100644 --- a/packages/grafana-data/src/field/fieldColor.test.ts +++ b/packages/grafana-data/src/field/fieldColor.test.ts @@ -3,9 +3,9 @@ import { Field, FieldColorModeId, FieldType } from '../types'; import { fieldColorModeRegistry, FieldValueColorCalculator, getFieldSeriesColor } from './fieldColor'; -function getTestField(mode: string, fixedColor?: string): Field { +function getTestField(mode: string, fixedColor?: string, name = 'name'): Field { return { - name: 'name', + name: name, type: FieldType.number, values: [], config: { @@ -21,11 +21,12 @@ function getTestField(mode: string, fixedColor?: string): Field { interface GetCalcOptions { mode: string; seriesIndex?: number; + name?: string; fixedColor?: string; } function getCalculator(options: GetCalcOptions): FieldValueColorCalculator { - const field = getTestField(options.mode, options.fixedColor); + const field = getTestField(options.mode, options.fixedColor, options.name); const mode = fieldColorModeRegistry.get(options.mode); field.state!.seriesIndex = options.seriesIndex; return mode.getCalculator(field, createTheme()); @@ -38,15 +39,21 @@ describe('fieldColorModeRegistry', () => { }); it('Palette classic with series index 0', () => { - const calcFn = getCalculator({ mode: FieldColorModeId.PaletteClassic, seriesIndex: 0 }); + const calcFn = getCalculator({ mode: FieldColorModeId.PaletteClassic, seriesIndex: 0, name: 'series1' }); expect(calcFn(70, 0, undefined)).toEqual('#73BF69'); }); it('Palette classic with series index 1', () => { - const calcFn = getCalculator({ mode: FieldColorModeId.PaletteClassic, seriesIndex: 1 }); + const calcFn = getCalculator({ mode: FieldColorModeId.PaletteClassic, seriesIndex: 1, name: 'series2' }); expect(calcFn(70, 0, undefined)).toEqual('#F2CC0C'); }); + it('Palette uses name', () => { + const calcFn1 = getCalculator({ mode: FieldColorModeId.PaletteClassicByName, seriesIndex: 0, name: 'same name' }); + const calcFn2 = getCalculator({ mode: FieldColorModeId.PaletteClassicByName, seriesIndex: 1, name: 'same name' }); + expect(calcFn1(12, 34, undefined)).toEqual(calcFn2(56, 78, undefined)); + }); + it('When color.seriesBy is set to last use that instead of v', () => { const field = getTestField('continuous-GrYlRd'); diff --git a/packages/grafana-data/src/field/fieldColor.ts b/packages/grafana-data/src/field/fieldColor.ts index a9d09572998..289177150de 100644 --- a/packages/grafana-data/src/field/fieldColor.ts +++ b/packages/grafana-data/src/field/fieldColor.ts @@ -1,6 +1,8 @@ import { interpolateRgbBasis } from 'd3-interpolate'; +import stringHash from 'string-hash'; import tinycolor from 'tinycolor2'; +import { colorManipulator } from '../themes'; import { GrafanaTheme2 } from '../themes/types'; import { reduceField } from '../transformations/fieldReducer'; import { FALLBACK_COLOR, Field, FieldColorModeId, Threshold } from '../types'; @@ -19,6 +21,7 @@ export interface FieldColorMode extends RegistryItem { getColors?: (theme: GrafanaTheme2) => string[]; isContinuous?: boolean; isByValue?: boolean; + useSeriesName?: boolean; } /** @internal */ @@ -57,6 +60,22 @@ export const fieldColorModeRegistry = new Registry