Initial refactoring to use graphx

This commit is contained in:
Ivan Daniluk 2018-07-12 12:31:15 +02:00
parent 9ee56d0720
commit a502d29b8f
No known key found for this signature in database
GPG Key ID: 97ED33CE024E1DBF
4 changed files with 14 additions and 13 deletions

View File

@ -4,8 +4,8 @@ import (
"flag" "flag"
"log" "log"
"github.com/divan/graph-experiments/graph" "github.com/divan/graphx/formats"
"github.com/divan/graph-experiments/layout" "github.com/divan/graphx/layout"
) )
func main() { func main() {
@ -13,7 +13,7 @@ func main() {
iterations := flag.Int("i", 600, "Graph layout iterations to run (0 = auto, buggy)") iterations := flag.Int("i", 600, "Graph layout iterations to run (0 = auto, buggy)")
flag.Parse() flag.Parse()
data, err := graph.NewGraphFromJSON("network.json") data, err := formats.FromD3JSON("network.json")
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
@ -26,7 +26,7 @@ func main() {
log.Printf("Loaded propagation data: %d timestamps\n", len(plog.Timestamps)) log.Printf("Loaded propagation data: %d timestamps\n", len(plog.Timestamps))
log.Printf("Initializing layout...") log.Printf("Initializing layout...")
repelling := layout.NewGravityForce(-100.0, layout.BarneHutMethod) repelling := layout.NewGravityForce(-10.0, layout.BarneHutMethod)
springs := layout.NewSpringForce(0.01, 5.0, layout.ForEachLink) springs := layout.NewSpringForce(0.01, 5.0, layout.ForEachLink)
drag := layout.NewDragForce(0.4, layout.ForEachNode) drag := layout.NewDragForce(0.4, layout.ForEachNode)
layout3D := layout.New(data, repelling, springs, drag) layout3D := layout.New(data, repelling, springs, drag)

4
ws.go
View File

@ -5,8 +5,8 @@ import (
"log" "log"
"net/http" "net/http"
"github.com/divan/graph-experiments/graph" "github.com/divan/graphx/graph"
"github.com/divan/graph-experiments/layout" "github.com/divan/graphx/layout"
"github.com/gorilla/websocket" "github.com/gorilla/websocket"
) )

View File

@ -5,14 +5,15 @@ import (
"encoding/json" "encoding/json"
"log" "log"
"github.com/divan/graph-experiments/export" "github.com/divan/graphx/formats"
"github.com/divan/graph-experiments/graph" "github.com/divan/graphx/graph"
"github.com/gorilla/websocket" "github.com/gorilla/websocket"
) )
func (ws *WSServer) sendGraphData(c *websocket.Conn) { func (ws *WSServer) sendGraphData(c *websocket.Conn) {
var buf bytes.Buffer var buf bytes.Buffer
err := export.NewJSON(&buf, false).ExportGraph(ws.graph) d3json := formats.NewD3JSON(&buf, false)
err := d3json.ExportGraph(ws.graph)
if err != nil { if err != nil {
log.Fatal("Can't marshal graph to JSON") log.Fatal("Can't marshal graph to JSON")
} }

View File

@ -21,11 +21,11 @@ func (ws *WSServer) updatePositions() {
// positions // positions
nodes := ws.layout.Nodes() nodes := ws.layout.Nodes()
positions := []*position{} positions := []*position{}
for i := 0; i < len(nodes); i++ { for _, node := range nodes {
pos := &position{ pos := &position{
X: nodes[i].X, X: node.X,
Y: nodes[i].Y, Y: node.Y,
Z: nodes[i].Z, Z: node.Z,
} }
positions = append(positions, pos) positions = append(positions, pos)
} }