Improve colors of graph
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Peter Stuifzand 2020-07-05 21:18:42 +02:00
parent 5bf14bd1d4
commit 64f7ebe8f9
2 changed files with 31 additions and 23 deletions

View File

@ -15,7 +15,7 @@ type graphBuilder struct {
edges []Edge
}
type NodeMap map[string]int
type NodeMap map[string]Node
func NewGraphBuilder(mp PagesRepository) (*graphBuilder, error) {
refs := make(Refs)
@ -44,7 +44,7 @@ func (gb *graphBuilder) prepareGraph() error {
gb.nodes = prepareNodes(gb.nodeMap, func(node Node) Node {
if node.Id == 0 {
var green string
green = "green"
green = "#f57900"
node.Color = &green
}
return node
@ -66,18 +66,18 @@ func (gb *graphBuilder) RemoveNodeWithSuffix(suffix string) {
}
func (gb *graphBuilder) buildFromCenter(name string) error {
_ = gb.addNode(name)
_ = gb.addNode(name, "#ef2929")
if ref, e := gb.refs[name]; e {
for _, item := range ref {
gb.addNode(item.Name)
gb.addNode(item.Name, "#fce94f")
}
}
for key, references := range gb.refs {
for _, item := range references {
if name == item.Name {
gb.addNode(key)
gb.addNode(key, "#ad7fa8")
}
}
}
@ -89,12 +89,12 @@ func prepareEdges(refs Refs, nodeMap NodeMap) []Edge {
var edges []Edge
edgeSet := make(map[Edge]bool)
for key, references := range refs {
if toID, e := nodeMap[key]; e {
if to, e := nodeMap[key]; e {
for _, item := range references {
if fromID, e := nodeMap[item.Name]; e {
if from, e := nodeMap[item.Name]; e {
edge := Edge{
From: fromID,
To: toID,
From: from.Id,
To: to.Id,
}
if _, e := edgeSet[edge]; !e {
edgeSet[edge] = true
@ -109,11 +109,12 @@ func prepareEdges(refs Refs, nodeMap NodeMap) []Edge {
func prepareNodes(nodeMap NodeMap, apply func(node Node) Node) []Node {
var nodes []Node
for name, id := range nodeMap {
for _, node := range nodeMap {
nodes = append(nodes, apply(Node{
Id: id,
Label: name,
Color: nil,
Id: node.Id,
Label: node.Label,
Color: node.Color,
Opacity: node.Opacity,
}))
}
return nodes
@ -121,18 +122,19 @@ func prepareNodes(nodeMap NodeMap, apply func(node Node) Node) []Node {
func (gb *graphBuilder) prepareNodeMap() {
for key, references := range gb.refs {
gb.addNode(key)
gb.addNode(key, "#5e8")
for _, item := range references {
gb.addNode(item.Name)
gb.addNode(item.Name, "#403")
}
}
}
func (gb *graphBuilder) addNode(key string) int {
func (gb *graphBuilder) addNode(key string, color string) int {
if _, e := gb.nodeMap[key]; !e {
gb.nodeMap[key] = gb.nodeCount
color := color
gb.nodeMap[key] = Node{gb.nodeCount, key, &color, 1}
gb.nodeCount += 1
}
return gb.nodeMap[key]
return gb.nodeMap[key].Id
}

16
main.go
View File

@ -101,9 +101,10 @@ type indexPage struct {
}
type Node struct {
Id int `json:"id"`
Label string `json:"label"`
Color *string `json:"color"`
Id int `json:"id"`
Label string `json:"label"`
Color *string `json:"color"`
Opacity float64 `json:"opacity"`
}
type Edge struct {
@ -818,11 +819,16 @@ func main() {
// Keep a copy of the nodes, buildFromCenter appends to the nodeMap
var nodes []string
for k, _ := range gb.nodeMap {
if k == "Daily_Notes" || strings.HasSuffix(k, "Mei_2020") {
continue
}
nodes = append(nodes, k)
}
for _, node := range nodes {
err = gb.buildFromCenter(node)
if name != "Daily_Notes" {
for _, node := range nodes {
err = gb.buildFromCenter(node)
}
}
err = gb.prepareGraph()