Skip to content

Commit f2d1095

Browse files
[Maps] add categorical palettes with 20 and 30 categories (#64701)
* [Maps] add categorical palettes with 20 and 30 categories * fix ts-lint Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
1 parent 1bb1091 commit f2d1095

File tree

10 files changed

+63
-46
lines changed

10 files changed

+63
-46
lines changed

x-pack/plugins/maps/common/constants.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,6 @@ export const COLOR_MAP_TYPE = {
174174
ORDINAL: 'ORDINAL',
175175
};
176176

177-
export const COLOR_PALETTE_MAX_SIZE = 10;
178-
179177
export const CATEGORICAL_DATA_TYPES = ['string', 'ip', 'boolean'];
180178
export const ORDINAL_DATA_TYPES = ['number', 'date'];
181179

x-pack/plugins/maps/public/layers/blended_vector_layer.ts

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -260,33 +260,40 @@ export class BlendedVectorLayer extends VectorLayer implements IVectorLayer {
260260
prevDataRequest: this.getDataRequest(dataRequestId),
261261
nextMeta: searchFilters,
262262
});
263-
if (canSkipFetch) {
264-
return;
265-
}
266-
267-
let isSyncClustered;
268-
try {
269-
syncContext.startLoading(dataRequestId, requestToken, searchFilters);
270-
const searchSource = await this._documentSource.makeSearchSource(searchFilters, 0);
271-
const resp = await searchSource.fetch();
272-
const maxResultWindow = await this._documentSource.getMaxResultWindow();
273-
isSyncClustered = resp.hits.total > maxResultWindow;
274-
syncContext.stopLoading(dataRequestId, requestToken, { isSyncClustered }, searchFilters);
275-
} catch (error) {
276-
if (!(error instanceof DataRequestAbortError)) {
277-
syncContext.onLoadError(dataRequestId, requestToken, error.message);
278-
}
279-
return;
280-
}
281263

282264
let activeSource;
283265
let activeStyle;
284-
if (isSyncClustered) {
285-
activeSource = this._clusterSource;
286-
activeStyle = this._clusterStyle;
266+
if (canSkipFetch) {
267+
// Even when source fetch is skipped, need to call super._syncData to sync StyleMeta and formatters
268+
if (this._isClustered) {
269+
activeSource = this._clusterSource;
270+
activeStyle = this._clusterStyle;
271+
} else {
272+
activeSource = this._documentSource;
273+
activeStyle = this._documentStyle;
274+
}
287275
} else {
288-
activeSource = this._documentSource;
289-
activeStyle = this._documentStyle;
276+
let isSyncClustered;
277+
try {
278+
syncContext.startLoading(dataRequestId, requestToken, searchFilters);
279+
const searchSource = await this._documentSource.makeSearchSource(searchFilters, 0);
280+
const resp = await searchSource.fetch();
281+
const maxResultWindow = await this._documentSource.getMaxResultWindow();
282+
isSyncClustered = resp.hits.total > maxResultWindow;
283+
syncContext.stopLoading(dataRequestId, requestToken, { isSyncClustered }, searchFilters);
284+
} catch (error) {
285+
if (!(error instanceof DataRequestAbortError)) {
286+
syncContext.onLoadError(dataRequestId, requestToken, error.message);
287+
}
288+
return;
289+
}
290+
if (isSyncClustered) {
291+
activeSource = this._clusterSource;
292+
activeStyle = this._clusterStyle;
293+
} else {
294+
activeSource = this._documentSource;
295+
activeStyle = this._documentStyle;
296+
}
290297
}
291298

292299
super._syncData(syncContext, activeSource, activeStyle);

x-pack/plugins/maps/public/layers/fields/es_agg_field.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,8 @@ export class ESAggField implements IESAggField {
125125
return this._esDocField ? this._esDocField.getOrdinalFieldMetaRequest() : null;
126126
}
127127

128-
async getCategoricalFieldMetaRequest(): Promise<unknown> {
129-
return this._esDocField ? this._esDocField.getCategoricalFieldMetaRequest() : null;
128+
async getCategoricalFieldMetaRequest(size: number): Promise<unknown> {
129+
return this._esDocField ? this._esDocField.getCategoricalFieldMetaRequest(size) : null;
130130
}
131131
}
132132

x-pack/plugins/maps/public/layers/fields/es_doc_field.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import { FIELD_ORIGIN } from '../../../common/constants';
88
import { ESTooltipProperty } from '../tooltips/es_tooltip_property';
99
import { ITooltipProperty, TooltipProperty } from '../tooltips/tooltip_property';
10-
import { COLOR_PALETTE_MAX_SIZE } from '../../../common/constants';
1110
import { indexPatterns } from '../../../../../../src/plugins/data/public';
1211
import { IFieldType } from '../../../../../../src/plugins/data/public';
1312
import { IField, AbstractField } from './field';
@@ -89,16 +88,16 @@ export class ESDocField extends AbstractField implements IField {
8988
};
9089
}
9190

