opal-d3 0.0.20170205
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +2 -0
- data/Gemfile +3 -0
- data/README.md +7 -0
- data/Rakefile +31 -0
- data/d3.js +16393 -0
- data/demo/Gemfile +4 -0
- data/demo/README.md +7 -0
- data/demo/app/data/elections_2016.rb +10 -0
- data/demo/app/data/harry_potter.rb +19 -0
- data/demo/app/data/iphones.rb +29 -0
- data/demo/app/data/london_population.rb +26 -0
- data/demo/app/data/man_vs_horse.rb +55 -0
- data/demo/app/data/mtg_modern_colors.rb +96 -0
- data/demo/app/data/mtg_modern_creatures.rb +116 -0
- data/demo/app/data/olympics_2016_medals.rb +100 -0
- data/demo/app/data/paradox.rb +60 -0
- data/demo/app/data/polish_pms.rb +28 -0
- data/demo/app/data/star_trek_voyager.rb +183 -0
- data/demo/app/data/weather_in_london.rb +381 -0
- data/demo/app/elections_2016.rb +19 -0
- data/demo/app/harry_potter.rb +35 -0
- data/demo/app/iphones.rb +47 -0
- data/demo/app/london_population.rb +46 -0
- data/demo/app/london_population_area.rb +42 -0
- data/demo/app/man_vs_horse.rb +53 -0
- data/demo/app/mtg_modern_colors.rb +49 -0
- data/demo/app/mtg_modern_creatures.rb +63 -0
- data/demo/app/olympics_2016_medals.rb +54 -0
- data/demo/app/paradox.rb +57 -0
- data/demo/app/polish_pms.rb +53 -0
- data/demo/app/star_trek_voyager.rb +39 -0
- data/demo/app/weather_in_london.rb +62 -0
- data/demo/assets/d3.js +16393 -0
- data/demo/assets/style.css +0 -0
- data/demo/config.ru +39 -0
- data/demo/views/index.erb +21 -0
- data/demo/views/visualization.erb +29 -0
- data/lib/opal/d3/arc.rb +21 -0
- data/lib/opal/d3/area.rb +49 -0
- data/lib/opal/d3/axis.rb +77 -0
- data/lib/opal/d3/band_scale.rb +29 -0
- data/lib/opal/d3/collections.rb +10 -0
- data/lib/opal/d3/color.rb +78 -0
- data/lib/opal/d3/continuous_scale.rb +64 -0
- data/lib/opal/d3/creator.rb +11 -0
- data/lib/opal/d3/curve.rb +74 -0
- data/lib/opal/d3/dsv.rb +103 -0
- data/lib/opal/d3/ease.rb +319 -0
- data/lib/opal/d3/format.rb +97 -0
- data/lib/opal/d3/histograms.rb +44 -0
- data/lib/opal/d3/interpolate.rb +125 -0
- data/lib/opal/d3/line.rb +29 -0
- data/lib/opal/d3/map.rb +52 -0
- data/lib/opal/d3/misc.rb +15 -0
- data/lib/opal/d3/native.rb +84 -0
- data/lib/opal/d3/nest.rb +100 -0
- data/lib/opal/d3/ordinal_scale.rb +56 -0
- data/lib/opal/d3/path.rb +22 -0
- data/lib/opal/d3/pie.rb +23 -0
- data/lib/opal/d3/point_scale.rb +26 -0
- data/lib/opal/d3/polygon.rb +16 -0
- data/lib/opal/d3/quadtree.rb +95 -0
- data/lib/opal/d3/quantile_scale.rb +21 -0
- data/lib/opal/d3/quantize_scale.rb +23 -0
- data/lib/opal/d3/radial_area.rb +49 -0
- data/lib/opal/d3/radial_line.rb +29 -0
- data/lib/opal/d3/random.rb +12 -0
- data/lib/opal/d3/search.rb +28 -0
- data/lib/opal/d3/selection.rb +149 -0
- data/lib/opal/d3/sequential_scale.rb +96 -0
- data/lib/opal/d3/set.rb +33 -0
- data/lib/opal/d3/stack.rb +11 -0
- data/lib/opal/d3/statistics.rb +81 -0
- data/lib/opal/d3/symbol.rb +70 -0
- data/lib/opal/d3/threshold_scale.rb +23 -0
- data/lib/opal/d3/time_format.rb +48 -0
- data/lib/opal/d3/time_interval.rb +81 -0
- data/lib/opal/d3/transformations.rb +13 -0
- data/lib/opal/d3/version.rb +5 -0
- data/lib/opal/d3.rb +62 -0
- data/lib/opal-d3.rb +9 -0
- data/opal-d3.gemspec +20 -0
- data/spec/arc_spec.rb +86 -0
- data/spec/area_spec.rb +102 -0
- data/spec/axis_spec.rb +174 -0
- data/spec/band_scale_spec.rb +73 -0
- data/spec/color_spec.rb +74 -0
- data/spec/continuous_scale_spec.rb +217 -0
- data/spec/coverage_spec.rb +23 -0
- data/spec/creator_spec.rb +15 -0
- data/spec/curve_spec.rb +214 -0
- data/spec/dsv_spec.rb +194 -0
- data/spec/ease_spec.rb +370 -0
- data/spec/format_spec.rb +87 -0
- data/spec/histograms_spec.rb +61 -0
- data/spec/html/d3.js +16393 -0
- data/spec/html/index.html.erb +12 -0
- data/spec/interpolate_spec.rb +152 -0
- data/spec/line_spec.rb +58 -0
- data/spec/map_spec.rb +80 -0
- data/spec/misc_spec.rb +19 -0
- data/spec/nest_spec.rb +89 -0
- data/spec/objects_spec.rb +22 -0
- data/spec/ordinal_scale_spec.rb +59 -0
- data/spec/path_spec.rb +65 -0
- data/spec/pie_spec.rb +114 -0
- data/spec/point_scale_spec.rb +58 -0
- data/spec/polygon_spec.rb +51 -0
- data/spec/quadtree_spec.rb +128 -0
- data/spec/quantile_scale_spec.rb +24 -0
- data/spec/quantize_scale_spec.rb +40 -0
- data/spec/radial_area_spec.rb +127 -0
- data/spec/radial_line_spec.rb +54 -0
- data/spec/random_spec.rb +34 -0
- data/spec/search_spec.rb +69 -0
- data/spec/selection_data_spec.rb +71 -0
- data/spec/selection_manipulation_spec.rb +179 -0
- data/spec/selection_spec.rb +214 -0
- data/spec/sequential_scale_spec.rb +90 -0
- data/spec/set_spec.rb +57 -0
- data/spec/spec_helper.rb +2 -0
- data/spec/stack_spec.rb +23 -0
- data/spec/statistics_spec.rb +65 -0
- data/spec/symbol_spec.rb +121 -0
- data/spec/threshold_scale_spec.rb +28 -0
- data/spec/time_format_spec.rb +99 -0
- data/spec/time_interval_spec.rb +304 -0
- data/spec/transformations_spec.rb +51 -0
- metadata +258 -0
@@ -0,0 +1,179 @@
|
|
1
|
+
describe "d3 - selection - DOM manipulation" do
|
2
|
+
after(:each) do
|
3
|
+
D3.select("#test-area").html("")
|
4
|
+
end
|
5
|
+
let(:root) { D3.select("#test-area") }
|
6
|
+
let(:html) { root.html }
|
7
|
+
|
8
|
+
describe do
|
9
|
+
before(:each) do
|
10
|
+
D3.select("div")
|
11
|
+
.select_all("span")
|
12
|
+
.data(%W[a b c d e])
|
13
|
+
.enter
|
14
|
+
.append("span")
|
15
|
+
.attr("class"){|d| d}
|
16
|
+
end
|
17
|
+
let(:classes) {
|
18
|
+
html.scan(/<span.*?<\/span>/).map{|x| x[/class="([^"]*)"/, 1] || "" }
|
19
|
+
}
|
20
|
+
|
21
|
+
it "selection.raise" do
|
22
|
+
D3.select(".c").raise
|
23
|
+
expect(classes).to eq(["a", "b", "d", "e", "c"])
|
24
|
+
end
|
25
|
+
|
26
|
+
it "selection.lower" do
|
27
|
+
D3.select(".c").lower
|
28
|
+
expect(classes).to eq(["c", "a", "b", "d", "e"])
|
29
|
+
end
|
30
|
+
|
31
|
+
it "selection.classed - add" do
|
32
|
+
D3.select_all("span").classed("a b", true)
|
33
|
+
expect(classes).to eq(["a b", "b a", "c a b", "d a b", "e a b"])
|
34
|
+
end
|
35
|
+
|
36
|
+
it "selection.classed - remove" do
|
37
|
+
D3.select_all("span").classed("a b", false)
|
38
|
+
expect(classes).to eq(["", "", "c", "d", "e"])
|
39
|
+
end
|
40
|
+
|
41
|
+
it "selection.classed - function" do
|
42
|
+
D3.select_all("span").classed("x"){|d,i| i.even?}
|
43
|
+
expect(classes).to eq(["a x", "b", "c x", "d", "e x"])
|
44
|
+
end
|
45
|
+
|
46
|
+
it "selection.classed - query" do
|
47
|
+
expect(D3.select(".c").classed("a")).to eq(false)
|
48
|
+
expect(D3.select(".c").classed("c")).to eq(true)
|
49
|
+
expect(D3.select(".c").classed("b c")).to eq(false)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
it "selection.attr / selection.style" do
|
54
|
+
d = D3.select_all("div")
|
55
|
+
d.append("p").attr("class", "big").style("color", "red")
|
56
|
+
expect(d.html).to eq(%Q[<p class="big" style="color: red;"></p>])
|
57
|
+
|
58
|
+
p = d.select_all("p")
|
59
|
+
expect(p.attr("class")).to eq("big")
|
60
|
+
expect(p.style("color")).to eq("rgb(255, 0, 0)")
|
61
|
+
end
|
62
|
+
|
63
|
+
it "selection.style priority" do
|
64
|
+
d = D3.select_all("div")
|
65
|
+
d.append("p").style("color", "red", "important")
|
66
|
+
expect(d.html).to eq(%Q[<p style="color: red !important;"></p>])
|
67
|
+
end
|
68
|
+
|
69
|
+
it "selection.property" do
|
70
|
+
d = D3.select_all("div")
|
71
|
+
d.html("<input type=radio name=a class=x><input type=radio name=a class=y>")
|
72
|
+
expect(D3.select(".x").property("checked")).to eq(false)
|
73
|
+
expect(D3.select(".y").property("checked")).to eq(false)
|
74
|
+
|
75
|
+
D3.select(".x").property("checked", true)
|
76
|
+
expect(D3.select(".x").property("checked")).to eq(true)
|
77
|
+
expect(D3.select(".y").property("checked")).to eq(false)
|
78
|
+
|
79
|
+
D3.select(".y").property("checked", true)
|
80
|
+
expect(D3.select(".x").property("checked")).to eq(false)
|
81
|
+
expect(D3.select(".y").property("checked")).to eq(true)
|
82
|
+
|
83
|
+
expect(d.html).to eq(
|
84
|
+
%Q[<input type="radio" name="a" class="x"><input type="radio" name="a" class="y">]
|
85
|
+
)
|
86
|
+
end
|
87
|
+
|
88
|
+
describe do
|
89
|
+
before(:each) do
|
90
|
+
D3.select("div")
|
91
|
+
.select_all("span")
|
92
|
+
.data(%W[a b c d e])
|
93
|
+
.enter
|
94
|
+
.append("span")
|
95
|
+
.attr("class"){|d| d}
|
96
|
+
end
|
97
|
+
|
98
|
+
it "selection.append name" do
|
99
|
+
D3.select_all("div").append("i")
|
100
|
+
expect(html).to eq(
|
101
|
+
%Q[<span class="a"></span><span class="b"></span><span class="c"></span><span class="d"></span><span class="e"></span><i></i>]
|
102
|
+
)
|
103
|
+
end
|
104
|
+
|
105
|
+
it "selection.append dom element" do
|
106
|
+
D3.select_all("div").append{ `document.createElement("b")` }
|
107
|
+
expect(html).to eq(
|
108
|
+
%Q[<span class="a"></span><span class="b"></span><span class="c"></span><span class="d"></span><span class="e"></span><b></b>]
|
109
|
+
)
|
110
|
+
end
|
111
|
+
|
112
|
+
it "selection.insert name" do
|
113
|
+
D3.select_all("div").insert("i")
|
114
|
+
expect(html).to eq(
|
115
|
+
%Q[<span class="a"></span><span class="b"></span><span class="c"></span><span class="d"></span><span class="e"></span><i></i>]
|
116
|
+
)
|
117
|
+
end
|
118
|
+
|
119
|
+
it "selection.insert dom element" do
|
120
|
+
D3.select_all("div").insert{ `document.createElement("b")` }
|
121
|
+
expect(html).to eq(
|
122
|
+
%Q[<span class="a"></span><span class="b"></span><span class="c"></span><span class="d"></span><span class="e"></span><b></b>]
|
123
|
+
)
|
124
|
+
end
|
125
|
+
|
126
|
+
it "selection.insert before" do
|
127
|
+
D3.select_all("div").insert("i", ".c")
|
128
|
+
expect(html).to eq(
|
129
|
+
%Q[<span class="a"></span><span class="b"></span><i></i><span class="c"></span><span class="d"></span><span class="e"></span>]
|
130
|
+
)
|
131
|
+
end
|
132
|
+
|
133
|
+
it "selection.insert before dom element" do
|
134
|
+
D3.select_all("div").insert("i"){ `document.getElementsByClassName("d")[0]` }
|
135
|
+
expect(html).to eq(
|
136
|
+
%Q[<span class="a"></span><span class="b"></span><span class="c"></span><i></i><span class="d"></span><span class="e"></span>]
|
137
|
+
)
|
138
|
+
end
|
139
|
+
|
140
|
+
it "selection.insert before - two functions" do
|
141
|
+
D3.select_all("div").insert(
|
142
|
+
proc{`document.createElement("b")`},
|
143
|
+
proc{`document.getElementsByClassName("d")[0]`}
|
144
|
+
)
|
145
|
+
expect(html).to eq(
|
146
|
+
%Q[<span class="a"></span><span class="b"></span><span class="c"></span><b></b><span class="d"></span><span class="e"></span>]
|
147
|
+
)
|
148
|
+
end
|
149
|
+
|
150
|
+
it "selection.remove" do
|
151
|
+
D3.select(".c").remove
|
152
|
+
expect(html).to eq(
|
153
|
+
%Q[<span class="a"></span><span class="b"></span><span class="d"></span><span class="e"></span>]
|
154
|
+
)
|
155
|
+
end
|
156
|
+
|
157
|
+
# This interface is somewhat awkward
|
158
|
+
it "selection.remove and add back" do
|
159
|
+
sel = D3.select(".c").remove
|
160
|
+
D3.select(".b").append{ sel.node.to_n }
|
161
|
+
expect(html).to eq(
|
162
|
+
%Q[<span class="a"></span><span class="b"><span class="c"></span></span><span class="d"></span><span class="e"></span>]
|
163
|
+
)
|
164
|
+
end
|
165
|
+
|
166
|
+
it "selection.sort" do
|
167
|
+
# ???
|
168
|
+
end
|
169
|
+
|
170
|
+
it "selection.order" do
|
171
|
+
# ???
|
172
|
+
end
|
173
|
+
end
|
174
|
+
end
|
175
|
+
|
176
|
+
__END__
|
177
|
+
|
178
|
+
selection.sort - sort elements in the document based on data.
|
179
|
+
selection.order - reorders elements in the document to match the selection.
|
@@ -0,0 +1,214 @@
|
|
1
|
+
describe "d3 - selection" do
|
2
|
+
after(:each) do
|
3
|
+
D3.select("#test-area").html("")
|
4
|
+
end
|
5
|
+
|
6
|
+
it "d3.selection" do
|
7
|
+
s = D3.selection
|
8
|
+
expect(s).to be_instance_of(D3::Selection)
|
9
|
+
expect(s.size).to eq(1)
|
10
|
+
end
|
11
|
+
|
12
|
+
it "d3.select" do
|
13
|
+
s = D3.select("div")
|
14
|
+
expect(s).to be_instance_of(D3::Selection)
|
15
|
+
expect(s.size).to eq(1)
|
16
|
+
expect(s.empty?).to eq(false)
|
17
|
+
|
18
|
+
s = D3.select("h6")
|
19
|
+
expect(s).to be_instance_of(D3::Selection)
|
20
|
+
expect(s.size).to eq(0)
|
21
|
+
expect(s.empty?).to eq(true)
|
22
|
+
end
|
23
|
+
|
24
|
+
it "d3.select_all" do
|
25
|
+
s = D3.select_all("div")
|
26
|
+
expect(s).to be_instance_of(D3::Selection)
|
27
|
+
expect(s.size).to eq(1)
|
28
|
+
expect(s.empty?).to eq(false)
|
29
|
+
|
30
|
+
s = D3.select_all("h6")
|
31
|
+
expect(s).to be_instance_of(D3::Selection)
|
32
|
+
expect(s.size).to eq(0)
|
33
|
+
expect(s.empty?).to eq(true)
|
34
|
+
end
|
35
|
+
|
36
|
+
describe "nested selections" do
|
37
|
+
before(:each) do
|
38
|
+
D3.select("div").html("
|
39
|
+
<p><b>1</b><b>2</b><b>3</b></p>
|
40
|
+
<p><b>4</b><b>5</b><b>6</b></p>
|
41
|
+
")
|
42
|
+
end
|
43
|
+
|
44
|
+
it "selection.select" do
|
45
|
+
expect(D3.select("p").select("b").size).to eq(1)
|
46
|
+
expect(D3.select_all("p").select("b").size).to eq(2)
|
47
|
+
end
|
48
|
+
|
49
|
+
it "selection.select_all" do
|
50
|
+
expect(D3.select("p").select_all("b").size).to eq(3)
|
51
|
+
expect(D3.select_all("p").select_all("b").size).to eq(6)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
describe "selection.filter - selector string" do
|
56
|
+
before(:each) do
|
57
|
+
D3.select("div").html("
|
58
|
+
<span class='a b'>1</span>
|
59
|
+
<span class='b c'>2</span>
|
60
|
+
<span class='c d'>3</span>
|
61
|
+
")
|
62
|
+
end
|
63
|
+
let(:a) { D3.select_all("span.a") }
|
64
|
+
let(:b) { D3.select_all("span.b") }
|
65
|
+
let(:c) { D3.select_all("span.c") }
|
66
|
+
let(:d) { D3.select_all("span.d") }
|
67
|
+
|
68
|
+
it do
|
69
|
+
expect(b.filter(".c").size).to eq(1)
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
describe "selection.filter - filter" do
|
74
|
+
before(:each) do
|
75
|
+
D3.select("div")
|
76
|
+
.select_all("span")
|
77
|
+
.data(%W[a b c d])
|
78
|
+
.enter
|
79
|
+
.append("span")
|
80
|
+
.text{|d| d}
|
81
|
+
end
|
82
|
+
|
83
|
+
it "function" do
|
84
|
+
D3.select_all("span").filter{|d| d =~ /[bc]/}.attr("class", "x")
|
85
|
+
expect(D3.select("div").html).to eq(
|
86
|
+
%Q[<span class="x">a</span><span>b</span><span>c</span><span class="x">d</span>]
|
87
|
+
)
|
88
|
+
end
|
89
|
+
|
90
|
+
it "function with index" do
|
91
|
+
D3.select_all("span").filter{|d,i| i.even?}.attr("class", "y")
|
92
|
+
expect(D3.select("div").html).to eq(
|
93
|
+
%Q[<span class="y">a</span><span>b</span><span class="y">c</span><span>d</span>]
|
94
|
+
)
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
describe "selection.each" do
|
99
|
+
before(:each) do
|
100
|
+
D3.select("div")
|
101
|
+
.select_all("span")
|
102
|
+
.data(%W[a b c d])
|
103
|
+
.enter
|
104
|
+
.append("span")
|
105
|
+
.text{|d| d}
|
106
|
+
end
|
107
|
+
it do
|
108
|
+
results = []
|
109
|
+
D3.select_all("span").each do |n|
|
110
|
+
results << n
|
111
|
+
end
|
112
|
+
expect(results).to eq(["a", "b", "c", "d"])
|
113
|
+
end
|
114
|
+
|
115
|
+
it do
|
116
|
+
results = []
|
117
|
+
D3.select_all("span").each do |n,i|
|
118
|
+
results << [n,i]
|
119
|
+
end
|
120
|
+
expect(results).to eq([["a", 0], ["b", 1], ["c", 2], ["d", 3]])
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
124
|
+
it "selection.append" do
|
125
|
+
div = D3.select_all("div")
|
126
|
+
ul = div.append("ul")
|
127
|
+
ul.append("li")
|
128
|
+
ul.append("li")
|
129
|
+
expect(div.html).to eq("<ul><li></li><li></li></ul>")
|
130
|
+
ul.select_all("li").text = "WOW"
|
131
|
+
expect(div.html).to eq("<ul><li>WOW</li><li>WOW</li></ul>")
|
132
|
+
end
|
133
|
+
|
134
|
+
it "selection.html / selection.text" do
|
135
|
+
div = D3.select_all("div")
|
136
|
+
expect(div.html).to eq("")
|
137
|
+
expect(div.text).to eq("")
|
138
|
+
|
139
|
+
div.html = "<h1>Hello, World!</h1>"
|
140
|
+
expect(div.html).to eq("<h1>Hello, World!</h1>")
|
141
|
+
expect(div.text).to eq("Hello, World!")
|
142
|
+
|
143
|
+
h1 = div.select("h1")
|
144
|
+
expect(h1.text).to eq("Hello, World!")
|
145
|
+
h1.text = "Goodbye, World!"
|
146
|
+
expect(div.html).to eq("<h1>Goodbye, World!</h1>")
|
147
|
+
expect(div.text).to eq("Goodbye, World!")
|
148
|
+
expect(h1.html).to eq("Goodbye, World!")
|
149
|
+
expect(h1.text).to eq("Goodbye, World!")
|
150
|
+
end
|
151
|
+
|
152
|
+
it "svg" do
|
153
|
+
D3.select("div")
|
154
|
+
.append("svg")
|
155
|
+
.attr("width", 960)
|
156
|
+
.attr("height", 500)
|
157
|
+
.append("g")
|
158
|
+
.attr("transform", "translate(20,20)")
|
159
|
+
.append("rect")
|
160
|
+
.attr("width", 920)
|
161
|
+
.attr("height", 460)
|
162
|
+
expect(D3.select("div").html).to eq(
|
163
|
+
%Q[<svg width="960" height="500"><g transform="translate(20,20)"><rect width="920" height="460"></rect></g></svg>])
|
164
|
+
end
|
165
|
+
|
166
|
+
describe do
|
167
|
+
before(:each) do
|
168
|
+
D3.select("div").html("
|
169
|
+
<p><b>1</b><b>2</b><b>3</b></p>
|
170
|
+
<p><b>4</b><b>5</b><b>6</b></p>
|
171
|
+
")
|
172
|
+
end
|
173
|
+
|
174
|
+
# These should use opal-browser, but that seems to be broken with phantomjs
|
175
|
+
# For now just expose raw js objects
|
176
|
+
it "selection.node" do
|
177
|
+
expect(D3.select_all("b").node).to be_instance_of(Native::Object)
|
178
|
+
end
|
179
|
+
|
180
|
+
it "selection.nodes" do
|
181
|
+
expect(D3.select_all("b").nodes).to be_instance_of(Array)
|
182
|
+
D3.select_all("b").nodes.each do |n|
|
183
|
+
expect(n).to be_instance_of(Native::Object)
|
184
|
+
end
|
185
|
+
end
|
186
|
+
end
|
187
|
+
end
|
188
|
+
|
189
|
+
__END__
|
190
|
+
|
191
|
+
# A lot of them have multiple interfaces, don't mark as done until sure
|
192
|
+
|
193
|
+
d3.selection - select the root document element.
|
194
|
+
d3.matcher - test whether an element matches a selector.
|
195
|
+
d3.selector - select an element.
|
196
|
+
d3.selectorAll - select elements.
|
197
|
+
d3.window - get a node’s owner window.
|
198
|
+
selection.merge - merge this selection with another.
|
199
|
+
|
200
|
+
selection.on - add or remove event listeners.
|
201
|
+
selection.dispatch - dispatch a custom event.
|
202
|
+
d3.event - the current user event, during interaction.
|
203
|
+
d3.customEvent - temporarily define a custom event.
|
204
|
+
d3.mouse - get the mouse position relative to a given container.
|
205
|
+
d3.touch - get a touch position relative to a given container.
|
206
|
+
d3.touches - get the touch positions relative to a given container.
|
207
|
+
|
208
|
+
selection.call - call a function with this selection.
|
209
|
+
|
210
|
+
d3.local - declares a new local variable.
|
211
|
+
local.set - set a local variable’s value.
|
212
|
+
local.get - get a local variable’s value.
|
213
|
+
local.remove - delete a local variable.
|
214
|
+
local.toString - get the property identifier of a local variable.
|
@@ -0,0 +1,90 @@
|
|
1
|
+
describe "d3 - sequential scale" do
|
2
|
+
let(:scale) { D3.scale_sequential(&D3.send(interpolator)).domain([0, 100]) }
|
3
|
+
let(:curve_direct) { (0..20).map{|t| D3.send(interpolator, t/20.0)} }
|
4
|
+
let(:curve_scale) { (0..20).map{|t| scale.(t*100/20.0)} }
|
5
|
+
|
6
|
+
it "d3.scale_sequential" do
|
7
|
+
expect(D3.scale_sequential(&D3.interpolate_viridis)).to be_instance_of(D3::SequentialScale)
|
8
|
+
expect(D3.scale_sequential{|t| D3.hsl(t * 360, 1, 0.5) }).to be_instance_of(D3::SequentialScale)
|
9
|
+
end
|
10
|
+
|
11
|
+
it "basics" do
|
12
|
+
s = D3.scale_sequential{|t| D3.hsl(t * 360, 1, 0.5).to_s }
|
13
|
+
expect(s.domain).to eq([0,1])
|
14
|
+
s2 = s.copy.domain([0,100]).clamp(true)
|
15
|
+
expect(s.domain).to eq([0,1])
|
16
|
+
expect(s2.domain).to eq([0,100])
|
17
|
+
expect(s.clamp).to eq(false)
|
18
|
+
expect(s2.clamp).to eq(true)
|
19
|
+
expect(s.(0.2)).to eq("rgb(204, 255, 0)")
|
20
|
+
expect(s.(1.2)).to eq("rgb(204, 255, 0)")
|
21
|
+
expect(s2.(20)).to eq("rgb(204, 255, 0)")
|
22
|
+
expect(s2.(120)).to eq("rgb(255, 0, 0)")
|
23
|
+
expect(s.interpolator.(0.3)).to eq("rgb(51, 255, 0)")
|
24
|
+
expect(s2.interpolator.(0.3)).to eq("rgb(51, 255, 0)")
|
25
|
+
end
|
26
|
+
|
27
|
+
describe "viridis" do
|
28
|
+
let(:interpolator) { :interpolate_viridis }
|
29
|
+
it do
|
30
|
+
expect(curve_direct).to eq(curve_scale)
|
31
|
+
expect(curve_scale).to eq(["#440154", "#471365", "#482475", "#463480", "#414487", "#3b528b", "#355f8d", "#2f6c8e", "#2a788e", "#25848e", "#21918c", "#1e9c89", "#22a884", "#2fb47c", "#44bf70", "#5ec962", "#7ad151", "#9bd93c", "#bddf26", "#dfe318", "#fde725"])
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
describe "inferno" do
|
36
|
+
let(:interpolator) { :interpolate_inferno }
|
37
|
+
it do
|
38
|
+
expect(curve_direct).to eq(curve_scale)
|
39
|
+
expect(curve_scale).to eq(["#000004", "#07051b", "#160b39", "#2b0b57", "#420a68", "#57106e", "#6a176e", "#7f1e6c", "#932667", "#a82e5f", "#bc3754", "#cc4248", "#dd513a", "#ea632a", "#f37819", "#f98e09", "#fca50a", "#fbbe23", "#f6d746", "#f1ef75", "#fcffa4"])
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
describe "magma" do
|
44
|
+
let(:interpolator) { :interpolate_magma }
|
45
|
+
it do
|
46
|
+
expect(curve_direct).to eq(curve_scale)
|
47
|
+
expect(curve_scale).to eq(["#000004", "#06051a", "#140e36", "#251255", "#3b0f70", "#51127c", "#641a80", "#782281", "#8c2981", "#a1307e", "#b73779", "#ca3e72", "#de4968", "#ed5a5f", "#f7705c", "#fc8961", "#fe9f6d", "#feb77e", "#fecf92", "#fde7a9", "#fcfdbf"])
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
describe "plasma" do
|
52
|
+
let(:interpolator) { :interpolate_plasma }
|
53
|
+
it do
|
54
|
+
expect(curve_direct).to eq(curve_scale)
|
55
|
+
expect(curve_scale).to eq(["#0d0887", "#2a0593", "#41049d", "#5601a4", "#6a00a8", "#7e03a8", "#8f0da4", "#a11b9b", "#b12a90", "#bf3984", "#cc4778", "#d6556d", "#e16462", "#ea7457", "#f2844b", "#f89540", "#fca636", "#feba2c", "#fcce25", "#f7e425", "#f0f921"])
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
describe "warm" do
|
60
|
+
let(:interpolator) { :interpolate_warm }
|
61
|
+
it do
|
62
|
+
expect(curve_direct).to eq(curve_scale)
|
63
|
+
expect(curve_scale).to eq(["rgb(110, 64, 170)", "rgb(129, 62, 176)", "rgb(150, 61, 179)", "rgb(171, 60, 178)", "rgb(191, 60, 175)", "rgb(210, 62, 167)", "rgb(228, 65, 157)", "rgb(242, 69, 145)", "rgb(254, 75, 131)", "rgb(255, 84, 115)", "rgb(255, 94, 99)", "rgb(255, 106, 84)", "rgb(255, 120, 71)", "rgb(255, 135, 59)", "rgb(251, 150, 51)", "rgb(239, 167, 47)", "rgb(226, 183, 47)", "rgb(212, 199, 51)", "rgb(198, 214, 60)", "rgb(186, 228, 73)", "rgb(175, 240, 91)"])
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
describe "cool" do
|
68
|
+
let(:interpolator) { :interpolate_cool }
|
69
|
+
it do
|
70
|
+
expect(curve_direct).to eq(curve_scale)
|
71
|
+
expect(curve_scale).to eq(["rgb(110, 64, 170)", "rgb(104, 73, 186)", "rgb(96, 84, 200)", "rgb(87, 97, 211)", "rgb(76, 110, 219)", "rgb(65, 125, 224)", "rgb(54, 140, 225)", "rgb(44, 156, 223)", "rgb(35, 171, 216)", "rgb(29, 186, 206)", "rgb(26, 199, 194)", "rgb(26, 212, 179)", "rgb(29, 223, 163)", "rgb(37, 232, 146)", "rgb(48, 239, 130)", "rgb(64, 243, 115)", "rgb(82, 246, 103)", "rgb(103, 247, 94)", "rgb(127, 246, 88)", "rgb(151, 243, 87)", "rgb(175, 240, 91)"])
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
describe "rainbow" do
|
76
|
+
let(:interpolator) { :interpolate_rainbow }
|
77
|
+
it do
|
78
|
+
expect(curve_direct).to eq(curve_scale)
|
79
|
+
expect(curve_scale).to eq(["rgb(110, 64, 170)", "rgb(150, 61, 179)", "rgb(191, 60, 175)", "rgb(228, 65, 157)", "rgb(254, 75, 131)", "rgb(255, 94, 99)", "rgb(255, 120, 71)", "rgb(251, 150, 51)", "rgb(226, 183, 47)", "rgb(198, 214, 60)", "rgb(175, 240, 91)", "rgb(127, 246, 88)", "rgb(82, 246, 103)", "rgb(48, 239, 130)", "rgb(29, 223, 163)", "rgb(26, 199, 194)", "rgb(35, 171, 216)", "rgb(54, 140, 225)", "rgb(76, 110, 219)", "rgb(96, 84, 200)", "rgb(110, 64, 170)"])
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
describe "cubehelix_default" do
|
84
|
+
let(:interpolator) { :interpolate_cubehelix_default }
|
85
|
+
it do
|
86
|
+
expect(curve_direct).to eq(curve_scale)
|
87
|
+
expect(curve_scale).to eq(["rgb(0, 0, 0)", "rgb(18, 8, 23)", "rgb(26, 21, 48)", "rgb(26, 39, 68)", "rgb(22, 61, 78)", "rgb(22, 83, 76)", "rgb(31, 102, 66)", "rgb(52, 115, 53)", "rgb(84, 121, 47)", "rgb(122, 122, 53)", "rgb(160, 121, 73)", "rgb(190, 121, 106)", "rgb(208, 126, 147)", "rgb(212, 138, 186)", "rgb(207, 156, 218)", "rgb(199, 179, 237)", "rgb(193, 202, 243)", "rgb(197, 222, 242)", "rgb(210, 238, 239)", "rgb(232, 248, 242)", "rgb(255, 255, 255)"])
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
data/spec/set_spec.rb
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
describe "d3-collections - sets" do
|
2
|
+
it "d3.set" do
|
3
|
+
expect(D3.set).to be_instance_of(D3::Set)
|
4
|
+
expect(D3.set.values).to eq([])
|
5
|
+
expect(D3.set([1,-2,3,1]).values).to eq(["1","-2","3"])
|
6
|
+
expect(D3.set([1,-2,3,-1], &:abs).values).to eq(["1","2","3"])
|
7
|
+
end
|
8
|
+
|
9
|
+
it "set.has?" do
|
10
|
+
expect(D3.set(["1"]).has?("2")).to eq(false)
|
11
|
+
# everything converted to strings
|
12
|
+
expect(D3.set(["1"]).has?("1")).to eq(true)
|
13
|
+
expect(D3.set([1]).has?(1)).to eq(true)
|
14
|
+
expect(D3.set([1]).has?("1")).to eq(true)
|
15
|
+
expect(D3.set(["1"]).has?(1)).to eq(true)
|
16
|
+
end
|
17
|
+
|
18
|
+
it "set.add" do
|
19
|
+
s = D3.set()
|
20
|
+
s.add(1).add(2).add(3).add(1)
|
21
|
+
expect(s.values).to eq(["1", "2", "3"])
|
22
|
+
end
|
23
|
+
|
24
|
+
it "set.clear" do
|
25
|
+
s = D3.set()
|
26
|
+
s.add(1).add(2).add(3).clear().add(1)
|
27
|
+
expect(s.values).to eq(["1"])
|
28
|
+
end
|
29
|
+
|
30
|
+
it "set.values" do
|
31
|
+
expect(D3.set().values).to eq([])
|
32
|
+
expect(D3.set([1]).values).to eq(["1"])
|
33
|
+
expect(D3.set([1,2,"1","2",3]).values).to eq(["1","2","3"])
|
34
|
+
end
|
35
|
+
|
36
|
+
it "set.each" do
|
37
|
+
called = []
|
38
|
+
D3.set([1,2,3]).each{|x|
|
39
|
+
called << x
|
40
|
+
}
|
41
|
+
expect(called).to eq(["1","2","3"])
|
42
|
+
end
|
43
|
+
|
44
|
+
it "set.empty?" do
|
45
|
+
a = D3.set()
|
46
|
+
expect(a).to be_empty
|
47
|
+
b = D3.set([1])
|
48
|
+
expect(b).to_not be_empty
|
49
|
+
end
|
50
|
+
|
51
|
+
it "set.size" do
|
52
|
+
a = D3.set()
|
53
|
+
expect(a.size).to eq(0)
|
54
|
+
b = D3.set([1,2,3])
|
55
|
+
expect(b.size).to eq(3)
|
56
|
+
end
|
57
|
+
end
|
data/spec/spec_helper.rb
ADDED
data/spec/stack_spec.rb
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
describe "d3 - stack" do
|
2
|
+
it "d3.stack" do
|
3
|
+
expect(D3.stack).to be_instance_of(D3::StackGenerator)
|
4
|
+
end
|
5
|
+
end
|
6
|
+
|
7
|
+
__END__
|
8
|
+
|
9
|
+
d3.stack - create a new stack generator.
|
10
|
+
stack - generate a stack for the given dataset.
|
11
|
+
stack.keys - set the keys accessor.
|
12
|
+
stack.value - set the value accessor.
|
13
|
+
stack.order - set the order accessor.
|
14
|
+
stack.offset - set the offset accessor.
|
15
|
+
d3.stackOrderAscending - put the smallest series on bottom.
|
16
|
+
d3.stackOrderDescending - put the largest series on bottom.
|
17
|
+
d3.stackOrderInsideOut - put larger series in the middle.
|
18
|
+
d3.stackOrderNone - use the given series order.
|
19
|
+
d3.stackOrderReverse - use the reverse of the given series order.
|
20
|
+
d3.stackOffsetExpand - normalize the baseline to zero and topline to one.
|
21
|
+
d3.stackOffsetNone - apply a zero baseline.
|
22
|
+
d3.stackOffsetSilhouette - center the streamgraph around zero.
|
23
|
+
d3.stackOffsetWiggle - minimize streamgraph wiggling.
|
@@ -0,0 +1,65 @@
|
|
1
|
+
describe "d3-array - statistics" do
|
2
|
+
it "d3.min" do
|
3
|
+
expect(D3.min([2,1,3])).to eq(1)
|
4
|
+
expect(D3.max([2,-1,-3,0], &:-@)).to eq(3)
|
5
|
+
expect(D3.min([2,1,nil,3])).to eq(1)
|
6
|
+
expect(D3.min(["20","3"])).to eq("20")
|
7
|
+
expect(D3.min([20,3])).to eq(3)
|
8
|
+
end
|
9
|
+
|
10
|
+
it "d3.max" do
|
11
|
+
expect(D3.max([2,1,3,0])).to eq(3)
|
12
|
+
expect(D3.max([2,-1,-3,0], &:abs)).to eq(3)
|
13
|
+
end
|
14
|
+
|
15
|
+
it "d3.extent" do
|
16
|
+
expect(D3.extent([2,-1,3])).to eq([-1,3])
|
17
|
+
expect(D3.extent([2,-1,nil,3])).to eq([-1,3])
|
18
|
+
expect(D3.extent([2,-1,3], &:abs)).to eq([1,3])
|
19
|
+
expect(D3.extent([1])).to eq([1,1])
|
20
|
+
expect(D3.extent([])).to eq([nil,nil])
|
21
|
+
end
|
22
|
+
|
23
|
+
it "d3.sum" do
|
24
|
+
expect(D3.sum([1,2,3])).to eq(6)
|
25
|
+
expect(D3.sum([1,2,3,nil])).to eq(6)
|
26
|
+
expect(D3.sum([1,2,3]){|x| x**2}).to eq(1+4+9)
|
27
|
+
expect(D3.sum([])).to eq(0)
|
28
|
+
end
|
29
|
+
|
30
|
+
it "d3.mean" do
|
31
|
+
expect(D3.mean([1,2,3])).to eq(2)
|
32
|
+
expect(D3.mean([1,2,3,nil])).to eq(2)
|
33
|
+
expect(D3.mean([1,2,3]){|x| x**2}).to eq((1+4+9)/3)
|
34
|
+
expect(D3.mean([1,2,3,4]){|x| x > 3 ? nil : x**2}).to eq((1+4+9)/3)
|
35
|
+
expect(D3.mean([])).to eq(nil)
|
36
|
+
end
|
37
|
+
|
38
|
+
it "d3.median" do
|
39
|
+
expect(D3.median([1,2,3])).to eq(2)
|
40
|
+
expect(D3.median([1,2,3,nil,nil])).to eq(2)
|
41
|
+
expect(D3.median([])).to eq(nil)
|
42
|
+
end
|
43
|
+
|
44
|
+
it "d3.quantile" do
|
45
|
+
# Examples from d3 documentation
|
46
|
+
expect(D3.quantile([0, 10, 30], 0)).to eq(0)
|
47
|
+
expect(D3.quantile([0, 10, 30], 0.5)).to eq(10)
|
48
|
+
expect(D3.quantile([0, 10, 30], 1)).to eq(30)
|
49
|
+
expect(D3.quantile([0, 10, 30], 0.25)).to eq(5)
|
50
|
+
expect(D3.quantile([0, 10, 30], 0.75)).to eq(20)
|
51
|
+
expect(D3.quantile([0, 10, 30], 0.1)).to eq(2)
|
52
|
+
end
|
53
|
+
|
54
|
+
it "d3.variance" do
|
55
|
+
expect(D3.variance([])).to eq(nil)
|
56
|
+
expect(D3.variance([1])).to eq(nil)
|
57
|
+
expect(D3.variance([10,20,30])).to eq(100)
|
58
|
+
end
|
59
|
+
|
60
|
+
it "d3.deviation" do
|
61
|
+
expect(D3.deviation([])).to eq(nil)
|
62
|
+
expect(D3.deviation([1])).to eq(nil)
|
63
|
+
expect(D3.deviation([10,20,30])).to eq(10)
|
64
|
+
end
|
65
|
+
end
|