Skip to content
This repository was archived by the owner on Apr 7, 2024. It is now read-only.

Commit cc28705

Browse files
fix: Fix orbits placement strategy packing for unbalanced graph components (#104)
* Fix orbits placement strategy packing for unbalanced graph components * Merge changes from master and clean up code --------- Co-authored-by: Mateusz Łopaciński <lop.mateusz.2001@gmail.com>
1 parent f196d3f commit cc28705

File tree

3 files changed

+76
-91
lines changed

3 files changed

+76
-91
lines changed

src/examples/Development.tsx

Lines changed: 57 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,91 +1,68 @@
1-
import { useEffect } from 'react';
21
import { SafeAreaView, StyleSheet, View } from 'react-native';
32
import { GestureHandlerRootView } from 'react-native-gesture-handler';
43

4+
import DirectedGraphComponent from '@/components/graphs/DirectedGraphComponent';
55
import DefaultEdgeLabelRenderer from '@/components/graphs/labels/renderers/DefaultEdgeLabelRenderer';
66
import { DirectedGraph } from '@/models/graphs';
77
import PannableScalableView from '@/views/PannableScalableView';
88

9-
import { DirectedGraphComponent } from '..';
10-
119
export default function App() {
1210
const graph = new DirectedGraph({
13-
vertices: [{ key: 'A', value: 'A' }]
14-
});
15-
16-
useEffect(() => {
17-
graph.insertVertex(
11+
vertices: [
12+
{ key: 'A', value: 'A' },
1813
{ key: 'B', value: 'B' },
19-
{
20-
duration: 400,
21-
onComplete: () => {
22-
graph.insertEdge(
23-
{ key: 'AB', value: 'AB', from: 'A', to: 'B' },
24-
{
25-
duration: 100
26-
}
27-
);
28-
graph.insertVertex(
29-
{ key: 'C', value: 'C' },
30-
{
31-
duration: 800,
32-
onComplete: () => {
33-
graph.insertEdge(
34-
{
35-
key: 'BC',
36-
value: 'BC',
37-
from: 'B',
38-
to: 'C'
39-
},
40-
{
41-
duration: 4000
42-
}
43-
);
44-
graph.insertVertex(
45-
{ key: 'D', value: 'D' },
46-
{
47-
duration: 1200,
48-
onComplete: () => {
49-
graph.insertEdge(
50-
{
51-
key: 'CD',
52-
value: 'CD',
53-
from: 'C',
54-
to: 'D'
55-
},
56-
{
57-
duration: 3000
58-
}
59-
);
60-
graph.insertVertex(
61-
{ key: 'E', value: 'E' },
62-
{
63-
duration: 1600,
64-
onComplete: () => {
65-
graph.insertEdge(
66-
{
67-
key: 'DE',
68-
value: 'DE',
69-
from: 'D',
70-
to: 'E'
71-
},
72-
{
73-
duration: 2000
74-
}
75-
);
76-
}
77-
}
78-
);
79-
}
80-
}
81-
);
82-
}
83-
}
84-
);
85-
}
86-
}
87-
);
88-
}, []);
14+
{ key: 'C', value: 'C' },
15+
{ key: 'D', value: 'D' },
16+
{ key: 'E', value: 'E' },
17+
{ key: 'F', value: 'F' },
18+
{ key: 'G', value: 'G' },
19+
{ key: 'H', value: 'H' },
20+
{ key: 'I', value: 'I' },
21+
{ key: 'J', value: 'J' },
22+
{ key: 'L', value: 'L' },
23+
{ key: 'M', value: 'M' },
24+
{ key: 'N', value: 'N' },
25+
{ key: 'O', value: 'O' },
26+
{ key: 'P', value: 'P' },
27+
{ key: 'Q', value: 'Q' },
28+
{ key: 'R', value: 'R' },
29+
{ key: 'S', value: 'S' },
30+
{ key: 'T', value: 'T' },
31+
{ key: 'U', value: 'U' },
32+
{ key: 'V', value: 'V' },
33+
{ key: 'W', value: 'W' },
34+
{ key: 'X', value: 'X' },
35+
{ key: 'Y', value: 'Y' },
36+
{ key: 'Z', value: 'Z' },
37+
{ key: 'AA', value: 'AA' },
38+
{ key: 'AB', value: 'AB' },
39+
{ key: 'AC', value: 'AC' },
40+
{ key: 'AD', value: 'AD' },
41+
{ key: 'AE', value: 'AE' },
42+
{ key: 'AF', value: 'AF' },
43+
{ key: 'AG', value: 'AG' },
44+
{ key: 'AH', value: 'AH' },
45+
{ key: 'AI', value: 'AI' },
46+
{ key: 'AJ', value: 'AJ' },
47+
{ key: 'AK', value: 'AK' },
48+
{ key: 'AL', value: 'AL' },
49+
{ key: 'AM', value: 'AM' },
50+
{ key: 'AN', value: 'AN' },
51+
{ key: 'AO', value: 'AO' },
52+
{ key: 'AP', value: 'AP' }
53+
],
54+
edges: [
55+
{ key: 'AB', from: 'A', to: 'B', value: 'AB' },
56+
{ key: 'AC', from: 'A', to: 'C', value: 'AC' },
57+
{ key: 'CF', from: 'C', to: 'F', value: 'CF' },
58+
{ key: 'CE', from: 'C', to: 'E', value: 'CE' },
59+
{ key: 'CD', from: 'C', to: 'D', value: 'CD' },
60+
// { key: 'DH', from: 'D', to: 'H', value: 'DH' },
61+
{ key: 'EL', from: 'E', to: 'L', value: 'EL' },
62+
{ key: 'FG', from: 'F', to: 'G', value: 'FG' },
63+
{ key: 'LJ', from: 'L', to: 'J', value: 'LJ' }
64+
]
65+
});
8966

