Skip to content

Commit

Permalink
[colorscale] add very basic custom threshold scale support
Browse files Browse the repository at this point in the history
  • Loading branch information
lixun910 committed Jan 20, 2024
1 parent f35cd60 commit 166ad0e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
11 changes: 8 additions & 3 deletions src/constants/src/default-settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
scaleQuantize,
scaleQuantile,
scaleOrdinal,
scaleThreshold,
scaleSqrt,
scaleLog,
scalePoint
Expand Down Expand Up @@ -391,7 +392,10 @@ export const SCALE_TYPES = keyMirror({
log: null,

// ordinal domain to linear range
point: null
point: null,

// custom domain to custom range
custom: null
});

export const SCALE_FUNC = {
Expand All @@ -401,7 +405,8 @@ export const SCALE_FUNC = {
[SCALE_TYPES.ordinal]: scaleOrdinal,
[SCALE_TYPES.sqrt]: scaleSqrt,
[SCALE_TYPES.log]: scaleLog,
[SCALE_TYPES.point]: scalePoint
[SCALE_TYPES.point]: scalePoint,
[SCALE_TYPES.custom]: scaleThreshold
};

export const ALL_FIELD_TYPES = keyMirror({
Expand Down Expand Up @@ -586,7 +591,7 @@ export const AGGREGATION_TYPE_OPTIONS: {id: string; label: string}[] = Object.en
}));

export const linearFieldScaleFunctions = {
[CHANNEL_SCALES.color]: [SCALE_TYPES.quantize, SCALE_TYPES.quantile],
[CHANNEL_SCALES.color]: [SCALE_TYPES.quantize, SCALE_TYPES.quantile, SCALE_TYPES.custom],
[CHANNEL_SCALES.radius]: [SCALE_TYPES.sqrt],
[CHANNEL_SCALES.size]: [SCALE_TYPES.linear, SCALE_TYPES.sqrt, SCALE_TYPES.log]
};
Expand Down
12 changes: 10 additions & 2 deletions src/layers/src/base-layer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -896,11 +896,19 @@ class Layer {
cMap.set(k, typeof v === 'string' ? hexToRgb(v) : v);
});

const scale = SCALE_FUNC[SCALE_TYPES.ordinal]()
// custom threshold color scale
if (colorScale === SCALE_TYPES.custom) {
return SCALE_FUNC[SCALE_TYPES.custom]()
.domain(cMap.keys())
.range(cMap.values())
.unknown(cMap.get(UNKNOWN_COLOR_KEY) || NO_VALUE_COLOR);
}

// custom ordinal (unique values) color scale
return SCALE_FUNC[SCALE_TYPES.ordinal]()
.domain(cMap.keys())
.range(cMap.values())
.unknown(cMap.get(UNKNOWN_COLOR_KEY) || NO_VALUE_COLOR);
return scale;
}
return this.getVisChannelScale(colorScale, colorDomain, colorRange.colors.map(hexToRgb));
}
Expand Down

0 comments on commit 166ad0e

Please sign in to comment.