Merge pull request #1 from status-im/graphx

Switch to graphx
This commit is contained in:
Ivan Daniluk 2018-07-19 18:54:42 +02:00 committed by GitHub
commit 4e63df16a0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 76 additions and 75 deletions

14
main.go
View File

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

View File

@ -1,4 +1,4 @@
(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
(function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r})()({1:[function(require,module,exports){
var { colorStr2Hex, autoColorNodes } = require('./js/colors.js');
require('./js/keys.js');
var accessorFn = require('./js/shitty_hacks.js');
@ -28,7 +28,16 @@ function setPropagation(plogData) {
}
function updatePositions(data) {
positions = data;
positions = [];
Object.keys(data).forEach((k) => {
let v = data[k];
positions[k] = {
x: v.X,
y: v.Y,
z: v.Z,
};
})
console.log("Positions", positions);
redrawGraph();
}
@ -136,8 +145,8 @@ var initGraph = function () {
sphere.__data = node; // Attach node data
nodesGroup.add(node.__sphere = sphere);
if (positions[idx] !== undefined) {
sphere.position.set(positions[idx].x, positions[idx].y, positions[idx].z);
if (positions[node.id] !== undefined) {
sphere.position.set(positions[node.id].x, positions[node.id].y, positions[node.id].z);
}
});
@ -185,9 +194,9 @@ var redrawGraph = function () {
const sphere = node.__sphere;
if (!sphere) return;
sphere.position.x = positions[idx].x;
sphere.position.y = positions[idx].y || 0;
sphere.position.z = positions[idx].z || 0;
sphere.position.x = positions[node.id].x;
sphere.position.y = positions[node.id].y || 0;
sphere.position.z = positions[node.id].z || 0;
});
@ -197,20 +206,8 @@ var redrawGraph = function () {
linePos = line.geometry.attributes.position;
// TODO: move this index into map/cache or even into original graph data
let start, end;
for (let i = 0; i < graphData.nodes.length; i++) {
if (graphData.nodes[i].id === link.source) {
start = i;
break;
}
}
for (let i = 0; i < graphData.nodes.length; i++) {
if (graphData.nodes[i].id === link["target"]) {
end = i;
break;
}
}
let start = link.source;
let end = link.target;
linePos.array[0] = positions[start].x;
linePos.array[1] = positions[start].y || 0;

View File

@ -27,7 +27,16 @@ function setPropagation(plogData) {
}
function updatePositions(data) {
positions = data;
positions = [];
Object.keys(data).forEach((k) => {
let v = data[k];
positions[k] = {
x: v.X,
y: v.Y,
z: v.Z,
};
})
console.log("Positions", positions);
redrawGraph();
}
@ -135,8 +144,8 @@ var initGraph = function () {
sphere.__data = node; // Attach node data
nodesGroup.add(node.__sphere = sphere);
if (positions[idx] !== undefined) {
sphere.position.set(positions[idx].x, positions[idx].y, positions[idx].z);
if (positions[node.id] !== undefined) {
sphere.position.set(positions[node.id].x, positions[node.id].y, positions[node.id].z);
}
});
@ -184,9 +193,9 @@ var redrawGraph = function () {
const sphere = node.__sphere;
if (!sphere) return;
sphere.position.x = positions[idx].x;
sphere.position.y = positions[idx].y || 0;
sphere.position.z = positions[idx].z || 0;
sphere.position.x = positions[node.id].x;
sphere.position.y = positions[node.id].y || 0;
sphere.position.z = positions[node.id].z || 0;
});
@ -196,20 +205,8 @@ var redrawGraph = function () {
linePos = line.geometry.attributes.position;
// TODO: move this index into map/cache or even into original graph data
let start, end;
for (let i = 0; i < graphData.nodes.length; i++) {
if (graphData.nodes[i].id === link.source) {
start = i;
break;
}
}
for (let i = 0; i < graphData.nodes.length; i++) {
if (graphData.nodes[i].id === link["target"]) {
end = i;
break;
}
}
let start = link.source;
let end = link.target;
linePos.array[0] = positions[start].x;
linePos.array[1] = positions[start].y || 0;

20
ws.go
View File

@ -5,8 +5,8 @@ import (
"log"
"net/http"
"github.com/divan/graph-experiments/graph"
"github.com/divan/graph-experiments/layout"
"github.com/divan/graphx/graph"
"github.com/divan/graphx/layout"
"github.com/gorilla/websocket"
)
@ -14,13 +14,14 @@ type WSServer struct {
upgrader websocket.Upgrader
hub []*websocket.Conn
Positions []*position
layout layout.Layout
//Positions []*position
Positions map[string]*layout.Object
layout *layout.Layout
graph *graph.Graph
propagation *PropagationLog
}
func NewWSServer(layout layout.Layout) *WSServer {
func NewWSServer(layout *layout.Layout) *WSServer {
ws := &WSServer{
upgrader: websocket.Upgrader{},
layout: layout,
@ -30,10 +31,11 @@ func NewWSServer(layout layout.Layout) *WSServer {
}
type WSResponse struct {
Type MsgType `json:"type"`
Positions []*position `json:"positions,omitempty"`
Graph json.RawMessage `json:"graph,omitempty"`
Propagation *PropagationLog `json:"propagation,omitempty"`
Type MsgType `json:"type"`
//Positions []*position `json:"positions,omitempty"`
Positions map[string]*layout.Object `json:"positions,omitempty"`
Graph json.RawMessage `json:"graph,omitempty"`
Propagation *PropagationLog `json:"propagation,omitempty"`
}
type WSRequest struct {

View File

@ -5,14 +5,15 @@ import (
"encoding/json"
"log"
"github.com/divan/graph-experiments/export"
"github.com/divan/graph-experiments/graph"
"github.com/divan/graphx/formats"
"github.com/divan/graphx/graph"
"github.com/gorilla/websocket"
)
func (ws *WSServer) sendGraphData(c *websocket.Conn) {
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 {
log.Fatal("Can't marshal graph to JSON")
}

View File

@ -1,11 +1,13 @@
package main
import "github.com/gorilla/websocket"
import (
"github.com/gorilla/websocket"
)
type position struct {
X int `json:"x"`
Y int `json:"y"`
Z int `json:"z"`
X float64 `json:"x"`
Y float64 `json:"y"`
Z float64 `json:"z"`
}
func (ws *WSServer) sendPositions(c *websocket.Conn) {
@ -19,17 +21,21 @@ func (ws *WSServer) sendPositions(c *websocket.Conn) {
func (ws *WSServer) updatePositions() {
// positions
nodes := ws.layout.Nodes()
positions := []*position{}
for i := 0; i < len(nodes); i++ {
pos := &position{
X: nodes[i].X,
Y: nodes[i].Y,
Z: nodes[i].Z,
nodes := ws.layout.Positions()
ws.Positions = nodes
/*
positions := []*position{}
for _, node := range nodes {
log.Println("node", node)
pos := &position{
X: node.X,
Y: node.Y,
Z: node.Z,
}
positions = append(positions, pos)
}
positions = append(positions, pos)
}
ws.Positions = positions
ws.Positions = positions
*/
ws.broadcastPositions()
}