/
MyRing.swift
55 lines (47 loc) · 1.59 KB
/
MyRing.swift
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
//
// ForceDirectedGraphSwiftUIExample.swift
// ForceDirectedGraphExample
//
// Created by li3zhen1 on 11/5/23.
//
import Foundation
import Grape
import SwiftUI
import ForceSimulation
struct MyRing: View {
@State var graphStates = ForceDirectedGraphState()
var body: some View {
ForceDirectedGraph(states: graphStates) {
Series(0..<20) { i in
NodeMark(id: 3 * i + 0)
.symbol(.circle)
.symbolSize(radius:4.0)
.foregroundStyle(.green)
NodeMark(id: 3 * i + 1)
.symbol(.pentagon)
.symbolSize(radius:5.0)
.foregroundStyle(.blue)
NodeMark(id: 3 * i + 2)
.symbol(.circle)
.symbolSize(radius:6.0)
.foregroundStyle(.yellow)
LinkMark(from: 3 * i + 0, to: 3 * i + 1)
LinkMark(from: 3 * i + 1, to: 3 * i + 2)
LinkMark(from: 3 * i + 0, to: 3 * ((i + 1) % 20) + 0)
LinkMark(from: 3 * i + 1, to: 3 * ((i + 1) % 20) + 1)
LinkMark(from: 3 * i + 2, to: 3 * ((i + 1) % 20) + 2)
}
} force: {
ManyBodyForce(strength: -15)
LinkForce(
originalLength: .constant(20.0),
stiffness: .weightedByDegree(k: { _, _ in 3.0})
)
CenterForce()
CollideForce()
}
.toolbar {
GraphStateToggle(graphStates: graphStates)
}
}
}