-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
39e6e1c
commit 2daecef
Showing
46 changed files
with
7,497 additions
and
2,246 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package color | ||
|
||
import ( | ||
"encoding/json" | ||
"image/color" | ||
"strconv" | ||
) | ||
|
||
// ColorScale is sequence of colors and numerical anchors between them | ||
type ColorScale struct { | ||
Colors []color.RGBA | ||
Points []float64 | ||
} | ||
|
||
// ColorMapping of manual match of string value to color | ||
type ColorMapping map[string]color.RGBA | ||
|
||
// ColorConfigVal is configuration for single key on how to color its value | ||
type ColorConfigVal struct { | ||
ColorMapping *ColorMapping `json:"ColorMapping"` | ||
ColorScale *ColorScale `json:"ColorScale"` | ||
} | ||
|
||
// Color for a given value | ||
func (cv ColorConfigVal) Color(v interface{}) color.Color { | ||
// convert value to string | ||
var key string | ||
if vs, ok := v.(string); ok { | ||
key = vs | ||
} else { | ||
vs, err := json.Marshal(v) | ||
if err != nil { | ||
panic(err) | ||
} | ||
key = string(vs) | ||
} | ||
|
||
// first check manual values | ||
if cv.ColorMapping != nil { | ||
if c, ok := (*cv.ColorMapping)[key]; ok { | ||
return c | ||
} | ||
} | ||
|
||
// then check scale if present | ||
if cv.ColorScale != nil && len(cv.ColorScale.Points) > 0 && len(cv.ColorScale.Points) == (len(cv.ColorScale.Colors)-1) { | ||
if vs, err := strconv.ParseFloat(key, 64); err == nil { | ||
idx := 0 | ||
for idx < len(cv.ColorScale.Points) && cv.ColorScale.Points[idx] <= vs { | ||
idx++ | ||
} | ||
if idx >= len(cv.ColorScale.Colors) { | ||
idx = len(cv.ColorScale.Colors) - 1 | ||
} | ||
return cv.ColorScale.Colors[idx] | ||
} | ||
|
||
} | ||
|
||
return color.White | ||
} | ||
|
||
// ColorConfig defines how to translate arbitrary values to some color | ||
type ColorConfig map[string]ColorConfigVal | ||
|
||
// Color checks if config is found for that key, and computes color based on config | ||
func (c ColorConfig) Color(k string, v interface{}) color.Color { | ||
vc, ok := c[k] | ||
if !ok { | ||
return color.White | ||
} | ||
return vc.Color(v) | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package dot | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/nikolaydubina/jsonl-graph/graph" | ||
) | ||
|
||
type Renderable interface { | ||
Render() string | ||
} | ||
|
||
// Orientation should match Graphviz allowed values | ||
type Orientation string | ||
|
||
const ( | ||
LR Orientation = "LR" | ||
TB Orientation = "TB" | ||
) | ||
|
||
// BasicGraph renders graph to Dot without colors with simples syntax without HTML. | ||
// TODO: consider adding colors in background https://stackoverflow.com/questions/17765301/-dot-how-to-change-the-colour-of-one-record-in-multi-record-shape | ||
type BasicGraph struct { | ||
orientation Orientation | ||
nodes []Renderable | ||
edges []Renderable | ||
} | ||
|
||
// NewBasicGraph creates renderable graph from graph data | ||
func NewBasicGraph( | ||
graph graph.Graph, | ||
orientation Orientation, | ||
) BasicGraph { | ||
nodes := make([]Renderable, 0, len(graph.Nodes)) | ||
for _, n := range graph.Nodes { | ||
node := Node{id: n.ID(), shape: RecordShape, label: BasicNodeLabel{n: n}} | ||
nodes = append(nodes, node) | ||
} | ||
|
||
edges := make([]Renderable, 0, len(graph.Edges)) | ||
for _, e := range graph.Edges { | ||
edges = append(edges, BasicEdge{from: e.From(), to: e.To()}) | ||
} | ||
|
||
return BasicGraph{ | ||
orientation: orientation, | ||
nodes: nodes, | ||
edges: edges, | ||
} | ||
} | ||
|
||
func (r BasicGraph) Render() string { | ||
s := "digraph G {\n" | ||
s += "rankdir=" + string(r.orientation) + "\n" | ||
|
||
for _, n := range r.nodes { | ||
s += n.Render() + "\n" | ||
|
||
} | ||
|
||
for _, e := range r.edges { | ||
s += e.Render() + "\n" | ||
} | ||
|
||
s += "}\n" | ||
|
||
return s | ||
} | ||
|
||
type BasicEdge struct { | ||
from string | ||
to string | ||
} | ||
|
||
func (r BasicEdge) Render() string { | ||
return fmt.Sprintf(`"%s" -> "%s"`, r.from, r.to) | ||
} |
Oops, something went wrong.