Skip to content

Commit

Permalink
Fix label formatting in color/value inputs (#507)
Browse files Browse the repository at this point in the history
  • Loading branch information
luisvasq committed Dec 11, 2024
1 parent 5007bd8 commit 382bd4c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 14 deletions.
18 changes: 12 additions & 6 deletions src/ui/views/map/ColorLegend.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { useSelector } from 'react-redux'

import {
getGradientLabel,
getNumLabel,
GroupCalcSelector,
RippleBox,
ScaleSelector,
Expand Down Expand Up @@ -120,28 +121,33 @@ const NumericalColorLegend = ({
[valueRange.colorGradient?.scale]
)

const getFormattedValue = useCallback(
(index) => getNumLabel(values[index], numberFormat, 'colorGradient'),
[numberFormat, values]
)

const getColorLabel = useCallback(
(index) =>
index > 0 && index < values.length - 1 // Within the bounds
? isStepScale
? `[${values[index - 1]}, ${values[index]}) "${getLabel(index)}"`
? `[${getFormattedValue(index - 1)}, ${getFormattedValue(index)}) "${getLabel(index)}"`
: `"${getLabel(index)}"`
: isStepScale
? `${index < 1 ? `(-\u221E, ${values[0]})` : `[${values[index]}, \u221E)`}`
? `${index < 1 ? `(-\u221E, ${getFormattedValue(index)})` : `[${getFormattedValue(index)}, \u221E)`}`
: `${index < 1 ? 'Min' : 'Max'}`,
[getLabel, isStepScale, values]
[getFormattedValue, getLabel, isStepScale, values]
)

const getValueLabel = useCallback(
(index) =>
index > 0 && index < values.length - 1 // Within the bounds
? isStepScale
? `Threshold \u279D [${values[index - 1]}, \u2B07)${labels[index] != null ? ` "${getLabel(index)}"` : ''}`
? `Threshold \u279D [${getFormattedValue(index - 1)}, \u2B07)${labels[index] != null ? ` "${getLabel(index)}"` : ''}`
: `Value${labels[index] != null ? ` \u279D "${getLabel(index)}"` : ''}`
: isStepScale
? `Threshold (Read-Only) \u279D ${index < 1 ? `(-\u221E, ${values[0]})` : `[${values[index]}, \u221E)`}`
? `Threshold (Read-Only) \u279D ${index < 1 ? `(-\u221E, ${getFormattedValue(index)})` : `[${getFormattedValue(index)}, \u221E)`}`
: `Value (Read-Only) \u279D ${index < 1 ? 'Min' : 'Max'}`,
[getLabel, isStepScale, labels, values]
[getFormattedValue, getLabel, isStepScale, labels, values.length]
)

const gradientStyle = useMemo(() => {
Expand Down
16 changes: 8 additions & 8 deletions src/ui/views/map/Legend.js
Original file line number Diff line number Diff line change
Expand Up @@ -242,8 +242,10 @@ export const useLegend = (mapId) => {
return { showLegendGroupNames, handleToggleLegendGroupNames }
}

const getNumLabel = (value, numberFormat, gradientKey) =>
NumberFormat.format(value, {
export const getNumLabel = (value, numberFormatRaw, gradientKey) => {
// eslint-disable-next-line no-unused-vars
const { unit, unitPlacement, ...numberFormat } = numberFormatRaw
return NumberFormat.format(value, {
...numberFormat,
// Formatting hierarchy: `props.*gradient.<key>` -> `settings.defaults.*gradient<key>` -> `props.<key>` -> `settings.defaults.<key>`
...{
Expand All @@ -254,21 +256,19 @@ const getNumLabel = (value, numberFormat, gradientKey) =>
numberFormat.notationDisplay,
},
})
}

export const getGradientLabel = (
labels,
values,
index,
numberFormatRaw,
numberFormat,
group,
gradientKey
) => {
// eslint-disable-next-line no-unused-vars
const { unit, unitPlacement, ...numberFormat } = numberFormatRaw
return group || labels[index] == null
) =>
group || labels[index] == null
? getNumLabel(values[index], numberFormat, gradientKey)
: labels[index]
}

export const getMinLabel = (labels, values, numberFormat, group, gradientKey) =>
getGradientLabel(labels, values, 0, numberFormat, group, gradientKey)
Expand Down

0 comments on commit 382bd4c

Please sign in to comment.