Skip to content

Kepler.gl is a powerful open source geospatial analysis tool for large-scale data sets.

[fix] Custom color scale fixes (#2770)

Igor Dykhta1689ed68ad72
diff --git a/src/components/package.json b/src/components/package.json
index dbbde05d2..d49b469b4 100644
--- a/src/components/package.json
+++ b/src/components/package.json
@@ -19,7 +19,7 @@
"url": "https://github.com/keplergl/kepler.gl.git"
},
"scripts": {
- "build": "rm -fr dist && babel src --out-dir dist --source-maps inline --extensions '.ts,.tsx,.js,.jsx' --ignore '**/*.d.ts'",
+ "build": "rm -fr dist && ../../node_modules/.bin/babel src --out-dir dist --source-maps inline --extensions '.ts,.tsx,.js,.jsx' --ignore '**/*.d.ts'",
"build:umd": "NODE_OPTIONS=--openssl-legacy-provider webpack --config ./webpack/umd.js --progress --env.prod",
"build:types": "tsc --project ./tsconfig.production.json",
"prepublish": "babel-node ../../scripts/license-header/bin --license ../../FILE-HEADER && yarn build && yarn build:types",
diff --git a/src/components/src/index.ts b/src/components/src/index.ts
index 19b1681ab..3c347295f 100644
--- a/src/components/src/index.ts
+++ b/src/components/src/index.ts
@@ -70,6 +70,7 @@ export {default as PanelHeaderActionFactory} from './side-panel/panel-header-act
export {default as PanelTabFactory} from './side-panel/panel-tab';
export {default as PanelToggleFactory} from './side-panel/panel-toggle';
export {CollapseButtonFactory, default as SidebarFactory} from './side-panel/side-bar';
+export {default as ColorScaleSelectorFactory} from './side-panel/layer-panel/color-scale-selector';
export {LayerBlendingSelector, default as LayerManagerFactory} from './side-panel/layer-manager';
export {default as ColorPalette} from './side-panel/layer-panel/color-palette';
diff --git a/src/components/src/side-panel/layer-panel/custom-palette.tsx b/src/components/src/side-panel/layer-panel/custom-palette.tsx
index 2d7882e1b..8c5f1bf96 100644
--- a/src/components/src/side-panel/layer-panel/custom-palette.tsx
+++ b/src/components/src/side-panel/layer-panel/custom-palette.tsx
@@ -295,8 +295,8 @@ export const EditableColorRange: React.FC<EditableColorRangeProps> = ({
editColorMap,
editable
}) => {
- const noMinBound = item.inputs[0] === null && index === 0;
- const noMaxBound = item.inputs[1] === null && isLast;
+ const noMinBound = !Number.isFinite(item.inputs[0]) && index === 0;
+ const noMaxBound = !Number.isFinite(item.inputs[1]) && isLast;
const onChangeLeft = useCallback(
val => {
if (editable && editColorMap) editColorMap(parseFloat(val), index - 1);
@@ -433,10 +433,9 @@ function CustomPaletteFactory(): React.FC<CustomPaletteProps> {
}) => {
const [isSorting, setIsSorting] = useState(false);
const {colors, colorMap} = customPalette;
- const colorBreaks = useMemo(
- () => (colorMap ? colorMapToColorBreaks(colorMap) : null),
- [colorMap]
- );
+ const colorBreaks = useMemo(() => (colorMap ? colorMapToColorBreaks(colorMap) : null), [
+ colorMap
+ ]);
const onPickerUpdate = useCallback(
color => {
diff --git a/src/layers/src/geojson-layer/geojson-layer.ts b/src/layers/src/geojson-layer/geojson-layer.ts
index cec164791..b38ee500f 100644
--- a/src/layers/src/geojson-layer/geojson-layer.ts
+++ b/src/layers/src/geojson-layer/geojson-layer.ts
@@ -38,7 +38,8 @@ import {
HIGHLIGH_COLOR_3D,
CHANNEL_SCALES,
ColorRange,
- LAYER_VIS_CONFIGS
+ LAYER_VIS_CONFIGS,
+ DEFAULT_COLOR_UI
} from '@kepler.gl/constants';
import {
VisConfigNumber,
@@ -399,8 +400,10 @@ export default class GeoJsonLayer extends Layer {
}
getDefaultLayerConfig(props: LayerBaseConfigPartial) {
+ const defaultLayerConfig = super.getDefaultLayerConfig(props ?? {});
return {
- ...super.getDefaultLayerConfig(props),
+ ...defaultLayerConfig,
+
columnMode: props?.columnMode ?? DEFAULT_COLUMN_MODE,
// add height visual channel
heightField: null,
@@ -415,7 +418,11 @@ export default class GeoJsonLayer extends Layer {
// add stroke color visual channel
strokeColorField: null,
strokeColorDomain: [0, 1],
- strokeColorScale: 'quantile'
+ strokeColorScale: 'quantile',
+ colorUI: {
+ ...defaultLayerConfig.colorUI,
+ strokeColorRange: DEFAULT_COLOR_UI
+ }
};
}
diff --git a/src/layers/src/point-layer/point-layer.ts b/src/layers/src/point-layer/point-layer.ts
index b03a5a645..f593cdc14 100644
--- a/src/layers/src/point-layer/point-layer.ts
+++ b/src/layers/src/point-layer/point-layer.ts
@@ -28,9 +28,9 @@ import {
LAYER_VIS_CONFIGS,
DEFAULT_LAYER_COLOR,
CHANNEL_SCALES,
- ColorRange
+ ColorRange,
+ DEFAULT_COLOR_UI
} from '@kepler.gl/constants';
-
import {getTextOffsetByRadius, formatTextLabelData} from '../layer-text-label';
import {
assignPointPairToLayerColumn,
@@ -362,14 +362,20 @@ export default class PointLayer extends Layer {
}
getDefaultLayerConfig(props: LayerBaseConfigPartial) {
+ const defaultLayerConfig = super.getDefaultLayerConfig(props ?? {});
return {
- ...super.getDefaultLayerConfig(props),
+ ...defaultLayerConfig,
+
columnMode: props?.columnMode ?? DEFAULT_COLUMN_MODE,
// add stroke color visual channel
strokeColorField: null,
strokeColorDomain: [0, 1],
- strokeColorScale: 'quantile'
+ strokeColorScale: 'quantile',
+ colorUI: {
+ ...defaultLayerConfig.colorUI,
+ strokeColorRange: DEFAULT_COLOR_UI
+ }
};
}
diff --git a/src/layers/src/s2-geometry-layer/s2-geometry-layer.ts b/src/layers/src/s2-geometry-layer/s2-geometry-layer.ts
index cfb0c17a2..cffe84184 100644
--- a/src/layers/src/s2-geometry-layer/s2-geometry-layer.ts
+++ b/src/layers/src/s2-geometry-layer/s2-geometry-layer.ts
@@ -6,7 +6,8 @@ import {
HIGHLIGH_COLOR_3D,
CHANNEL_SCALES,
ColorRange,
- LAYER_VIS_CONFIGS
+ LAYER_VIS_CONFIGS,
+ DEFAULT_COLOR_UI
} from '@kepler.gl/constants';
import {default as KeplerTable} from '@kepler.gl/table';
import Layer, {
@@ -224,8 +225,9 @@ export default class S2GeometryLayer extends Layer {
}
getDefaultLayerConfig(props: LayerBaseConfigPartial) {
+ const defaultLayerConfig = super.getDefaultLayerConfig(props ?? {});
return {
- ...super.getDefaultLayerConfig(props),
+ ...defaultLayerConfig,
// add height visual channel
heightField: null,
@@ -235,7 +237,11 @@ export default class S2GeometryLayer extends Layer {
// add stroke color visual channel
strokeColorField: null,
strokeColorDomain: [0, 1],
- strokeColorScale: 'quantile'
+ strokeColorScale: 'quantile',
+ colorUI: {
+ ...defaultLayerConfig.colorUI,
+ strokeColorRange: DEFAULT_COLOR_UI
+ }
};
}
diff --git a/src/utils/src/color-utils.ts b/src/utils/src/color-utils.ts
index f0c73d6ca..764f3644a 100644
--- a/src/utils/src/color-utils.ts
+++ b/src/utils/src/color-utils.ts
@@ -262,7 +262,7 @@ export function removeCustomPaletteColor(customPalette: ColorRange, index: numbe
if (Array.isArray(colorMap)) {
// find colorMap index
const colorMapIndex = colorMap.findIndex(cm => cm[1] === oldValue);
- if (colorMapIndex > 0) {
+ if (colorMapIndex >= 0) {
update.colorMap = [...colorMap];
update.colorMap.splice(colorMapIndex, 1);
}
diff --git a/src/utils/src/data-scale-utils.ts b/src/utils/src/data-scale-utils.ts
index 2187b48f3..eb4ef80ad 100644
--- a/src/utils/src/data-scale-utils.ts
+++ b/src/utils/src/data-scale-utils.ts
@@ -196,6 +196,7 @@ function getScaleLabels(
});
}
+const customScaleLabelFormat = d => String(d);
/**
* Get linear / quant scale color breaks
*/
@@ -205,7 +206,7 @@ export function getQuantLegends(scale: D3ScaleFunction, labelFormat: LabelFormat
}
const labels =
scale.scaleType === 'threshold' || scale.scaleType === 'custom'
- ? getThresholdLabels(scale, labelFormat)
+ ? getThresholdLabels(scale, customScaleLabelFormat)
: getScaleLabels(scale, labelFormat);
const data = scale.range();
@@ -337,9 +338,16 @@ export function getVisualChannelScaleByZoom({
* Convert color breaks UI input into colorRange.colorMap
*/
export function colorBreaksToColorMap(colorBreaks: ColorBreak[] | ColorBreakOrdinal[]): ColorMap {
- const colorMap = colorBreaks.map(colorBreak => {
+ const colorMap = colorBreaks.map((colorBreak, i) => {
// [value, hex]
- return [colorBreak.inputs ? colorBreak.inputs[1] : colorBreak.label, colorBreak.data];
+ return [
+ colorBreak.inputs
+ ? i === colorBreaks.length - 1
+ ? null // last
+ : colorBreak.inputs[1]
+ : colorBreak.label,
+ colorBreak.data
+ ];
});
// @ts-ignore tuple
@@ -348,20 +356,21 @@ export function colorBreaksToColorMap(colorBreaks: ColorBreak[] | ColorBreakOrdi
/**
* Convert colorRange.colorMap into color breaks UI input
- * @type {typeof import('./data-scale-utils').colorMapToColorBreaks}
*/
-export function colorMapToColorBreaks(colorMap?: ColorMap, domain?: number[]): ColorBreak[] | null {
+export function colorMapToColorBreaks(colorMap?: ColorMap): ColorBreak[] | null {
if (!colorMap) {
return null;
}
const colorBreaks = colorMap.map(([value, color], i) => {
const range =
i === 0
- ? [domain ? domain[0] : null, value]
+ ? // first
+ [-Infinity, value]
: // last
i === colorMap.length - 1
- ? [colorMap[i - 1][0], domain ? domain[1] : null]
- : [colorMap[i - 1][0], value];
+ ? [colorMap[i - 1][0], Infinity]
+ : // else
+ [colorMap[i - 1][0], value];
return {
data: color,
range,
diff --git a/test/browser/components/side-panel/channel-by-value-selctor-test.js b/test/browser/components/side-panel/channel-by-value-selctor-test.js
index d8532745f..c3472ce12 100644
--- a/test/browser/components/side-panel/channel-by-value-selctor-test.js
+++ b/test/browser/components/side-panel/channel-by-value-selctor-test.js
@@ -1,400 +1,400 @@
// // SPDX-License-Identifier: MIT
// // Copyright contributors to the kepler.gl project
-// /* eslint-disable max-statements */
-
-// import {layerColorUIChange, layerVisualChannelConfigChange} from '@kepler.gl/actions';
-// import {keplerGlReducerCore as coreReducer} from '@kepler.gl/reducers';
-// import cloneDeep from 'lodash.clonedeep';
-// import React from 'react';
-// import sinon from 'sinon';
-// import test from 'tape';
-// import {IntlWrapper, mountWithTheme} from 'test/helpers/component-utils';
-
-// import {
-// DropdownSelect,
-// ColorBreaksPanelFactory,
-// ColorScaleSelectorFactory,
-// DimensionScaleSelectorFactory,
-// appInjector,
-// ChannelByValueSelectorFactory,
-// FieldSelectorFactory,
-// DropdownList,
-// ListItem,
-// Typeahead,
-// Button,
-// ColorPaletteItem,
-// ColorSwatch,
-// EditableColorRange
-// } from '@kepler.gl/components';
-
-// import {StateWFilesFiltersLayerColor} from 'test/helpers/mock-state';
-
-// const ChannelByValueSelector = appInjector.get(ChannelByValueSelectorFactory);
-// const DimensionScaleSelector = appInjector.get(DimensionScaleSelectorFactory);
-// const ColorScaleSelector = appInjector.get(ColorScaleSelectorFactory);
-// const FieldSelector = appInjector.get(FieldSelectorFactory);
-// const ColorBreaksPanel = appInjector.get(ColorBreaksPanelFactory);
-// const CustomPalette = appInjector.get(CustomPaletteFactory);
-
-// function nop() {
-// return;
-// }
-
-// const ExpectedCustomPalette = {
-// customPalette: {
-// name: 'Uber Viz Sequential 2',
-// type: 'sequential',
-// category: 'Uber',
-// colors: ['#E6FAFA', '#AAD7DA', '#68B4BB', '#00939C'],
-// colorMap: [
-// [3032, '#E6FAFA'],
-// [6063, '#AAD7DA'],
-// [9093, '#68B4BB'],
-// [12120, '#00939C']
-// ]
-// },
-// showSketcher: false,
-// showDropdown: false,
-// showColorChart: true,
-// colorRangeConfig: {
-// type: 'all',
-// steps: 6,
-// reversed: false,
-// custom: false,
-// customBreaks: true
-// }
-// };
-
-// // const updateLayerVisualChannelConfig = sinon.spy();
-// // const updateLayerColorUI = sinon.spy();
-// test('Components -> ChannelByValueSelector -> ColorScaleSelector -> disabled', t => {
-// const InitialState = cloneDeep(StateWFilesFiltersLayerColor);
-// const {layers, datasets} = InitialState.visState;
-// const pointLayer = layers[0];
-// const updateLayerVisualChannelConfig = sinon.spy();
-
-// let wrapper;
-// t.doesNotThrow(() => {
-// wrapper = mountWithTheme(
-// <IntlWrapper>
-// <ChannelByValueSelector
-// layer={pointLayer}
-// channel={pointLayer.visualChannels.color}
-// onChange={updateLayerVisualChannelConfig}
-// fields={datasets[pointLayer.config.dataId].fields}
-// setColorUI={nop}
-// />
-// </IntlWrapper>
-// );
-// }, 'Should not fail rendering point layer');
-
-// // colorField: string
-// t.equal(wrapper.find(DimensionScaleSelector).length, 1, 'Should render 1 DimensionScaleSelector');
-// t.equal(wrapper.find(ColorScaleSelector).length, 1, 'Should render 1 ColorScaleSelector');
-// t.equal(
-// wrapper
-// .find(ColorScaleSelector)
-// .at(0)
-// .props().disabled,
-// true,
-// 'Should disabled color scale select if only 1 option'
-// );
-
-// t.equal(
-// wrapper
-// .find(ColorScaleSelector)
-// .at(0)
-// .find(DropdownSelect).length,
-// 1,
-// 'should render 1 DropdownSelect in ColorScaleSelector'
-// );
-// t.equal(
-// wrapper
-// .find(ColorScaleSelector)
-// .at(0)
-// .find(DropdownSelect)
-// .at(0)
-// .find(ListItem)
-// .at(0)
-// .find('span')
-// .text(),
-// 'Ordinal',
-// 'should render correct scale value'
-// );
-
-// // change color field
-// const fieldSelector = wrapper.find(FieldSelector).at(0);
-// fieldSelector
-// .find('.item-selector__dropdown')
-// .at(0)
-// .simulate('click');
-
-// // dropdownSelect.simulate('click');
-
-// t.equal(
-// wrapper
-// .find(FieldSelector)
-// .at(0)
-// .find(Typeahead).length,
-// 1,
-// 'should render Typeahead'
-// );
-// t.equal(
-// wrapper
-// .find(FieldSelector)
-// .at(0)
-// .find(DropdownList).length,
-// 1,
-// 'should render 1 DropdownList'
-// );
-// t.equal(
-// wrapper
-// .find(FieldSelector)
-// .at(0)
-// .find('.list__item').length,
-// 10,
-// 'should render 10 ListItem'
-// );
-
-// t.equal(
-// wrapper
-// .find(FieldSelector)
-// .at(0)
-// .find('.list__item')
-// .at(5)
-// .find('.list__item__anchor')
-// .text(),
-// 'id',
-// 'Should render id field at index:5'
-// );
-// // select id field
-// wrapper
-// .find(FieldSelector)
-// .at(0)
-// .find('.list__item')
-// .at(5)
-// .simulate('click');
-
-// t.ok(updateLayerVisualChannelConfig.calledOnce, 'Should call updateLayerVisualChannelConfig');
-
-// t.deepEqual(
-// updateLayerVisualChannelConfig.args[0],
-// [{colorField: datasets[pointLayer.config.dataId].fields[6]}, 'color'],
-// 'should pass color field'
-// );
-
-// // set point layer color field to integar id
-// const updatedState = coreReducer(
-// InitialState,
-// layerVisualChannelConfigChange(pointLayer, ...updateLayerVisualChannelConfig.args[0])
-// );
-
-// t.equal(
-// updatedState.visState.layers[0].config.colorField.name,
-// 'id',
-// 'should set color field to id'
-// );
-
-// t.end();
-// });
-
-// test('Components -> ChannelByValueSelector -> ColorScaleSelector -> ColorBreakDisplay', t => {
-// const InitialState = cloneDeep(StateWFilesFiltersLayerColor);
-// const {layers, datasets} = InitialState.visState;
-// const pointLayer = layers[0];
-// const updateLayerVisualChannelConfig = sinon.spy();
-// const setColorUI = sinon.spy();
-
-// const updatedState = coreReducer(
-// InitialState,
-// layerVisualChannelConfigChange(
-// pointLayer,
-// {colorField: datasets[pointLayer.config.dataId].fields[6]},
-// 'color'
-// )
-// );
-
-// const pointLayer1 = updatedState.visState.layers[0];
-// t.equal(pointLayer1.config.colorField.name, 'id', 'should set color field to id');
-// const InitialProps = {
-// layer: pointLayer1,
-// channel: pointLayer1.visualChannels.color,
-// onChange: updateLayerVisualChannelConfig,
-// fields: updatedState.visState.datasets[pointLayer1.config.dataId].fields,
-// setColorUI
-// };
-// let wrapper;
-// t.doesNotThrow(() => {
-// wrapper = mountWithTheme(
-// <IntlWrapper>
-// <ChannelByValueSelector {...InitialProps} />
-// </IntlWrapper>
-// );
-// }, 'Should not fail rendering point layer');
-
-// // colorField: string
-// t.equal(wrapper.find(DimensionScaleSelector).length, 1, 'Should render 1 DimensionScaleSelector');
-// t.equal(wrapper.find(ColorScaleSelector).length, 1, 'Should render 1 ColorScaleSelector');
-// t.equal(
-// wrapper
-// .find(ColorScaleSelector)
-// .at(0)
-// .props().disabled,
-// false,
-// 'Should not disabled color scale select'
-// );
-
-// // scale optons
-// t.equal(
-// wrapper
-// .find(ColorScaleSelector)
-// .at(0)
-// .find('.list__item').length,
-// 3,
-// 'should render 3 scale options'
-// );
-
-// t.equal(wrapper.find(ColorBreaksPanel).length, 1, 'Should render 1 ColorBreaksPanel');
-// t.equal(wrapper.find(ColorPaletteItem).length, 4, 'Should render 4 ColorPaletteItem');
-
-// const expectedText = [
-// ['#E6FAFA', '1', '3032'],
-// ['#AAD7DA', '3032', '6063'],
-// ['#68B4BB', '6063', '9093'],
-// ['#00939C', '9093', '12120']
-// ];
-
-// for (let i = 0; i < 4; i++) {
-// const inputTexts = wrapper
-// .find(ColorPaletteItem)
-// .at(i)
-// .find('.custom-palette-hex__input__text');
-
-// t.equal(
-// wrapper
-// .find(ColorPaletteItem)
-// .at(i)
-// .find(ColorSwatch)
-// .props().color,
-// expectedText[i][0],
-// 'should display correct color'
-// );
-// t.equal(inputTexts.at(0).text(), expectedText[i][1], 'should display correct range');
-// t.equal(inputTexts.at(1).text(), expectedText[i][2], 'should display correct range');
-// }
-
-// // select custom scale
-// wrapper
-// .find(ColorScaleSelector)
-// .at(0)
-// .find('.list__item')
-// .at(2)
-// .simulate('click');
-
-// t.ok(setColorUI.calledOnce, 'should call setColorUI');
-// const expectedArgs = [
-// 'colorRange',
-// {
-// showColorChart: true,
-// colorRangeConfig: {
-// customBreaks: true
-// }
-// }
-// ];
-// t.deepEqual(setColorUI.args[0], expectedArgs, 'should set customBreaks to true');
-
-// const updatedState1 = coreReducer(updatedState, layerColorUIChange(pointLayer1, ...expectedArgs));
-
-// t.deepEqual(
-// updatedState1.visState.layers[0].config.colorUI.colorRange,
-// ExpectedCustomPalette,
-// 'Should set customPalette and colorMap'
-// );
-
-// wrapper.setProps({
-// children: <ChannelByValueSelector {...InitialProps} layer={updatedState1.visState.layers[0]} />
-// });
-
-// t.equal(wrapper.find(CustomPalette).length, 1, 'should render 1 CustomPalette');
-// t.equal(wrapper.find(EditableColorRange).length, 4, 'Should render 4 EditableColorRange');
-// t.equal(wrapper.find(ColorSwatch).length, 4, 'Should render 4 ColorSwatch');
-
-// for (let i = 0; i < 4; i++) {
-// const inputs = wrapper
-// .find(EditableColorRange)
-// .at(i)
-// .find('input');
-// if (i === 0 || i === 3) {
-// t.equal(inputs.length, 1, 'should render 1 input');
-// t.equal(
-// inputs.at(0).props().value,
-// Number(expectedText[i][i === 0 ? 2 : 1]),
-// 'should display correct range'
-// );
-// } else {
-// t.equal(inputs.length, 2, 'should render 2 inputs');
-
-// t.equal(
-// inputs.at(0).props().value,
-// Number(expectedText[i][1]),
-// 'should display correct range'
-// );
-// t.equal(
-// inputs.at(1).props().value,
-// Number(expectedText[i][2]),
-// 'should display correct range'
-// );
-// }
-// }
-
-// wrapper
-// .find(EditableColorRange)
-// .at(1)
-// .find('input')
-// .at(1)
-// .simulate('change', {target: {value: '5000'}});
-// t.notOk(setColorUI.calledTwice, 'should not call setColorUI');
-
-// wrapper
-// .find(EditableColorRange)
-// .at(1)
-// .find('input')
-// .at(1)
-// .simulate('blur');
-// t.ok(setColorUI.calledTwice, 'should call setColorUI');
-// const expectedCustomPalette1 = {
-// ...ExpectedCustomPalette.customPalette,
-// colorMap: [
-// [3032, '#E6FAFA'],
-// [5000, '#AAD7DA'],
-// [9093, '#68B4BB'],
-// [12120, '#00939C']
-// ]
-// };
-
-// const expectedArgs2 = [
-// 'colorRange',
-// {
-// customPalette: expectedCustomPalette1
-// }
-// ];
-// t.deepEqual(setColorUI.args[1], expectedArgs2, 'should set customBreaks.colorMap');
-
-// // on confirm
-// t.equal(wrapper.find(CustomPalette).find(Button).length, 2, 'should render 2 buttons');
-// wrapper
-// .find(CustomPalette)
-// .find(Button)
-// .at(0)
-// .simulate('click');
-
-// t.ok(updateLayerVisualChannelConfig.calledOnce, 'should call updateLayerVisualChannelConfig');
-
-// t.deepEqual(
-// updateLayerVisualChannelConfig.args[0],
-// [{colorScale: 'custom'}, 'color', {colorRange: ExpectedCustomPalette.customPalette}],
-// 'should pass custom scale and custom colorRange'
-// );
-// t.end();
-// });
+/* eslint-disable max-statements */
+
+import {layerColorUIChange, layerVisualChannelConfigChange} from '@kepler.gl/actions';
+import {keplerGlReducerCore as coreReducer} from '@kepler.gl/reducers';
+import cloneDeep from 'lodash.clonedeep';
+import React from 'react';
+import sinon from 'sinon';
+import test from 'tape';
+import {IntlWrapper, mountWithTheme} from 'test/helpers/component-utils';
+
+import {
+ DropdownSelect,
+ ColorBreaksPanelFactory,
+ ColorScaleSelectorFactory,
+ CustomPaletteFactory,
+ DimensionScaleSelectorFactory,
+ appInjector,
+ ChannelByValueSelectorFactory,
+ FieldSelectorFactory,
+ DropdownList,
+ ListItem,
+ Typeahead,
+ Button,
+ ColorPaletteItem,
+ ColorSwatch,
+ EditableColorRange
+} from '@kepler.gl/components';
+
+import {StateWFilesFiltersLayerColor} from 'test/helpers/mock-state';
+
+const ChannelByValueSelector = appInjector.get(ChannelByValueSelectorFactory);
+const DimensionScaleSelector = appInjector.get(DimensionScaleSelectorFactory);
+const ColorScaleSelector = appInjector.get(ColorScaleSelectorFactory);
+const FieldSelector = appInjector.get(FieldSelectorFactory);
+const ColorBreaksPanel = appInjector.get(ColorBreaksPanelFactory);
+const CustomPalette = appInjector.get(CustomPaletteFactory);
+
+function nop() {
+ return;
+}
+
+const ExpectedCustomPalette = {
+ customPalette: {
+ name: 'Uber Viz Sequential 2',
+ type: 'sequential',
+ category: 'Uber',
+ colors: ['#E6FAFA', '#AAD7DA', '#68B4BB', '#00939C'],
+ colorMap: [
+ [3032, '#E6FAFA'],
+ [6063, '#AAD7DA'],
+ [9093, '#68B4BB'],
+ [null, '#00939C']
+ ]
+ },
+ showSketcher: false,
+ showDropdown: false,
+ showColorChart: true,
+ colorRangeConfig: {
+ type: 'all',
+ steps: 6,
+ reversed: false,
+ custom: false,
+ customBreaks: true
+ }
+};
+
+// const updateLayerVisualChannelConfig = sinon.spy();
+// const updateLayerColorUI = sinon.spy();
+test('Components -> ChannelByValueSelector -> ColorScaleSelector -> disabled', t => {
+ const InitialState = cloneDeep(StateWFilesFiltersLayerColor);
+ const {layers, datasets} = InitialState.visState;
+ const pointLayer = layers[0];
+ const updateLayerVisualChannelConfig = sinon.spy();
+
+ let wrapper;
+ t.doesNotThrow(() => {
+ wrapper = mountWithTheme(
+ <IntlWrapper>
+ <ChannelByValueSelector
+ layer={pointLayer}
+ channel={pointLayer.visualChannels.color}
+ onChange={updateLayerVisualChannelConfig}
+ fields={datasets[pointLayer.config.dataId].fields}
+ setColorUI={nop}
+ />
+ </IntlWrapper>
+ );
+ }, 'Should not fail rendering point layer');
+
+ // colorField: string
+ t.equal(wrapper.find(DimensionScaleSelector).length, 1, 'Should render 1 DimensionScaleSelector');
+ t.equal(wrapper.find(ColorScaleSelector).length, 1, 'Should render 1 ColorScaleSelector');
+ t.equal(
+ wrapper
+ .find(ColorScaleSelector)
+ .at(0)
+ .props().disabled,
+ true,
+ 'Should disabled color scale select if only 1 option'
+ );
+
+ t.equal(
+ wrapper
+ .find(ColorScaleSelector)
+ .at(0)
+ .find(DropdownSelect).length,
+ 1,
+ 'should render 1 DropdownSelect in ColorScaleSelector'
+ );
+ t.equal(
+ wrapper
+ .find(ColorScaleSelector)
+ .at(0)
+ .find(DropdownSelect)
+ .at(0)
+ .find(ListItem)
+ .at(0)
+ .find('span')
+ .text(),
+ 'Ordinal',
+ 'should render correct scale value'
+ );
+
+ // change color field
+ const fieldSelector = wrapper.find(FieldSelector).at(0);
+ fieldSelector
+ .find('.item-selector__dropdown')
+ .at(0)
+ .simulate('click');
+
+ // dropdownSelect.simulate('click');
+
+ t.equal(
+ wrapper
+ .find(FieldSelector)
+ .at(0)
+ .find(Typeahead).length,
+ 1,
+ 'should render Typeahead'
+ );
+ t.equal(
+ wrapper
+ .find(FieldSelector)
+ .at(0)
+ .find(DropdownList).length,
+ 1,
+ 'should render 1 DropdownList'
+ );
+ t.equal(
+ wrapper
+ .find(FieldSelector)
+ .at(0)
+ .find('.list__item').length,
+ 10,
+ 'should render 10 ListItem'
+ );
+
+ t.equal(
+ wrapper
+ .find(FieldSelector)
+ .at(0)
+ .find('.list__item')
+ .at(5)
+ .find('.list__item__anchor')
+ .text(),
+ 'uid',
+ 'Should render id field at index:5'
+ );
+ // select id field
+ wrapper
+ .find(FieldSelector)
+ .at(0)
+ .find('.list__item')
+ .at(5)
+ .simulate('click');
+
+ t.ok(updateLayerVisualChannelConfig.calledOnce, 'Should call updateLayerVisualChannelConfig');
+
+ t.deepEqual(
+ updateLayerVisualChannelConfig.args[0],
+ [{colorField: datasets[pointLayer.config.dataId].fields[6]}, 'color'],
+ 'should pass color field'
+ );
+
+ // set point layer color field to integar id
+ const updatedState = coreReducer(
+ InitialState,
+ layerVisualChannelConfigChange(pointLayer, ...updateLayerVisualChannelConfig.args[0])
+ );
+
+ t.equal(
+ updatedState.visState.layers[0].config.colorField.name,
+ 'uid',
+ 'should set color field to id'
+ );
+
+ t.end();
+});
+
+test.only('Components -> ChannelByValueSelector -> ColorScaleSelector -> ColorBreakDisplay', t => {
+ const InitialState = cloneDeep(StateWFilesFiltersLayerColor);
+ const {layers, datasets} = InitialState.visState;
+ const pointLayer = layers[0];
+ const updateLayerVisualChannelConfig = sinon.spy();
+ const setColorUI = sinon.spy();
+
+ const updatedState = coreReducer(
+ InitialState,
+ layerVisualChannelConfigChange(
+ pointLayer,
+ {colorField: datasets[pointLayer.config.dataId].fields[6]},
+ 'color'
+ )
+ );
+
+ const pointLayer1 = updatedState.visState.layers[0];
+ t.equal(pointLayer1.config.colorField.name, 'uid', 'should set color field to id');
+ const InitialProps = {
+ layer: pointLayer1,
+ channel: pointLayer1.visualChannels.color,
+ onChange: updateLayerVisualChannelConfig,
+ fields: updatedState.visState.datasets[pointLayer1.config.dataId].fields,
+ setColorUI
+ };
+ let wrapper;
+ t.doesNotThrow(() => {
+ wrapper = mountWithTheme(
+ <IntlWrapper>
+ <ChannelByValueSelector {...InitialProps} />
+ </IntlWrapper>
+ );
+ }, 'Should not fail rendering point layer');
+
+ // colorField: string
+ t.equal(wrapper.find(DimensionScaleSelector).length, 1, 'Should render 1 DimensionScaleSelector');
+ t.equal(wrapper.find(ColorScaleSelector).length, 1, 'Should render 1 ColorScaleSelector');
+ t.equal(
+ wrapper
+ .find(ColorScaleSelector)
+ .at(0)
+ .props().disabled,
+ false,
+ 'Should not disabled color scale select'
+ );
+
+ // scale optons
+ t.equal(
+ wrapper
+ .find(ColorScaleSelector)
+ .at(0)
+ .find('.list__item').length,
+ 3,
+ 'should render 3 scale options'
+ );
+
+ t.equal(wrapper.find(ColorBreaksPanel).length, 1, 'Should render 1 ColorBreaksPanel');
+ t.equal(wrapper.find(ColorPaletteItem).length, 4, 'Should render 4 ColorPaletteItem');
+
+ const expectedText = [
+ ['#E6FAFA', '1', '3032'],
+ ['#AAD7DA', '3032', '6063'],
+ ['#68B4BB', '6063', '9093'],
+ ['#00939C', '9093', '12120']
+ ];
+
+ for (let i = 0; i < 4; i++) {
+ const inputTexts = wrapper
+ .find(ColorPaletteItem)
+ .at(i)
+ .find('div.custom-palette-hex__input__text');
+ t.equal(
+ wrapper
+ .find(ColorPaletteItem)
+ .at(i)
+ .find(ColorSwatch)
+ .props().color,
+ expectedText[i][0],
+ 'should display correct color'
+ );
+ t.equal(inputTexts.at(0).text(), expectedText[i][1], 'should display correct range');
+ t.equal(inputTexts.at(1).text(), expectedText[i][2], 'should display correct range');
+ }
+
+ // select custom scale
+ wrapper
+ .find(ColorScaleSelector)
+ .at(0)
+ .find('.list__item')
+ .at(2)
+ .simulate('click');
+
+ t.ok(setColorUI.calledOnce, 'should call setColorUI');
+ const expectedArgs = [
+ 'colorRange',
+ {
+ showColorChart: true,
+ colorRangeConfig: {
+ customBreaks: true
+ }
+ }
+ ];
+ t.deepEqual(setColorUI.args[0], expectedArgs, 'should set customBreaks to true');
+
+ const updatedState1 = coreReducer(updatedState, layerColorUIChange(pointLayer1, ...expectedArgs));
+
+ t.deepEqual(
+ updatedState1.visState.layers[0].config.colorUI.colorRange,
+ ExpectedCustomPalette,
+ 'Should set customPalette and colorMap'
+ );
+
+ wrapper.setProps({
+ children: <ChannelByValueSelector {...InitialProps} layer={updatedState1.visState.layers[0]} />
+ });
+
+ t.equal(wrapper.find(CustomPalette).length, 1, 'should render 1 CustomPalette');
+ t.equal(wrapper.find(EditableColorRange).length, 4, 'Should render 4 EditableColorRange');
+ t.equal(wrapper.find(ColorSwatch).length, 4, 'Should render 4 ColorSwatch');
+
+ for (let i = 0; i < 4; i++) {
+ const inputs = wrapper
+ .find(EditableColorRange)
+ .at(i)
+ .find('input');
+ if (i === 0 || i === 3) {
+ t.equal(inputs.length, 1, 'should render 1 input');
+ t.equal(
+ inputs.at(0).props().value,
+ Number(expectedText[i][i === 0 ? 2 : 1]),
+ 'should display correct range'
+ );
+ } else {
+ t.equal(inputs.length, 2, 'should render 2 inputs');
+
+ t.equal(
+ inputs.at(0).props().value,
+ Number(expectedText[i][1]),
+ 'should display correct range'
+ );
+ t.equal(
+ inputs.at(1).props().value,
+ Number(expectedText[i][2]),
+ 'should display correct range'
+ );
+ }
+ }
+
+ wrapper
+ .find(EditableColorRange)
+ .at(1)
+ .find('input')
+ .at(1)
+ .simulate('change', {target: {value: '5000'}});
+ t.notOk(setColorUI.calledTwice, 'should not call setColorUI');
+
+ wrapper
+ .find(EditableColorRange)
+ .at(1)
+ .find('input')
+ .at(1)
+ .simulate('blur');
+ t.ok(setColorUI.calledTwice, 'should call setColorUI');
+ const expectedCustomPalette1 = {
+ ...ExpectedCustomPalette.customPalette,
+ colorMap: [
+ [3032, '#E6FAFA'],
+ [5000, '#AAD7DA'],
+ [9093, '#68B4BB'],
+ [null, '#00939C']
+ ]
+ };
+
+ const expectedArgs2 = [
+ 'colorRange',
+ {
+ customPalette: expectedCustomPalette1
+ }
+ ];
+ t.deepEqual(setColorUI.args[1], expectedArgs2, 'should set customBreaks.colorMap');
+
+ // on confirm
+ t.equal(wrapper.find(CustomPalette).find(Button).length, 2, 'should render 2 buttons');
+ wrapper
+ .find(CustomPalette)
+ .find(Button)
+ .at(0)
+ .simulate('click');
+
+ t.ok(updateLayerVisualChannelConfig.calledOnce, 'should call updateLayerVisualChannelConfig');
+
+ t.deepEqual(
+ updateLayerVisualChannelConfig.args[0],
+ [{colorScale: 'custom'}, 'color', {colorRange: ExpectedCustomPalette.customPalette}],
+ 'should pass custom scale and custom colorRange'
+ );
+ t.end();
+});
diff --git a/test/fixtures/state-saved-v0.js b/test/fixtures/state-saved-v0.js
index 130971976..825d477cc 100644
--- a/test/fixtures/state-saved-v0.js
+++ b/test/fixtures/state-saved-v0.js
@@ -1198,7 +1198,8 @@ mergedLayer2.config = {
textLabel: [DEFAULT_TEXT_LABEL],
colorUI: {
color: DEFAULT_COLOR_UI,
- colorRange: DEFAULT_COLOR_UI
+ colorRange: DEFAULT_COLOR_UI,
+ strokeColorRange: DEFAULT_COLOR_UI
},
hidden: false,
visConfig: {
@@ -1349,7 +1350,8 @@ mergedLayer4.config = {
textLabel: [DEFAULT_TEXT_LABEL],
colorUI: {
color: DEFAULT_COLOR_UI,
- colorRange: DEFAULT_COLOR_UI
+ colorRange: DEFAULT_COLOR_UI,
+ strokeColorRange: DEFAULT_COLOR_UI
},
heightField: null,
heightDomain: [0, 1],
diff --git a/test/fixtures/state-saved-v1-1.js b/test/fixtures/state-saved-v1-1.js
index 42f7a95b0..af5ff8ca0 100644
--- a/test/fixtures/state-saved-v1-1.js
+++ b/test/fixtures/state-saved-v1-1.js
@@ -921,7 +921,8 @@ mergedLayer0.config = {
radiusDomain: [0, 1],
colorUI: {
color: DEFAULT_COLOR_UI,
- colorRange: DEFAULT_COLOR_UI
+ colorRange: DEFAULT_COLOR_UI,
+ strokeColorRange: DEFAULT_COLOR_UI
},
visConfig: {
opacity: 0.8,
@@ -2736,7 +2737,8 @@ mergedLayer1.config = {
radiusDomain: [0, 1],
colorUI: {
color: DEFAULT_COLOR_UI,
- colorRange: DEFAULT_COLOR_UI
+ colorRange: DEFAULT_COLOR_UI,
+ strokeColorRange: DEFAULT_COLOR_UI
},
visConfig: {
opacity: 0.8,
diff --git a/test/fixtures/state-saved-v1-3.js b/test/fixtures/state-saved-v1-3.js
index b3c4198ac..ade3824cd 100644
--- a/test/fixtures/state-saved-v1-3.js
+++ b/test/fixtures/state-saved-v1-3.js
@@ -374,7 +374,8 @@ mergedLayer0.config = {
textLabel: [DEFAULT_TEXT_LABEL],
colorUI: {
color: DEFAULT_COLOR_UI,
- colorRange: DEFAULT_COLOR_UI
+ colorRange: DEFAULT_COLOR_UI,
+ strokeColorRange: DEFAULT_COLOR_UI
},
visConfig: {
radius: 10,
diff --git a/test/fixtures/state-saved-v1-4.js b/test/fixtures/state-saved-v1-4.js
index 1fbc579fd..df193c48d 100644
--- a/test/fixtures/state-saved-v1-4.js
+++ b/test/fixtures/state-saved-v1-4.js
@@ -230,7 +230,8 @@ mergedLayer0.config = {
],
colorUI: {
color: DEFAULT_COLOR_UI,
- colorRange: DEFAULT_COLOR_UI
+ colorRange: DEFAULT_COLOR_UI,
+ strokeColorRange: DEFAULT_COLOR_UI
},
visConfig: {
radius: 12.5,
diff --git a/test/node/reducers/vis-state-test.js b/test/node/reducers/vis-state-test.js
index cd5a5b522..6ef19ad86 100644
--- a/test/node/reducers/vis-state-test.js
+++ b/test/node/reducers/vis-state-test.js
@@ -4102,7 +4102,8 @@ test('#visStateReducer -> LAYER_COLOR_UI_CHANGE. show dropdown', t => {
...DEFAULT_COLOR_UI,
showDropdown: 0
},
- colorRange: DEFAULT_COLOR_UI
+ colorRange: DEFAULT_COLOR_UI,
+ strokeColorRange: DEFAULT_COLOR_UI
};
t.deepEqual(
@@ -4124,7 +4125,7 @@ test('#visStateReducer -> LAYER_COLOR_UI_CHANGE. show dropdown', t => {
);
t.deepEqual(
nextState1.layers[0].config.colorUI,
- {color: DEFAULT_COLOR_UI, colorRange: DEFAULT_COLOR_UI},
+ {color: DEFAULT_COLOR_UI, colorRange: DEFAULT_COLOR_UI, strokeColorRange: DEFAULT_COLOR_UI},
'should update colorUI.showDropdown'
);
@@ -4137,6 +4138,7 @@ test('#visStateReducer -> LAYER_COLOR_UI_CHANGE. show dropdown', t => {
const expectedColorUI2 = {
color: DEFAULT_COLOR_UI,
+ strokeColorRange: DEFAULT_COLOR_UI,
colorRange: {
...DEFAULT_COLOR_UI,
showDropdown: 0,
@@ -4183,6 +4185,7 @@ test('#visStateReducer -> LAYER_COLOR_UI_CHANGE. colorRangeConfig.step', t => {
const expectedColorUI = {
color: DEFAULT_COLOR_UI,
+ strokeColorRange: DEFAULT_COLOR_UI,
colorRange: {
...DEFAULT_COLOR_UI,
showDropdown: 0,
@@ -4223,6 +4226,7 @@ test('#visStateReducer -> LAYER_COLOR_UI_CHANGE. colorRangeConfig.step', t => {
const expectedColorUI2 = {
color: DEFAULT_COLOR_UI,
+ strokeColorRange: DEFAULT_COLOR_UI,
colorRange: {
...DEFAULT_COLOR_UI,
showDropdown: 0,
@@ -4265,6 +4269,7 @@ test('#visStateReducer -> LAYER_COLOR_UI_CHANGE. colorRangeConfig.step', t => {
const expectedColorUI3 = {
color: DEFAULT_COLOR_UI,
+ strokeColorRange: DEFAULT_COLOR_UI,
colorRange: {
...DEFAULT_COLOR_UI,
showDropdown: 0,
@@ -4316,6 +4321,7 @@ test('#visStateReducer -> LAYER_COLOR_UI_CHANGE. colorRangeConfig.step', t => {
const expectedColorUI4 = {
color: DEFAULT_COLOR_UI,
+ strokeColorRange: DEFAULT_COLOR_UI,
colorRange: {
...DEFAULT_COLOR_UI,
showDropdown: 0,
@@ -4365,6 +4371,7 @@ test('#visStateReducer -> LAYER_COLOR_UI_CHANGE. custom Palette', t => {
const expectedColorUI = {
color: DEFAULT_COLOR_UI,
+ strokeColorRange: DEFAULT_COLOR_UI,
colorRange: {
...DEFAULT_COLOR_UI,
customPalette: {
@@ -4399,6 +4406,7 @@ test('#visStateReducer -> LAYER_COLOR_UI_CHANGE. custom Palette', t => {
const expectedColorUI2 = {
color: DEFAULT_COLOR_UI,
+ strokeColorRange: DEFAULT_COLOR_UI,
colorRange: {
...DEFAULT_COLOR_UI,
customPalette: {
@@ -4434,6 +4442,7 @@ test('#visStateReducer -> LAYER_COLOR_UI_CHANGE. custom Palette', t => {
const expectedColorUI3 = {
color: DEFAULT_COLOR_UI,
+ strokeColorRange: DEFAULT_COLOR_UI,
colorRange: {
...DEFAULT_COLOR_UI,
showSketcher: 1,
@@ -4468,6 +4477,7 @@ test('#visStateReducer -> LAYER_COLOR_UI_CHANGE. custom Palette', t => {
const expectedColorUI4 = {
color: DEFAULT_COLOR_UI,
+ strokeColorRange: DEFAULT_COLOR_UI,
colorRange: {
...DEFAULT_COLOR_UI,
showSketcher: 1,
@@ -4519,6 +4529,7 @@ test('#visStateReducer -> LAYER_COLOR_UI_CHANGE. custom Palette', t => {
const expectedColorUI6 = {
color: DEFAULT_COLOR_UI,
+ strokeColorRange: DEFAULT_COLOR_UI,
colorRange: {
...DEFAULT_COLOR_UI,
showSketcher: 1,
@@ -4582,6 +4593,7 @@ test('#visStateReducer -> LAYER_COLOR_UI_CHANGE. custom Palette', t => {
const expectedColorUI7 = {
color: DEFAULT_COLOR_UI,
+ strokeColorRange: DEFAULT_COLOR_UI,
colorRange: {
showSketcher: 1,
// keep the customPalette
@@ -4633,6 +4645,50 @@ test('#visStateReducer -> LAYER_COLOR_UI_CHANGE. custom breaks', t => {
const expectedColorUI = {
color: DEFAULT_COLOR_UI,
+ strokeColorRange: DEFAULT_COLOR_UI,
+ colorRange: {
+ ...DEFAULT_COLOR_UI,
+ customPalette: {
+ ...oldColorRange,
+ colorMap: [
+ ['driver_analytics', '#E6FAFA'],
+ ['driver_analytics_0', '#AAD7DA'],
+ ['driver_gps', '#68B4BB']
+ ]
+ },
+ colorRangeConfig: {
+ type: 'all',
+ steps: 6,
+ reversed: false,
+ custom: false,
+ customBreaks: true
+ }
+ }
+ };
+
+ t.deepEqual(
+ nextState.layers[0].config.colorUI,
+ expectedColorUI,
+ 'should set customBreaks: true and update colorUI.customPalette with default colorMap'
+ );
+ t.end();
+});
+
+test('#visStateReducer -> LAYER_COLOR_UI_CHANGE. custom breaks', t => {
+ const initialState = CloneDeep(StateWFilesFiltersLayerColor.visState);
+ const pointLayer = initialState.layers[0];
+ const oldColorRange = CloneDeep(pointLayer.config.visConfig.colorRange);
+
+ const nextState = reducer(
+ initialState,
+ VisStateActions.layerColorUIChange(pointLayer, 'colorRange', {
+ colorRangeConfig: {customBreaks: true}
+ })
+ );
+
+ const expectedColorUI = {
+ color: DEFAULT_COLOR_UI,
+ strokeColorRange: DEFAULT_COLOR_UI,
colorRange: {
...DEFAULT_COLOR_UI,
customPalette: {
diff --git a/test/node/utils/color-util-test.js b/test/node/utils/color-util-test.js
index 5ec51aae3..b6caa2d15 100644
--- a/test/node/utils/color-util-test.js
+++ b/test/node/utils/color-util-test.js
@@ -215,6 +215,24 @@ test('ColorUtil -> removeCustomPaletteColor', t => {
}
},
msg: 'Should remove 1 color at the end'
+ },
+ {
+ input: [CUSTOM_PALETTE, 0],
+ expected: {
+ ...CUSTOM_PALETTE,
+ colors: ['#030303', '#050505', '#070707'],
+ colorMap: [
+ [33, '#030303'],
+ [41, '#050505'],
+ [null, '#070707']
+ ],
+ colorLegends: {
+ '#030303': 'B',
+ '#050505': 'C',
+ '#070707': 'D'
+ }
+ },
+ msg: 'Should remove 1 color at the end'
}
];