Skip to content

Commit b863de3

Browse files
Add prop in map to allow zoom in and out on highlight
1 parent 7e88df2 commit b863de3

File tree

9 files changed

+101
-42
lines changed

9 files changed

+101
-42
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@undp/data-viz",
3-
"version": "2.2.1",
3+
"version": "2.2.2",
44
"main": "./dist/index.cjs",
55
"module": "./dist/index.js",
66
"browser": "./dist/index.umd.js",

src/App.tsx

Lines changed: 35 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,43 @@
11
import '@undp/design-system-react/style.css';
22
import './styles/styles.css';
3-
import { DotDensityMap } from './Components/Graphs/Maps/DotDensityMap';
3+
import { A, H3, P } from '@undp/design-system-react/Typography';
44

55
function App() {
66
return (
7-
<DotDensityMap
8-
data={[
9-
{
10-
lat: 20,
11-
long: 10,
12-
},
13-
{
14-
lat: 25,
15-
long: 26,
16-
},
17-
{
18-
lat: 0,
19-
long: 0,
20-
},
21-
{
22-
lat: 15,
23-
long: 5,
24-
},
25-
{
26-
lat: 10,
27-
long: 20,
28-
},
29-
]}
30-
onSeriesMouseClick={function YJ() {}}
31-
onSeriesMouseOver={function YJ() {}}
32-
/>
7+
<div
8+
style={{
9+
height: '90vh',
10+
maxWidth: '712px',
11+
margin: '0 auto',
12+
padding: '2rem',
13+
display: 'flex',
14+
flexDirection: 'column',
15+
justifyContent: 'center',
16+
alignItems: 'center',
17+
}}
18+
>
19+
<img width='56' alt='undp-logo' src='/undp-logo-blue.svg' />
20+
<H3 style={{ textAlign: 'center', paddingTop: '24px' }}>UNDP Data Visualization Library</H3>
21+
<P style={{ textAlign: 'center' }}>
22+
This visualization library for react, developed by the United Nations Development Programme,
23+
offers various interactive visualizations such as graphs, maps, and animated charts. You can
24+
access the documentation{' '}
25+
<A href='https://dataviz.design.undp.org/' target='_blank' rel='noreferrer'>
26+
here
27+
</A>
28+
.
29+
</P>
30+
<P
31+
style={{
32+
textAlign: 'center',
33+
}}
34+
>
35+
For any feedback or inquiries, please feel free to reach out to us at{' '}
36+
<A href='mailto:data@undp.org' target='_blank' rel='noreferrer'>
37+
data@undp.org
38+
</A>
39+
</P>
40+
</div>
3341
);
3442
}
3543

