Repository navigation
Expand file tree
/
Copy pathd3.go
More file actions
76 lines (58 loc) · 1.5 KB
/
Copy pathd3.go
File metadata and controls
76 lines (58 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package d3
import (
"net/url"
"github.com/gopherjs/gopherjs/js"
"honnef.co/go/js/util"
)
var d3 = js.Global.Get("d3")
type Selection struct {
js.Object
}
func Select(sel string) Selection {
return Selection{d3.Call("select", sel)}
}
func SelectAll(sel string) Selection {
return Selection{d3.Call("selectAll", sel)}
}
func (s Selection) Append(name string) Selection {
return Selection{s.Call("append", name)}
}
func (s Selection) Attr(name, value string) Selection {
return Selection{s.Call("attr", name, value)}
}
func (s Selection) Select(sel string) Selection {
return Selection{s.Call("select", sel)}
}
func (s Selection) SelectAll(sel string) Selection {
return Selection{s.Call("selectAll", sel)}
}
func (s Selection) Enter() Selection {
return Selection{s.Call("enter")}
}
func (s Selection) SetStyle(name, value string) {
if len(value) == 0 {
s.Call("style", name, nil)
} else {
s.Call("style", name, value)
}
}
type Data interface {
Underlying() js.Object
}
func (sel Selection) SetData(data []Data) Selection {
mapped := make([]js.Object, len(data))
for i, e := range data {
mapped[i] = e.Underlying()
}
return Selection{sel.Call("data", mapped)}
}
func JSON(url *url.URL, fn func(err error, data js.Object)) {
d3.Call("json", url.String(), func(err js.Object, data js.Object) {
fn(util.Error(err), data)
})
}
type Formatter func(js.Object) string
func Format(spec string) Formatter {
f := d3.Call("format", spec)
return func(o js.Object) string { return f.Invoke(o).Str() }
}