Skip to content

Commit

Permalink
[maps] fix marker size scale issue for counts (#132057)
Browse files Browse the repository at this point in the history
* [maps] fix marker size scale issue for counts

* fix test names

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
  • Loading branch information
nreese and kibanamachine authored May 12, 2022
1 parent 94b894a commit c9372dc
Show file tree
Hide file tree
Showing 4 changed files with 181 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ describe('pluckStyleMetaFromFeatures', () => {
});
});

test('Should extract scaled field range', async () => {
test('Should extract range', async () => {
const features = [
{
type: 'Feature',
Expand All @@ -197,7 +197,7 @@ describe('pluckStyleMetaFromFeatures', () => {
coordinates: [0, 0],
},
properties: {
myDynamicField: 1,
myDynamicField: 3,
},
},
{
Expand Down Expand Up @@ -242,9 +242,9 @@ describe('pluckStyleMetaFromFeatures', () => {
myDynamicField: {
categories: [],
range: {
delta: 9,
delta: 7,
max: 10,
min: 1,
min: 3,
},
},
},
Expand All @@ -255,6 +255,65 @@ describe('pluckStyleMetaFromFeatures', () => {
},
});
});

test('Should extract range with "min = 1" for count field', async () => {
const features = [
{
type: 'Feature',
geometry: {
type: 'Point',
coordinates: [0, 0],
},
properties: {
count: 3,
},
},
{
type: 'Feature',
geometry: {
type: 'Point',
coordinates: [0, 0],
},
properties: {
count: 10,
},
},
] as Feature[];
const dynamicColorOptions = {
type: COLOR_MAP_TYPE.ORDINAL,
field: {
origin: FIELD_ORIGIN.SOURCE,
name: 'count',
},
} as ColorDynamicOptions;
const field = new InlineField({
fieldName: dynamicColorOptions.field!.name,
source: {} as unknown as IVectorSource,
origin: dynamicColorOptions.field!.origin,
dataType: 'number',
});
field.isCount = () => {
return true;
};
const dynamicColorProperty = new DynamicColorProperty(
dynamicColorOptions,
VECTOR_STYLES.FILL_COLOR,
field,
{} as unknown as IVectorLayer,
() => {
return null;
} // getFieldFormatter
);

const styleMeta = await pluckStyleMetaFromFeatures(features, Object.values(VECTOR_SHAPE_TYPE), [
dynamicColorProperty,
]);
expect(styleMeta.fieldMeta.count.range).toEqual({
delta: 9,
max: 10,
min: 1,
});
});
});

describe('pluckCategoricalStyleMetaFromFeatures', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,18 +113,22 @@ function pluckOrdinalStyleMetaFromFeatures(
property: IDynamicStyleProperty<DynamicStylePropertyOptions>,
features: Feature[]
): RangeFieldMeta | null {
if (!property.isOrdinal()) {
const field = property.getField();
if (!field || !property.isOrdinal()) {
return null;
}

const name = property.getFieldName();
let min = Infinity;
const isCount = field.isCount();
let min = isCount ? 1 : Infinity;
let max = -Infinity;
for (let i = 0; i < features.length; i++) {
const feature = features[i];
const newValue = feature.properties ? parseFloat(feature.properties[name]) : NaN;
if (!isNaN(newValue)) {
min = Math.min(min, newValue);
if (!isCount) {
min = Math.min(min, newValue);
}
max = Math.max(max, newValue);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import { FIELD_ORIGIN } from '../../../../../common/constants';
import { TileMetaFeature } from '../../../../../common/descriptor_types';
import { pluckOrdinalStyleMeta } from './pluck_style_meta';
import { IField } from '../../../fields/field';
import { DynamicSizeProperty } from '../../../styles/vector/properties/dynamic_size_property';

describe('pluckOrdinalStyleMeta', () => {
test('should pluck range from metaFeatures', () => {
const mockField = {
isCount: () => {
return false;
},
pluckRangeFromTileMetaFeature: (metaFeature: TileMetaFeature) => {
return {
max: metaFeature.properties['aggregations.avg_of_bytes.max'],
min: metaFeature.properties['aggregations.avg_of_bytes.min'],
};
},
} as unknown as IField;
const mockStyleProperty = {
getField: () => {
return mockField;
},
isOrdinal: () => {
return true;
},
getFieldOrigin: () => {
return FIELD_ORIGIN.SOURCE;
},
} as unknown as DynamicSizeProperty;
const metaFeatures = [
{
properties: {
'aggregations.avg_of_bytes.max': 7565,
'aggregations.avg_of_bytes.min': 1622,
},
} as unknown as TileMetaFeature,
{
properties: {
'aggregations.avg_of_bytes.max': 11869,
'aggregations.avg_of_bytes.min': 659,
},
} as unknown as TileMetaFeature,
];
expect(pluckOrdinalStyleMeta(mockStyleProperty, metaFeatures, undefined)).toEqual({
max: 11869,
min: 659,
delta: 11210,
});
});

test('should pluck range with min: 1 from metaFeatures for count field', () => {
const mockField = {
isCount: () => {
return true;
},
pluckRangeFromTileMetaFeature: (metaFeature: TileMetaFeature) => {
return {
max: metaFeature.properties['aggregations._count.max'],
min: metaFeature.properties['aggregations._count.min'],
};
},
} as unknown as IField;
const mockStyleProperty = {
getField: () => {
return mockField;
},
isOrdinal: () => {
return true;
},
getFieldOrigin: () => {
return FIELD_ORIGIN.SOURCE;
},
} as unknown as DynamicSizeProperty;
const metaFeatures = [
{
properties: {
'aggregations._count.max': 35,
'aggregations._count.min': 3,
},
} as unknown as TileMetaFeature,
{
properties: {
'aggregations._count.max': 36,
'aggregations._count.min': 5,
},
} as unknown as TileMetaFeature,
];
expect(pluckOrdinalStyleMeta(mockStyleProperty, metaFeatures, undefined)).toEqual({
max: 36,
min: 1,
delta: 35,
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ function pluckCategoricalStyleMeta(
return [];
}

function pluckOrdinalStyleMeta(
export function pluckOrdinalStyleMeta(
property: IDynamicStyleProperty<DynamicStylePropertyOptions>,
metaFeatures: TileMetaFeature[],
joinPropertiesMap: PropertiesMap | undefined
Expand All @@ -80,21 +80,26 @@ function pluckOrdinalStyleMeta(
return null;
}

let min = Infinity;
const isCount = field.isCount();
let min = isCount ? 1 : Infinity;
let max = -Infinity;
if (property.getFieldOrigin() === FIELD_ORIGIN.SOURCE) {
for (let i = 0; i < metaFeatures.length; i++) {
const range = field.pluckRangeFromTileMetaFeature(metaFeatures[i]);
if (range) {
min = Math.min(range.min, min);
if (!isCount) {
min = Math.min(range.min, min);
}
max = Math.max(range.max, max);
}
}
} else if (property.getFieldOrigin() === FIELD_ORIGIN.JOIN && joinPropertiesMap) {
joinPropertiesMap.forEach((value: { [key: string]: unknown }) => {
const propertyValue = value[field.getName()];
if (typeof propertyValue === 'number') {
min = Math.min(propertyValue as number, min);
if (!isCount) {
min = Math.min(propertyValue as number, min);
}
max = Math.max(propertyValue as number, max);
}
});
Expand Down

0 comments on commit c9372dc

Please sign in to comment.