92-
async getCategoricalFieldMetaRequest(): Promise<unknown> {
91+
async getCategoricalFieldMetaRequest(size: number): Promise<unknown> {
9392
const indexPatternField = await this._getIndexPatternField();
94-
if (!indexPatternField) {
93+
if (!indexPatternField || size <= 0) {
9594
return null;
9695
}
9796

9897
// TODO remove local typing once Kibana has figured out a core place for Elasticsearch aggregation request types
9998
// https://github.com/elastic/kibana/issues/60102
10099
const topTerms: { size: number; script?: unknown; field?: string } = {
101-
size: COLOR_PALETTE_MAX_SIZE - 1, // need additional color for the "other"-value
100+
size: size - 1, // need additional color for the "other"-value
102101
};
103102
if (indexPatternField.scripted) {
104103
topTerms.script = {

x-pack/plugins/maps/public/layers/fields/field.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export interface IField {
1919
getOrigin(): FIELD_ORIGIN;
2020
isValid(): boolean;
2121
getOrdinalFieldMetaRequest(): Promise<unknown>;
22-
getCategoricalFieldMetaRequest(): Promise<unknown>;
22+
getCategoricalFieldMetaRequest(size: number): Promise<unknown>;
2323
}
2424

2525
export class AbstractField implements IField {
@@ -76,7 +76,7 @@ export class AbstractField implements IField {
7676
return null;
7777
}
7878

79-
async getCategoricalFieldMetaRequest(): Promise<unknown> {
79+
async getCategoricalFieldMetaRequest(size: number): Promise<unknown> {
8080
return null;
8181
}
8282
}

x-pack/plugins/maps/public/layers/styles/color_utils.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import tinycolor from 'tinycolor2';
99
import chroma from 'chroma-js';
1010
import { euiPaletteColorBlind } from '@elastic/eui/lib/services';
1111
import { ColorGradient } from './components/color_gradient';
12-
import { COLOR_PALETTE_MAX_SIZE } from '../../../common/constants';
1312
import { vislibColorMaps } from '../../../../../../src/plugins/charts/public';
1413

1514
const GRADIENT_INTERVALS = 8;
@@ -120,7 +119,15 @@ export function getLinearGradient(colorStrings) {
120119
const COLOR_PALETTES_CONFIGS = [
121120
{
122121
id: 'palette_0',
123-
colors: DEFAULT_FILL_COLORS.slice(0, COLOR_PALETTE_MAX_SIZE),
122+
colors: euiPaletteColorBlind(),
123+
},
124+
{
125+
id: 'palette_20',
126+
colors: euiPaletteColorBlind(2),
127+
},
128+
{
129+
id: 'palette_30',
130+
colors: euiPaletteColorBlind(3),
124131
},
125132
];
126133

@@ -133,7 +140,7 @@ export const COLOR_PALETTES = COLOR_PALETTES_CONFIGS.map(palette => {
133140
const paletteDisplay = palette.colors.map(color => {
134141
const style = {
135142
backgroundColor: color,
136-
width: '10%',
143+
width: `${100 / palette.colors.length}%`,
137144
position: 'relative',
138145
height: '100%',
139146
display: 'inline-block',

x-pack/plugins/maps/public/layers/styles/vector/properties/dynamic_color_property.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,11 @@ export class DynamicColorProperty extends DynamicStyleProperty {
9090
return this._options.type === COLOR_MAP_TYPE.CATEGORICAL;
9191
}
9292

93+
getNumberOfCategories() {
94+
const colors = getColorPalette(this._options.colorCategory);
95+
return colors ? colors.length : 0;
96+
}
97+
9398
supportsMbFeatureState() {
9499
return true;
95100
}

x-pack/plugins/maps/public/layers/styles/vector/properties/dynamic_style_property.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export interface IDynamicStyleProperty extends IStyleProperty {
2323
getFieldOrigin(): FIELD_ORIGIN | undefined;
2424
getRangeFieldMeta(): RangeFieldMeta;
2525
getCategoryFieldMeta(): CategoryFieldMeta;
26+
getNumberOfCategories(): number;
2627
isFieldMetaEnabled(): boolean;
2728
isOrdinal(): boolean;
2829
supportsFieldMeta(): boolean;

x-pack/plugins/maps/public/layers/styles/vector/properties/dynamic_style_property.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,7 @@
77
import _ from 'lodash';
88
import { AbstractStyleProperty } from './style_property';
99
import { DEFAULT_SIGMA } from '../vector_style_defaults';
10-
import {
11-
COLOR_PALETTE_MAX_SIZE,
12-
STYLE_TYPE,
13-
SOURCE_META_ID_ORIGIN,
14-
FIELD_ORIGIN,
15-
} from '../../../../../common/constants';
10+
import { STYLE_TYPE, SOURCE_META_ID_ORIGIN, FIELD_ORIGIN } from '../../../../../common/constants';
1611
import React from 'react';
1712
import { OrdinalLegend } from './components/ordinal_legend';
1813
import { CategoricalLegend } from './components/categorical_legend';
@@ -120,6 +115,10 @@ export class DynamicStyleProperty extends AbstractStyleProperty {
120115
return false;
121116
}
122117

118+
getNumberOfCategories() {
119+
return 0;
120+
}
121+
123122
hasOrdinalBreaks() {
124123
return false;
125124
}
@@ -149,7 +148,7 @@ export class DynamicStyleProperty extends AbstractStyleProperty {
149148
if (this.isOrdinal()) {
150149
return this._field.getOrdinalFieldMetaRequest();
151150
} else if (this.isCategorical()) {
152-
return this._field.getCategoricalFieldMetaRequest();
151+
return this._field.getCategoricalFieldMetaRequest(this.getNumberOfCategories());
153152
} else {
154153
return null;
155154
}
@@ -190,7 +189,8 @@ export class DynamicStyleProperty extends AbstractStyleProperty {
190189
}
191190

192191
pluckCategoricalStyleMetaFromFeatures(features) {
193-
if (!this.isCategorical()) {
192+
const size = this.getNumberOfCategories();
193+
if (!this.isCategorical() || size <= 0) {
194194
return null;
195195
}
196196

@@ -217,7 +217,7 @@ export class DynamicStyleProperty extends AbstractStyleProperty {
217217
ordered.sort((a, b) => {
218218
return b.count - a.count;
219219
});
220-
const truncated = ordered.slice(0, COLOR_PALETTE_MAX_SIZE);
220+
const truncated = ordered.slice(0, size);
221221
return {
222222
categories: truncated,
223223
};

x-pack/plugins/maps/public/layers/vector_layer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,7 @@ export class VectorLayer extends AbstractLayer {
464464
}
465465

466466
const dynamicStyleFields = dynamicStyleProps.map(dynamicStyleProp => {
467-
return dynamicStyleProp.getField().getName();
467+
return `${dynamicStyleProp.getField().getName()}${dynamicStyleProp.getNumberOfCategories()}`;
468468
});
469469

470470
const nextMeta = {

0 commit comments

Comments
 (0)