Skip to content

Commit

Permalink
fix(locationMap): duplicate zones + color palette for low max
Browse files Browse the repository at this point in the history
  • Loading branch information
pl-buiquang authored and Mehdi-BOUYAHIA committed Jul 4, 2024
1 parent 126c333 commit 3946210
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 22 deletions.
44 changes: 24 additions & 20 deletions src/components/Dashboard/Preview/LocationMap/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ const IrisZones = (props: IrisZonesProps) => {
const [dataLoadingProgress, setDataLoadingProgress] = useState(0)
const [zones, setZones] = useState<Array<{ shape: LatLngTuple[]; meta: { count: number; name: string } }>>([])
const [visibleZones, setVisibleZones] = useState<
Array<{ shape: LatLngTuple[]; meta: { count: number; name: string } }>
Array<{ shape: LatLngTuple[]; meta: { count: number; name: string; id?: string } }>
>([])
const [bounds, setBounds] = useState<LatLngBounds | null>(null)
const [loadedBounds, setLoadedBounds] = useState<LatLngBounds[]>([])
Expand Down Expand Up @@ -147,6 +147,7 @@ const IrisZones = (props: IrisZonesProps) => {
const existingNames = existingZones.map((zone) => zone.meta.name)
const newZones = existingZones.concat(
locations
.filter((ft, i) => locations.findIndex((f) => ft.id === f.id) === i) // to filter out duplicates
.filter((ft) => ft.name && existingNames.indexOf(ft.name) === -1)
.map((ft) => {
return {
Expand All @@ -156,7 +157,8 @@ const IrisZones = (props: IrisZonesProps) => {
) || [],
meta: {
count: ft.extension?.find((ext) => ext.url === LOCATION_COUNT_EXTENSION_URL)?.valueInteger || 0,
name: ft.name || ''
name: ft.name || '',
id: ft.id
}
}
})
Expand Down Expand Up @@ -286,24 +288,26 @@ const IrisZones = (props: IrisZonesProps) => {
))}
</div>
)}
{visibleZones.map((zone, i) => (
<Polygon
key={i}
pathOptions={{
color: colorize(colorPalette, zone.meta.count, maxCount),
opacity: (BORDER_RELATIVE_OPACITY * zoneOpacity) / 100,
fillOpacity: zoneOpacity / 100
}}
positions={zone.shape}
>
<Tooltip sticky>
<div>
<b>{zone.meta.name}</b>
</div>
<div>Total: {zone.meta.count}</div>
</Tooltip>
</Polygon>
))}
{visibleZones.map((zone, i) => {
return (
<Polygon
key={i}
pathOptions={{
color: colorize(colorPalette, zone.meta.count, maxCount),
opacity: (BORDER_RELATIVE_OPACITY * zoneOpacity) / 100,
fillOpacity: zoneOpacity / 100
}}
positions={zone.shape}
>
<Tooltip sticky>
<div>
<b>{zone.meta.name}</b>
</div>
<div>Total: {zone.meta.count}</div>
</Tooltip>
</Polygon>
)
})}
<MapLegend maxCount={maxCount} colorPalette={colorPalette} />
</div>
)
Expand Down
8 changes: 6 additions & 2 deletions src/components/Dashboard/Preview/LocationMap/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,12 @@ export const getColorPalette = (colors: string[], maxCount: number): string[] =>
if (maxCount >= colors.length) {
return colors
}
if (maxCount === 2) {
return [colors[0], colors[colors.length - 1]]
}
const colorPalette: string[] = [colors[0]]
for (let i = 1; i < maxCount; i++) {
const inc = Math.floor(colors.length / maxCount)
for (let i = inc; i < colors.length; i += inc) {
colorPalette.push(colors[i])
}
return colorPalette
Expand All @@ -108,7 +112,7 @@ export const getColorPalette = (colors: string[], maxCount: number): string[] =>
*/
export const colorize = (colorPalette: string[], count: number, maxCount: number): string => {
const step = maxCount / colorPalette.length
const colorIndex = Math.floor(count / step)
const colorIndex = Math.floor((count - 0.1) / step)
if (colorIndex >= colorPalette.length) {
return colorPalette[colorPalette.length - 1]
}
Expand Down

0 comments on commit 3946210

Please sign in to comment.