From ec75e11ee0d2d469ab41eab6f73506d9333738c3 Mon Sep 17 00:00:00 2001 From: Paul Bui-Quang Date: Wed, 3 Jul 2024 16:43:30 +0200 Subject: [PATCH] feat(locationMap): add info on map + better color palette management --- .../Dashboard/Preview/LocationMap/index.tsx | 26 +++++++++++-------- .../Dashboard/Preview/LocationMap/utils.ts | 11 ++++++++ src/components/Dashboard/Preview/Preview.tsx | 10 ++++++- 3 files changed, 35 insertions(+), 12 deletions(-) diff --git a/src/components/Dashboard/Preview/LocationMap/index.tsx b/src/components/Dashboard/Preview/LocationMap/index.tsx index 06f1c99b3..cdb66087a 100644 --- a/src/components/Dashboard/Preview/LocationMap/index.tsx +++ b/src/components/Dashboard/Preview/LocationMap/index.tsx @@ -13,6 +13,7 @@ import { _explodeBoundsIntoMesh, colorize, computeNearFilter, + getColorPalette, isBoundCovered, parseShape, uncoveredBoundMeshUnits @@ -68,6 +69,7 @@ const IrisZones = (props: IrisZonesProps) => { const [loadedBounds, setLoadedBounds] = useState([]) const [maxCount, setMaxCount] = useState(MAX_COUNT_DEFAULT) const [zoneOpacity, setZoneOpacity] = useState(ZONE_COLOR_OPACITY * 100) + const colorPalette = getColorPalette(COLOR_PALETTE, maxCount) const updateBounds = useCallback((newBounds: LatLngBounds) => { setBounds((prevBounds) => { @@ -188,11 +190,13 @@ const IrisZones = (props: IrisZonesProps) => { const newVisibleZones = zones.filter((zone) => zone.shape.find((shape) => bounds?.contains(shape))) setVisibleZones(newVisibleZones) const maxCount = - d3.quantile( - newVisibleZones.map((zone) => zone.meta.count), - MAX_COUNT_QUANTILE - ) || Math.max(...newVisibleZones.map((zone) => zone.meta.count)) - setMaxCount(maxCount) + newVisibleZones.length === 0 + ? MAX_COUNT_DEFAULT + : d3.quantile( + newVisibleZones.map((zone) => zone.meta.count), + MAX_COUNT_QUANTILE + ) || Math.max(...newVisibleZones.map((zone) => zone.meta.count)) + setMaxCount(Math.floor(maxCount)) }, [zones, bounds]) /***************************************************** */ @@ -286,7 +290,7 @@ const IrisZones = (props: IrisZonesProps) => { { ))} - + ) } -const MapLegend = (props: { maxCount: number }) => { - const { maxCount } = props +const MapLegend = (props: { maxCount: number; colorPalette: string[] }) => { + const { maxCount, colorPalette } = props return (
{
Densité
- {COLOR_PALETTE.map((color, i) => ( + {colorPalette.map((color, i) => (
))}
@@ -337,7 +341,7 @@ const MapLegend = (props: { maxCount: number }) => { }} >
1
-
{Math.max(Math.round(maxCount / 10), 1) * 10}+
+
{maxCount}+
diff --git a/src/components/Dashboard/Preview/LocationMap/utils.ts b/src/components/Dashboard/Preview/LocationMap/utils.ts index 1bdde1152..49918cbfe 100644 --- a/src/components/Dashboard/Preview/LocationMap/utils.ts +++ b/src/components/Dashboard/Preview/LocationMap/utils.ts @@ -92,6 +92,17 @@ export const uncoveredBoundMeshUnits = (map: L.Map, bounds: LatLngBounds, loaded return boundMesh.filter((b) => !loadedBounds.some((lb) => lb.contains(b))) } +export const getColorPalette = (colors: string[], maxCount: number): string[] => { + if (maxCount >= colors.length) { + return colors + } + const colorPalette: string[] = [colors[0]] + for (let i = 1; i < maxCount; i++) { + colorPalette.push(colors[i]) + } + return colorPalette +} + /** * Colorize a count based on a color palette */ diff --git a/src/components/Dashboard/Preview/Preview.tsx b/src/components/Dashboard/Preview/Preview.tsx index 3ae09c631..4f43c4395 100644 --- a/src/components/Dashboard/Preview/Preview.tsx +++ b/src/components/Dashboard/Preview/Preview.tsx @@ -8,7 +8,9 @@ import { Table, TableBody, TableContainer, - Typography + Typography, + IconButton, + Tooltip } from '@mui/material' import { TableCellWrapper } from 'components/ui/TableCell/styles' @@ -17,6 +19,7 @@ import BarChart from './Charts/BarChart' import GroupedBarChart from './Charts/GroupedBarChart' import DonutChart from './Charts/DonutChart' import PyramidChart from './Charts/PyramidChart' +import InfoIcon from '@mui/icons-material/Info' import useStyles from './styles' import clsx from 'clsx' @@ -239,6 +242,11 @@ const Preview: React.FC = ({ Répartition spatiale par zone IRIS + + + + +