Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export function createTextLayerSpecification(
} else {
// @ts-expect-error - typing for calculateMCDALayer needs fixing, it actually returns ExpressionSpecification
values = textDimension.mcdaValue.config.layers.map((layer) =>
calculateMCDALayer(layer),
calculateMCDALayer(layer, false, layer.normalization === 'no'),
);
units = textDimension.mcdaValue.config.layers.map((layer) =>
layer.normalization === 'no' ? (layer.unit ?? '') : '',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { arraysAreEqualWithStrictOrder } from '~utils/common/equality';
import { sentimentDefault, sentimentReversed } from './constants';
import { JsMath, MapMath } from './operations';
import type { IsomorphMath } from './operations';
import type { MCDAConfig, TransformationFunction } from '../types';
import type { MCDALayer, TransformationFunction } from '../types';

const equalSentiments = (a: Array<string>, b: Array<string>) =>
arraysAreEqualWithStrictOrder(a, b);
Expand Down Expand Up @@ -178,17 +178,22 @@ export const calculateLayerPipeline =
type: 'view' | 'layerStyle',
getValue: (axis: { num: string; den: string }) => { num: T; den: T },
) =>
({
axis,
range,
coefficient,
sentiment,
transformationFunction,
transformation,
normalization,
outliers,
datasetStats,
}: MCDAConfig['layers'][0]) => {
(
layer: MCDALayer,
forceMinMaxInLayerStyle?: boolean,
preventValueInversion?: boolean,
) => {
const {
axis,
range,
coefficient,
sentiment,
transformationFunction,
transformation,
normalization,
outliers,
datasetStats,
} = layer;
// @ts-expect-error - IsomorphCalculations typing needs fixing. The code works though, so for now ignoring the ts error
const operations: IsomorphCalculations<number> =
type === 'layerStyle' ? inStyleCalculations : inViewCalculations;
Expand Down Expand Up @@ -246,13 +251,16 @@ export const calculateLayerPipeline =
}
}
let normalized = tX;
if (normalization === 'max-min' || type === 'layerStyle') {
// always apply min-max normalization for layer style,
// because we need to have (0..1) values in expressions for proper colors interpolation
if (
normalization === 'max-min' ||
(type === 'layerStyle' && forceMinMaxInLayerStyle)
) {
// always apply min-max normalization for mcda style with sentiments colors,
// because we need to have (0..1) values in expressions for proper colors interpolation in sentiments colors (see sentimentPaint())
normalized = operations.normalize({ x: tX, min: tMin, max: tMax });
}
let oriented = inverted ? operations.invert(normalized) : normalized;
if (type === 'view' && normalization === 'no') {
if ((type === 'view' && normalization === 'no') || preventValueInversion) {
// don't invert non-normalized values, because applying (1-value) doesn't make sense for them
oriented = normalized;
}
Expand Down
18 changes: 15 additions & 3 deletions src/core/logical_layers/renderers/stylesConfigs/mcda/mcdaStyle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,20 @@ export function filterSetup(

export function linearNormalization(
layers: MCDAConfig['layers'],
forceMinMax: boolean = false,
): ExpressionSpecification {
if (layers.length === 1) {
return ['/', calculateMCDALayer(layers.at(0)!), layers.at(0)!.coefficient];
return [
'/',
calculateMCDALayer(layers.at(0)!, forceMinMax),
layers.at(0)!.coefficient,
];
} else {
return ['/', ['+', ...layers.map(calculateMCDALayer)], sumBy(layers, 'coefficient')];
return [
'/',
['+', ...layers.map((layer) => calculateMCDALayer(layer, forceMinMax))],
sumBy(layers, 'coefficient'),
];
}
}

Expand Down Expand Up @@ -180,7 +189,10 @@ export function createMCDAStyle(config: MCDAConfig): FillLayerSpecification {
[] as [number, number] | [],
);

const mcdaResult = linearNormalization(config.layers);
// always apply min-max normalization for mcda style with sentiments colors,
// because we need to have (0..1) values in expressions for proper colors interpolation in sentiments colors (see sentimentPaint())
const forceMinMaxForLayerStyle = config.colors.type === 'sentiments';
const mcdaResult = linearNormalization(config.layers, forceMinMaxForLayerStyle);

const layerStyle: FillLayerSpecification = {
// TODO: this id is useless and gets replaced in renderer. Needs refactoring
Expand Down
Loading