Skip to content

Commit

Permalink
Fix legend matching bounds
Browse files Browse the repository at this point in the history
Previously the creation of legend bounds for continuous scales used
hardcoded minimum (0) and maximum (10,000). This may result in the
first and/or last legend entry having associated bounds which could
never match any values.

Given the various locations this function is called it's easier to
set the min/max as (-)Infinity rather than consider the observed min/max.

Closes #1644
Closes #1654
  • Loading branch information
jameshadfield committed Mar 27, 2023
1 parent d0a4ac1 commit db10308
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/util/colorScale.js
Original file line number Diff line number Diff line change
Expand Up @@ -391,15 +391,19 @@ function booleanColorScale(val) {
return "#CBB742";
}

/**
* @param {Array<Number>} legendValues
* @returns {Record<Number, [Number, Number]>}
*/
function createLegendBounds(legendValues) {
const valBetween = (x0, x1) => x0 + 0.5*(x1-x0);
const len = legendValues.length;
const legendBounds = {};
legendBounds[legendValues[0]] = [0, valBetween(legendValues[0], legendValues[1])];
legendBounds[legendValues[0]] = [-Infinity, valBetween(legendValues[0], legendValues[1])];
for (let i = 1; i < len - 1; i++) {
legendBounds[legendValues[i]] = [valBetween(legendValues[i-1], legendValues[i]), valBetween(legendValues[i], legendValues[i+1])];
}
legendBounds[legendValues[len-1]] = [valBetween(legendValues[len-2], legendValues[len-1]), 10000];
legendBounds[legendValues[len-1]] = [valBetween(legendValues[len-2], legendValues[len-1]), Infinity];
return legendBounds;
}

Expand Down

0 comments on commit db10308

Please sign in to comment.