-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathApp.tsx
207 lines (190 loc) · 5.84 KB
/
App.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import OgmaLib, {
Edge,
Node,
Point,
RawGraph,
NodeGrouping as NodeGroupingTransformation,
} from "@linkurious/ogma";
import { useEffect, useState, createRef, useCallback } from "react";
// loading indicator
import { LoadingOverlay } from "@mantine/core";
// for geo mode
import * as L from "leaflet";
// components
import {
Ogma,
NodeStyleRule,
EdgeStyleRule,
Tooltip,
NodeGrouping,
Popup,
Geo,
NodeGroupingProps,
} from "../../src";
// cusotm components:
// layout component, to be applied on certain events
import { LayoutService } from "./components/Layout";
// outlines canvas layer with halos
import { GraphOutlines } from "./components/GraphOutlines";
// control panel
import { Controls } from "./components/Controls";
import { MousePosition } from "./components/MousePosition";
import { Logo } from "./components/Logo";
import { UpdateGroupingButton } from "./components/UpdateGroupingButton";
import "@mantine/core/styles.css";
// to enable geo mode integration
OgmaLib.libraries["leaflet"] = L;
type ND = unknown;
type ED = unknown;
export default function App() {
// graph state
const [graph, setGraph] = useState<RawGraph>();
const [loading, setLoading] = useState(true);
// UI states
const [popupOpen, setPopupOpen] = useState(false);
const [clickedNode, setClickedNode] = useState<Node>();
// ogma instance and grouping references
const ref = createRef<OgmaLib>();
const groupingRef = createRef<NodeGroupingTransformation<ND, ED>>();
// grouping and geo states
const [nodeGrouping, setNodeGrouping] = useState(true);
const [geoEnabled, setGeoEnabled] = useState(false);
// styling states
const [nodeSize, setNodeSize] = useState(5);
const [edgeWidth, setEdgeWidth] = useState(0.5);
const [groupingOptions, setGroupingOptions] = useState<
NodeGroupingProps<any, any>
>({
groupIdFunction: (node) => {
const categories = node.getData("categories");
if (!categories) return undefined;
return categories[0] === "INVESTOR" ? "INVESTOR" : undefined;
},
nodeGenerator: (nodes) => {
return { data: { multiplier: nodes.size } };
},
disabled: true,
});
// UI layers
const [outlines, setOutlines] = useState(false);
const [tooltipPositon, setTooltipPosition] = useState<Point>({
x: 0,
y: 0,
});
const [target, setTarget] = useState<Node | Edge | null>();
const requestSetTooltipPosition = useCallback((pos: Point) => {
requestAnimationFrame(() => setTooltipPosition(pos));
}, []);
const popupPosition = useCallback(
() => (clickedNode ? clickedNode.getPosition() : null),
[clickedNode]
);
const onPopupClose = useCallback(() => setPopupOpen(false), []);
// load the graph
useEffect(() => {
setLoading(true);
fetch("data.json")
.then((res) => res.json())
.then((data: RawGraph) => {
setGraph(data);
setLoading(false);
});
}, []);
// nothing to render yet
if (loading) return <LoadingOverlay zIndex={400} />;
return (
<div className="App">
<Logo />
<Ogma
ref={ref}
graph={graph}
onReady={(ogma) => {
ogma.events
.on("click", ({ target }) => {
if (target && target.isNode) {
setClickedNode(target);
setPopupOpen(true);
}
})
.on("mousemove", () => {
const ptr = ogma.getPointerInformation();
requestSetTooltipPosition(
ogma.view.screenToGraphCoordinates({ x: ptr.x, y: ptr.y })
);
setTarget(ptr.target);
})
// locate graph when the nodes are added
.on("addNodes", () =>
ogma.view.locateGraph({ duration: 250, padding: 50 })
);
}}
>
{/* Styling */}
<NodeStyleRule
attributes={{
color: "#247BA0",
radius: (n) => (n?.getData("multiplier") || 1) * nodeSize, // the label is the value os the property name.
text: {
content: (node) => node?.getData("properties.name"),
font: "IBM Plex Sans",
},
}}
/>
<EdgeStyleRule attributes={{ width: edgeWidth }} />
{/* Layout */}
<LayoutService />
{/* Grouping */}
<NodeGrouping
ref={groupingRef}
disabled={!nodeGrouping && !geoEnabled}
groupIdFunction={groupingOptions.groupIdFunction}
nodeGenerator={groupingOptions.nodeGenerator}
duration={500}
/>
{/* context-aware UI */}
<Popup
position={popupPosition}
onClose={onPopupClose}
isOpen={!!clickedNode && popupOpen}
>
{!!clickedNode && (
<div className="content">{`Node ${clickedNode.getId()}:`}</div>
)}
</Popup>
<Tooltip
visible={!!target && !popupOpen}
placement="right"
position={tooltipPositon}
>
<div className="x">
{target
? `${target.isNode ? "Node" : "Edge"} #${target.getId()}`
: "nothing"}
</div>
</Tooltip>
<GraphOutlines visible={outlines} />
{/* Geo mode */}
<Geo
enabled={geoEnabled}
longitudePath="properties.longitude"
latitudePath="properties.latitude"
/>
<MousePosition />
<UpdateGroupingButton
options={groupingOptions}
update={(options) => setGroupingOptions(options)}
/>
</Ogma>
<Controls
toggleNodeGrouping={(value) => setNodeGrouping(value)}
nodeGrouping={nodeGrouping}
setNodeSize={setNodeSize}
setEdgeWidth={setEdgeWidth}
outlines={outlines}
setOutlines={setOutlines}
geoEnabled={geoEnabled}
setGeoEnabled={setGeoEnabled}
/>
</div>
);
}