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,152 @@
|
|
1
|
+
describe "d3 - interpolate" do
|
2
|
+
let(:f) { D3.send(name, a, b) }
|
3
|
+
let(:v) { f.(0.5) }
|
4
|
+
let(:f2) { D3.send(name).(a, b) }
|
5
|
+
let(:v2) { f2.(0.5) }
|
6
|
+
|
7
|
+
describe "numbers" do
|
8
|
+
let(:a) { 10 }
|
9
|
+
let(:b) { 20 }
|
10
|
+
|
11
|
+
describe "d3.interpolate_number" do
|
12
|
+
let(:name) { :interpolate_number }
|
13
|
+
it do
|
14
|
+
expect(f.(0.3)).to eq(13)
|
15
|
+
expect(f.(0.31)).to eq(13.1)
|
16
|
+
expect(f.(0.31)).to eq(f2.(0.31))
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
describe "d3.interpolate_round" do
|
21
|
+
let(:name) { :interpolate_round }
|
22
|
+
it do
|
23
|
+
expect(f.(0.3)).to eq(13)
|
24
|
+
expect(f.(0.31)).to eq(13)
|
25
|
+
expect(f.(0.31)).to eq(f2.(0.31))
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
describe "d3.interpolate_string" do
|
31
|
+
let(:a) { "300 12px sans-serif" }
|
32
|
+
let(:b) { "500 36px Comic-Sans" }
|
33
|
+
let(:name) { :interpolate_string }
|
34
|
+
|
35
|
+
it do
|
36
|
+
expect(f.(0.5)).to eq("400 24px Comic-Sans")
|
37
|
+
expect(f.(0.5)).to eq(f2.(0.5))
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
describe "d3.interpolate_array" do
|
42
|
+
let(:a) { [0, 200] }
|
43
|
+
let(:b) { [10, 500] }
|
44
|
+
let(:name) { :interpolate_array }
|
45
|
+
|
46
|
+
it do
|
47
|
+
expect(f.(0.5)).to eq([5, 350])
|
48
|
+
expect(f.(0.5)).to eq(f2.(0.5))
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
describe "d3.interpolate_date" do
|
53
|
+
let(:a) { Time.parse("June 30, 2015 00:00:00 UTC") }
|
54
|
+
let(:b) { Time.parse("December 31, 2016 12:00:00 UTC") }
|
55
|
+
let(:name) { :interpolate_date }
|
56
|
+
|
57
|
+
it do
|
58
|
+
expect(f.(0.5)).to eq(Time.parse("March 31, 2016 06:00:00 UTC"))
|
59
|
+
expect(f.(0.5)).to eq(f2.(0.5))
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
describe "colors" do
|
64
|
+
let(:a) { "purple" }
|
65
|
+
let(:b) { "orange" }
|
66
|
+
|
67
|
+
describe "d3.interpolate_rgb" do
|
68
|
+
let(:name) { :interpolate_rgb }
|
69
|
+
it do
|
70
|
+
expect(v).to eq("rgb(192, 83, 64)")
|
71
|
+
expect(v).to eq(v2)
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
describe "basis" do
|
76
|
+
let(:f) { D3.send(name, [a, b]) }
|
77
|
+
let(:f2) { D3.send(name).([a, b]) }
|
78
|
+
|
79
|
+
describe "d3.interpolate_rgb_basis" do
|
80
|
+
let(:name) { :interpolate_rgb_basis }
|
81
|
+
it do
|
82
|
+
expect(v).to eq("rgb(192, 83, 64)")
|
83
|
+
expect(v).to eq(v2)
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
describe "d3.interpolate_rgb_basis_closed" do
|
88
|
+
let(:name) { :interpolate_rgb_basis_closed }
|
89
|
+
it do
|
90
|
+
expect(v).to eq("rgb(213, 110, 43)")
|
91
|
+
expect(v).to eq(v2)
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
describe "d3.interpolate_hsl" do
|
97
|
+
let(:name) { :interpolate_hsl }
|
98
|
+
it do
|
99
|
+
expect(v).to eq("rgb(192, 0, 34)")
|
100
|
+
expect(v).to eq(v2)
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
describe "d3.interpolate_hsl_long" do
|
105
|
+
let(:name) { :interpolate_hsl_long }
|
106
|
+
it do
|
107
|
+
expect(v).to eq("rgb(0, 192, 158)")
|
108
|
+
expect(v).to eq(v2)
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
describe "d3.interpolate_lab" do
|
113
|
+
let(:name) { :interpolate_lab }
|
114
|
+
it do
|
115
|
+
expect(v).to eq("rgb(197, 93, 91)")
|
116
|
+
expect(v).to eq(v2)
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
describe "d3.interpolate_hcl" do
|
121
|
+
let(:name) { :interpolate_hcl }
|
122
|
+
it do
|
123
|
+
expect(v).to eq("rgb(236, 46, 83)")
|
124
|
+
expect(v).to eq(v2)
|
125
|
+
end
|
126
|
+
end
|
127
|
+
|
128
|
+
describe "d3.interpolate_hcl_long" do
|
129
|
+
let(:name) { :interpolate_hcl_long }
|
130
|
+
it do
|
131
|
+
expect(v).to eq("rgb(0, 153, 169)")
|
132
|
+
expect(v).to eq(v2)
|
133
|
+
end
|
134
|
+
end
|
135
|
+
|
136
|
+
describe "d3.interpolate_cubehelix" do
|
137
|
+
let(:name) { :interpolate_cubehelix }
|
138
|
+
it do
|
139
|
+
expect(v).to eq("rgb(255, 32, 68)")
|
140
|
+
expect(v).to eq(v2)
|
141
|
+
end
|
142
|
+
end
|
143
|
+
|
144
|
+
describe "d3.interpolate_cubehelix_long" do
|
145
|
+
let(:name) { :interpolate_cubehelix_long }
|
146
|
+
it do
|
147
|
+
expect(v).to eq("rgb(0, 194, 158)")
|
148
|
+
expect(v).to eq(v2)
|
149
|
+
end
|
150
|
+
end
|
151
|
+
end
|
152
|
+
end
|
data/spec/line_spec.rb
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
describe "d3 - line" do
|
2
|
+
it "d3.line" do
|
3
|
+
expect(D3.line).to be_instance_of(D3::LineGenerator)
|
4
|
+
end
|
5
|
+
|
6
|
+
let(:simple_data) { [[1,2],[3,5],[4,9]] }
|
7
|
+
let(:extended_data) { [[1,2],[3,5],[4,9],[6,10],[7,8]] }
|
8
|
+
|
9
|
+
it "basics" do
|
10
|
+
expect(D3.line.(simple_data)).to eq("M1,2L3,5L4,9")
|
11
|
+
end
|
12
|
+
|
13
|
+
it "curve" do
|
14
|
+
expect(D3.line.curve).to be_instance_of(D3::Curve)
|
15
|
+
expect(D3.line.curve(D3.curve_natural).(simple_data)).to eq(%W[
|
16
|
+
M1,2
|
17
|
+
C1.75,2.9166666666666665,2.5,3.833333333333333,3,5
|
18
|
+
C3.5,6.166666666666667,3.75,7.583333333333334,4,9
|
19
|
+
].join)
|
20
|
+
end
|
21
|
+
|
22
|
+
it "x/y accessors" do
|
23
|
+
line = D3.line
|
24
|
+
line.x{|(x,y)| x*10}.y{|(x,y)| y*100}
|
25
|
+
expect(line.x.([23,42])).to eq(230)
|
26
|
+
expect(line.y.([23,42])).to eq(4200)
|
27
|
+
expect(line.(simple_data)).to eq("M10,200L30,500L40,900")
|
28
|
+
end
|
29
|
+
|
30
|
+
it ".x constant" do
|
31
|
+
line = D3.line.x(10)
|
32
|
+
expect(line.(simple_data)).to eq("M10,2L10,5L10,9")
|
33
|
+
expect(line.x.(42)).to eq(10)
|
34
|
+
line.x = 20
|
35
|
+
expect(line.x.(42)).to eq(20)
|
36
|
+
end
|
37
|
+
|
38
|
+
it ".y constant" do
|
39
|
+
line = D3.line.y(10)
|
40
|
+
expect(line.(simple_data)).to eq("M1,10L3,10L4,10")
|
41
|
+
expect(line.y.(42)).to eq(10)
|
42
|
+
line.y = 20
|
43
|
+
expect(line.y.(42)).to eq(20)
|
44
|
+
end
|
45
|
+
|
46
|
+
it ".defined" do
|
47
|
+
expect(D3.line.defined(false).(simple_data)).to eq(nil)
|
48
|
+
expect(D3.line.defined{false}.(simple_data)).to eq(nil)
|
49
|
+
expect(D3.line.defined(true).(simple_data)).to eq("M1,2L3,5L4,9")
|
50
|
+
expect(D3.line.defined{true}.(simple_data)).to eq("M1,2L3,5L4,9")
|
51
|
+
expect(D3.line.(extended_data)).to eq("M1,2L3,5L4,9L6,10L7,8")
|
52
|
+
expect(D3.line.defined{|(x,y)| x.odd?}.(extended_data)).to eq("M1,2L3,5M7,8Z")
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
__END__
|
57
|
+
|
58
|
+
line.context - set the rendering context.
|
data/spec/map_spec.rb
ADDED
@@ -0,0 +1,80 @@
|
|
1
|
+
describe "d3-collections - maps" do
|
2
|
+
it "d3.map" do
|
3
|
+
expect(D3.map).to be_instance_of(D3::Map)
|
4
|
+
end
|
5
|
+
|
6
|
+
it "d3.has?" do
|
7
|
+
expect(D3.map().has?("1")).to eq(false)
|
8
|
+
expect(D3.map(["a","b"]).has?("0")).to eq(true)
|
9
|
+
expect(D3.map(["a","b"]).has?(0)).to eq(true)
|
10
|
+
expect(D3.map(["a","b"]).has?(2)).to eq(false)
|
11
|
+
expect(D3.map(["a","b"]).has?("a")).to eq(false)
|
12
|
+
end
|
13
|
+
|
14
|
+
it "d3.get" do
|
15
|
+
a = D3.map(["a", "b"])
|
16
|
+
expect(nil).to eq(nil)
|
17
|
+
expect(a[0]).to eq("a")
|
18
|
+
expect(a[2]).to eq(nil)
|
19
|
+
expect(a.get(0)).to eq("a")
|
20
|
+
expect(a.get(2)).to eq(nil)
|
21
|
+
end
|
22
|
+
|
23
|
+
it "d3.set" do
|
24
|
+
a = D3.map.set("foo", 1).set("bar", 2)
|
25
|
+
a["baz"] = 3
|
26
|
+
expect(a["foo"]).to eq(1)
|
27
|
+
expect(a["bar"]).to eq(2)
|
28
|
+
expect(a["baz"]).to eq(3)
|
29
|
+
end
|
30
|
+
|
31
|
+
it "map.remove" do
|
32
|
+
a = D3.map.set("a", 1).set("b", 2).remove("a").set("c", 3)
|
33
|
+
expect(a.keys).to eq(["b", "c"])
|
34
|
+
end
|
35
|
+
|
36
|
+
it "map.clear" do
|
37
|
+
a = D3.map.set("a", 1).set("b", 2).clear.set("c", 3)
|
38
|
+
expect(a.keys).to eq(["c"])
|
39
|
+
end
|
40
|
+
|
41
|
+
it "map.keys" do
|
42
|
+
a = D3.map.set("foo", 1).set("bar", 2)
|
43
|
+
expect(a.keys).to eq(["foo", "bar"])
|
44
|
+
end
|
45
|
+
|
46
|
+
it "map.values" do
|
47
|
+
a = D3.map.set("foo", 1).set("bar", 2)
|
48
|
+
expect(a.values).to eq([1, 2])
|
49
|
+
end
|
50
|
+
|
51
|
+
it "map.entries" do
|
52
|
+
a = D3.map.set("foo", 1).set("bar", 2)
|
53
|
+
expect(a.entries).to eq([["foo", 1], ["bar", 2]])
|
54
|
+
end
|
55
|
+
|
56
|
+
it "map.to_h" do
|
57
|
+
a = D3.map.set("foo", 1).set("bar", 2)
|
58
|
+
expect(a.to_h).to eq({"foo" => 1, "bar" => 2})
|
59
|
+
end
|
60
|
+
|
61
|
+
it "map.each" do
|
62
|
+
called = []
|
63
|
+
a = D3.map.set("foo", 1).set("bar", 2)
|
64
|
+
a.each do |v,k|
|
65
|
+
called << [k,v]
|
66
|
+
end
|
67
|
+
expect(called).to eq([["foo",1], ["bar",2]])
|
68
|
+
|
69
|
+
end
|
70
|
+
|
71
|
+
it "map.empty" do
|
72
|
+
expect(D3.map).to be_empty
|
73
|
+
expect(D3.map(["a"])).to_not be_empty
|
74
|
+
end
|
75
|
+
|
76
|
+
it "map.size" do
|
77
|
+
expect(D3.map.size).to eq(0)
|
78
|
+
expect(D3.map(["a"]).size).to eq(1)
|
79
|
+
end
|
80
|
+
end
|
data/spec/misc_spec.rb
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
describe "d3 misc methods" do
|
2
|
+
it "d3.version" do
|
3
|
+
expect(D3.version).to eq("4.4.3")
|
4
|
+
end
|
5
|
+
|
6
|
+
it "d3.namespace" do
|
7
|
+
expect(D3.namespace("svg:text")).to eq({"space"=>"http://www.w3.org/2000/svg", "local"=>"text"})
|
8
|
+
end
|
9
|
+
|
10
|
+
it "d3.namespaces" do
|
11
|
+
expect(D3.namespaces).to eq({
|
12
|
+
"svg"=>"http://www.w3.org/2000/svg",
|
13
|
+
"xhtml"=>"http://www.w3.org/1999/xhtml",
|
14
|
+
"xlink"=>"http://www.w3.org/1999/xlink",
|
15
|
+
"xml"=>"http://www.w3.org/XML/1998/namespace",
|
16
|
+
"xmlns"=>"http://www.w3.org/2000/xmlns/"
|
17
|
+
})
|
18
|
+
end
|
19
|
+
end
|
data/spec/nest_spec.rb
ADDED
@@ -0,0 +1,89 @@
|
|
1
|
+
describe "d3-collections - nests" do
|
2
|
+
it "d3.nest" do
|
3
|
+
expect(D3.nest).to be_instance_of(D3::Nest)
|
4
|
+
end
|
5
|
+
|
6
|
+
it "zero levels" do
|
7
|
+
data = [61, 35, 24, 37, 9, 20, 42, 55, 58, 57]
|
8
|
+
nest = D3.nest.sort_values(:ascending)
|
9
|
+
expect(nest.entries(data)).to eq(data)
|
10
|
+
expect(nest.map(data)).to eq(data)
|
11
|
+
end
|
12
|
+
|
13
|
+
it "one level" do
|
14
|
+
data = [61, 35, 24, 37, 9, 20, 42, 55, 58, 57]
|
15
|
+
nest = D3.nest.key{|x| (x / 10).floor}.sort_keys(:ascending).sort_values(:descending)
|
16
|
+
expect(nest.map(data).to_h).to eq(
|
17
|
+
{"6"=>[61], "3"=>[37, 35], "2"=>[24, 20], "0"=>[9], "4"=>[42], "5"=>[58, 57, 55]}
|
18
|
+
)
|
19
|
+
expect(nest.entries(data)).to eq([
|
20
|
+
["0", [9]],
|
21
|
+
["2", [24, 20]],
|
22
|
+
["3", [37, 35]],
|
23
|
+
["4", [42]],
|
24
|
+
["5", [58, 57, 55]],
|
25
|
+
["6", [61]],
|
26
|
+
])
|
27
|
+
o = nest.object(data)
|
28
|
+
expect(`JSON.stringify(#{o})`).to eq(
|
29
|
+
'{"0":[9],"2":[24,20],"3":[37,35],"4":[42],"5":[58,57,55],"6":[61]}'
|
30
|
+
)
|
31
|
+
end
|
32
|
+
|
33
|
+
it "one level and rollup" do
|
34
|
+
data = [61, 35, 24, 37, 9, 20, 42, 55, 58, 57]
|
35
|
+
# rollup and sorting don't seem to work together
|
36
|
+
nest = D3.nest.key{|x| (x / 10).floor}.sort_keys(:ascending)
|
37
|
+
.sort_values(:descending)
|
38
|
+
.rollup{|*x| x.join(":")}
|
39
|
+
expect(nest.map(data).to_h).to eq(
|
40
|
+
{"6"=>"61", "3"=>"35:37", "2"=>"24:20", "0"=>"9", "4"=>"42", "5"=>"55:58:57"}
|
41
|
+
)
|
42
|
+
expect(nest.entries(data)).to eq([
|
43
|
+
["0", "9"],
|
44
|
+
["2", "24:20"],
|
45
|
+
["3", "35:37"],
|
46
|
+
["4", "42"],
|
47
|
+
["5", "55:58:57"],
|
48
|
+
["6", "61"]
|
49
|
+
])
|
50
|
+
end
|
51
|
+
|
52
|
+
it "three levels" do
|
53
|
+
data = (1..25).to_a + (80..85).to_a
|
54
|
+
nest = D3.nest.key{|x| "d#{ x % 2}" }.sort_keys(:ascending)
|
55
|
+
.key{|x| "t#{ x % 3}" }.sort_keys(:descending)
|
56
|
+
.key{|x| "v#{ x % 5}" }.sort_keys(:ascending)
|
57
|
+
.sort_values(:descending)
|
58
|
+
expect(nest.entries(data)).to eq([
|
59
|
+
["d0", [["t2", [["v0", [80, 20]], ["v2", [2]], ["v3", [8]], ["v4", [14]]]],
|
60
|
+
["t1", [["v0", [10]], ["v1", [16]], ["v2", [82, 22]], ["v4", [4]]]],
|
61
|
+
["t0", [["v1", [6]], ["v2", [12]], ["v3", [18]], ["v4", [84, 24]]]]]],
|
62
|
+
["d1", [["t2", [["v0", [5]], ["v1", [11]], ["v2", [17]], ["v3", [83, 23]]]],
|
63
|
+
["t1", [["v0", [85, 25]], ["v1", [1]], ["v2", [7]], ["v3", [13]], ["v4", [19]]]],
|
64
|
+
["t0", [["v0", [15]], ["v1", [81, 21]], ["v3", [3]], ["v4", [9]]]]]]
|
65
|
+
])
|
66
|
+
expect(nest.map(data).inspect).to eq(
|
67
|
+
%Q[#<D3::Map: {"d1"=>#<D3::Map: {"t1"=>#<D3::Map: {"v1"=>[1], "v2"=>[7], "v3"=>[13], "v4"=>[19], "v0"=>[85, 25]}>, "t0"=>#<D3::Map: {"v3"=>[3], "v4"=>[9], "v0"=>[15], "v1"=>[81, 21]}>, "t2"=>#<D3::Map: {"v0"=>[5], "v1"=>[11], "v2"=>[17], "v3"=>[83, 23]}>}>, "d0"=>#<D3::Map: {"t2"=>#<D3::Map: {"v2"=>[2], "v3"=>[8], "v4"=>[14], "v0"=>[80, 20]}>, "t1"=>#<D3::Map: {"v4"=>[4], "v0"=>[10], "v1"=>[16], "v2"=>[82, 22]}>, "t0"=>#<D3::Map: {"v1"=>[6], "v2"=>[12], "v3"=>[18], "v4"=>[84, 24]}>}>}>]
|
68
|
+
)
|
69
|
+
end
|
70
|
+
|
71
|
+
it "three levels and rollup" do
|
72
|
+
data = (1..25).to_a + (80..85).to_a
|
73
|
+
nest = D3.nest.key{|x| "d#{ x % 2}" }.sort_keys(:ascending)
|
74
|
+
.key{|x| "t#{ x % 3}" }.sort_keys(:descending)
|
75
|
+
.key{|x| "v#{ x % 5}" }.sort_keys(:ascending)
|
76
|
+
.rollup{|*x| x.join(":")}
|
77
|
+
expect(nest.entries(data)).to eq([
|
78
|
+
["d0", [["t2", [["v0", "20:80"], ["v2", "2"], ["v3", "8"], ["v4", "14"]]],
|
79
|
+
["t1", [["v0", "10"], ["v1", "16"], ["v2", "22:82"], ["v4", "4"]]],
|
80
|
+
["t0", [["v1", "6"], ["v2", "12"], ["v3", "18"], ["v4", "24:84"]]]]],
|
81
|
+
["d1", [["t2", [["v0", "5"], ["v1", "11"], ["v2", "17"], ["v3", "23:83"]]],
|
82
|
+
["t1", [["v0", "25:85"], ["v1", "1"], ["v2", "7"], ["v3", "13"], ["v4", "19"]]],
|
83
|
+
["t0", [["v0", "15"], ["v1", "21:81"], ["v3", "3"], ["v4", "9"]]]]]
|
84
|
+
])
|
85
|
+
expect(nest.map(data).inspect).to eq(
|
86
|
+
%Q[#<D3::Map: {"d1"=>#<D3::Map: {"t1"=>#<D3::Map: {"v1"=>"1", "v2"=>"7", "v3"=>"13", "v4"=>"19", "v0"=>"25:85"}>, "t0"=>#<D3::Map: {"v3"=>"3", "v4"=>"9", "v0"=>"15", "v1"=>"21:81"}>, "t2"=>#<D3::Map: {"v0"=>"5", "v1"=>"11", "v2"=>"17", "v3"=>"23:83"}>}>, "d0"=>#<D3::Map: {"t2"=>#<D3::Map: {"v2"=>"2", "v3"=>"8", "v4"=>"14", "v0"=>"20:80"}>, "t1"=>#<D3::Map: {"v4"=>"4", "v0"=>"10", "v1"=>"16", "v2"=>"22:82"}>, "t0"=>#<D3::Map: {"v1"=>"6", "v2"=>"12", "v3"=>"18", "v4"=>"24:84"}>}>}>]
|
87
|
+
)
|
88
|
+
end
|
89
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# These operate on JS native objects, and might not be that useful from Opal
|
2
|
+
# Not converting these to work on ruby objects, as ruby standard library
|
3
|
+
# already has all relevant methods
|
4
|
+
describe "d3-collections - objects" do
|
5
|
+
let(:object) { `{a: 1, b: 2, c: 3}` }
|
6
|
+
|
7
|
+
it "d3.keys" do
|
8
|
+
expect(D3.keys(object)).to eq(["a", "b", "c"])
|
9
|
+
end
|
10
|
+
|
11
|
+
it "d3.values" do
|
12
|
+
expect(D3.values(object)).to eq([1, 2, 3])
|
13
|
+
end
|
14
|
+
|
15
|
+
it "d3.entries" do
|
16
|
+
expect(D3.entries(object)).to eq([
|
17
|
+
["a",1],
|
18
|
+
["b",2],
|
19
|
+
["c",3],
|
20
|
+
])
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
describe "d3 ordinal scale" do
|
2
|
+
it "d3.scale_ordinal" do
|
3
|
+
expect(D3.scale_ordinal).to be_instance_of(D3::OrdinalScale)
|
4
|
+
end
|
5
|
+
|
6
|
+
let(:d) { ("a".."z").to_a }
|
7
|
+
let(:r) { D3.scheme_category_10 }
|
8
|
+
let(:colors) { D3.scale_ordinal.domain(d).range(r) }
|
9
|
+
|
10
|
+
it "basics" do
|
11
|
+
expect(colors.domain).to eq(d)
|
12
|
+
expect(colors.range).to eq(r)
|
13
|
+
expect(colors.("c")).to eq("#2ca02c")
|
14
|
+
expect(colors.("z")).to eq("#8c564b")
|
15
|
+
# default implicit
|
16
|
+
expect(colors.("X")).to eq("#e377c2")
|
17
|
+
expect(colors.("Y")).to eq("#7f7f7f")
|
18
|
+
expect(colors.("X")).to eq("#e377c2")
|
19
|
+
end
|
20
|
+
|
21
|
+
it "unknown" do
|
22
|
+
expect(D3.scale_implicit).to eq(:implicit)
|
23
|
+
expect(colors.unknown).to eq(:implicit)
|
24
|
+
|
25
|
+
colors.unknown("#ffffff")
|
26
|
+
expect(colors.unknown).to eq("#ffffff")
|
27
|
+
expect(colors.("c")).to eq("#2ca02c")
|
28
|
+
expect(colors.("C")).to eq("#ffffff")
|
29
|
+
|
30
|
+
colors.unknown(:implicit)
|
31
|
+
expect(colors.unknown).to eq(:implicit)
|
32
|
+
expect(colors.("c")).to eq("#2ca02c")
|
33
|
+
expect(colors.("C")).to eq("#e377c2")
|
34
|
+
end
|
35
|
+
|
36
|
+
it "copy" do
|
37
|
+
cc = colors.copy.domain([1,2,3])
|
38
|
+
expect(colors.domain).to eq(d)
|
39
|
+
expect(cc.domain).to eq([1,2,3])
|
40
|
+
expect(colors.range).to eq(r)
|
41
|
+
expect(cc.range).to eq(r)
|
42
|
+
end
|
43
|
+
|
44
|
+
it "d3.scheme_category_10" do
|
45
|
+
expect(D3.scheme_category_10).to eq(["#1f77b4", "#ff7f0e", "#2ca02c", "#d62728", "#9467bd", "#8c564b", "#e377c2", "#7f7f7f", "#bcbd22", "#17becf"])
|
46
|
+
end
|
47
|
+
|
48
|
+
it "d3.scheme_category_20" do
|
49
|
+
expect(D3.scheme_category_20).to eq(["#1f77b4", "#aec7e8", "#ff7f0e", "#ffbb78", "#2ca02c", "#98df8a", "#d62728", "#ff9896", "#9467bd", "#c5b0d5", "#8c564b", "#c49c94", "#e377c2", "#f7b6d2", "#7f7f7f", "#c7c7c7", "#bcbd22", "#dbdb8d", "#17becf", "#9edae5"])
|
50
|
+
end
|
51
|
+
|
52
|
+
it "d3.scheme_category_20b" do
|
53
|
+
expect(D3.scheme_category_20b).to eq(["#393b79", "#5254a3", "#6b6ecf", "#9c9ede", "#637939", "#8ca252", "#b5cf6b", "#cedb9c", "#8c6d31", "#bd9e39", "#e7ba52", "#e7cb94", "#843c39", "#ad494a", "#d6616b", "#e7969c", "#7b4173", "#a55194", "#ce6dbd", "#de9ed6"])
|
54
|
+
end
|
55
|
+
|
56
|
+
it "d3.scheme_category_20c" do
|
57
|
+
expect(D3.scheme_category_20c).to eq(["#3182bd", "#6baed6", "#9ecae1", "#c6dbef", "#e6550d", "#fd8d3c", "#fdae6b", "#fdd0a2", "#31a354", "#74c476", "#a1d99b", "#c7e9c0", "#756bb1", "#9e9ac8", "#bcbddc", "#dadaeb", "#636363", "#969696", "#bdbdbd", "#d9d9d9"])
|
58
|
+
end
|
59
|
+
end
|
data/spec/path_spec.rb
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
describe "d3 - path" do
|
2
|
+
it "d3.path" do
|
3
|
+
expect(D3.path).to be_instance_of(D3::Path)
|
4
|
+
end
|
5
|
+
|
6
|
+
it "path.move_to" do
|
7
|
+
expect(D3.path.move_to(10,20).to_s).to eq("M10,20")
|
8
|
+
expect(D3.path.move_to(10,20).move_to(30,40).to_s).to eq("M10,20M30,40")
|
9
|
+
end
|
10
|
+
|
11
|
+
it "path.line_to" do
|
12
|
+
expect(D3.path.line_to(30,40).to_s).to eq("L30,40")
|
13
|
+
expect(D3.path.line_to(10,20).line_to(30,40).to_s).to eq("L10,20L30,40")
|
14
|
+
end
|
15
|
+
|
16
|
+
it "path.close_path" do
|
17
|
+
expect(D3.path.line_to(10,20).close_path.to_s).to eq("L10,20Z")
|
18
|
+
expect(D3.path.line_to(10,20).close_path.line_to(30,40).close_path.to_s).to eq("L10,20ZL30,40Z")
|
19
|
+
end
|
20
|
+
|
21
|
+
it "path.rect" do # (x,y,w,h) not (x1,y1,x2,y2)
|
22
|
+
expect(D3.path.rect(10,20,30,40).to_s).to eq("M10,20h30v40h-30Z")
|
23
|
+
expect(D3.path.rect(10,20,30,40).rect(50,60,70,80).to_s).to eq("M10,20h30v40h-30ZM50,60h70v80h-70Z")
|
24
|
+
end
|
25
|
+
|
26
|
+
it "path.quadratic_curve_to" do
|
27
|
+
expect(D3.path.quadratic_curve_to(10,20,30,40).to_s).to eq("Q10,20,30,40")
|
28
|
+
expect(D3.path.quadratic_curve_to(10,20,30,40)
|
29
|
+
.quadratic_curve_to(50,60,70,80).to_s).to eq("Q10,20,30,40Q50,60,70,80")
|
30
|
+
end
|
31
|
+
|
32
|
+
it "path.bezier_curve_to" do
|
33
|
+
expect(D3.path.bezier_curve_to(10,20,30,40,50,60).to_s).to eq("C10,20,30,40,50,60")
|
34
|
+
expect(D3.path.bezier_curve_to(10,20,30,40,50,60)
|
35
|
+
.bezier_curve_to(70,80,90,100,110,120).to_s).to eq(
|
36
|
+
"C10,20,30,40,50,60C70,80,90,100,110,120")
|
37
|
+
end
|
38
|
+
|
39
|
+
it "path.arc" do
|
40
|
+
expect(D3.path.arc(100,100,50,90,225).to_s).to eq(
|
41
|
+
"M77.59631919354149,144.6998331800279"+
|
42
|
+
"A50,50,0,1,1,122.40368080645851,55.300166819972105"+
|
43
|
+
"A50,50,0,1,1,77.59631919354149,144.6998331800279"
|
44
|
+
)
|
45
|
+
expect(D3.path.arc(100,100,50,90,225,false)
|
46
|
+
.arc(100,100,50,90,225,true).to_s).to eq(
|
47
|
+
"M77.59631919354149,144.6998331800279"+
|
48
|
+
"A50,50,0,1,1,122.40368080645851,55.300166819972105" +
|
49
|
+
"A50,50,0,1,1,77.59631919354149,144.6998331800279" +
|
50
|
+
"A50,50,0,1,0,118.36596838651226,53.495256099773734"
|
51
|
+
)
|
52
|
+
end
|
53
|
+
|
54
|
+
it "path.arc_to" do
|
55
|
+
# To be honest it's not clear to me how that's supposed to work exactly
|
56
|
+
# but we're just doing bindings here
|
57
|
+
expect(D3.path
|
58
|
+
.move_to(10,10)
|
59
|
+
.arc_to(20,20,30,10,10)
|
60
|
+
.to_s).to eq(
|
61
|
+
"M10,10"+
|
62
|
+
"L12.928932188134526,12.928932188134526"+
|
63
|
+
"A10,10,0,0,0,27.071067811865476,12.928932188134526")
|
64
|
+
end
|
65
|
+
end
|
data/spec/pie_spec.rb
ADDED
@@ -0,0 +1,114 @@
|
|
1
|
+
describe "d3 - pie" do
|
2
|
+
let(:generated) { pie.(data) }
|
3
|
+
let(:rounded) {
|
4
|
+
generated.map{|arc| arc.map{|k,v| [k, v.is_a?(Float) ? v.round(2) : v] }.to_h}
|
5
|
+
}
|
6
|
+
let(:data) { [1, 3, 4] }
|
7
|
+
let(:pie) { D3.pie }
|
8
|
+
|
9
|
+
it "d3.pie" do
|
10
|
+
expect(D3.pie).to be_instance_of(D3::PieGenerator)
|
11
|
+
end
|
12
|
+
|
13
|
+
describe "basics" do
|
14
|
+
it do
|
15
|
+
expect(rounded).to eq([
|
16
|
+
{data: 1, index: 2, value: 1, start_angle: 5.50, end_angle: 6.28, pad_angle: 0},
|
17
|
+
{data: 3, index: 1, value: 3, start_angle: 3.14, end_angle: 5.50, pad_angle: 0},
|
18
|
+
{data: 4, index: 0, value: 4, start_angle: 0.00, end_angle: 3.14, pad_angle: 0},
|
19
|
+
])
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
describe "pie.start_angle" do
|
24
|
+
it do
|
25
|
+
expect(pie.start_angle.()).to eq(0)
|
26
|
+
pie.start_angle(2)
|
27
|
+
expect(pie.start_angle.()).to eq(2)
|
28
|
+
expect(rounded).to eq([
|
29
|
+
{data: 1, index: 2, value: 1, start_angle: 5.75, end_angle: 6.28, pad_angle: 0},
|
30
|
+
{data: 3, index: 1, value: 3, start_angle: 4.14, end_angle: 5.75, pad_angle: 0},
|
31
|
+
{data: 4, index: 0, value: 4, start_angle: 2.00, end_angle: 4.14, pad_angle: 0},
|
32
|
+
])
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
describe "pie.end_angle" do
|
37
|
+
it do
|
38
|
+
expect(pie.end_angle.()).to eq(2*Math::PI)
|
39
|
+
pie.end_angle(4)
|
40
|
+
expect(pie.end_angle.()).to eq(4)
|
41
|
+
expect(rounded).to eq([
|
42
|
+
{data: 1, index: 2, value: 1, start_angle: 3.50, end_angle: 4.00, pad_angle: 0},
|
43
|
+
{data: 3, index: 1, value: 3, start_angle: 2.00, end_angle: 3.50, pad_angle: 0},
|
44
|
+
{data: 4, index: 0, value: 4, start_angle: 0.00, end_angle: 2.00, pad_angle: 0},
|
45
|
+
])
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
describe "pie.pad_angle" do
|
50
|
+
it do
|
51
|
+
expect(pie.pad_angle.()).to eq(0)
|
52
|
+
pie.pad_angle(0.1)
|
53
|
+
expect(pie.pad_angle.()).to eq(0.1)
|
54
|
+
expect(rounded).to eq([
|
55
|
+
{data: 1, index: 2, value: 1, start_angle: 5.44, end_angle: 6.28, pad_angle: 0.1},
|
56
|
+
{data: 3, index: 1, value: 3, start_angle: 3.09, end_angle: 5.44, pad_angle: 0.1},
|
57
|
+
{data: 4, index: 0, value: 4, start_angle: 0.00, end_angle: 3.09, pad_angle: 0.1},
|
58
|
+
])
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
describe "accessors" do
|
63
|
+
let(:data) {[
|
64
|
+
["a", 10],
|
65
|
+
["b", 5],
|
66
|
+
["d", 15],
|
67
|
+
["c", 20],
|
68
|
+
]}
|
69
|
+
let(:pie) { D3.pie.value{|d| d[1]} }
|
70
|
+
let(:order) {
|
71
|
+
generated
|
72
|
+
.sort_by{|arc| arc[:index] }
|
73
|
+
.map{|arc| arc.select{|k,v| k =~ /data|value/ }}
|
74
|
+
}
|
75
|
+
it "pie.value" do
|
76
|
+
expect(order).to eq([
|
77
|
+
{"data"=>["c", 20], "value"=>20},
|
78
|
+
{"data"=>["d", 15], "value"=>15},
|
79
|
+
{"data"=>["a", 10], "value"=>10},
|
80
|
+
{"data"=>["b", 5], "value"=> 5},
|
81
|
+
])
|
82
|
+
end
|
83
|
+
|
84
|
+
it "pie.sort" do
|
85
|
+
pie.sort{|a,b| a <=> b}
|
86
|
+
expect(order).to eq([
|
87
|
+
{"data"=>["a", 10], "value"=>10},
|
88
|
+
{"data"=>["b", 5], "value"=> 5},
|
89
|
+
{"data"=>["c", 20], "value"=>20},
|
90
|
+
{"data"=>["d", 15], "value"=>15},
|
91
|
+
])
|
92
|
+
end
|
93
|
+
|
94
|
+
it "pie.sort - nil" do
|
95
|
+
pie.sort(nil)
|
96
|
+
expect(order).to eq([
|
97
|
+
{"data"=>["a", 10], "value"=>10},
|
98
|
+
{"data"=>["b", 5], "value"=> 5},
|
99
|
+
{"data"=>["d", 15], "value"=>15},
|
100
|
+
{"data"=>["c", 20], "value"=>20},
|
101
|
+
])
|
102
|
+
end
|
103
|
+
|
104
|
+
it "pie.sort_values" do
|
105
|
+
pie.sort_values{|a,b| a <=> b}
|
106
|
+
expect(order).to eq([
|
107
|
+
{"data"=>["b", 5], "value"=> 5},
|
108
|
+
{"data"=>["a", 10], "value"=>10},
|
109
|
+
{"data"=>["d", 15], "value"=>15},
|
110
|
+
{"data"=>["c", 20], "value"=>20},
|
111
|
+
])
|
112
|
+
end
|
113
|
+
end
|
114
|
+
end
|