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

Commit 15c61f3

Browse files
Fix empty position setting and Orbits placement strategy bug (#84)
* Fix empty position setting and Orbits placement strategy bug * Fix padding between disconnected graph parts --------- Co-authored-by: Mateusz Łopaciński <lop.mateusz.2001@gmail.com>
1 parent 7244d8b commit 15c61f3

File tree

9 files changed

+46
-34
lines changed

9 files changed

+46
-34
lines changed

src/components/graphs/GraphComponent.tsx

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,21 @@
1-
import { Group, Vector } from '@shopify/react-native-skia';
2-
import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
3-
import { useAnimatedReaction } from 'react-native-reanimated';
4-
1+
import DefaultEdgeArrowRenderer from './arrows/renderers/DefaultEdgeArrowRenderer';
2+
import EdgeComponent from './edges/EdgeComponent';
3+
import DefaultCurvedEdgeRenderer from './edges/curved/renderers/DefaultCurvedEdgeRenderer';
4+
import DefaultStraightEdgeRenderer from './edges/straight/renderers/DefaultStraightEdgeRenderer';
5+
import VertexComponent from './vertices/VertexComponent';
6+
import DefaultVertexRenderer from './vertices/renderers/DefaultVertexRenderer';
57
import {
68
ARROW_COMPONENT_SETTINGS,
79
CURVED_EDGE_COMPONENT_SETTINGS,
810
LABEL_COMPONENT_SETTINGS,
911
STRAIGHT_EDGE_COMPONENT_SETTINGS,
1012
VERTEX_COMPONENT_SETTINGS
1113
} from '@/constants/components';
14+
import {
15+
RANDOM_PLACEMENT_SETTING,
16+
SHARED_PLACEMENT_SETTINGS,
17+
TRIANGLES_PLACEMENT_SETTING
18+
} from '@/constants/placement';
1219
import { GraphEventsContextType } from '@/context/graphEvents';
1320
import { useGraphObserver } from '@/hooks';
1421
import { EdgeComponentProps } from '@/types/components';
@@ -24,17 +31,14 @@ import {
2431
DirectedEdgeSettings,
2532
GraphLayout,
2633
GraphSettings,
27-
GraphSettingsWithDefaults
34+
GraphSettingsWithDefaults,
35+
RandomPlacementSettings
2836
} from '@/types/settings';
2937
import { animateVerticesToFinalPositions } from '@/utils/animations';
3038
import { placeVertices } from '@/utils/placement';
31-
32-
import DefaultEdgeArrowRenderer from './arrows/renderers/DefaultEdgeArrowRenderer';
33-
import DefaultCurvedEdgeRenderer from './edges/curved/renderers/DefaultCurvedEdgeRenderer';
34-
import EdgeComponent from './edges/EdgeComponent';
35-
import DefaultStraightEdgeRenderer from './edges/straight/renderers/DefaultStraightEdgeRenderer';
36-
import DefaultVertexRenderer from './vertices/renderers/DefaultVertexRenderer';
37-
import VertexComponent from './vertices/VertexComponent';
39+
import { Group, Vector } from '@shopify/react-native-skia';
40+
import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
41+
import { useAnimatedReaction } from 'react-native-reanimated';
3842

3943
export type GraphComponentPrivateProps<V, E> = {
4044
boundingRect: AnimatedBoundingRect;
@@ -111,6 +115,10 @@ export default function GraphComponent<
111115
const memoSettings = useMemo(() => {
112116
const newSettings: GraphSettingsWithDefaults<V, E> = {
113117
...settings,
118+
placement: {
119+
...RANDOM_PLACEMENT_SETTING as RandomPlacementSettings,
120+
...settings?.placement
121+
},
114122
components: {
115123
...settings?.components,
116124
vertex: {

src/constants/placement.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export const SHARED_PLACEMENT_SETTINGS = {
55
};
66

77
export const RANDOM_PLACEMENT_SETTING = {
8+
strategy: 'random',
89
layoutType: 'triangles' as RandomLayoutType,
910
density: 0.5
1011
};

src/examples/Development.tsx

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ import { useEffect } from 'react';
22
import { SafeAreaView, StyleSheet, View } from 'react-native';
33
import { GestureHandlerRootView } from 'react-native-gesture-handler';
44

5-
import DirectedGraphComponent from '@/components/graphs/DirectedGraphComponent';
65
import DefaultEdgeLabelRenderer from '@/components/graphs/labels/renderers/DefaultEdgeLabelRenderer';
7-
import { DirectedGraph } from '@/models/graphs';
6+
import UndirectedGraphComponent from '@/components/graphs/UndirectedGraphComponent';
7+
import { UndirectedGraph } from '@/models/graphs';
88
import PannableScalableView from '@/views/PannableScalableView';
99

1010
const ADDED_COMPONENTS = [
@@ -94,7 +94,9 @@ let idx = 0;
9494
let mode = 0;
9595

9696
export default function App() {
97-
const graph = DirectedGraph.fromData({ vertices: [{ key: 'A', data: 'A' }] });
97+
const graph = new UndirectedGraph({
98+
vertices: [{ key: 'A', data: 'A' }]
99+
});
98100

99101
// TODO - remove this useEffect after testing
100102
useEffect(() => {
@@ -132,7 +134,7 @@ export default function App() {
132134
console.error(e);
133135
return;
134136
}
135-
}, 250);
137+
}, 500);
136138

137139
return () => clearInterval(interval);
138140
}, []);
@@ -142,11 +144,11 @@ export default function App() {
142144
<GestureHandlerRootView style={styles.gestureHandler}>
143145
<View style={styles.background}>
144146
<PannableScalableView objectFit='contain' controls>
145-
<DirectedGraphComponent
147+
<UndirectedGraphComponent
146148
graph={graph}
147149
settings={{
148150
placement: {
149-
strategy: 'trees',
151+
strategy: 'orbits',
150152
minVertexSpacing: 100
151153
},
152154
components: {

src/models/graphs/DirectedGraph.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,12 @@ export default class DirectedGraph<V, E> extends Graph<
1111
DirectedEdge<E, V>,
1212
DirectedEdgeData<E>
1313
> {
14-
// eslint-disable-next-line no-shadow
15-
static fromData<V, E>(data: {
14+
constructor(data: {
1615
vertices: Array<VertexData<V>>;
1716
edges?: Array<DirectedEdgeData<E>>;
18-
}): DirectedGraph<V, E> {
19-
const instance = new DirectedGraph<V, E>();
20-
instance.insertBatch(data);
21-
return instance;
17+
}) {
18+
super();
19+
this.insertBatch(data);
2220
}
2321

2422
override isDirected(): this is DirectedGraph<V, E> {

src/models/graphs/UndirectedGraph.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,12 @@ export default class UndirectedGraph<V, E> extends Graph<
1111
UndirectedEdge<E, V>,
1212
UndirectedEdgeData<E>
1313
> {
14-
// eslint-disable-next-line no-shadow
15-
static fromData<V, E>(data: {
14+
constructor(data: {
1615
vertices: Array<VertexData<V>>;
1716
edges?: Array<UndirectedEdgeData<E>>;
18-
}): UndirectedGraph<V, E> {
19-
const instance = new UndirectedGraph<V, E>();
20-
instance.insertBatch(data);
21-
return instance;
17+
}) {
18+
super();
19+
this.insertBatch(data);
2220
}
2321

2422
override isDirected() {

src/utils/algorithms/graphs.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ export const findGraphDiameter = <V, E>(
7676

7777
// Find the farthest vertex from the start vertex and the path
7878
// between them
79-
let farthestDistance = 0;
79+
let farthestDistance = -1;
8080
let farthestVertex: Vertex<V, E> | null = null;
8181

8282
const parents = bfs([startVertex], ({ vertex, depth }) => {

src/utils/placement/shared.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99

1010
export const calcContainerDimensions = (
1111
placedVertices: PlacedVerticesPositions,
12+
minVertexSpacing: number,
1213
vertexRadius: number
1314
): Dimensions => {
1415
let minX = 0;
@@ -24,8 +25,8 @@ export const calcContainerDimensions = (
2425
}
2526

2627
return {
27-
width: maxX - minX + 2 * vertexRadius,
28-
height: maxY - minY + 2 * vertexRadius
28+
width: maxX - minX + 2 * vertexRadius + minVertexSpacing,
29+
height: maxY - minY + 2 * vertexRadius + minVertexSpacing
2930
};
3031
};
3132

src/utils/placement/strategies/orbits.placement.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,11 @@ const placeVerticesOnOrbits = <V, E>(
4040
// Arrange vertices in sectors
4141
const arrangedVertices = arrangeVertices(rootVertex);
4242
// Calculate the layout of the component
43+
const minVertexSpacing =
44+
settings.minVertexSpacing ?? SHARED_PLACEMENT_SETTINGS.minVertexSpacing;
4345
const layerRadiuses = calcLayerRadiuses(
4446
arrangedVertices,
45-
settings.minVertexSpacing ?? SHARED_PLACEMENT_SETTINGS.minVertexSpacing,
47+
minVertexSpacing,
4648
vertexRadius,
4749
settings
4850
);
@@ -54,6 +56,7 @@ const placeVerticesOnOrbits = <V, E>(
5456
// Calc container dimensions
5557
const containerDimensions = calcContainerDimensions(
5658
placedVerticesPositions,
59+
minVertexSpacing,
5760
vertexRadius
5861
);
5962
componentsLayouts.push({

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ const placeVerticesOnTrees = <V, E>(
4040
// Calculate container dimensions
4141
const containerDimensions = calcContainerDimensions(
4242
placedVerticesPositions,
43+
minVertexSpacing,
4344
vertexRadius
4445
);
4546
componentsLayouts.push({

0 commit comments

Comments
 (0)