Skip to content

Commit 2fb7fc6

Browse files
Add layout option to circlePacking and also update design system library
1 parent 45bd8b2 commit 2fb7fc6

File tree

9 files changed

+65
-11
lines changed

9 files changed

+65
-11
lines changed

package-lock.json

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

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@undp/data-viz",
3-
"version": "2.3.4",
3+
"version": "2.3.6",
44
"main": "./dist/index.cjs",
55
"module": "./dist/index.js",
66
"browser": "./dist/index.umd.js",
@@ -441,6 +441,7 @@
441441
"d3-array": "^3.2.4",
442442
"d3-delaunay": "^6.0.4",
443443
"d3-force": "^3.0.0",
444+
"d3-force-boundary": "^0.0.3",
444445
"d3-format": "^3.1.0",
445446
"d3-geo": "^3.1.1",
446447
"d3-hierarchy": "^3.1.2",
@@ -466,7 +467,7 @@
466467
"peerDependencies": {
467468
"@dnd-kit/core": "^6.3.1",
468469
"@dnd-kit/modifiers": "^9.0.0",
469-
"@undp/design-system-react": "^2.0.1",
470+
"@undp/design-system-react": "^2.0.4",
470471
"dom-to-svg": "^0.12.2",
471472
"file-saver": "^2.0.5",
472473
"handlebars": "^4.7.8",

src/Components/Dashboard/GraphEl.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1391,6 +1391,7 @@ function GraphEl(props: Props) {
13911391
leftMargin: settings?.leftMargin,
13921392
rightMargin: settings?.rightMargin,
13931393
colorDomain: settings?.colorDomain,
1394+
circularBoundary: settings?.circularBoundary,
13941395
colorLegendTitle:
13951396
Object.keys(settings || {}).indexOf('colorLegendTitle') !== -1
13961397
? settings?.colorLegendTitle

src/Components/Graphs/CirclePackingGraph/Graph.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { forceCollide, forceManyBody, forceSimulation, forceX, forceY } from 'd3
33
import orderBy from 'lodash.orderby';
44
import { scaleSqrt } from 'd3-scale';
55
import { extent } from 'd3-array';
6+
import forceBoundary from 'd3-force-boundary';
67
import { cn } from '@undp/design-system-react/cn';
78
import { Spinner } from '@undp/design-system-react/Spinner';
89
import { P } from '@undp/design-system-react/Typography';
@@ -145,6 +146,7 @@ export const Graph = (props: Props) => {
145146
forceCollide((d: any) => (radiusScale ? radiusScale(d.size || 0) + 1 : radius + 1)),
146147
)
147148
.force('charge', forceManyBody().strength(-15))
149+
.force('boundary', forceBoundary(0, 0, graphWidth, graphHeight).strength(0.2).border(50))
148150
.alphaDecay(0.05)
149151
.tick(10000);
150152

src/Components/Graphs/CirclePackingGraph/getMaxCircleRadius.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@ export function getMaxCircleRadius(
22
values: (number | undefined | null)[],
33
width: number,
44
height: number,
5+
circularBoundary?: boolean,
56
) {
67
const filteredValues = values.filter(d => d !== undefined && d !== null);
7-
const containerRadius = Math.min(width, height) / 2;
8+
const containerRadius =
9+
circularBoundary === false
10+
? Math.sqrt((width * height) / Math.PI)
11+
: Math.min(width, height) / 2;
812
const totalValue = filteredValues.reduce((sum, v) => sum + v, 0);
913
const maxValue = Math.max(...filteredValues);
1014
const getPackingEfficiency = (n: number) => {
@@ -21,7 +25,7 @@ export function getMaxCircleRadius(
2125
return (
2226
containerRadius *
2327
Math.sqrt(maxValue / totalValue) *
24-
getEfficiencyBecauseOfContainerRadius(containerRadius) *
25-
getPackingEfficiency(filteredValues.length)
28+
(circularBoundary ? getEfficiencyBecauseOfContainerRadius(containerRadius) : 1) *
29+
(circularBoundary ? getPackingEfficiency(filteredValues.length) : 1)
2630
);
2731
}

src/Components/Graphs/CirclePackingGraph/index.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ interface Props {
7272
maxRadiusValue?: number;
7373

7474
// Graph Parameters
75+
/** If true, the graph will be rendered in a circle layout */
76+
circularBoundary?: boolean;
7577
/** Maximum radius of the circle */
7678
radius?: number;
7779
/** Toggle visibility of labels */
@@ -162,6 +164,7 @@ export function CirclePackingGraph(props: Props) {
162164
classNames,
163165
dimmedOpacity = 0.3,
164166
precision = 2,
167+
circularBoundary = true,
165168
} = props;
166169
const [svgWidth, setSvgWidth] = useState(0);
167170
const [svgHeight, setSvgHeight] = useState(0);
@@ -272,6 +275,7 @@ export function CirclePackingGraph(props: Props) {
272275
data.map(d => d.size),
273276
svgWidth,
274277
svgHeight,
278+
circularBoundary,
275279
)
276280
: radius
277281
}

src/Types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1020,6 +1020,7 @@ export interface GraphSettingsDataType {
10201020
hideAxisLine?: boolean;
10211021
projectionRotate?: [number, number] | [number, number, number];
10221022
rewindCoordinatesInMapData?: boolean;
1023+
circularBoundary?: boolean;
10231024
}
10241025

10251026
export interface InfoBoxDataType {

src/declarations.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
// declarations.d.ts
22
declare module 'pmtiles';
33
declare module '@undp/design-system-react/tailwind.config';
4+
declare module 'd3-force-boundary';

src/stories/Graph+Maps+Charts/CirclePacking.stories.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ const meta: Meta<PagePropsAndCustomArgs> = {
9191
// Graph parameters
9292
showLabels: { table: { defaultValue: { summary: 'true' } } },
9393
showValues: { table: { defaultValue: { summary: 'true' } } },
94+
circularBoundary: { table: { defaultValue: { summary: 'true' } } },
9495
showColorScale: { table: { defaultValue: { summary: 'false' } } },
9596
showNAColor: { table: { defaultValue: { summary: 'true' } } },
9697
highlightedDataPoints: {

0 commit comments

Comments
 (0)