Skip to content

Commit aaadc29

Browse files
[Maps] use EuiColorPalettePicker (#69190) (#71510)
* [Maps] use EuiColorPalettePicker and Eui palettes * use new ramps to create mb style * update ColorMapSelect to use EuiColorPalettePicker * move color_utils test to color_palettes * clean up heatmap constants * tslint * fix test expects * fix merge mistake * update jest expects * remove .chromium folder * another jest expect update * remove charts from kibana.json * remove unneeded jest.mock Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com> Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
1 parent fe4adf4 commit aaadc29

26 files changed

+446
-520
lines changed

x-pack/plugins/maps/kibana.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
"server": true,
2222
"extraPublicDirs": ["common/constants"],
2323
"requiredBundles": [
24-
"charts",
2524
"kibanaReact",
2625
"kibanaUtils",
2726
"savedObjects"

x-pack/plugins/maps/public/classes/sources/es_geo_grid_source/clusters_layer_wizard.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import {
2828
VECTOR_STYLES,
2929
STYLE_TYPE,
3030
} from '../../../../common/constants';
31-
import { COLOR_GRADIENTS } from '../../styles/color_utils';
31+
import { NUMERICAL_COLOR_PALETTES } from '../../styles/color_palettes';
3232

3333
export const clustersLayerWizardConfig: LayerWizard = {
3434
categories: [LAYER_WIZARD_CATEGORY.ELASTICSEARCH],
@@ -57,7 +57,7 @@ export const clustersLayerWizardConfig: LayerWizard = {
5757
name: COUNT_PROP_NAME,
5858
origin: FIELD_ORIGIN.SOURCE,
5959
},
60-
color: COLOR_GRADIENTS[0].value,
60+
color: NUMERICAL_COLOR_PALETTES[0].value,
6161
type: COLOR_MAP_TYPE.ORDINAL,
6262
},
6363
},

x-pack/plugins/maps/public/classes/sources/es_pew_pew_source/point_2_point_layer_wizard.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import {
1818
VECTOR_STYLES,
1919
STYLE_TYPE,
2020
} from '../../../../common/constants';
21-
import { COLOR_GRADIENTS } from '../../styles/color_utils';
21+
import { NUMERICAL_COLOR_PALETTES } from '../../styles/color_palettes';
2222
// @ts-ignore
2323
import { CreateSourceEditor } from './create_source_editor';
2424
import { LayerWizard, RenderWizardArguments } from '../../layers/layer_wizard_registry';
@@ -50,7 +50,7 @@ export const point2PointLayerWizardConfig: LayerWizard = {
5050
name: COUNT_PROP_NAME,
5151
origin: FIELD_ORIGIN.SOURCE,
5252
},
53-
color: COLOR_GRADIENTS[0].value,
53+
color: NUMERICAL_COLOR_PALETTES[0].value,
5454
},
5555
},
5656
[VECTOR_STYLES.LINE_WIDTH]: {

x-pack/plugins/maps/public/classes/styles/_index.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
@import 'components/color_gradient';
1+
@import 'heatmap/components/legend/color_gradient';
22
@import 'vector/components/style_prop_editor';
33
@import 'vector/components/color/color_stops';
44
@import 'vector/components/symbol/icon_select';
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License;
4+
* you may not use this file except in compliance with the Elastic License.
5+
*/
6+
import {
7+
getColorRampCenterColor,
8+
getOrdinalMbColorRampStops,
9+
getColorPalette,
10+
} from './color_palettes';
11+
12+
describe('getColorPalette', () => {
13+
it('Should create RGB color ramp', () => {
14+
expect(getColorPalette('Blues')).toEqual([
15+
'#ecf1f7',
16+
'#d9e3ef',
17+
'#c5d5e7',
18+
'#b2c7df',
19+
'#9eb9d8',
20+
'#8bacd0',
21+
'#769fc8',
22+
'#6092c0',
23+
]);
24+
});
25+
});
26+
27+
describe('getColorRampCenterColor', () => {
28+
it('Should get center color from color ramp', () => {
29+
expect(getColorRampCenterColor('Blues')).toBe('#9eb9d8');
30+
});
31+
});
32+
33+
describe('getOrdinalMbColorRampStops', () => {
34+
it('Should create color stops for custom range', () => {
35+
expect(getOrdinalMbColorRampStops('Blues', 0, 1000)).toEqual([
36+
0,
37+
'#ecf1f7',
38+
125,
39+
'#d9e3ef',
40+
250,
41+
'#c5d5e7',
42+
375,
43+
'#b2c7df',
44+
500,
45+
'#9eb9d8',
46+
625,
47+
'#8bacd0',
48+
750,
49+
'#769fc8',
50+
875,
51+
'#6092c0',
52+
]);
53+
});
54+
55+
it('Should snap to end of color stops for identical range', () => {
56+
expect(getOrdinalMbColorRampStops('Blues', 23, 23)).toEqual([23, '#6092c0']);
57+
});
58+
});
Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License;
4+
* you may not use this file except in compliance with the Elastic License.
5+
*/
6+
7+
import tinycolor from 'tinycolor2';
8+
import {
9+
// @ts-ignore
10+
euiPaletteForStatus,
11+
// @ts-ignore
12+
euiPaletteForTemperature,
13+
// @ts-ignore
14+
euiPaletteCool,
15+
// @ts-ignore
16+
euiPaletteWarm,
17+
// @ts-ignore
18+
euiPaletteNegative,
19+
// @ts-ignore
20+
euiPalettePositive,
21+
// @ts-ignore
22+
euiPaletteGray,
23+
// @ts-ignore
24+
euiPaletteColorBlind,
25+
} from '@elastic/eui/lib/services';
26+
import { EuiColorPalettePickerPaletteProps } from '@elastic/eui';
27+
28+
export const DEFAULT_HEATMAP_COLOR_RAMP_NAME = 'theclassic';
29+
30+
export const DEFAULT_FILL_COLORS: string[] = euiPaletteColorBlind();
31+
export const DEFAULT_LINE_COLORS: string[] = [
32+
...DEFAULT_FILL_COLORS.map((color: string) => tinycolor(color).darken().toHexString()),
33+
// Explicitly add black & white as border color options
34+
'#000',
35+
'#FFF',
36+
];
37+
38+
const COLOR_PALETTES: EuiColorPalettePickerPaletteProps[] = [
39+
{
40+
value: 'Blues',
41+
palette: euiPaletteCool(8),
42+
type: 'gradient',
43+
},
44+
{
45+
value: 'Greens',
46+
palette: euiPalettePositive(8),
47+
type: 'gradient',
48+
},
49+
{
50+
value: 'Greys',
51+
palette: euiPaletteGray(8),
52+
type: 'gradient',
53+
},
54+
{
55+
value: 'Reds',
56+
palette: euiPaletteNegative(8),
57+
type: 'gradient',
58+
},
59+
{
60+
value: 'Yellow to Red',
61+
palette: euiPaletteWarm(8),
62+
type: 'gradient',
63+
},
64+
{
65+
value: 'Green to Red',
66+
palette: euiPaletteForStatus(8),
67+
type: 'gradient',
68+
},
69+
{
70+
value: 'Blue to Red',
71+
palette: euiPaletteForTemperature(8),
72+
type: 'gradient',
73+
},
74+
{
75+
value: DEFAULT_HEATMAP_COLOR_RAMP_NAME,
76+
palette: [
77+
'rgb(65, 105, 225)', // royalblue
78+
'rgb(0, 256, 256)', // cyan
79+
'rgb(0, 256, 0)', // lime
80+
'rgb(256, 256, 0)', // yellow
81+
'rgb(256, 0, 0)', // red
82+
],
83+
type: 'gradient',
84+
},
85+
{
86+
value: 'palette_0',
87+
palette: euiPaletteColorBlind(),
88+
type: 'fixed',
89+
},
90+
{
91+
value: 'palette_20',
92+
palette: euiPaletteColorBlind({ rotations: 2 }),
93+
type: 'fixed',
94+
},
95+
{
96+
value: 'palette_30',
97+
palette: euiPaletteColorBlind({ rotations: 3 }),
98+
type: 'fixed',
99+
},
100+
];
101+
102+
export const NUMERICAL_COLOR_PALETTES = COLOR_PALETTES.filter(
103+
(palette: EuiColorPalettePickerPaletteProps) => {
104+
return palette.type === 'gradient';
105+
}
106+
);
107+
108+
export const CATEGORICAL_COLOR_PALETTES = COLOR_PALETTES.filter(
109+
(palette: EuiColorPalettePickerPaletteProps) => {
110+
return palette.type === 'fixed';
111+
}
112+
);
113+
114+
export function getColorPalette(colorPaletteId: string): string[] {
115+
const colorPalette = COLOR_PALETTES.find(({ value }: EuiColorPalettePickerPaletteProps) => {
116+
return value === colorPaletteId;
117+
});
118+
return colorPalette ? (colorPalette.palette as string[]) : [];
119+
}
120+
121+
export function getColorRampCenterColor(colorPaletteId: string): string | null {
122+
if (!colorPaletteId) {
123+
return null;
124+
}
125+
const palette = getColorPalette(colorPaletteId);
126+
return palette.length === 0 ? null : palette[Math.floor(palette.length / 2)];
127+
}
128+
129+
// Returns an array of color stops
130+
// [ stop_input_1: number, stop_output_1: color, stop_input_n: number, stop_output_n: color ]
131+
export function getOrdinalMbColorRampStops(
132+
colorPaletteId: string,
133+
min: number,
134+
max: number
135+
): Array<number | string> | null {
136+
if (!colorPaletteId) {
137+
return null;
138+
}
139+
140+
if (min > max) {
141+
return null;
142+
}
143+
144+
const palette = getColorPalette(colorPaletteId);
145+
if (palette.length === 0) {
146+
return null;
147+
}
148+
149+
if (max === min) {
150+
// just return single stop value
151+
return [max, palette[palette.length - 1]];
152+
}
153+
154+
const delta = max - min;
155+
return palette.reduce(
156+
(accu: Array<number | string>, stopColor: string, idx: number, srcArr: string[]) => {
157+
const stopNumber = min + (delta * idx) / srcArr.length;
158+
return [...accu, stopNumber, stopColor];
159+
},
160+
[]
161+
);
162+
}
163+
164+
export function getLinearGradient(colorStrings: string[]): string {
165+
const intervals = colorStrings.length;
166+
let linearGradient = `linear-gradient(to right, ${colorStrings[0]} 0%,`;
167+
for (let i = 1; i < intervals - 1; i++) {
168+
linearGradient = `${linearGradient} ${colorStrings[i]} \
169+
${Math.floor((100 * i) / (intervals - 1))}%,`;
170+
}
171+
return `${linearGradient} ${colorStrings[colorStrings.length - 1]} 100%)`;
172+
}

x-pack/plugins/maps/public/classes/styles/color_utils.test.ts

Lines changed: 0 additions & 104 deletions
This file was deleted.

0 commit comments

Comments
 (0)