From 2c77a90a479376e9001fea44acd9536c2f1ba621 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment?= Date: Wed, 15 Nov 2023 11:15:48 +0100 Subject: [PATCH 1/4] fix: use new colors to map & doughnut --- .../components/charts/doughnut/Doughnut.tsx | 14 ++++++++-- webapp-next/components/charts/map/Legends.tsx | 16 +++++------ webapp-next/utils/map/props.ts | 27 ++++++++++++++----- webapp-next/utils/tools.ts | 15 ++++++++--- 4 files changed, 52 insertions(+), 20 deletions(-) diff --git a/webapp-next/components/charts/doughnut/Doughnut.tsx b/webapp-next/components/charts/doughnut/Doughnut.tsx index ed4d7a9..afe396a 100644 --- a/webapp-next/components/charts/doughnut/Doughnut.tsx +++ b/webapp-next/components/charts/doughnut/Doughnut.tsx @@ -1,7 +1,11 @@ import { doughnutProps } from '@/utils/chartjs/props'; import { Cm2dContext } from '@/utils/cm2d-provider'; import { orders, sortByOrder } from '@/utils/orders'; -import { capitalizeString, getLabelFromKey } from '@/utils/tools'; +import { + capitalizeString, + chartsAvailableColors, + getLabelFromKey +} from '@/utils/tools'; import { useContext, useEffect, useState } from 'react'; import { Doughnut } from 'react-chartjs-2'; @@ -45,11 +49,17 @@ export const ChartDoughnut = (props: Props) => { label = getLabelFromKey(ds.label.toString()); } + const colors = yValues.map( + (_, index) => + chartsAvailableColors[index % chartsAvailableColors.length] + ); + return { label: capitalizeString(label), data: yValues, fill: true, - borderRadius: 10 + borderRadius: 10, + backgroundColor: colors }; }) ); diff --git a/webapp-next/components/charts/map/Legends.tsx b/webapp-next/components/charts/map/Legends.tsx index a5db16f..a097a91 100644 --- a/webapp-next/components/charts/map/Legends.tsx +++ b/webapp-next/components/charts/map/Legends.tsx @@ -3,23 +3,23 @@ import { Box, Flex, Text } from '@chakra-ui/react'; export function Legends() { const legends = [ { - color: 'green.100', - borderColor: 'green.300', + color: '#c9e7c8', + borderColor: '#4daf4a', content: '0-10%' }, { - color: 'blue.100', - borderColor: 'blue.300', + color: '#c3d8e9', + borderColor: '#377eb8', content: '10-20%' }, { - color: 'orange.100', - borderColor: 'orange.300', + color: '#ffd8b2', + borderColor: '#ff7f00', content: '20-30%' }, { - color: 'red.100', - borderColor: 'red.300', + color: '#f6baba', + borderColor: '#e41a1c', content: '30%+' } ]; diff --git a/webapp-next/utils/map/props.ts b/webapp-next/utils/map/props.ts index 5da6aeb..5c72bcc 100644 --- a/webapp-next/utils/map/props.ts +++ b/webapp-next/utils/map/props.ts @@ -1,5 +1,5 @@ import { orders, sortByOrder } from '../orders'; -import { capitalizeString, getLabelFromKey, isNC } from '../tools'; +import { capitalizeString, getLabelFromKey, hexToRGB, isNC } from '../tools'; export const getMapProps = ( id: string, @@ -33,11 +33,26 @@ export const getMapProps = ( } const stateColors = { - GREEN: { initial: '#C6F6D5', hover: '#68D391' }, - BLUE: { initial: '#E9F1FF', hover: '#A7C4FD' }, - ORANGE: { initial: '#FEEBCB', hover: '#F8AB4E' }, - RED: { initial: '#FED7D7', hover: '#FC8181' }, - NEUTRAL: { initial: '#EDF2F7', hover: '#CBD5E0' } + GREEN: { + initial: '#c9e7c8', + hover: '#4daf4a' + }, + BLUE: { + initial: '#c3d8e9', + hover: '#377eb8' + }, + ORANGE: { + initial: '#ffd8b2', + hover: '#ff7f00' + }, + RED: { + initial: '#f6baba', + hover: '#e41a1c' + }, + NEUTRAL: { + initial: '#e0e0e0', + hover: '#999999' + } }; const getCountFromKey = (key: number): number => { diff --git a/webapp-next/utils/tools.ts b/webapp-next/utils/tools.ts index 7e42e9c..93185ce 100644 --- a/webapp-next/utils/tools.ts +++ b/webapp-next/utils/tools.ts @@ -384,7 +384,7 @@ export function getLastDayOfMonth(date: Date): Date { return lastDay; } -const availableColors = [ +export const chartsAvailableColors = [ '#e41a1c', // Bright red '#377eb8', // Vivid blue '#4daf4a', // Strong green @@ -408,9 +408,16 @@ const availableColors = [ ]; export function getRandomColor(index?: number): string { let sIndex = index; - if (sIndex === undefined || sIndex >= availableColors.length) - sIndex = Math.floor(Math.random() * availableColors.length); - return availableColors[sIndex]; + if (sIndex === undefined || sIndex >= chartsAvailableColors.length) + sIndex = Math.floor(Math.random() * chartsAvailableColors.length); + return chartsAvailableColors[sIndex]; +} + +export function hexToRGB(hex: string): string { + let r = parseInt(hex.slice(1, 3), 16); + let g = parseInt(hex.slice(3, 5), 16); + let b = parseInt(hex.slice(5, 7), 16); + return `${r}, ${g}, ${b}`; } export function isStringContainingDate(str: string): boolean { From d6ce66f4a204380df4027a7e2ff3c2469568c3c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment?= Date: Wed, 15 Nov 2023 17:53:59 +0100 Subject: [PATCH 2/4] feat: map details --- webapp-next/components/charts/map/Map.tsx | 47 +++++++++------ .../components/charts/map/MapDetails.tsx | 59 +++++++++++++++++++ .../map/{Legends.tsx => MapLegends.tsx} | 2 +- webapp-next/utils/map/props.ts | 17 ++++-- webapp-next/utils/tools.ts | 10 ++++ 5 files changed, 112 insertions(+), 23 deletions(-) create mode 100644 webapp-next/components/charts/map/MapDetails.tsx rename webapp-next/components/charts/map/{Legends.tsx => MapLegends.tsx} (96%) diff --git a/webapp-next/components/charts/map/Map.tsx b/webapp-next/components/charts/map/Map.tsx index 11a3361..b6d66ac 100644 --- a/webapp-next/components/charts/map/Map.tsx +++ b/webapp-next/components/charts/map/Map.tsx @@ -1,8 +1,10 @@ +import { Cm2dContext } from '@/utils/cm2d-provider'; import { getMapProps } from '@/utils/map/props'; +import { MapConfig } from '@/utils/map/type'; import { Flex } from '@chakra-ui/react'; -import React, { useContext } from 'react'; -import { Legends } from './Legends'; -import { Cm2dContext } from '@/utils/cm2d-provider'; +import React, { useContext, useState } from 'react'; +import { MapDetails } from './MapDetails'; +import { MapLegends } from './MapLegends'; type Props = { id: string; @@ -12,6 +14,7 @@ type Props = { export default function MapIframe(props: Props) { const iframeRef = React.useRef(null); const { datasets, id } = props; + const [mapConfig, setMapConfig] = useState(null); const context = useContext(Cm2dContext); if (!context) { @@ -25,6 +28,7 @@ export default function MapIframe(props: Props) { if (iframe) { const doc = (iframe as any).contentWindow.document; + const mapProps = getMapProps(id, datasets, saveAggregateX); doc.open(); doc.write(` @@ -35,13 +39,13 @@ export default function MapIframe(props: Props) { }, true); @@ -50,6 +54,8 @@ export default function MapIframe(props: Props) { `); doc.close(); + + if (mapProps.config) setMapConfig(mapProps.config); } }; @@ -65,19 +71,26 @@ export default function MapIframe(props: Props) { return ( - + + + +