egraph 6.0.0-alpha.0 → 6.0.0-alpha.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/bundler/LICENSE +21 -0
- package/dist/bundler/README.md +161 -0
- package/dist/no-modules/LICENSE +21 -0
- package/dist/no-modules/README.md +161 -0
- package/dist/nodejs/LICENSE +21 -0
- package/dist/nodejs/README.md +161 -0
- package/dist/web/LICENSE +21 -0
- package/dist/web/README.md +161 -0
- package/package.json +1 -1
- package/umd/egraph.js +840 -0
- package/umd/egraph_bg.wasm +0 -0
- package/dist/bundler/egraph_wasm.d.ts +0 -524
- package/dist/bundler/egraph_wasm.js +0 -2
- package/dist/bundler/egraph_wasm_bg.wasm +0 -0
- package/dist/no-modules/egraph_wasm.d.ts +0 -651
- package/dist/no-modules/egraph_wasm.js +0 -1569
- package/dist/no-modules/egraph_wasm_bg.wasm +0 -0
- package/dist/nodejs/egraph_wasm.d.ts +0 -524
- package/dist/nodejs/egraph_wasm.js +0 -1534
- package/dist/nodejs/egraph_wasm_bg.wasm +0 -0
- package/dist/web/egraph_wasm.d.ts +0 -648
- package/dist/web/egraph_wasm.js +0 -1544
- package/dist/web/egraph_wasm_bg.wasm +0 -0
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
# egraph
|
|
2
|
+
|
|
3
|
+
egraph is a fast graph drawing library implemented in Rust and WebAssembly.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```shell-session
|
|
8
|
+
$ npm install egraph
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
```javascript
|
|
14
|
+
import { Graph } from 'egraph'
|
|
15
|
+
import {
|
|
16
|
+
Simulation,
|
|
17
|
+
ManyBodyForce,
|
|
18
|
+
LinkForce,
|
|
19
|
+
CenterForce
|
|
20
|
+
} from 'egraph/layout/force-directed'
|
|
21
|
+
|
|
22
|
+
// creating a graph
|
|
23
|
+
const graph = new Graph()
|
|
24
|
+
|
|
25
|
+
// adding vertices
|
|
26
|
+
graph.addNode(0)
|
|
27
|
+
graph.addNode(1)
|
|
28
|
+
graph.addNode(2)
|
|
29
|
+
|
|
30
|
+
// adding edges
|
|
31
|
+
graph.addEdge(0, 1)
|
|
32
|
+
graph.addEdge(0, 2)
|
|
33
|
+
graph.addEdge(1, 2)
|
|
34
|
+
|
|
35
|
+
// creating forces
|
|
36
|
+
const manyBodyForce = new ManyBodyForce()
|
|
37
|
+
const linkForce = new LinkForce()
|
|
38
|
+
const centerForce = new CenterForce()
|
|
39
|
+
|
|
40
|
+
// creating simulation
|
|
41
|
+
const simulation = new Simulation()
|
|
42
|
+
simulation.add(manyBodyForce)
|
|
43
|
+
simulation.add(linkForce)
|
|
44
|
+
simulation.add(centerForce)
|
|
45
|
+
|
|
46
|
+
// position calculation
|
|
47
|
+
const layout = simulation.start(graph)
|
|
48
|
+
|
|
49
|
+
// printing result
|
|
50
|
+
for (const u of graph.nodes()) {
|
|
51
|
+
console.log(layout.nodes[u])
|
|
52
|
+
}
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
egraph-wasm is implemented using wasm-bindgen.
|
|
56
|
+
For more detailed usage, please read [wasm-bindgen document](https://rustwasm.github.io/wasm-bindgen/).
|
|
57
|
+
|
|
58
|
+
## Examples
|
|
59
|
+
|
|
60
|
+
### Drawing SVG with React.js
|
|
61
|
+
|
|
62
|
+
```javascript
|
|
63
|
+
import React from 'react'
|
|
64
|
+
import { render } from 'react-dom'
|
|
65
|
+
import { Graph } from 'egraph'
|
|
66
|
+
import {
|
|
67
|
+
Simulation,
|
|
68
|
+
ManyBodyForce,
|
|
69
|
+
LinkForce,
|
|
70
|
+
CenterForce
|
|
71
|
+
} from 'egraph/layout/force-directed'
|
|
72
|
+
|
|
73
|
+
const graph = new Graph()
|
|
74
|
+
graph.addNode(0)
|
|
75
|
+
graph.addNode(1)
|
|
76
|
+
graph.addNode(2)
|
|
77
|
+
graph.addEdge(0, 1)
|
|
78
|
+
graph.addEdge(0, 2)
|
|
79
|
+
graph.addEdge(1, 2)
|
|
80
|
+
|
|
81
|
+
const manyBodyForce = new ManyBodyForce()
|
|
82
|
+
const linkForce = new LinkForce()
|
|
83
|
+
linkForce.distance = () => 200
|
|
84
|
+
const centerForce = new CenterForce()
|
|
85
|
+
|
|
86
|
+
const simulation = new Simulation()
|
|
87
|
+
simulation.add(manyBodyForce)
|
|
88
|
+
simulation.add(linkForce)
|
|
89
|
+
simulation.add(centerForce)
|
|
90
|
+
|
|
91
|
+
const layout = simulation.start(graph)
|
|
92
|
+
|
|
93
|
+
const width = 600
|
|
94
|
+
const height = 400
|
|
95
|
+
render(
|
|
96
|
+
<div>
|
|
97
|
+
<svg width={width} height={height}>
|
|
98
|
+
<g transform={`translate(${width / 2},${height / 2})`}>
|
|
99
|
+
<g>
|
|
100
|
+
{Array.from(graph.edges()).map(([u, v]) => {
|
|
101
|
+
const { x: x0, y: y0 } = positions[u]
|
|
102
|
+
const { x: x1, y: y1 } = positions[v]
|
|
103
|
+
return (
|
|
104
|
+
<g key={e}>
|
|
105
|
+
<path d={`M ${x0} ${y0} L ${x1} ${y1}`} stroke='#000' />
|
|
106
|
+
</g>
|
|
107
|
+
)
|
|
108
|
+
})}
|
|
109
|
+
</g>
|
|
110
|
+
<g>
|
|
111
|
+
{Array.from(graph.nodes()).map((u) => {
|
|
112
|
+
const { x, y } = positions[u]
|
|
113
|
+
return (
|
|
114
|
+
<g key={i} transform={`translate(${x},${y})`}>
|
|
115
|
+
<circle r='10' fill='#000' />
|
|
116
|
+
</g>
|
|
117
|
+
)
|
|
118
|
+
})}
|
|
119
|
+
</g>
|
|
120
|
+
</g>
|
|
121
|
+
</svg>
|
|
122
|
+
</div>,
|
|
123
|
+
document.querySelector('#display')
|
|
124
|
+
)
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
### Loading JSON
|
|
128
|
+
|
|
129
|
+
```javascript
|
|
130
|
+
import { Graph } from 'egraph'
|
|
131
|
+
import {
|
|
132
|
+
Simulation,
|
|
133
|
+
ManyBodyForce,
|
|
134
|
+
LinkForce,
|
|
135
|
+
CenterForce
|
|
136
|
+
} from 'egraph/layout/force-directed'
|
|
137
|
+
;(async () => {
|
|
138
|
+
const request = await fetch('miserables.json')
|
|
139
|
+
const data = await request.json()
|
|
140
|
+
|
|
141
|
+
const { Graph } = mod
|
|
142
|
+
const graph = new Graph()
|
|
143
|
+
for (const node of data.nodes) {
|
|
144
|
+
graph.addNode(node.id, node)
|
|
145
|
+
}
|
|
146
|
+
for (const link of data.links) {
|
|
147
|
+
graph.addEdge(link.source, link.target, link)
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
for (const u of graph.nodes()) {
|
|
151
|
+
console.log(graph.node(u))
|
|
152
|
+
}
|
|
153
|
+
for (const [u, v] of graph.edges()) {
|
|
154
|
+
console.log(graph.edge(u, v))
|
|
155
|
+
}
|
|
156
|
+
})()
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
## License
|
|
160
|
+
|
|
161
|
+
MIT
|