Fix graph description issue

This commit is contained in:
Ivan Danyliuk 2018-10-22 20:40:27 +02:00
parent 0e5e974d67
commit 04106c6fb0
No known key found for this signature in database
GPG Key ID: 97ED33CE024E1DBF
3 changed files with 47 additions and 3 deletions

View File

@ -1,8 +1,12 @@
package main
import (
"encoding/json"
"errors"
"io"
"runtime"
"github.com/divan/graphx/graph"
"github.com/gopherjs/vecty"
)
@ -37,3 +41,44 @@ func (p *Page) ApplyForces() {
p.webgl.updatePositions()
p.webgl.rt.Disable()
}
// GraphFromJSON is a custom version of graphx JSON importer, as we want to use
// some additional fields (Description).
// TODO(divan): that's probably can be done better within the limits of graphx library.
func GraphFromJSON(r io.Reader) (*graph.Graph, string, error) {
// decode into temporary struct to process
var res struct {
Description string `json:"description"`
Nodes []*graph.BasicNode `json:"nodes"`
Links []*struct {
Source string `json:"source"`
Target string `json:"target"`
} `json:"links"`
}
err := json.NewDecoder(r).Decode(&res)
if err != nil {
return nil, "", err
}
if len(res.Nodes) == 0 {
return nil, "", errors.New("empty graph")
}
// convert links IDs into indices
g := graph.NewGraphMN(len(res.Nodes), len(res.Links))
for _, node := range res.Nodes {
g.AddNode(node)
}
for _, link := range res.Links {
err := g.AddLink(link.Source, link.Target)
if err != nil {
return nil, "", err
}
}
g.UpdateCache()
return g, res.Description, nil
}

View File

@ -6,7 +6,6 @@ import (
"fmt"
"io"
"github.com/divan/graphx/formats"
"github.com/divan/graphx/graph"
)
@ -38,12 +37,11 @@ func LoadNetwork(file string) (*Network, error) {
// LoadNetworkFromReader loads network information from the io.Reader.
func LoadNetworkFromReader(r io.Reader) (*Network, error) {
g, err := formats.FromD3JSONReader(r)
g, desc, err := GraphFromJSON(r)
if err != nil {
return nil, fmt.Errorf("parse JSON: %v", err)
}
desc := "TBD"
return &Network{
Description: desc,
Data: g,

View File

@ -16,6 +16,7 @@ const DefaultNetwork = "grid25.json"
// NetworkSelector represents widget for choosing or uploading network topology
// to be used for visualization.
// TODO: move to widgets package
type NetworkSelector struct {
vecty.Core