Skip to content

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

[fix] Custom Color Scale fixes (#2875)

Igor Dykhta8335ba4108ef
diff --git a/src/components/src/hooks/use-legend-position.ts b/src/components/src/hooks/use-legend-position.ts
index da40cb91f..d5b5b45a3 100644
--- a/src/components/src/hooks/use-legend-position.ts
+++ b/src/components/src/hooks/use-legend-position.ts
@@ -84,10 +84,10 @@ export default function useLegendPosition({
: {y: bottomOffset, anchorY: 'bottom'})
};
}, [isSidePanelShown]);
- const updatePosition = useCallback(() => onChangeSettings({position: calcPosition()}), [
- calcPosition,
- onChangeSettings
- ]);
+ const updatePosition = useCallback(
+ () => onChangeSettings({position: calcPosition()}),
+ [calcPosition, onChangeSettings]
+ );
const startResize = useCallback(() => {
const content = legendContentRef.current?.querySelector('.map-control__panel-content');
diff --git a/src/components/src/map/map-legend.tsx b/src/components/src/map/map-legend.tsx
index 445d3a54b..0e4d72690 100644
--- a/src/components/src/map/map-legend.tsx
+++ b/src/components/src/map/map-legend.tsx
@@ -4,6 +4,7 @@
import React, {FC, useCallback, useState, ComponentType} from 'react';
import styled from 'styled-components';
import {rgb} from 'd3-color';
+import {format as d3Format} from 'd3-format';
import {useIntl} from 'react-intl';
import ColorLegendFactory, {LegendRowFactory} from '../common/color-legend';
import RadiusLegend from '../common/radius-legend';
@@ -204,6 +205,9 @@ export function LayerColorLegendFactory(
disableEdit={disableEdit}
isFixed={isFixed}
mapState={mapState}
+ labelFormat={
+ colorField?.displayFormat ? d3Format(colorField?.displayFormat) : null
+ }
/>
) : (
<SingleColorLegend
diff --git a/src/components/src/side-panel/layer-panel/color-breaks-panel.tsx b/src/components/src/side-panel/layer-panel/color-breaks-panel.tsx
index 4374f944a..dfee30702 100644
--- a/src/components/src/side-panel/layer-panel/color-breaks-panel.tsx
+++ b/src/components/src/side-panel/layer-panel/color-breaks-panel.tsx
@@ -173,7 +173,7 @@ function ColorBreaksPanelFactory(
return (
<ColorBreaksPanelWrapper>
- {dataset ? (
+ {dataset && allBins.length > 1 ? (
<ColumnStatsChart
colorField={colorField}
dataset={dataset}
diff --git a/src/components/src/side-panel/layer-panel/color-scale-selector.tsx b/src/components/src/side-panel/layer-panel/color-scale-selector.tsx
index 7da360ecf..29791bfb6 100644
--- a/src/components/src/side-panel/layer-panel/color-scale-selector.tsx
+++ b/src/components/src/side-panel/layer-panel/color-scale-selector.tsx
@@ -13,7 +13,6 @@ import {
getLayerColorScale,
getLegendOfScale,
histogramFromValues,
- histogramFromOrdinal,
histogramFromThreshold,
getHistogramDomain,
hasColorMap
@@ -157,17 +156,15 @@ function ColorScaleSelectorFactory(
[range, domain, scaleType, layer]
);
- const colorBreaks = useMemo(
- () =>
- colorScale
- ? getLegendOfScale({
- scale: colorScale,
- scaleType,
- fieldType: field?.type ?? ALL_FIELD_TYPES.real
- })
- : null,
- [colorScale, scaleType, field?.type]
- );
+ const colorBreaks = useMemo(() => {
+ return colorScale
+ ? getLegendOfScale({
+ scale: colorScale.byZoom && domain ? colorScale(domain?.length - 1) : colorScale,
+ scaleType,
+ fieldType: field?.type ?? ALL_FIELD_TYPES.real
+ })
+ : null;
+ }, [colorScale, scaleType, field?.type, domain]);
const columnStats = field?.filterProps?.columnStats;
@@ -188,10 +185,8 @@ function ColorScaleSelectorFactory(
}
return columnStats?.bins
? columnStats?.bins
- : field?.type === ALL_FIELD_TYPES.string
- ? histogramFromOrdinal(colorScale?.domain() || [], dataset.allIndexes, fieldValueAccessor)
: histogramFromValues(dataset.allIndexes, HISTOGRAM_BINS, fieldValueAccessor);
- }, [aggregatedBins, columnStats, dataset, fieldValueAccessor, colorScale, field?.type]);
+ }, [aggregatedBins, columnStats, dataset, fieldValueAccessor]);
const histogramDomain = useMemo(() => {
return getHistogramDomain({aggregatedBins, columnStats, dataset, fieldValueAccessor});
@@ -221,22 +216,17 @@ function ColorScaleSelectorFactory(
val => {
// highlight selected option
if (getOptionValue(val) === SCALE_TYPES.custom) {
- const colorMap = range.colorMap
- ? range.colorMap
- : colorBreaks
- ? colorBreaksToColorMap(colorBreaks)
- : undefined;
- const colors =
- field?.type === ALL_FIELD_TYPES.string
- ? range.colors.slice(0, colorMap?.length)
- : range.colors;
// update custom breaks
const customPalette: NestedPartial<ColorRange> = {
name: 'color.customPalette',
type: 'custom',
category: 'Custom',
- colors,
- colorMap
+ colors: range.colors,
+ colorMap: range.colorMap
+ ? range.colorMap
+ : colorBreaks
+ ? colorBreaksToColorMap(colorBreaks)
+ : null
};
setColorUI({
showColorChart: true,
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 9392c2de9..a5f2f048e 100644
--- a/src/components/src/side-panel/layer-panel/custom-palette.tsx
+++ b/src/components/src/side-panel/layer-panel/custom-palette.tsx
@@ -407,19 +407,7 @@ export const CustomPaletteInput: React.FC<CustomPaletteInputProps> = ({
editColorMap={editColorMapValue}
editable
/>
- ) : (
- colorBreaks &&
- colorBreaks[index] && (
- <ColorPaletteInput
- value={colorBreaks[index].label}
- id={`color-palette-input-${index}-left`}
- width="auto"
- textAlign="end"
- editable={false}
- onChange={v => v}
- />
- )
- )}
+ ) : null}
</div>
<div className="custom-palette-input__right">
{!disableAppend ? (
diff --git a/src/constants/src/color-palettes.ts b/src/constants/src/color-palettes.ts
index 8304b75cc..7c63dc0af 100644
--- a/src/constants/src/color-palettes.ts
+++ b/src/constants/src/color-palettes.ts
@@ -367,6 +367,14 @@ const FSQWarmTone = {
colorBlindSafe: true
};
+const FSQCoolTone = {
+ name: 'FSQ Cool Tone',
+ type: PALETTE_TYPES.QUA,
+ category: CATEGORIES.COLORBLIND,
+ colors: ['#11439F', '#297EE8', '#95C6C9', '#FECE5A', '#FFDDBF', '#9FB1B7', '#5281B5', '#B9D0FB'],
+ colorBlindSafe: true
+};
+
/**
* Build Categorical color palette
*/
@@ -572,7 +580,8 @@ const BRANDED_PALETTES: ColorPalette[] = [
TolLight,
OkabeIto,
FSQBrand,
- FSQWarmTone
+ FSQWarmTone,
+ FSQCoolTone
]
.map(recipe =>
recipe.type === PALETTE_TYPES.QUA ? buildCategoricalPalette(recipe) : buildCustomPalette(recipe)
diff --git a/src/constants/src/default-settings.ts b/src/constants/src/default-settings.ts
index 5b8848462..a35102e46 100644
--- a/src/constants/src/default-settings.ts
+++ b/src/constants/src/default-settings.ts
@@ -666,7 +666,7 @@ export const linearFieldAggrScaleFunctions = {
};
export const ordinalFieldScaleFunctions = {
- [CHANNEL_SCALES.color]: [SCALE_TYPES.ordinal, SCALE_TYPES.custom],
+ [CHANNEL_SCALES.color]: [SCALE_TYPES.ordinal],
[CHANNEL_SCALES.radius]: [SCALE_TYPES.point],
[CHANNEL_SCALES.size]: [SCALE_TYPES.point]
};
diff --git a/src/layers/src/base-layer.ts b/src/layers/src/base-layer.ts
index b9c232cf4..212aa2a9f 100644
--- a/src/layers/src/base-layer.ts
+++ b/src/layers/src/base-layer.ts
@@ -66,7 +66,9 @@ import {
ValueOf,
VisualChannel,
VisualChannels,
- VisualChannelDomain
+ VisualChannelDomain,
+ VisualChannelField,
+ VisualChannelScale
} from '@kepler.gl/types';
import {
getScaleFunction,
@@ -81,10 +83,15 @@ import {
getThresholdsFromQuantiles
} from '@kepler.gl/utils';
-export type {LayerBaseConfig, VisualChannel, VisualChannels, VisualChannelDomain, AggregatedBin};
-
-export type VisualChannelField = Field | null;
-export type VisualChannelScale = keyof typeof SCALE_TYPES;
+export type {
+ AggregatedBin,
+ LayerBaseConfig,
+ VisualChannel,
+ VisualChannels,
+ VisualChannelDomain,
+ VisualChannelField,
+ VisualChannelScale
+};
export type LayerBaseConfigPartial = {dataId: LayerBaseConfig['dataId']} & Partial<LayerBaseConfig>;
@@ -194,7 +201,7 @@ class Layer implements KeplerLayer {
visConfigSettings: {
[key: string]: ValueOf<LayerVisConfigSettings>;
};
- config: LayerBaseConfig;
+ config: LayerBaseConfig & Partial<LayerColorConfig & LayerSizeConfig>;
// TODO: define _oldDataUpdateTriggers
_oldDataUpdateTriggers: any;
@@ -789,9 +796,10 @@ class Layer implements KeplerLayer {
return columns;
}
- updateLayerConfig<LayerConfig extends LayerBaseConfig = LayerBaseConfig>(
- newConfig: Partial<LayerConfig>
- ): Layer {
+ updateLayerConfig<
+ LayerConfig extends LayerBaseConfig &
+ Partial<LayerColorConfig & LayerSizeConfig> = LayerBaseConfig
+ >(newConfig: Partial<LayerConfig>): Layer {
this.config = {...this.config, ...newConfig};
return this;
}
@@ -1022,22 +1030,13 @@ class Layer implements KeplerLayer {
colorDomain: VisualChannelDomain,
colorRange: ColorRange
): GetVisChannelScaleReturnType {
- if (
- hasColorMap(colorRange) &&
- (colorScale === SCALE_TYPES.custom || colorScale === SCALE_TYPES.ordinal)
- ) {
+ if (hasColorMap(colorRange) && colorScale === SCALE_TYPES.custom) {
const cMap = new Map();
colorRange.colorMap?.forEach(([k, v]) => {
cMap.set(k, typeof v === 'string' ? hexToRgb(v) : v);
});
- // in kepler, scaleThreshold function will be used for SCALE_TYPES.custom
- // so we adjust scaleType to SCALE_TYPES.custom to ordinal if necessary
- const isOrdinalDomain = (colorDomain as (string | number)[]).every(
- i => typeof i === 'string'
- );
- const scaleType =
- colorScale === SCALE_TYPES.custom && !isOrdinalDomain ? colorScale : SCALE_TYPES.ordinal;
+ const scaleType = colorScale === SCALE_TYPES.custom ? colorScale : SCALE_TYPES.ordinal;
const scale = getScaleFunction(scaleType, cMap.values(), cMap.keys(), false);
scale.unknown(cMap.get(UNKNOWN_COLOR_KEY) || NO_VALUE_COLOR);
diff --git a/src/layers/src/point-layer/point-layer.ts b/src/layers/src/point-layer/point-layer.ts
index 4943a2b24..32b6eb535 100644
--- a/src/layers/src/point-layer/point-layer.ts
+++ b/src/layers/src/point-layer/point-layer.ts
@@ -313,7 +313,6 @@ export default class PointLayer extends Layer {
if (defaultColorField) {
this.updateLayerConfig({
- // @ts-expect-error Remove this after updateLayerConfig converted into generic function
colorField: defaultColorField
});
this.updateLayerVisualChannel(dataset, 'color');
diff --git a/src/reducers/src/vis-state-updaters.ts b/src/reducers/src/vis-state-updaters.ts
index de6292704..c09d02ff9 100644
--- a/src/reducers/src/vis-state-updaters.ts
+++ b/src/reducers/src/vis-state-updaters.ts
@@ -629,7 +629,7 @@ export function layerToggleVisibilityUpdater(
return toggleLayerForMapUpdater(newState, toggleLayerForMap(mapIndex, layerId));
} else {
// [case 2]: toggle global layer visibility
- let newLayer = layer.updateLayerConfig({isVisible});
+ const newLayer = layer.updateLayerConfig({isVisible});
const idx = newState.layers.findIndex(l => l.id === layerId);
newState = updatelayerVisibilty(newState, newLayer, isVisible);
@@ -3242,7 +3242,24 @@ export function setColumnDisplayFormatUpdater(
});
const newDataset = copyTableAndUpdate(dataset, {fields: newFields as Field[]});
- return pick_('datasets')(merge_({[dataId]: newDataset}))(state);
+ let newState = pick_('datasets')(merge_({[dataId]: newDataset}))(state);
+
+ // update colorField displayFormat
+ newState = {
+ ...newState,
+ layers: newState.layers.map(layer =>
+ layer.config?.colorField?.name && layer.config.colorField.name in formats
+ ? layer.updateLayerConfig({
+ colorField: {
+ ...layer.config.colorField,
+ displayFormat: formats[layer.config.colorField.name]
+ }
+ })
+ : layer
+ )
+ };
+
+ return newState;
}
/**
diff --git a/src/table/src/kepler-table.ts b/src/table/src/kepler-table.ts
index f3d61e2aa..742888b5c 100644
--- a/src/table/src/kepler-table.ts
+++ b/src/table/src/kepler-table.ts
@@ -496,9 +496,7 @@ class KeplerTable<F extends Field = Field> {
case SCALE_TYPES.linear:
case SCALE_TYPES.sqrt:
default:
- return field.type === ALL_FIELD_TYPES.string
- ? getOrdinalDomain(dataContainer, valueAccessor)
- : getLinearDomain(filteredIndexForDomain, indexValueAccessor);
+ return getLinearDomain(filteredIndexForDomain, indexValueAccessor);
}
}
diff --git a/src/types/layers.d.ts b/src/types/layers.d.ts
index f248f01ce..816dbe214 100644
--- a/src/types/layers.d.ts
+++ b/src/types/layers.d.ts
@@ -70,6 +70,9 @@ export type SupportedColumnMode = {
hasHelp?: boolean;
};
+export type VisualChannelField = Field | null;
+export type VisualChannelScale = keyof typeof SCALE_TYPES;
+
export type LayerColorConfig = {
colorField: VisualChannelField;
colorDomain: VisualChannelDomain;
@@ -185,7 +188,7 @@ export type ColorRange = {
category?: string;
colors: HexColor[];
reversed?: boolean;
- colorMap?: ColorMap;
+ colorMap?: ColorMap | null;
colorLegends?: ColorLegends;
};
diff --git a/src/utils/src/data-scale-utils.ts b/src/utils/src/data-scale-utils.ts
index 4b0eeb97c..aeb52e4c2 100644
--- a/src/utils/src/data-scale-utils.ts
+++ b/src/utils/src/data-scale-utils.ts
@@ -42,7 +42,7 @@ export type D3ScaleFunction = Record<string, any> & ((x: any) => any);
type FilterProps = any;
type KeplerTable = any;
-export type LabelFormat = (n: number) => string;
+export type LabelFormat = (n: number, type?: string) => string;
type dataValueAccessor = <T>(param: T) => T;
type dataContainerValueAccessor = (d: {index: number}, dc: DataContainerInterface) => any;
type sort = (a: any, b: any) => any;
@@ -230,14 +230,13 @@ export function getQuantLegends(scale: D3ScaleFunction, labelFormat: LabelFormat
if (typeof scale.invertExtent !== 'function') {
return [];
}
+ const thresholdLabelFormat = (n, type) =>
+ n && labelFormat ? labelFormat(n) : n ? formatNumber(n, type) : 'no value';
const labels =
- scale.scaleType === 'threshold' || scale.scaleType === 'custom'
- ? getThresholdLabels(
- scale,
- scale.scaleType === 'custom'
- ? customScaleLabelFormat
- : n => (n ? formatNumber(n) : 'no value')
- )
+ scale.scaleType === 'threshold'
+ ? getThresholdLabels(scale, thresholdLabelFormat)
+ : scale.scaleType === 'custom'
+ ? getThresholdLabels(scale, customScaleLabelFormat)
: getScaleLabels(scale, labelFormat);
const data = scale.range();
@@ -295,7 +294,7 @@ export function getLegendOfScale({
if (!scale || scale.byZoom) {
return [];
}
- if (scaleType === SCALE_TYPES.ordinal || fieldType === ALL_FIELD_TYPES.string) {
+ if (scaleType === SCALE_TYPES.ordinal) {
return getOrdinalLegends(scale);
}
@@ -391,18 +390,11 @@ export function colorBreaksToColorMap(colorBreaks: ColorBreak[] | ColorBreakOrdi
/**
* Convert colorRange.colorMap into color breaks UI input
*/
-export function colorMapToColorBreaks(colorMap?: ColorMap): ColorBreak[] | null {
+export function colorMapToColorBreaks(colorMap?: ColorMap | null): ColorBreak[] | null {
if (!colorMap) {
return null;
}
const colorBreaks = colorMap.map(([value, color], i) => {
- if (typeof value === 'string') {
- // for ordinal string value
- return {
- data: color,
- label: value
- };
- }
const range =
i === 0
? // first
diff --git a/src/utils/src/index.ts b/src/utils/src/index.ts
index 42e942e8a..82bf71647 100644
--- a/src/utils/src/index.ts
+++ b/src/utils/src/index.ts
@@ -152,7 +152,8 @@ export {
getDomainStepsbyZoom,
getThresholdsFromQuantiles,
getQuantLabelFormat,
- getHistogramDomain
+ getHistogramDomain,
+ getQuantLegends
} from './data-scale-utils';
export type {ColorBreak, ColorBreakOrdinal, DomainQuantiles, DomainStops} from './data-scale-utils';
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 b1ed9241b..bfe19c90e 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
@@ -98,8 +98,8 @@ test('Components -> ChannelByValueSelector -> ColorScaleSelector -> disabled', t
t.equal(wrapper.find(ColorScaleSelector).length, 1, 'Should render 1 ColorScaleSelector');
t.equal(
wrapper.find(ColorScaleSelector).at(0).props().disabled,
- false,
- 'Should enable ordinal and custom color scale select'
+ true,
+ 'Should disabled color scale select if only 1 option'
);
t.equal(
diff --git a/test/browser/components/side-panel/color-selector-test.js b/test/browser/components/side-panel/color-selector-test.js
index acb3a280e..d10f49de6 100644
--- a/test/browser/components/side-panel/color-selector-test.js
+++ b/test/browser/components/side-panel/color-selector-test.js
@@ -400,7 +400,7 @@ test('Components -> LayerColorRangeSelector.render -> ColorSelector -> ColorRang
t.equal(crs.length, 1, 'should render 1 ColorRangeSelector');
const item = crs.find('ColorPaletteItem');
- t.equal(item.length, 64, `should render ${KEPLER_COLOR_PALETTES.length} Palettes`);
+ t.equal(item.length, 65, `should render ${KEPLER_COLOR_PALETTES.length} Palettes`);
t.equal(
item.at(0).find(ColorPalette).find('.color-range-palette__block').length,
@@ -500,7 +500,7 @@ test('Components -> LayerColorRangeSelector.render -> ColorSelector -> ColorRang
t.equal(pc.length, 5, 'should render 5 PaletteConfig');
const cpg = crs.find('ColorPaletteItem');
- t.equal(cpg.length, 64, `should render ${KEPLER_COLOR_PALETTES.length} Palettes`);
+ t.equal(cpg.length, 65, `should render ${KEPLER_COLOR_PALETTES.length} Palettes`);
const typeSelect = crs.find(PaletteConfig).at(0);
t.equal(typeSelect.find('.side-panel-panel__label').text(), 'Type', 'should render type');
diff --git a/test/node/utils/data-scale-utils-test.js b/test/node/utils/data-scale-utils-test.js
index e9fc4b051..e3ed76ad0 100644
--- a/test/node/utils/data-scale-utils-test.js
+++ b/test/node/utils/data-scale-utils-test.js
@@ -3,6 +3,8 @@
import test from 'tape';
import cloneDeep from 'lodash.clonedeep';
+import {format as d3Format} from 'd3-format';
+
import {
getOrdinalDomain,
getQuantileDomain,
@@ -12,9 +14,11 @@ import {
getThresholdsFromQuantiles,
getDomainStepsbyZoom,
getQuantLabelFormat,
- getHistogramDomain
+ getHistogramDomain,
+ getQuantLegends
} from '@kepler.gl/utils';
import {StateWFilesFiltersLayerColor} from 'test/helpers/mock-state';
+import {SCALE_FUNC} from '@kepler.gl/constants';
function numberSort(a, b) {
return a - b;
@@ -209,3 +213,41 @@ test('DataScaleUtils -> getHistogramDomain', t => {
t.end();
});
+
+test('DataScaleUtils -> getQuantLegends', t => {
+ // scale, labelFormat
+ const labelFormat = d3Format('.1s');
+ const domain = [3.1, 5.2, 7.3];
+
+ const scaleFunction = SCALE_FUNC.threshold().domain(domain).range(domain);
+ scaleFunction.scaleType = 'linear';
+
+ const legends = getQuantLegends(scaleFunction, labelFormat);
+
+ const expectedColorBreaks = [
+ {data: 3.1, label: 'NaN to 3', range: [undefined, 3.1], inputs: [NaN, 3]},
+ {data: 5.2, label: '3 to 5', range: [3.1, 5.2], inputs: [3, 5]},
+ {data: 7.3, label: '5 to 7', range: [5.2, 7.3], inputs: [5, 7]}
+ ];
+
+ t.deepEqual(legends, expectedColorBreaks, 'should create correct threshold legends');
+
+ const customDomain = [3.1, 5.2, 7.3, null];
+ const customScaleFunction = SCALE_FUNC.threshold().domain(customDomain).range(customDomain);
+ customScaleFunction.scaleType = 'custom';
+ const customLegends = getQuantLegends(customScaleFunction, labelFormat);
+
+ const expectedCustomColorBreaks = [
+ {data: 3.1, label: 'Less than 3.1', range: [undefined, 3.1], inputs: [null, 3.1]},
+ {data: 5.2, label: '3.1 to 5.2', range: [3.1, 5.2], inputs: [3.1, 5.2]},
+ {data: 7.3, label: '5.2 to 7.3', range: [5.2, 7.3], inputs: [5.2, 7.3]},
+ {data: null, label: '7.3 or more', range: [7.3, null], inputs: [7.3, null]}
+ ];
+
+ t.deepEqual(
+ customLegends,
+ expectedCustomColorBreaks,
+ 'should create correct threshold legends from custom breaks'
+ );
+ t.end();
+});