Skip to content

Commit

Permalink
CARTO: Correctly map aggregation weights & methods (#6771)
Browse files Browse the repository at this point in the history
  • Loading branch information
felixpalmer committed Mar 24, 2022
1 parent 94ade66 commit ac7a87d
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 9 deletions.
11 changes: 9 additions & 2 deletions modules/carto/src/api/layer-map.ts
Expand Up @@ -31,6 +31,13 @@ const SCALE_FUNCS = {
};
export type SCALE_TYPE = keyof typeof SCALE_FUNCS;

export const AGGREGATION = {
average: 'MEAN',
maximum: 'MAX',
minimum: 'MIN',
sum: 'SUM'
};

const hexToRGBA = c => {
const {r, g, b, opacity} = rgb(c);
return [r, g, b, 255 * opacity];
Expand Down Expand Up @@ -61,7 +68,7 @@ const sharedPropMap = {
};

const aggregationVisConfig = {
colorAggregation: 'colorAggregation',
colorAggregation: x => ({colorAggregation: AGGREGATION[x] || AGGREGATION.sum}),
colorRange: x => ({colorRange: x.colors.map(hexToRGBA)}),
coverage: 'coverage',
elevationPercentile: ['elevationLowerPercentile', 'elevationUpperPercentile'],
Expand Down Expand Up @@ -123,7 +130,7 @@ export function getLayer(
hexagonId: {
Layer: H3HexagonLayer,
propMap: {visConfig: {coverage: 'coverage'}},
defaultProps: {getHexagon: d => d[hexagonId]}
defaultProps: {getHexagon: d => d[hexagonId], stroked: false}
}
}[type];

Expand Down
13 changes: 9 additions & 4 deletions modules/carto/src/api/parseMap.ts
Expand Up @@ -137,7 +137,7 @@ function createStyleProps(config, mapping) {
return result;
}

/* eslint-disable complexity */
/* eslint-disable complexity, max-statements */
function createChannelProps(visualChannels, type, config, data) {
const {colorField, colorScale, sizeField, sizeScale, strokeColorField, strokeColorScale} =
visualChannels;
Expand All @@ -149,17 +149,22 @@ function createChannelProps(visualChannels, type, config, data) {
const {textLabel, visConfig} = config;
const result: Record<string, any> = {};
const textLabelField = textLabel && textLabel.field;
if (colorField) {

if (type === 'grid' || type === 'hexagon') {
result.colorScaleType = colorScale;
if (colorField) {
result.getColorWeight = d => d[colorField.name];
}
} else if (colorField) {
result.getFillColor = getColorAccessor(
colorField,
colorScale,
visConfig.colorRange,
1, // Rely on layer opacity
data
);
} else if (type === 'grid' || type === 'hexagon') {
result.colorScaleType = colorScale;
}

if (strokeColorField) {
const opacity = visConfig.strokeOpacity !== undefined ? visConfig.strokeOpacity : 1;
result.getLineColor = getColorAccessor(
Expand Down
7 changes: 4 additions & 3 deletions test/modules/carto/data/visState.json
Expand Up @@ -667,6 +667,7 @@
"autoHighlight": true,
"pickable": true,
"visible": true,
"stroked": false,
"extruded": true,
"elevationScale": 70.1,
"opacity": 0.8,
Expand Down Expand Up @@ -1167,7 +1168,7 @@
"extruded": false,
"elevationScale": 5,
"opacity": 0.8,
"colorAggregation": "count",
"colorAggregation": "SUM",
"colorRange": [
[90, 24, 70, 255],
[144, 12, 63, 255],
Expand Down Expand Up @@ -1310,7 +1311,6 @@
"extruded": false,
"elevationScale": 5,
"opacity": 0.8,
"colorAggregation": "count",
"colorRange": [
[255, 255, 204, 255],
[217, 240, 163, 255],
Expand All @@ -1324,6 +1324,7 @@
"elevationUpperPercentile": 100,
"lowerPercentile": 0,
"upperPercentile": 100,
"colorAggregation": "SUM",
"radius": 300,
"highlightColor": [252, 242, 26, 255],
"colorScaleType": "quantile"
Expand Down Expand Up @@ -1455,7 +1456,7 @@
"elevationScale": 5,
"opacity": 0.8,
"radiusPixels": 21.7,
"colorAggregation": "average",
"colorAggregation": "MEAN",
"colorRange": [
[254, 240, 217, 255],
[253, 212, 158, 255],
Expand Down

0 comments on commit ac7a87d

Please sign in to comment.