Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Maps] Remove client-side scaling of ordinal values #58528

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
27f8825
remove ordinal scaling
thomasneirynck Feb 25, 2020
5578571
fix unit test
thomasneirynck Feb 25, 2020
acbe878
fix
thomasneirynck Feb 25, 2020
df2f28f
snap to end of range
thomasneirynck Feb 26, 2020
8b193db
remove cruft
thomasneirynck Feb 26, 2020
4332be2
Merge branch 'master' into maps/remove_ordinal_scaling
thomasneirynck Feb 26, 2020
86de74e
tmp commit
thomasneirynck Feb 26, 2020
cb592ee
Merge branch 'master' into maps/remove_ordinal_scaling
thomasneirynck Feb 26, 2020
b80e3c3
coerce to number and snap value
thomasneirynck Feb 26, 2020
0a8063c
Merge branch 'master' of github.com:elastic/kibana into maps/remove_o…
thomasneirynck Feb 26, 2020
78c92a6
reduce dupe
thomasneirynck Feb 26, 2020
4d057ef
Merge branch 'master' into maps/remove_ordinal_scaling
thomasneirynck Mar 5, 2020
371950c
Merge branch 'master' of github.com:elastic/kibana into maps/remove_o…
thomasneirynck Mar 5, 2020
bceda65
fixx merge
thomasneirynck Mar 5, 2020
14e5fd4
fix
thomasneirynck Mar 5, 2020
b08e9b0
add unit test
thomasneirynck Mar 5, 2020
cd00b43
Merge branch 'master' into maps/remove_ordinal_scaling
thomasneirynck Mar 11, 2020
cf87ce8
Merge branch 'master' into maps/remove_ordinal_scaling
thomasneirynck Mar 11, 2020
52d3b8e
fix functional test
thomasneirynck Mar 12, 2020
6652c93
Merge branch 'master' into maps/remove_ordinal_scaling
thomasneirynck Mar 12, 2020
37a6016
Merge branch 'master' into maps/remove_ordinal_scaling
elasticmachine Mar 12, 2020
40c0bf5
Merge branch 'master' into maps/remove_ordinal_scaling
thomasneirynck Mar 17, 2020
2479cbe
feedback
thomasneirynck Mar 17, 2020
d824d11
improve docs
thomasneirynck Mar 17, 2020
551b267
Merge branch 'master' into maps/remove_ordinal_scaling
thomasneirynck Mar 17, 2020
8c3ea16
remove duplicated feature-state system properties
thomasneirynck Mar 17, 2020
4682b5d
Merge branch 'master' into maps/remove_ordinal_scaling
thomasneirynck Mar 18, 2020
974c221
fix tests
thomasneirynck Mar 18, 2020
7610ed7
remove unused method
thomasneirynck Mar 18, 2020
276ae43
create new test block
thomasneirynck Mar 18, 2020
c62b1e5
functional test
thomasneirynck Mar 19, 2020
7c94a0d
retain test
thomasneirynck Mar 19, 2020
aa49420
remove crufty method
thomasneirynck Mar 19, 2020
74147cf
ensure not fieldmeta is requested
thomasneirynck Mar 19, 2020
55a4dfd
simplify
thomasneirynck Mar 19, 2020
e678938
Merge branch 'master' into maps/remove_ordinal_scaling
elasticmachine Mar 23, 2020
dd3fe64
Merge branch 'master' into maps/remove_ordinal_scaling
thomasneirynck Mar 23, 2020
b2fb955
add unit tests
thomasneirynck Mar 23, 2020
0342357
Merge branch 'maps/remove_ordinal_scaling' of github.com:thomasneiryn…
thomasneirynck Mar 23, 2020
0257429
improve readability
thomasneirynck Mar 23, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,17 @@ export function getColorRampCenterColor(colorRampName) {

// Returns an array of color stops
// [ stop_input_1: number, stop_output_1: color, stop_input_n: number, stop_output_n: color ]
export function getOrdinalColorRampStops(colorRampName, numberColors = GRADIENT_INTERVALS) {
export function getOrdinalColorRampStops(colorRampName, min, max) {
if (!colorRampName) {
return null;
}

const numberColors = GRADIENT_INTERVALS;
const delta = max - min;
return getHexColorRangeStrings(colorRampName, numberColors).reduce(
(accu, stopColor, idx, srcArr) => {
const stopNumber = idx / srcArr.length; // number between 0 and 1, increasing as index increases
// const stopNumber = idx / srcArr.length; // number between 0 and 1, increasing as index increases
const stopNumber = delta > 0 ? min + (delta * idx) / srcArr.length : 1;
thomasneirynck marked this conversation as resolved.
Show resolved Hide resolved
return [...accu, stopNumber, stopColor];
},
[]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ describe('getColorRampCenterColor', () => {

describe('getColorRampStops', () => {
it('Should create color stops for color ramp', () => {
expect(getOrdinalColorRampStops('Blues')).toEqual([
expect(getOrdinalColorRampStops('Blues', 0, 1)).toEqual([
thomasneirynck marked this conversation as resolved.
Show resolved Hide resolved
0,
'#f7faff',
0.125,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export class HeatmapStyle extends AbstractStyle {

const { colorRampName } = this._descriptor;
if (colorRampName && colorRampName !== DEFAULT_HEATMAP_COLOR_RAMP_NAME) {
const colorStops = getOrdinalColorRampStops(colorRampName);
const colorStops = getOrdinalColorRampStops(colorRampName, 0, 1);
thomasneirynck marked this conversation as resolved.
Show resolved Hide resolved
thomasneirynck marked this conversation as resolved.
Show resolved Hide resolved
thomasneirynck marked this conversation as resolved.
Show resolved Hide resolved
mbMap.setPaintProperty(layerId, 'heatmap-color', [
'interpolate',
['linear'],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,6 @@ export class DynamicColorProperty extends DynamicStyleProperty {
return true;
}

isOrdinalScaled() {
return this.isOrdinal() && !this.isCustomOrdinalColorRamp();
}

isOrdinalRanged() {
return this.isOrdinal() && !this.isCustomOrdinalColorRamp();
}
Expand Down Expand Up @@ -232,7 +228,11 @@ export class DynamicColorProperty extends DynamicStyleProperty {
return [...accumulatedStops, nextStop.stop, nextStop.color];
}, []);
} else {
return getOrdinalColorRampStops(this._options.color);
const rangeFieldMeta = this.getFieldMeta();
if (!rangeFieldMeta) {
return null;
}
return getOrdinalColorRampStops(this._options.color, rangeFieldMeta.min, rangeFieldMeta.max);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,4 @@ export class DynamicOrientationProperty extends DynamicStyleProperty {
supportsFeatureState() {
return false;
}

isOrdinalScaled() {
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -121,25 +121,28 @@ export class DynamicSizeProperty extends DynamicStyleProperty {
}

getMbSizeExpression() {
if (this._isSizeDynamicConfigComplete(this._options)) {
return this._getMbDataDrivenSize({
targetName: this.getComputedFieldName(),
minSize: this._options.minSize,
maxSize: this._options.maxSize,
});
if (!this._isSizeDynamicConfigComplete(this._options)) {
return null;
}
return null;

return this._getMbDataDrivenSize({
targetName: this.getComputedFieldName(),
minSize: this._options.minSize,
maxSize: this._options.maxSize,
});
}

thomasneirynck marked this conversation as resolved.
Show resolved Hide resolved
_getMbDataDrivenSize({ targetName, minSize, maxSize }) {
const rangeFieldMeta = this.getFieldMeta();
thomasneirynck marked this conversation as resolved.
Show resolved Hide resolved

const lookup = this.supportsFeatureState() ? 'feature-state' : 'get';
return [
'interpolate',
['linear'],
['coalesce', [lookup, targetName], 0],
0,
rangeFieldMeta.min,
minSize,
1,
rangeFieldMeta.max,
maxSize,
];
}
Expand All @@ -149,7 +152,8 @@ export class DynamicSizeProperty extends DynamicStyleProperty {
this._field &&
this._field.isValid() &&
_.has(this._options, 'minSize') &&
_.has(this._options, 'maxSize')
_.has(this._options, 'maxSize') &&
this.getFieldMeta()
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import _ from 'lodash';
import { AbstractStyleProperty } from './style_property';
import { DEFAULT_SIGMA } from '../vector_style_defaults';
import { COLOR_PALETTE_MAX_SIZE, STYLE_TYPE } from '../../../../../common/constants';
import { scaleValue, getComputedFieldName } from '../style_util';
import { getComputedFieldName } from '../style_util';
import React from 'react';
import { OrdinalLegend } from './components/ordinal_legend';
import { CategoricalLegend } from './components/categorical_legend';
Expand Down Expand Up @@ -84,7 +84,7 @@ export class DynamicStyleProperty extends AbstractStyleProperty {

supportsFieldMeta() {
if (this.isOrdinal()) {
return this.isComplete() && this.isOrdinalScaled() && this._field.supportsFieldMeta();
return this.isComplete() && this._field.supportsFieldMeta();
thomasneirynck marked this conversation as resolved.
Show resolved Hide resolved
} else if (this.isCategorical()) {
return this.isComplete() && this._field.supportsFieldMeta();
} else {
Expand All @@ -109,10 +109,6 @@ export class DynamicStyleProperty extends AbstractStyleProperty {
return true;
}

isOrdinalScaled() {
return true;
}

getFieldMetaOptions() {
return _.get(this.getOptions(), 'fieldMetaOptions', {});
}
Expand Down Expand Up @@ -245,9 +241,6 @@ export class DynamicStyleProperty extends AbstractStyleProperty {
}

const valueAsFloat = parseFloat(value);
if (this.isOrdinalScaled()) {
return scaleValue(valueAsFloat, this.getFieldMeta());
}
if (isNaN(valueAsFloat)) {
return 0;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,4 @@ export class DynamicTextProperty extends DynamicStyleProperty {
supportsFeatureState() {
return false;
}

isOrdinalScaled() {
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,22 +34,6 @@ export function isOnlySingleFeatureType(featureType, supportedFeatures, hasFeatu
}, true);
}

export function scaleValue(value, range) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still think we need to preserve this logic and snap NaN values to less then min so they have the same behavior and do not cause errors. Also, snapping values less then min and over max need to be preserved. When using aggregations to fetch min/max, there will be values greater than and less then min/max

Copy link
Contributor Author

@thomasneirynck thomasneirynck Feb 26, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks, agreed.

After discussion, we're moving this clamping to the actual mb-expression. This way, we do not need to retain this separate scaleValue code-path. It has following template:

['max', ['min', ['to-number', [lookupFunction, fieldName]], maxValue], minValue],

if (isNaN(value) || !range) {
return -1; //Nothing to scale, put outside scaled range
}

if (range.delta === 0 || value >= range.max) {
return 1; //snap to end of scaled range
}

if (value <= range.min) {
return 0; //snap to beginning of scaled range
}

return (value - range.min) / range.delta;
}

export function assignCategoriesToPalette({ categories, paletteValues }) {
const stops = [];
let fallback = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { isOnlySingleFeatureType, scaleValue, assignCategoriesToPalette } from './style_util';
import { isOnlySingleFeatureType, assignCategoriesToPalette } from './style_util';
import { VECTOR_SHAPE_TYPES } from '../../sources/vector_feature_types';

describe('isOnlySingleFeatureType', () => {
Expand Down Expand Up @@ -62,32 +62,6 @@ describe('isOnlySingleFeatureType', () => {
});
});

describe('scaleValue', () => {
test('Should scale value between 0 and 1', () => {
expect(scaleValue(5, { min: 0, max: 10, delta: 10 })).toBe(0.5);
});

test('Should snap value less then range min to 0', () => {
expect(scaleValue(-1, { min: 0, max: 10, delta: 10 })).toBe(0);
});

test('Should snap value greater then range max to 1', () => {
expect(scaleValue(11, { min: 0, max: 10, delta: 10 })).toBe(1);
});

test('Should snap value to 1 when tere is not range delta', () => {
expect(scaleValue(10, { min: 10, max: 10, delta: 0 })).toBe(1);
});

test('Should put value as -1 when value is not provided', () => {
expect(scaleValue(undefined, { min: 0, max: 10, delta: 10 })).toBe(-1);
});

test('Should put value as -1 when range is not provided', () => {
expect(scaleValue(5, undefined)).toBe(-1);
});
});

describe('assignCategoriesToPalette', () => {
test('Categories and palette values have same length', () => {
const categories = [{ key: 'alpah' }, { key: 'bravo' }, { key: 'charlie' }, { key: 'delta' }];
Expand Down