/
graph.go
126 lines (110 loc) Β· 2.58 KB
/
graph.go
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
package examples
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"os"
"github.com/go-echarts/go-echarts/v2/charts"
"github.com/go-echarts/go-echarts/v2/components"
"github.com/go-echarts/go-echarts/v2/opts"
)
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 graphNpmDep() *charts.Graph {
graph := charts.NewGraph()
graph.SetGlobalOptions(
charts.WithTitleOpts(opts.Title{
Title: "npm dependencies demo",
}))
f, err := ioutil.ReadFile("fixtures/npmdepgraph.json")
if err != nil {
panic(err)
}
type Data struct {
Nodes []opts.GraphNode
Links []opts.GraphLink
}
var data Data
if err := json.Unmarshal(f, &data); err != nil {
fmt.Println(err)
}
graph.AddSeries("graph", data.Nodes, data.Links).
SetSeriesOptions(
charts.WithGraphChartOpts(opts.GraphChart{
Layout: "none",
Roam: opts.Bool(true),
FocusNodeAdjacency: opts.Bool(true),
}),
charts.WithEmphasisOpts(opts.Emphasis{
Label: &opts.Label{
Show: opts.Bool(true),
Color: "black",
Position: "left",
},
}),
charts.WithLineStyleOpts(opts.LineStyle{
Curveness: 0.3,
}),
)
return graph
}
type GraphExamples struct{}
func (GraphExamples) Examples() {
page := components.NewPage()
page.AddCharts(
graphBase(),
graphCircle(),
graphNpmDep(),
)
f, err := os.Create("examples/html/graph.html")
if err != nil {
panic(err)
}
page.Render(io.MultiWriter(f))
}