9067
return (
9168
<SafeAreaView style={styles.container}>
@@ -96,8 +73,8 @@ export default function App() {
9673
graph={graph}
9774
settings={{
9875
placement: {
99-
strategy: 'circle',
100-
minVertexSpacing: 100
76+
strategy: 'orbits',
77+
minVertexSpacing: 120
10178
},
10279
components: {
10380
edge: {

src/utils/placement/shared.ts

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import {
99

1010
export const calcContainerBoundingRect = (
1111
placedVertices: PlacedVerticesPositions,
12-
minVertexSpacing: number,
13-
vertexRadius: number
12+
minVertexSpacing = 0,
13+
vertexRadius = 0
1414
): BoundingRect => {
1515
'worklet';
1616
let minX = 0;
@@ -64,14 +64,23 @@ export const arrangeGraphComponents = (
6464
const packed = potpack(preparedComponents);
6565
// Translate graph components to correct positions on the screen
6666
const verticesPositions = Object.fromEntries(
67-
preparedComponents.flatMap(({ verticesPositions: positions, x, y, w, h }) =>
68-
Object.entries(positions).map(([key, { x: vx, y: vy }]) => [
69-
key,
70-
{
71-
x: vx + x + (w - packed.w) / 2,
72-
y: vy + y + (h - packed.h) / 2
73-
}
74-
])
67+
preparedComponents.flatMap(
68+
({ verticesPositions: positions, x, y, w, h }) => {
69+
const { left, right, top, bottom } =
70+
calcContainerBoundingRect(positions);
71+
72+
// calculate the shift of graph center relative to component center
73+
const widthShift = w / 2 - (left + right) / 2;
74+
const heightShift = h / 2 - (top + bottom) / 2;
75+
76+
return Object.entries(positions).map(([key, { x: vx, y: vy }]) => [
77+
key,
78+
{
79+
x: vx + x - packed.w / 2 + widthShift,
80+
y: vy + y - packed.h / 2 + heightShift
81+
}
82+
]);
83+
}
7584
)
7685
);
7786

src/utils/placement/strategies/trees.placement.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ const placeVerticesOnTrees = <V, E>(
3838
vertexRadius
3939
);
4040
// Calculate container dimensions
41-
4241
componentsLayouts.push({
4342
verticesPositions,
4443
boundingRect: calcContainerBoundingRect(

0 commit comments

Comments
 (0)