Skip to content

Commit

Permalink
Continue refactoring to un-hardcode indicators
Browse files Browse the repository at this point in the history
  • Loading branch information
yongrenjie committed Nov 3, 2023
1 parent 04de06b commit b43c48d
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 20 deletions.
7 changes: 7 additions & 0 deletions web/src/data/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ const allIndicators: Map<IndicatorName, Indicator> = new Map([
["air_quality", {
// This is an actual prose name of the indicator used in the UI.
"short": "Air pollution",
// This is used in the map hover text. Generally you'll want this to be
// the same as 'short', but in the original DemoLand app 'accessibility'
// is condensed to 'access.' to save space.
"hover": "Air pollution",
// 'less' and 'more' are used in the charts to describe what smaller and
// larger values mean respectively.
"less": "cleaner",
Expand All @@ -87,6 +91,7 @@ const allIndicators: Map<IndicatorName, Indicator> = new Map([
}],
["house_price", {
"short": "House prices",
"hover": "House prices",
"less": "cheaper",
"more": "more expensive",
"less_diff": "decreased",
Expand All @@ -96,6 +101,7 @@ const allIndicators: Map<IndicatorName, Indicator> = new Map([
}],
["job_accessibility", {
"short": "Job accessibility",
"hover": "Job access.",
"less": "lower",
"more": "higher",
"less_diff": "decreased",
Expand All @@ -105,6 +111,7 @@ const allIndicators: Map<IndicatorName, Indicator> = new Map([
}],
["greenspace_accessibility", {
"short": "Greenspace accessibility",
"hover": "Greenspace access.",
"less": "lower",
"more": "higher",
"less_diff": "decreased",
Expand Down
2 changes: 1 addition & 1 deletion web/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

export type IndicatorName = "air_quality" | "house_price" | "job_accessibility" | "greenspace_accessibility";

export type Indicator = { short: string, less: string, more: string, less_diff: string, more_diff: string, colormap: string, colormapReversed: boolean };
export type Indicator = { short: string, hover: string, less: string, more: string, less_diff: string, more_diff: string, colormap: string, colormapReversed: boolean };

/* Model inputs (land use) */

Expand Down
18 changes: 8 additions & 10 deletions web/src/utils/colors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import colormap from "colormap";
import { type IndicatorName, type LayerName, } from "src/types";
import config from "src/data/config";

export function makeColormap(indicator: IndicatorName | "diff", n: number) {
export function makeColormap(indicator: IndicatorName | "diff", n: number): string[] {
if (indicator === "diff") {
return colormap({
colormap: "RdBu",
Expand All @@ -23,12 +23,11 @@ export function makeColormap(indicator: IndicatorName | "diff", n: number) {
}
}

const colormaps: { [key: string]: string[] } = {
"air_quality": makeColormap("air_quality", 100),
"house_price": makeColormap("house_price", 100),
"job_accessibility": makeColormap("job_accessibility", 100),
"greenspace_accessibility": makeColormap("greenspace_accessibility", 100),
"diff": makeColormap("diff", 100),
const colormaps: Map<IndicatorName | "diff", string[]> = new Map([
["diff", makeColormap("diff", 100)],
])
for (const indiName of config.allIndicators.keys()) {
colormaps.set(indiName, makeColormap(indiName, 100));
}

function getColorFromMap(map: string[], value: number, min: number, max: number) {
Expand All @@ -46,14 +45,13 @@ function getColorFromMap(map: string[], value: number, min: number, max: number)
* @param value The value of the layer in the scenario
*/
export function getColor(layerName: LayerName, value: number) {

if (layerName === "signature_type") {
// Categorical variable
return config.signatures[value].color;
}
else {
// Continuous variables, use the respective colormaps
return getColorFromMap(colormaps[layerName], value, config.scale.min, config.scale.max);
return getColorFromMap(colormaps.get(layerName), value, config.scale.min, config.scale.max);
}
}

Expand Down Expand Up @@ -81,6 +79,6 @@ export function getDiffColor(layerName: LayerName, value: number, cmpValue: numb
// Continuous variables, use 'diff' colormap
return value === cmpValue
? "rgba(0, 0, 0, 0.1)"
: getColorFromMap(colormaps["diff"], value - cmpValue, -maxDiffExtents.get(layerName), maxDiffExtents.get(layerName));
: getColorFromMap(colormaps.get("diff"), value - cmpValue, -maxDiffExtents.get(layerName), maxDiffExtents.get(layerName));
}
}
5 changes: 1 addition & 4 deletions web/src/utils/hover.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,7 @@ function makeHoverHtml(feat: GeoJSON.Feature,
}${Math.abs(chg).toFixed(1)}%)`;
}
return [
`<span>${indi.short.replace(
"accessibility",
"access."
)}</span>`,
`<span>${indi.hover}</span>`,
`<span class="right-align-grid-item ${activeFactor === name ? "strong" : ""
}">`,
activeFactor === name ? `${makeColoredBlock(color)}&nbsp;` : ``,
Expand Down
12 changes: 7 additions & 5 deletions web/src/utils/scenarios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,14 @@ function fromValuesObject(
valuesMap.set(oa, new Map());
foundAreaNames.add(oa);
for (const [key, value] of Object.entries(map)) {
if (value === null) {
throw new Error(`Null value found in scenario values for OA '${oa}'.${src}`);
}
const layerName = key as LayerName;
valuesMap.get(oa)
.set(layerName, rescale(layerName, preprocess(value as number), scaleFactors));
if (config.allLayers.has(layerName)) {
if (value === null) {
throw new Error(`Null value found in scenario values for OA '${oa}'.${src}`);
}
valuesMap.get(oa)
.set(layerName, rescale(layerName, preprocess(value as number), scaleFactors));
}
}
}
}
Expand Down

0 comments on commit b43c48d

Please sign in to comment.