-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathgraph.go
More file actions
120 lines (102 loc) · 2.55 KB
/
graph.go
File metadata and controls
120 lines (102 loc) · 2.55 KB
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
package main
import (
"io"
"os"
"strconv"
"github.com/go-echarts/go-echarts/v2/charts"
"github.com/go-echarts/go-echarts/v2/components"
"github.com/go-echarts/go-echarts/v2/opts"
"github.com/specterops/dawgs/container"
"github.com/specterops/dawgs/graph"
)
var graphNodes = []opts.GraphNode{
{Name: "Node1"},
{Name: "Node2"},
{Name: "Node3"},
{Name: "Node4"},
{Name: "Node5"},
{Name: "Node6"},
{Name: "Node7"},
{Name: "Node8"},
}
func genLinks() []opts.GraphLink {
links := make([]opts.GraphLink, 0)
for i := 0; i < len(graphNodes); i++ {
for j := 0; j < len(graphNodes); j++ {
links = append(links, opts.GraphLink{Source: graphNodes[i].Name, Target: graphNodes[j].Name})
}
}
return links
}
func graphBase() *charts.Graph {
graph := charts.NewGraph()
graph.SetGlobalOptions(
charts.WithTitleOpts(opts.Title{Title: "basic graph example"}),
)
graph.AddSeries("graph", graphNodes, genLinks(),
charts.WithGraphChartOpts(
opts.GraphChart{Force: &opts.GraphForce{Repulsion: 8000}},
),
)
return graph
}
func graphCircle() *charts.Graph {
graph := charts.NewGraph()
graph.SetGlobalOptions(
charts.WithTitleOpts(opts.Title{Title: "Circular layout"}),
)
graph.AddSeries("graph", graphNodes, genLinks()).
SetSeriesOptions(
charts.WithGraphChartOpts(
opts.GraphChart{
Force: &opts.GraphForce{Repulsion: 8000},
Layout: "circular",
}),
charts.WithLabelOpts(opts.Label{Show: opts.Bool(true), Position: "right"}),
)
return graph
}
func graphDigraph(digraph container.DirectedGraph, direction graph.Direction) *charts.Graph {
graph := charts.NewGraph()
graph.SetGlobalOptions(
charts.WithTitleOpts(opts.Title{
Title: "demo",
}))
var (
nodes []opts.GraphNode
links []opts.GraphLink
)
digraph.EachNode(func(node uint64) bool {
sourceNode := strconv.FormatUint(node, 10)
nodes = append(nodes, opts.GraphNode{
Name: sourceNode,
})
digraph.EachAdjacentNode(node, direction, func(adjacent uint64) bool {
links = append(links, opts.GraphLink{
Source: sourceNode,
Target: strconv.FormatUint(adjacent, 10),
})
return true
})
return true
})
graph.AddSeries("graph", nodes, links).
SetSeriesOptions(
charts.WithGraphChartOpts(opts.GraphChart{
Force: &opts.GraphForce{Repulsion: 8000},
}),
)
return graph
}
func doTheGraph(digraph container.DirectedGraph, direction graph.Direction) {
page := components.NewPage()
page.AddCharts(
graphDigraph(digraph, direction),
)
f, err := os.Create("graph.html")
if err != nil {
panic(err)
}
page.Render(io.MultiWriter(f))
f.Close()
}