From 0a10b3ec2ae1d3b0f975e6f54cd906d9a495eaff Mon Sep 17 00:00:00 2001 From: Severiano Badajoz Date: Tue, 25 Aug 2020 12:25:55 -0700 Subject: [PATCH] sort object keys to our specification before generating user colormap (#1792) --- client/src/util/stateManager/colorHelpers.js | 29 ++++++++++++++------ 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/client/src/util/stateManager/colorHelpers.js b/client/src/util/stateManager/colorHelpers.js index be5fe113..4eaea2b7 100644 --- a/client/src/util/stateManager/colorHelpers.js +++ b/client/src/util/stateManager/colorHelpers.js @@ -94,15 +94,26 @@ export const createColorTable = memoize(_createColorTable); export function loadUserColorConfig(userColors) { const convertedUserColors = {}; Object.keys(userColors).forEach((category) => { - const [colors, scaleMap] = Object.keys(userColors[category]).reduce( - (acc, label, i) => { - const color = parseRGB(userColors[category][label]); - acc[0][label] = color; - acc[1][i] = d3.rgb(255 * color[0], 255 * color[1], 255 * color[2]); - return acc; - }, - [{}, {}] - ); + // We cannot iterate over keys without sorting + // because we handle categorical values in alphabetical order __ignoring case__ + // while Object.keys() _usually_ is ordered alphabetically where all upper characters are less than lowercase (A, B, C, a, b, c) + const [colors, scaleMap] = Object.keys(userColors[category]) + .sort((a, b) => { + a = a.toLowerCase(); + b = b.toLowerCase(); + if (a === b) return 0; + if (a > b) return 1; + return -1; + }) + .reduce( + (acc, label, i) => { + const color = parseRGB(userColors[category][label]); + acc[0][label] = color; + acc[1][i] = d3.rgb(255 * color[0], 255 * color[1], 255 * color[2]); + return acc; + }, + [{}, {}] + ); const scale = (i) => scaleMap[i]; convertedUserColors[category] = { colors, scale }; });