Skip to content

Commit

Permalink
[Maps] Add field and style-meta types (#58766) (#59450)
Browse files Browse the repository at this point in the history
Co-authored-by: Elastic Machine <[email protected]>
  • Loading branch information
thomasneirynck and elasticmachine authored Mar 7, 2020
1 parent 322a992 commit 30f3868
Show file tree
Hide file tree
Showing 11 changed files with 291 additions and 168 deletions.
33 changes: 33 additions & 0 deletions x-pack/legacy/plugins/maps/common/descriptor_types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,36 @@ export type VectorLayerDescriptor = LayerDescriptor & {
joins?: JoinDescriptor[];
style?: unknown;
};

export type RangeFieldMeta = {
min: number;
max: number;
delta: number;
isMinOutsideStdRange?: boolean;
isMaxOutsideStdRange?: boolean;
};

export type Category = {
key: string;
count: number;
};

export type CategoryFieldMeta = {
categories: Category[];
};

export type GeometryTypes = {
isPointsOnly: boolean;
isLinesOnly: boolean;
isPolygonsOnly: boolean;
};

export type StyleMetaDescriptor = {
geometryTypes?: GeometryTypes;
fieldMeta: {
[key: string]: {
range: RangeFieldMeta;
categories: CategoryFieldMeta;
};
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export class OrdinalLegend extends React.Component {
this._loadParams();
}
render() {
const fieldMeta = this.props.style.getFieldMeta();
const fieldMeta = this.props.style.getRangeFieldMeta();

let minLabel = EMPTY_VALUE;
let maxLabel = EMPTY_VALUE;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ export class DynamicColorProperty extends DynamicStyleProperty {
};
}

const fieldMeta = this.getFieldMeta();
const fieldMeta = this.getCategoryFieldMeta();
if (!fieldMeta || !fieldMeta.categories) {
return EMPTY_STOPS;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ import { shallow } from 'enzyme';

import { VECTOR_STYLES } from '../vector_style_defaults';
import { DynamicColorProperty } from './dynamic_color_property';
import { COLOR_MAP_TYPE } from '../../../../../common/constants';
import { StyleMeta } from '../style_meta';
import { COLOR_MAP_TYPE, FIELD_ORIGIN } from '../../../../../common/constants';

const mockField = {
async getLabel() {
Expand All @@ -28,35 +29,59 @@ const mockField = {
getRootName() {
return 'foobar';
},
getOrigin() {
return FIELD_ORIGIN.SOURCE;
},
supportsFieldMeta() {
return true;
},
};

const getOrdinalFieldMeta = () => {
return { min: 0, max: 100 };
};

const getCategoricalFieldMeta = () => {
return {
categories: [
{
key: 'US',
count: 10,
class MockStyle {
getStyleMeta() {
return new StyleMeta({
geometryTypes: {
isPointsOnly: false,
isLinesOnly: false,
isPolygonsOnly: false,
},
{
key: 'CN',
count: 8,
fieldMeta: {
foobar: {
range: { min: 0, max: 100 },
categories: {
categories: [
{
key: 'US',
count: 10,
},
{
key: 'CN',
count: 8,
},
],
},
},
},
],
};
};
const makeProperty = (options, getFieldMeta) => {
});
}
}

class MockLayer {
getStyle() {
return new MockStyle();
}

findDataRequestById() {
return null;
}
}

const makeProperty = options => {
return new DynamicColorProperty(
options,
VECTOR_STYLES.LINE_COLOR,
mockField,
getFieldMeta,
new MockLayer(),
() => {
return x => x + '_format';
}
Expand All @@ -69,13 +94,10 @@ const defaultLegendParams = {
};

test('Should render ordinal legend', async () => {
const colorStyle = makeProperty(
{
color: 'Blues',
type: undefined,
},
getOrdinalFieldMeta
);
const colorStyle = makeProperty({
color: 'Blues',
type: undefined,
});

const legendRow = colorStyle.renderLegendDetailRow(defaultLegendParams);

Expand All @@ -85,23 +107,20 @@ test('Should render ordinal legend', async () => {
});

test('Should render ordinal legend with breaks', async () => {
const colorStyle = makeProperty(
{
type: COLOR_MAP_TYPE.ORDINAL,
useCustomColorRamp: true,
customColorRamp: [
{
stop: 0,
color: '#FF0000',
},
{
stop: 10,
color: '#00FF00',
},
],
},
getOrdinalFieldMeta
);
const colorStyle = makeProperty({
type: COLOR_MAP_TYPE.ORDINAL,
useCustomColorRamp: true,
customColorRamp: [
{
stop: 0,
color: '#FF0000',
},
{
stop: 10,
color: '#00FF00',
},
],
});

const legendRow = colorStyle.renderLegendDetailRow(defaultLegendParams);

Expand All @@ -116,14 +135,11 @@ test('Should render ordinal legend with breaks', async () => {
});

test('Should render categorical legend with breaks from default', async () => {
const colorStyle = makeProperty(
{
type: COLOR_MAP_TYPE.CATEGORICAL,
useCustomColorPalette: false,
colorCategory: 'palette_0',
},
getCategoricalFieldMeta
);
const colorStyle = makeProperty({
type: COLOR_MAP_TYPE.CATEGORICAL,
useCustomColorPalette: false,
colorCategory: 'palette_0',
});

const legendRow = colorStyle.renderLegendDetailRow(defaultLegendParams);

Expand All @@ -138,27 +154,24 @@ test('Should render categorical legend with breaks from default', async () => {
});

test('Should render categorical legend with breaks from custom', async () => {
const colorStyle = makeProperty(
{
type: COLOR_MAP_TYPE.CATEGORICAL,
useCustomColorPalette: true,
customColorPalette: [
{
stop: null, //should include the default stop
color: '#FFFF00',
},
{
stop: 'US_STOP',
color: '#FF0000',
},
{
stop: 'CN_STOP',
color: '#00FF00',
},
],
},
getCategoricalFieldMeta
);
const colorStyle = makeProperty({
type: COLOR_MAP_TYPE.CATEGORICAL,
useCustomColorPalette: true,
customColorPalette: [
{
stop: null, //should include the default stop
color: '#FFFF00',
},
{
stop: 'US_STOP',
color: '#FF0000',
},
{
stop: 'CN_STOP',
color: '#00FF00',
},
],
});

const legendRow = colorStyle.renderLegendDetailRow(defaultLegendParams);

Expand All @@ -182,11 +195,10 @@ test('Should pluck the categorical style-meta', async () => {
const colorStyle = makeProperty({
type: COLOR_MAP_TYPE.CATEGORICAL,
colorCategory: 'palette_0',
getCategoricalFieldMeta,
});

const features = makeFeatures(['CN', 'CN', 'US', 'CN', 'US', 'IN']);
const meta = colorStyle.pluckStyleMetaFromFeatures(features);
const meta = colorStyle.pluckCategoricalStyleMetaFromFeatures(features);

expect(meta).toEqual({
categories: [
Expand All @@ -201,10 +213,9 @@ test('Should pluck the categorical style-meta from fieldmeta', async () => {
const colorStyle = makeProperty({
type: COLOR_MAP_TYPE.CATEGORICAL,
colorCategory: 'palette_0',
getCategoricalFieldMeta,
});

const meta = colorStyle.pluckStyleMetaFromFieldMetaData({
const meta = colorStyle.pluckCategoricalStyleMetaFromFieldMetaData({
foobar: {
buckets: [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export class DynamicIconProperty extends DynamicStyleProperty {
}

return assignCategoriesToPalette({
categories: _.get(this.getFieldMeta(), 'categories', []),
categories: _.get(this.getCategoryFieldMeta(), 'categories', []),
paletteValues: getIconPalette(this._options.iconPaletteId),
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ function getSymbolSizeIcons() {
}

export class DynamicSizeProperty extends DynamicStyleProperty {
constructor(options, styleName, field, getFieldMeta, getFieldFormatter, isSymbolizedAsIcon) {
super(options, styleName, field, getFieldMeta, getFieldFormatter);
constructor(options, styleName, field, vectorLayer, getFieldFormatter, isSymbolizedAsIcon) {
super(options, styleName, field, vectorLayer, getFieldFormatter);
this._isSymbolizedAsIcon = isSymbolizedAsIcon;
}

Expand Down
Loading

0 comments on commit 30f3868

Please sign in to comment.