-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #843 from CodeForAfrica/ft/hurumap-choropleth-map
Choropleth Map
- Loading branch information
Showing
7 changed files
with
264 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { Typography, Grid } from "@mui/material"; | ||
|
||
export default function Legend({ legend, title = "Average Temperature", sx }) { | ||
return ( | ||
<Grid | ||
spacing={2} | ||
sx={(theme) => ({ | ||
position: "absolute", | ||
zIndex: 1000, | ||
width: "fit-content", | ||
bottom: theme.spacing(30), | ||
right: theme.spacing(20), | ||
backgroundColor: theme.palette.background.paper, | ||
padding: theme.spacing(1), | ||
boxShadow: theme.shadows[3], | ||
...sx, | ||
})} | ||
> | ||
<Typography | ||
variant="subtitle2" | ||
sx={(theme) => ({ | ||
color: theme.palette.text.primary, | ||
marginBottom: theme.spacing(2), | ||
})} | ||
> | ||
{title} | ||
</Typography> | ||
{legend?.map(({ min, max, color }) => ( | ||
<Grid | ||
container | ||
key={`${min}-${max}`} | ||
alignItems="center" | ||
spacing={1} | ||
sx={(theme) => ({ | ||
padding: theme.spacing(1), | ||
})} | ||
> | ||
<Grid | ||
item | ||
sx={(theme) => ({ | ||
backgroundColor: color, | ||
width: theme.spacing(4), | ||
height: theme.spacing(4), | ||
})} | ||
/> | ||
<Grid item xs> | ||
<Typography | ||
variant="subtitle1" | ||
sx={(theme) => ({ color: theme.palette.text.primary })} | ||
> | ||
{min} - {max} | ||
</Typography> | ||
</Grid> | ||
</Grid> | ||
))} | ||
</Grid> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
/* eslint-disable import/prefer-default-export */ | ||
|
||
import { defaultChoroplethStyles } from "./geoStyles"; | ||
|
||
const roundToNearestHalf = (num) => { | ||
return Math.round(num * 2) / 2; | ||
}; | ||
|
||
const calculateThresholds = (min, max, steps) => { | ||
const stepSize = (max - min) / steps; | ||
const thresholds = []; | ||
|
||
for (let i = 0; i < steps; i += 1) { | ||
thresholds.push({ | ||
min: roundToNearestHalf(min + i * stepSize), | ||
max: roundToNearestHalf(min + (i + 1) * stepSize), | ||
}); | ||
} | ||
|
||
return thresholds; | ||
}; | ||
|
||
const generateLegend = ( | ||
min, | ||
max, | ||
positiveThresholds, | ||
positiveColorRange, | ||
negativeThresholds, | ||
negativeColorRange, | ||
zeroColor, | ||
) => { | ||
const legend = []; | ||
|
||
if (negativeThresholds.length) { | ||
negativeThresholds.forEach((threshold, index) => { | ||
legend.push({ | ||
min: threshold.min, | ||
max: threshold.max, | ||
color: negativeColorRange[index], | ||
}); | ||
}); | ||
} | ||
|
||
if (min <= 0 && max >= 0) { | ||
legend.push({ | ||
min: 0, | ||
max: 0, | ||
color: zeroColor, | ||
}); | ||
} | ||
|
||
if (positiveThresholds.length) { | ||
positiveThresholds.forEach((threshold, index) => { | ||
legend.push({ | ||
min: threshold.min, | ||
max: threshold.max, | ||
color: positiveColorRange[index], | ||
}); | ||
}); | ||
} | ||
|
||
return legend; | ||
}; | ||
|
||
export const generateChoropleth = (colors, locations, mapType) => { | ||
if (mapType !== "choropleth") return null; | ||
|
||
const filteredLocations = locations.filter(({ count }) => count !== null); | ||
const counts = filteredLocations.map(({ count }) => count); | ||
const hasNegativeValues = counts.some((count) => count < 0); | ||
const hasPositiveValues = counts.some((count) => count > 0); | ||
const maxCount = Math.max(...counts); | ||
const minCount = Math.min(...counts); | ||
const roundedMinCount = Math.floor(minCount); | ||
const roundedMaxCount = Math.ceil(maxCount); | ||
|
||
const negativeColorRange = | ||
colors?.negative_color_range || | ||
defaultChoroplethStyles.negative_color_range; | ||
const positiveColorRange = | ||
colors?.positive_color_range || | ||
defaultChoroplethStyles.positive_color_range; | ||
const zeroColor = colors?.zero_color || defaultChoroplethStyles.zero_color; | ||
const opacity = colors?.opacity || defaultChoroplethStyles.opacity; | ||
|
||
const positiveThresholds = hasPositiveValues | ||
? calculateThresholds( | ||
roundedMinCount, | ||
roundedMaxCount, | ||
positiveColorRange.length, | ||
) | ||
: []; | ||
const negativeThresholds = hasNegativeValues | ||
? calculateThresholds( | ||
roundedMinCount, | ||
roundedMaxCount, | ||
negativeColorRange.length, | ||
) | ||
: []; | ||
|
||
const legend = generateLegend( | ||
roundedMinCount, | ||
roundedMaxCount, | ||
positiveThresholds, | ||
positiveColorRange, | ||
negativeThresholds, | ||
negativeColorRange, | ||
zeroColor, | ||
); | ||
|
||
const getColor = (count) => { | ||
if (count === 0) return zeroColor; | ||
const colorRange = count > 0 ? positiveColorRange : negativeColorRange; | ||
const thresholds = count > 0 ? positiveThresholds : negativeThresholds; | ||
const index = thresholds.findIndex( | ||
(threshold) => count >= threshold.min && count < threshold.max, | ||
); | ||
return colorRange[index]; | ||
}; | ||
|
||
const choroplethData = filteredLocations.map(({ code, count }) => { | ||
const color = getColor(count); | ||
|
||
return { | ||
code, | ||
count, | ||
fillColor: color, | ||
opacity, | ||
}; | ||
}); | ||
|
||
return { choropleth: choroplethData, legend }; | ||
}; |