|
1 require("../env"); |
|
2 require("../../d3"); |
|
3 |
|
4 var vows = require("vows"), |
|
5 assert = require("assert"); |
|
6 |
|
7 var suite = vows.describe("d3.svg.line"); |
|
8 |
|
9 suite.addBatch({ |
|
10 "line": { |
|
11 topic: function() { |
|
12 return d3.svg.line; |
|
13 }, |
|
14 |
|
15 "x defaults to a function accessor": function(line) { |
|
16 var l = line(); |
|
17 assert.pathEqual(l([[1, 2], [4, 3]]), "M1,2L4,3"); |
|
18 assert.typeOf(l.x(), "function"); |
|
19 }, |
|
20 "x can be defined as a constant": function(line) { |
|
21 var l = line().x(0); |
|
22 assert.pathEqual(l([[1, 2], [4, 3]]), "M0,2L0,3"); |
|
23 assert.equal(l.x(), 0); |
|
24 }, |
|
25 "x can be defined as a function": function(line) { |
|
26 var l = line().x(f), t = {}, dd = [], ii = [], tt = []; |
|
27 function f(d, i) { dd.push(d); ii.push(i); tt.push(this); return 0; } |
|
28 assert.pathEqual(l.call(t, [[1, 2], [4, 3]]), "M0,2L0,3"); |
|
29 assert.deepEqual(dd, [[1, 2], [4, 3]], "expected data, got {actual}"); |
|
30 assert.deepEqual(ii, [0, 1], "expected index, got {actual}"); |
|
31 assert.deepEqual(tt, [t, t], "expected this, got {actual}"); |
|
32 }, |
|
33 |
|
34 "y defaults to a function accessor": function(line) { |
|
35 var l = line(); |
|
36 assert.pathEqual(l([[1, 2], [4, 3]]), "M1,2L4,3"); |
|
37 assert.typeOf(l.y(), "function"); |
|
38 }, |
|
39 "y can be defined as a constant": function(line) { |
|
40 var l = line().y(0); |
|
41 assert.pathEqual(l([[1, 2], [4, 3]]), "M1,0L4,0"); |
|
42 assert.equal(l.y(), 0); |
|
43 }, |
|
44 "y can be defined as a function": function(line) { |
|
45 var l = line().y(f), t = {}, dd = [], ii = [], tt = []; |
|
46 function f(d, i) { dd.push(d); ii.push(i); tt.push(this); return 0; } |
|
47 assert.pathEqual(l.call(t, [[1, 2], [4, 3]]), "M1,0L4,0"); |
|
48 assert.deepEqual(dd, [[1, 2], [4, 3]], "expected data, got {actual}"); |
|
49 assert.deepEqual(ii, [0, 1], "expected index, got {actual}"); |
|
50 assert.deepEqual(tt, [t, t], "expected this, got {actual}"); |
|
51 }, |
|
52 |
|
53 "interpolate defaults to linear": function(line) { |
|
54 assert.equal(line().interpolate(), "linear"); |
|
55 }, |
|
56 "interpolate can be defined as a constant": function(line) { |
|
57 var l = line().interpolate("step-before"); |
|
58 assert.pathEqual(l([[0, 0], [1, 1]]), "M0,0V1H1"); |
|
59 assert.equal(l.interpolate(), "step-before"); |
|
60 }, |
|
61 |
|
62 "tension defaults to .7": function(line) { |
|
63 assert.equal(line().tension(), .7); |
|
64 }, |
|
65 "tension can be specified as a constant": function(line) { |
|
66 var l = line().tension(.5); |
|
67 assert.equal(l.tension(), .5); |
|
68 }, |
|
69 |
|
70 "returns null if input points array is empty": function(line) { |
|
71 assert.isNull(line()([])); |
|
72 }, |
|
73 |
|
74 "interpolate(linear)": { |
|
75 "supports linear interpolation": function(line) { |
|
76 var l = line().interpolate("linear"); |
|
77 assert.pathEqual(l([[0, 0], [1, 1], [2, 0], [3, 1], [4, 0]]), "M0,0L1,1L2,0L3,1L4,0"); |
|
78 } |
|
79 }, |
|
80 |
|
81 "interpolate(step)": { |
|
82 "supports step-before interpolation": function(line) { |
|
83 var l = line().interpolate("step-before"); |
|
84 assert.pathEqual(l([[0, 0]]), "M0,0"); |
|
85 assert.pathEqual(l([[0, 0], [1, 1]]), "M0,0V1H1"); |
|
86 assert.pathEqual(l([[0, 0], [1, 1], [2, 0]]), "M0,0V1H1V0H2"); |
|
87 }, |
|
88 "supports step-after interpolation": function(line) { |
|
89 var l = line().interpolate("step-after"); |
|
90 assert.pathEqual(l([[0, 0]]), "M0,0"); |
|
91 assert.pathEqual(l([[0, 0], [1, 1]]), "M0,0H1V1"); |
|
92 assert.pathEqual(l([[0, 0], [1, 1], [2, 0]]), "M0,0H1V1H2V0"); |
|
93 } |
|
94 }, |
|
95 |
|
96 "interpolate(basis)": { |
|
97 "supports basis interpolation": function(line) { |
|
98 var l = line().interpolate("basis"); |
|
99 assert.pathEqual(l([[0, 0], [1, 1], [2, 0], [3, 1], [4, 0]]), "M0,0C0,0,0,0,0.16666666666666666,0.16666666666666666C0.3333333333333333,0.3333333333333333,0.6666666666666666,0.6666666666666666,1,0.6666666666666666C1.3333333333333333,0.6666666666666666,1.6666666666666665,0.3333333333333333,2,0.3333333333333333C2.333333333333333,0.3333333333333333,2.6666666666666665,0.6666666666666666,3,0.6666666666666666C3.333333333333333,0.6666666666666666,3.6666666666666665,0.3333333333333333,3.833333333333333,0.16666666666666666C4,0,4,0,3.9999999999999996,0"); |
|
100 }, |
|
101 "supports basis-open interpolation": function(line) { |
|
102 var l = line().interpolate("basis-open"); |
|
103 assert.pathEqual(l([[0, 0], [1, 1], [2, 0], [3, 1], [4, 0]]), "M1,0.6666666666666666C1.3333333333333333,0.6666666666666666,1.6666666666666665,0.3333333333333333,2,0.3333333333333333C2.333333333333333,0.3333333333333333,2.6666666666666665,0.6666666666666666,3,0.6666666666666666"); |
|
104 }, |
|
105 "supports basis-closed interpolation": function(line) { |
|
106 var l = line().interpolate("basis-closed"); |
|
107 assert.pathEqual(l([[0, 0], [1, 1], [2, 0], [3, 1], [4, 0]]), "M2,0.3333333333333333C2.333333333333333,0.3333333333333333,2.6666666666666665,0.6666666666666666,3,0.6666666666666666C3.333333333333333,0.6666666666666666,3.6666666666666665,0.3333333333333333,3.1666666666666665,0.16666666666666666C2.6666666666666665,0,1.3333333333333333,0,0.8333333333333333,0.16666666666666666C0.3333333333333333,0.3333333333333333,0.6666666666666666,0.6666666666666666,1,0.6666666666666666C1.3333333333333333,0.6666666666666666,1.6666666666666665,0.3333333333333333,2,0.3333333333333333"); |
|
108 }, |
|
109 "basis interpolation reverts to linear with fewer than three points": function(line) { |
|
110 var l = line().interpolate("basis"), d = line(); |
|
111 assert.pathEqual(l([[0, 0]]), d([[0, 0]])); |
|
112 assert.pathEqual(l([[0, 0], [1, 1]]), d([[0, 0], [1, 1]])); |
|
113 }, |
|
114 "basis-open interpolation reverts to linear with fewer than four points": function(line) { |
|
115 var l = line().interpolate("basis-open"), d = line(); |
|
116 assert.pathEqual(l([[0, 0]]), d([[0, 0]])); |
|
117 assert.pathEqual(l([[0, 0], [1, 1]]), d([[0, 0], [1, 1]])); |
|
118 assert.pathEqual(l([[0, 0], [1, 1], [2, 0]]), d([[0, 0], [1, 1], [2, 0]])); |
|
119 }, |
|
120 "basis-closed interpolation reverts to linear with fewer than three points": function(line) { |
|
121 var l = line().interpolate("basis-open"), d = line(); |
|
122 assert.pathEqual(l([[0, 0]]), d([[0, 0]])); |
|
123 assert.pathEqual(l([[0, 0], [1, 1]]), d([[0, 0], [1, 1]])); |
|
124 } |
|
125 }, |
|
126 |
|
127 "interpolate(bundle)": { |
|
128 "supports bundle interpolation": function(line) { |
|
129 var l = line().interpolate("bundle"); |
|
130 assert.pathEqual(l([[0, 0], [1, 1], [2, 0], [3, 1], [4, 0]]), "M0,0C0,0,0,0,0.16666666666666666,0.11666666666666665C0.3333333333333333,0.2333333333333333,0.6666666666666666,0.4666666666666666,1,0.4666666666666666C1.3333333333333333,0.4666666666666666,1.6666666666666665,0.2333333333333333,2,0.2333333333333333C2.333333333333333,0.2333333333333333,2.6666666666666665,0.4666666666666666,3,0.4666666666666666C3.333333333333333,0.4666666666666666,3.6666666666666665,0.2333333333333333,3.833333333333333,0.11666666666666665C4,0,4,0,4,0"); |
|
131 }, |
|
132 "observes the specified tension": function(line) { |
|
133 var l = line().interpolate("bundle").tension(1); |
|
134 assert.pathEqual(l([[0, 0], [1, 1], [2, 0], [3, 1], [4, 0]]), line().interpolate("basis")([[0, 0], [1, 1], [2, 0], [3, 1], [4, 0]])); |
|
135 } |
|
136 }, |
|
137 |
|
138 "interpolate(cardinal)": { |
|
139 "supports cardinal interpolation": function(line) { |
|
140 var l = line().interpolate("cardinal"); |
|
141 assert.pathEqual(l([[0, 0], [1, 1], [2, 0], [3, 1], [4, 0]]), "M0,0Q0.8,1,1,1C1.3,1,1.7,0,2,0S2.7,1,3,1Q3.2,1,4,0"); |
|
142 }, |
|
143 "supports cardinal-open interpolation": function(line) { |
|
144 var l = line().interpolate("cardinal-open"); |
|
145 assert.pathEqual(l([[0, 0], [1, 1], [2, 0], [3, 1], [4, 0]]), "M1,1C1.3,1,1.7,0,2,0S2.7,1,3,1"); |
|
146 }, |
|
147 "supports cardinal-closed interpolation": function(line) { |
|
148 var l = line().interpolate("cardinal-closed"); |
|
149 assert.pathEqual(l([[0, 0], [1, 1], [2, 0], [3, 1], [4, 0]]), "M0,0C-0.45,0.15,0.7,1,1,1S1.7,0,2,0S2.7,1,3,1S4.45,0.15,4,0S0.45,-0.15,0,0"); |
|
150 }, |
|
151 "cardinal interpolation reverts to linear with fewer than three points": function(line) { |
|
152 var l = line().interpolate("cardinal"), d = line(); |
|
153 assert.pathEqual(l([[0, 0]]), d([[0, 0]])); |
|
154 assert.pathEqual(l([[0, 0], [1, 1]]), d([[0, 0], [1, 1]])); |
|
155 }, |
|
156 "cardinal-open interpolation reverts to linear with fewer than four points": function(line) { |
|
157 var l = line().interpolate("cardinal-open"), d = line(); |
|
158 assert.pathEqual(l([[0, 0]]), d([[0, 0]])); |
|
159 assert.pathEqual(l([[0, 0], [1, 1]]), d([[0, 0], [1, 1]])); |
|
160 assert.pathEqual(l([[0, 0], [1, 1], [2, 0]]), d([[0, 0], [1, 1], [2, 0]])); |
|
161 }, |
|
162 "cardinal-closed interpolation reverts to linear with fewer than three points": function(line) { |
|
163 var l = line().interpolate("cardinal-open"), d = line(); |
|
164 assert.pathEqual(l([[0, 0]]), d([[0, 0]])); |
|
165 assert.pathEqual(l([[0, 0], [1, 1]]), d([[0, 0], [1, 1]])); |
|
166 }, |
|
167 "observes the specified tension": function(line) { |
|
168 var l = line().tension(.5); |
|
169 assert.pathEqual(l.interpolate("cardinal")([[0, 0], [1, 1], [2, 0], [3, 1], [4, 0]]), "M0,0Q0.6666666666666667,1,1,1C1.5,1,1.5,0,2,0S2.5,1,3,1Q3.3333333333333335,1,4,0"); |
|
170 assert.pathEqual(l.interpolate("cardinal-open")([[0, 0], [1, 1], [2, 0], [3, 1], [4, 0]]), "M1,1C1.5,1,1.5,0,2,0S2.5,1,3,1"); |
|
171 assert.pathEqual(l.interpolate("cardinal-closed")([[0, 0], [1, 1], [2, 0], [3, 1], [4, 0]]), "M0,0C-0.75,0.25,0.5,1,1,1S1.5,0,2,0S2.5,1,3,1S4.75,0.25,4,0S0.75,-0.25,0,0"); |
|
172 } |
|
173 }, |
|
174 |
|
175 "interpolate(monotone)": { |
|
176 "supports monotone interpolation": function(line) { |
|
177 var l = line().interpolate("monotone"); |
|
178 assert.pathEqual(l([[0, 0], [1, 1], [2, 1], [3, 0], [4, 0]]), "M0,0C0.08333333333333333,0.08333333333333333,0.6666666666666667,1,1,1S1.6666666666666667,1,2,1S2.6666666666666665,0,3,0S3.8333333333333335,0,4,0"); |
|
179 }, |
|
180 "monotone interpolation reverts to linear with fewer than three points": function(line) { |
|
181 var l = line().interpolate("monotone"), d = line(); |
|
182 assert.pathEqual(l([[0, 0]]), d([[0, 0]])); |
|
183 assert.pathEqual(l([[0, 0], [1, 1]]), d([[0, 0], [1, 1]])); |
|
184 } |
|
185 } |
|
186 } |
|
187 }); |
|
188 |
|
189 suite.export(module); |