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] Support categorical styling for numbers and dates #57908

Merged
merged 41 commits into from
Mar 9, 2020
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
f544734
boilerplate
thomasneirynck Feb 18, 2020
4be903f
styling
thomasneirynck Feb 18, 2020
894a5ba
fix
thomasneirynck Feb 18, 2020
3f798be
remove cruft
thomasneirynck Feb 20, 2020
cc0738d
Improving color map select styles
miukimiu Feb 20, 2020
67fa1b8
enable for icon-fields
thomasneirynck Feb 23, 2020
09af833
remove unused
thomasneirynck Feb 23, 2020
42d7cd3
only include styleable fields
thomasneirynck Feb 23, 2020
6cf2e34
remove number fields
thomasneirynck Feb 23, 2020
42b1d88
remove date fields
thomasneirynck Feb 23, 2020
f927b75
add tooltip
thomasneirynck Feb 23, 2020
567c36b
change text
thomasneirynck Feb 23, 2020
1a8e11a
Merge branch 'master' into number_ordinals
elasticmachine Feb 24, 2020
417c44a
Merge branch 'master' into number_ordinals
thomasneirynck Feb 26, 2020
988b567
feedback
thomasneirynck Feb 26, 2020
5d09375
remove restriction
thomasneirynck Feb 26, 2020
fa3898d
Merge branch 'master' of github.com:elastic/kibana into number_ordinals
thomasneirynck Feb 26, 2020
d6565ff
add unit test
thomasneirynck Feb 26, 2020
444cbbf
more typing
thomasneirynck Feb 26, 2020
d029094
add style ts boilerplate
thomasneirynck Feb 27, 2020
970aeda
lint
thomasneirynck Feb 27, 2020
fbad9fe
Merge branch 'master' into maps/numbers_ordinals
thomasneirynck Mar 5, 2020
c6e2535
split tests
thomasneirynck Mar 5, 2020
6067fcd
Merge branch 'master' into maps/numbers_ordinals
thomasneirynck Mar 6, 2020
ef9e742
tslint fixes
thomasneirynck Mar 6, 2020
b6764d8
lint fix
thomasneirynck Mar 6, 2020
cb0e37a
fix name
thomasneirynck Mar 6, 2020
fee6793
fix faulty merge
thomasneirynck Mar 6, 2020
89d461d
init feedback
thomasneirynck Mar 6, 2020
fae5e84
more edits
thomasneirynck Mar 6, 2020
2958ba7
test
thomasneirynck Mar 7, 2020
cc08409
language
thomasneirynck Mar 7, 2020
3c4cd9b
add dummy test
thomasneirynck Mar 8, 2020
9180698
Merge branch 'master' into maps/numbers_ordinals
thomasneirynck Mar 9, 2020
6a3b99a
remove agg type
thomasneirynck Mar 9, 2020
7813255
remove getAggType
thomasneirynck Mar 9, 2020
1c7391d
add more boilerplate
thomasneirynck Mar 9, 2020
02c725e
remove cruft
thomasneirynck Mar 9, 2020
4756563
remove aggtype
thomasneirynck Mar 9, 2020
e7153db
formatting
thomasneirynck Mar 9, 2020
a66bb34
remove from constants
thomasneirynck Mar 9, 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
1 change: 1 addition & 0 deletions x-pack/legacy/plugins/maps/common/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ export const COLOR_MAP_TYPE = {
export const COLOR_PALETTE_MAX_SIZE = 10;

export const CATEGORICAL_DATA_TYPES = ['string', 'ip', 'boolean'];
export const ORDINAL_DATA_TYPES = ['number', 'date'];

export const SYMBOLIZE_AS_TYPES = {
CIRCLE: 'circle',
Expand Down
12 changes: 0 additions & 12 deletions x-pack/legacy/plugins/maps/public/layers/layer.js
Original file line number Diff line number Diff line change
Expand Up @@ -332,18 +332,6 @@ export class AbstractLayer {
return [];
}

async getDateFields() {
return [];
}

async getNumberFields() {
return [];
}

async getCategoricalFields() {
return [];
}

async getFields() {
return [];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,6 @@ export class AbstractESAggSource extends AbstractESSource {
return this.getMetricFields().map(esAggMetric => esAggMetric.makeMetricAggConfig());
}

async getNumberFields() {
return this.getMetricFields();
}

async filterAndFormatPropertiesToHtmlForMetricFields(properties) {
const metricFields = this.getMetricFields();
const tooltipPropertiesPromises = [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import {
ES_GEO_FIELD_TYPE,
DEFAULT_MAX_BUCKETS_LIMIT,
SORT_ORDER,
CATEGORICAL_DATA_TYPES,
} from '../../../../common/constants';
import { i18n } from '@kbn/i18n';
import { getDataSourceLabel } from '../../../../common/i18n_getters';
Expand Down Expand Up @@ -159,49 +158,6 @@ export class ESSearchSource extends AbstractESSource {
);
}

async getNumberFields() {
try {
const indexPattern = await this.getIndexPattern();
return indexPattern.fields.getByType('number').map(field => {
return this.createField({ fieldName: field.name });
});
} catch (error) {
return [];
}
}

async getDateFields() {
try {
const indexPattern = await this.getIndexPattern();
return indexPattern.fields.getByType('date').map(field => {
return this.createField({ fieldName: field.name });
});
} catch (error) {
return [];
}
}

async getCategoricalFields() {
try {
const indexPattern = await this.getIndexPattern();

const aggFields = [];
CATEGORICAL_DATA_TYPES.forEach(dataType => {
indexPattern.fields.getByType(dataType).forEach(field => {
if (field.aggregatable) {
aggFields.push(field);
}
});
});
return aggFields.map(field => {
return this.createField({ fieldName: field.name });
});
} catch (error) {
//error surfaces in the LayerTOC UI
return [];
}
}

async getFields() {
try {
const indexPattern = await this.getIndexPattern();
Expand Down
12 changes: 0 additions & 12 deletions x-pack/legacy/plugins/maps/public/layers/sources/vector_source.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,22 +111,10 @@ export class AbstractVectorSource extends AbstractSource {
return null;
}

async getDateFields() {
return [];
}

async getNumberFields() {
return [];
}

async getFields() {
return [...(await this.getDateFields()), ...(await this.getNumberFields())];
thomasneirynck marked this conversation as resolved.
Show resolved Hide resolved
}

async getCategoricalFields() {
return [];
}

async getLeftJoinFields() {
return [];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,18 @@

import React, { Component, Fragment } from 'react';

import { EuiSuperSelect, EuiSpacer } from '@elastic/eui';
import {
EuiSpacer,
EuiSelect,
EuiSuperSelect,
EuiFlexGroup,
EuiFlexItem,
EuiToolTip,
} from '@elastic/eui';
import { ColorStopsOrdinal } from './color_stops_ordinal';
import { COLOR_MAP_TYPE } from '../../../../../../common/constants';
import { ColorStopsCategorical } from './color_stops_categorical';
import { i18n } from '@kbn/i18n';

const CUSTOM_COLOR_MAP = 'CUSTOM_COLOR_MAP';

Expand All @@ -27,6 +35,53 @@ export class ColorMapSelect extends Component {
};
}

_renderColorMapToggle() {
const options = [
{
value: COLOR_MAP_TYPE.ORDINAL,
text: i18n.translate('xpack.maps.styles.dynamicColorSelect.quantitativeLabel', {
defaultMessage: 'Quantitative',
}),
},
{
value: COLOR_MAP_TYPE.CATEGORICAL,
text: i18n.translate('xpack.maps.styles.dynamicColorSelect.qualitativeLavel', {
defaultMessage: 'Qualitative',
}),
},
];

const selectedValue = this.props.styleProperty.isOrdinal()
? COLOR_MAP_TYPE.ORDINAL
: COLOR_MAP_TYPE.CATEGORICAL;

return (
<EuiToolTip
position="top"
content={i18n.translate(
'xpack.maps.styles.dynamicColorSelect.qualitativeOrQuantitativeTooltip',
{
defaultMessage:
'Choose `quantitative` to map the value as a number to a color on a range. Choose `qualitative` to map the value as a category to a color from a palette',
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@gchaps I could probably use some help with some good help-text here 😃 Any ideas to make this read better?

Copy link
Contributor

Choose a reason for hiding this comment

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

Instead of a tooltip, how about EuiFormRow help text for the selected value?

Copy link
Contributor

Choose a reason for hiding this comment

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

Do either of these work?

Choose Quantitative to map the value to a number in a color range, or Qualitative to map to a category in a color palette.

Choose Quantitative to map by number in a color range, or Qualitative to categorize by color palette.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

thx @gchaps !

to avoid some clutter in the UX (which is getting really long), we were thinking if we could avoid the help-text if the select-box uses "As number" and "As category" for the options. It'd still using your help-text in the aria-label.

image

Copy link
Contributor

Choose a reason for hiding this comment

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

That wording is much clearer than using quantitative and qualitative.

But should it be "By number" and "By category"?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@gchaps maybe (?) :) not super familiar with the nuances here.

It's basically to express the difference in how the value should be interpreted. Should the value be interpreted as a number on a range, then we can map it to a continuous color-range. Should the value be interpreted as a category, then we just assign a color-value.

I can make this change to 'by', but 'as' seems to fit a little better by what it's trying to express.

Copy link
Contributor

Choose a reason for hiding this comment

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

Got it. 'as' is the better choice.

}
)}
>
<EuiSelect
options={options}
value={selectedValue}
onChange={this.props.onColorMapTypeChange}
aria-label={i18n.translate(
'xpack.maps.styles.dynamicColorSelect.qualitativeOrQuantitativeAriaLabel',
{
defaultMessage: 'Select to style by gradient or color palette',
}
)}
compressed
/>
</EuiToolTip>
);
}

_onColorMapSelect = selectedValue => {
const useCustomColorMap = selectedValue === CUSTOM_COLOR_MAP;
this.props.onChange({
Expand Down Expand Up @@ -57,30 +112,24 @@ export class ColorMapSelect extends Component {

if (this.props.colorMapType === COLOR_MAP_TYPE.ORDINAL) {
return (
<Fragment>
<EuiSpacer size="s" />
<ColorStopsOrdinal
colorStops={this.state.customColorMap}
onChange={this._onCustomColorMapChange}
/>
</Fragment>
<ColorStopsOrdinal
colorStops={this.state.customColorMap}
onChange={this._onCustomColorMapChange}
/>
);
}

return (
<Fragment>
<EuiSpacer size="s" />
<ColorStopsCategorical
colorStops={this.state.customColorMap}
field={this.props.styleProperty.getField()}
getValueSuggestions={this.props.styleProperty.getValueSuggestions}
onChange={this._onCustomColorMapChange}
/>
</Fragment>
<ColorStopsCategorical
colorStops={this.state.customColorMap}
field={this.props.styleProperty.getField()}
getValueSuggestions={this.props.styleProperty.getValueSuggestions}
onChange={this._onCustomColorMapChange}
/>
);
}

render() {
_renderColorMapSelections() {
const colorMapOptionsWithCustom = [
{
value: CUSTOM_COLOR_MAP,
Expand All @@ -98,15 +147,39 @@ export class ColorMapSelect extends Component {
: '';
}

let toggle;
if (this.props.showColorMapTypeToggle) {
toggle = <EuiFlexItem grow={false}>{this._renderColorMapToggle()}</EuiFlexItem>;
} else {
toggle = <Fragment />;
thomasneirynck marked this conversation as resolved.
Show resolved Hide resolved
}

return (
<EuiFlexGroup gutterSize={'none'}>
{toggle}
<EuiFlexItem>
<EuiSuperSelect
compressed
options={colorMapOptionsWithCustom}
onChange={this._onColorMapSelect}
valueOfSelected={valueOfSelected}
hasDividers={true}
/>
</EuiFlexItem>
</EuiFlexGroup>
);
}

render() {
return (
<Fragment>
<EuiSuperSelect
options={colorMapOptionsWithCustom}
onChange={this._onColorMapSelect}
valueOfSelected={valueOfSelected}
hasDividers={true}
/>
{this._renderColorStopsInput()}
{this._renderColorMapSelections()}

<EuiSpacer size="s" />

<EuiFlexGroup>
<EuiFlexItem>{this._renderColorStopsInput()}</EuiFlexItem>
thomasneirynck marked this conversation as resolved.
Show resolved Hide resolved
</EuiFlexGroup>
</Fragment>
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ import { CATEGORICAL_DATA_TYPES, COLOR_MAP_TYPE } from '../../../../../../common
import { COLOR_GRADIENTS, COLOR_PALETTES } from '../../../color_utils';
import { i18n } from '@kbn/i18n';

function getDefaultColorMapType(fieldType) {
return CATEGORICAL_DATA_TYPES.includes(fieldType)
? COLOR_MAP_TYPE.CATEGORICAL
: COLOR_MAP_TYPE.ORDINAL;
}

export function DynamicColorForm({
fields,
onDynamicStyleChange,
Expand Down Expand Up @@ -40,21 +46,42 @@ export function DynamicColorForm({
};

const onFieldChange = async ({ field }) => {
const { name, origin, type } = field;
const { name, origin, type: fieldType } = field;
const defaultColorMapType = getDefaultColorMapType(fieldType);
onDynamicStyleChange(styleProperty.getStyleName(), {
...styleOptions,
field: { name, origin },
type: CATEGORICAL_DATA_TYPES.includes(type)
? COLOR_MAP_TYPE.CATEGORICAL
: COLOR_MAP_TYPE.ORDINAL,
type: defaultColorMapType,
thomasneirynck marked this conversation as resolved.
Show resolved Hide resolved
});
};

const onColorMapTypeChange = async e => {
const colorMapType = e.target.value;
onDynamicStyleChange(styleProperty.getStyleName(), {
...styleOptions,
type: colorMapType,
});
};

const getField = () => {
const fieldName = styleProperty.getFieldName();
if (!fieldName) {
return null;
}

return fields.find(field => {
return field.name === fieldName;
});
};

const renderColorMapSelect = () => {
if (!styleOptions.field || !styleOptions.field.name) {
const field = getField();
if (!field) {
return null;
}

const showColorMapTypeToggle = !CATEGORICAL_DATA_TYPES.includes(field.type);

if (styleProperty.isOrdinal()) {
return (
<ColorMapSelect
Expand All @@ -63,29 +90,33 @@ export function DynamicColorForm({
defaultMessage: 'Custom color ramp',
})}
onChange={onColorMapSelect}
onColorMapTypeChange={onColorMapTypeChange}
colorMapType={COLOR_MAP_TYPE.ORDINAL}
color={styleOptions.color}
customColorMap={styleOptions.customColorRamp}
useCustomColorMap={_.get(styleOptions, 'useCustomColorRamp', false)}
styleProperty={styleProperty}
showColorMapTypeToggle={showColorMapTypeToggle}
/>
);
} else if (styleProperty.isCategorical()) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Why re-nest this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

these are equally weighted code-paths, and helps readability at same indentations. early returns work better for guard statements on top of the function.

return (
<ColorMapSelect
colorMapOptions={COLOR_PALETTES}
customOptionLabel={i18n.translate('xpack.maps.style.customColorPaletteLabel', {
defaultMessage: 'Custom color palette',
})}
onColorMapTypeChange={onColorMapTypeChange}
onChange={onColorMapSelect}
colorMapType={COLOR_MAP_TYPE.CATEGORICAL}
color={styleOptions.colorCategory}
customColorMap={styleOptions.customColorPalette}
useCustomColorMap={_.get(styleOptions, 'useCustomColorPalette', false)}
styleProperty={styleProperty}
showColorMapTypeToggle={showColorMapTypeToggle}
/>
);
}

return (
<ColorMapSelect
colorMapOptions={COLOR_PALETTES}
customOptionLabel={i18n.translate('xpack.maps.style.customColorPaletteLabel', {
defaultMessage: 'Custom color palette',
})}
onChange={onColorMapSelect}
colorMapType={COLOR_MAP_TYPE.CATEGORICAL}
color={styleOptions.colorCategory}
customColorMap={styleOptions.customColorPalette}
useCustomColorMap={_.get(styleOptions, 'useCustomColorPalette', false)}
styleProperty={styleProperty}
/>
);
};

return (
Expand Down
Loading