src/Components/Dashboard/GraphEl.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -903,6 +903,7 @@ function GraphEl(props: Props) {
903903
classNames: settings?.classNames,
904904
zoomInteraction: settings?.zoomInteraction,
905905
animate: settings?.animate,
906+
zoomAndCenterByHighlightedIds: settings?.zoomAndCenterByHighlightedIds,
906907
};
907908
case 'biVariateChoroplethMap':
908909
return {
@@ -962,6 +963,7 @@ function GraphEl(props: Props) {
962963
classNames: settings?.classNames,
963964
zoomInteraction: settings?.zoomInteraction,
964965
animate: settings?.animate,
966+
zoomAndCenterByHighlightedIds: settings?.zoomAndCenterByHighlightedIds,
965967
};
966968
case 'dotDensityMap':
967969
return {

src/Components/Graphs/Maps/BiVariateMap/Graph.tsx

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ interface Props {
7373
animate: AnimateDataType;
7474
dimmedOpacity: number;
7575
customLayers: CustomLayerDataType[];
76+
zoomAndCenterByHighlightedIds: boolean;
7677
}
7778

7879
export function Graph(props: Props) {
@@ -109,6 +110,7 @@ export function Graph(props: Props) {
109110
dimmedOpacity,
110111
customLayers,
111112
collapseColorScaleByDefault,
113+
zoomAndCenterByHighlightedIds,
112114
} = props;
113115
const [showLegend, setShowLegend] = useState(
114116
collapseColorScaleByDefault === undefined ? !(width < 680) : !collapseColorScaleByDefault,
@@ -165,10 +167,29 @@ export function Graph(props: Props) {
165167
// eslint-disable-next-line react-hooks/exhaustive-deps
166168
}, [height, width, zoomInteraction]);
167169

168-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
169-
const bounds = bbox(mapData as any);
170-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
171-
const center = centerOfMass(mapData as any);
170+
const bounds = bbox({
171+
...mapData,
172+
features: zoomAndCenterByHighlightedIds
173+
? mapData.features.filter(
174+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
175+
(d: any) =>
176+
(highlightedIds || []).length === 0 ||
177+
highlightedIds.indexOf(d.properties[mapProperty]) !== -1,
178+
)
179+
: mapData.features,
180+
});
181+
182+
const center = centerOfMass({
183+
...mapData,
184+
features: zoomAndCenterByHighlightedIds
185+
? mapData.features.filter(
186+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
187+
(d: any) =>
188+
(highlightedIds || []).length === 0 ||
189+
highlightedIds.indexOf(d.properties[mapProperty]) !== -1,
190+
)
191+
: mapData.features,
192+
});
172193
const lonDiff = bounds[2] - bounds[0];
173194
const latDiff = bounds[3] - bounds[1];
174195
const scaleX = (((width * 190) / 960) * 360) / lonDiff;

src/Components/Graphs/Maps/BiVariateMap/index.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ interface Props {
9494
isWorldMap?: boolean;
9595
/** Map projection type */
9696
mapProjection?: MapProjectionTypes;
97+
/** Toggle if the map is centered and zoomed to the highlighted ids. */
98+
zoomAndCenterByHighlightedIds?: boolean;
9799
/** Extend of the allowed zoom in the map */
98100
zoomScaleExtend?: [number, number];
99101
/** Extend of the allowed panning in the map */
@@ -197,6 +199,7 @@ export function BiVariateChoroplethMap(props: Props) {
197199
customLayers = [],
198200
timeline = { enabled: false, autoplay: false, showOnlyActiveDate: true },
199201
collapseColorScaleByDefault,
202+
zoomAndCenterByHighlightedIds = false,
200203
} = props;
201204

202205
const [svgWidth, setSvgWidth] = useState(0);
@@ -402,6 +405,7 @@ export function BiVariateChoroplethMap(props: Props) {
402405
dimmedOpacity={dimmedOpacity}
403406
customLayers={customLayers}
404407
collapseColorScaleByDefault={collapseColorScaleByDefault}
408+
zoomAndCenterByHighlightedIds={zoomAndCenterByHighlightedIds}
405409
/>
406410
) : (
407411
<div

src/Components/Graphs/Maps/ChoroplethMap/Graph.tsx

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ interface Props {
6969
dimmedOpacity: number;
7070
customLayers: CustomLayerDataType[];
7171
collapseColorScaleByDefault?: boolean;
72+
zoomAndCenterByHighlightedIds: boolean;
7273
}
7374

7475
export function Graph(props: Props) {
@@ -104,6 +105,7 @@ export function Graph(props: Props) {
104105
dimmedOpacity,
105106
customLayers,
106107
collapseColorScaleByDefault,
108+
zoomAndCenterByHighlightedIds,
107109
} = props;
108110
const [selectedColor, setSelectedColor] = useState<string | undefined>(undefined);
109111
const zoomRef = useRef<ZoomBehavior<SVGSVGElement, unknown> | null>(null);
@@ -165,10 +167,29 @@ export function Graph(props: Props) {
165167
// eslint-disable-next-line react-hooks/exhaustive-deps
166168
}, [height, width, zoomInteraction]);
167169

168-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
169-
const bounds = bbox(mapData as any);
170-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
171-
const center = centerOfMass(mapData as any);
170+
const bounds = bbox({
171+
...mapData,
172+
features: zoomAndCenterByHighlightedIds
173+
? mapData.features.filter(
174+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
175+
(d: any) =>
176+
(highlightedIds || []).length === 0 ||
177+
highlightedIds.indexOf(d.properties[mapProperty]) !== -1,
178+
)
179+
: mapData.features,
180+
});
181+
182+
const center = centerOfMass({
183+
...mapData,
184+
features: zoomAndCenterByHighlightedIds
185+
? mapData.features.filter(
186+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
187+
(d: any) =>
188+
(highlightedIds || []).length === 0 ||
189+
highlightedIds.indexOf(d.properties[mapProperty]) !== -1,
190+
)
191+
: mapData.features,
192+
});
172193
const lonDiff = bounds[2] - bounds[0];
173194
const latDiff = bounds[3] - bounds[1];
174195
const scaleX = (((width * 190) / 960) * 360) / lonDiff;

src/Components/Graphs/Maps/ChoroplethMap/index.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ interface Props {
8080
mapData?: any;
8181
/** Scaling factor for the map. Multiplies the scale number to scale. */
8282
scale?: number;
83+
/** Toggle if the map is centered and zoomed to the highlighted ids. */
84+
zoomAndCenterByHighlightedIds?: boolean;
8385
/** Center point of the map */
8486
centerPoint?: [number, number];
8587
/** Defines the zoom mode for the map */
@@ -196,6 +198,7 @@ export function ChoroplethMap(props: Props) {
196198
customLayers = [],
197199
timeline = { enabled: false, autoplay: false, showOnlyActiveDate: true },
198200
collapseColorScaleByDefault,
201+
zoomAndCenterByHighlightedIds = false,
199202
} = props;
200203
const [svgWidth, setSvgWidth] = useState(0);
201204
const [svgHeight, setSvgHeight] = useState(0);
@@ -396,6 +399,7 @@ export function ChoroplethMap(props: Props) {
396399
: animate || { duration: 0, once: true, amount: 0 }
397400
}
398401
customLayers={customLayers}
402+
zoomAndCenterByHighlightedIds={zoomAndCenterByHighlightedIds}
399403
collapseColorScaleByDefault={collapseColorScaleByDefault}
400404
/>
401405
) : (

src/Types.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -474,8 +474,7 @@ export interface DashboardColumnDataType extends DashboardColumnDataWithoutGraph
474474
graphType: GraphType;
475475
}
476476

477-
export interface PerformanceIntensiveDashboardColumnDataType
478-
extends DashboardColumnDataWithoutGraphType {
477+
export interface PerformanceIntensiveDashboardColumnDataType extends DashboardColumnDataWithoutGraphType {
479478
graphType: GraphType | ThreeDGraphType | GeoHubGraphType;
480479
}
481480

@@ -517,8 +516,7 @@ export interface DashboardLayoutDataType extends DashboardLayoutDataWithoutRowsT
517516
height?: number;
518517
}[];
519518
}
520-
export interface PerformanceIntensiveDashboardLayoutDataType
521-
extends DashboardLayoutDataWithoutRowsType {
519+
export interface PerformanceIntensiveDashboardLayoutDataType extends DashboardLayoutDataWithoutRowsType {
522520
rows: {
523521
columns: PerformanceIntensiveDashboardColumnDataType[];
524522
height?: number;
@@ -1018,6 +1016,7 @@ export interface GraphSettingsDataType {
10181016
mapColorLegendTitle?: string;
10191017
choroplethScaleType?: Exclude<ScaleDataType, 'linear'>;
10201018
trackColor?: string;
1019+
zoomAndCenterByHighlightedIds?: boolean;
10211020
}
10221021

10231022
export interface InfoBoxDataType {

0 commit comments

Comments
 (0)