Skip to content

Commit 235b425

Browse files
committed
add barpolar hover tests
1 parent 0f62c0f commit 235b425

File tree

1 file changed

+326
-0
lines changed

1 file changed

+326
-0
lines changed

test/jasmine/tests/barpolar_test.js

+326
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,326 @@
1+
var Plotly = require('@lib');
2+
var Lib = require('@src/lib');
3+
4+
var createGraphDiv = require('../assets/create_graph_div');
5+
var destroyGraphDiv = require('../assets/destroy_graph_div');
6+
var failTest = require('../assets/fail_test');
7+
8+
describe('Test barpolar hover:', function() {
9+
var gd;
10+
11+
afterEach(destroyGraphDiv);
12+
13+
function run(specs) {
14+
gd = createGraphDiv();
15+
16+
var data = specs.traces.map(function(t) {
17+
t.type = 'barpolar';
18+
return t;
19+
});
20+
21+
var layout = Lib.extendFlat({
22+
showlegend: false,
23+
width: 400,
24+
height: 400,
25+
margin: {t: 0, b: 0, l: 0, r: 0, pad: 0}
26+
}, specs.layout || {});
27+
28+
return Plotly.plot(gd, data, layout).then(function() {
29+
var subplot = gd._fullLayout.polar._subplot;
30+
31+
var results = gd.calcdata.map(function(cd) {
32+
var trace = cd[0].trace;
33+
var pointData = {
34+
index: false,
35+
distance: 20,
36+
cd: cd,
37+
trace: trace,
38+
subplot: subplot,
39+
maxHoverDistance: 20
40+
};
41+
var pts = trace._module.hoverPoints(pointData, specs.xval, specs.yval, 'closest');
42+
return pts ? pts[0] : {distance: Infinity};
43+
});
44+
45+
// assert closest point (the one corresponding to the hover label)
46+
// if two points are equidistant, pick pt of 'above' trace
47+
results.sort(function(a, b) {
48+
return a.distance - b.distance || b.trace.index - a.trace.index;
49+
});
50+
var actual = results[0];
51+
var exp = specs.exp;
52+
53+
for(var k in exp) {
54+
var msg = '- key ' + k;
55+
if(k === 'x') {
56+
expect(actual.x0).toBe(exp.x, msg);
57+
expect(actual.x1).toBe(exp.x, msg);
58+
} else if(k === 'y') {
59+
expect(actual.y0).toBe(exp.y, msg);
60+
expect(actual.y1).toBe(exp.y, msg);
61+
} else if(k === 'curveNumber') {
62+
expect(actual.trace.index).toBe(exp.curveNumber, msg);
63+
} else {
64+
expect(actual[k]).toBe(exp[k], msg);
65+
}
66+
}
67+
});
68+
}
69+
70+
[{
71+
desc: 'base',
72+
traces: [{
73+
r: [1, 2, 3],
74+
theta: [0, 90, 180]
75+
}],
76+
xval: 1,
77+
yval: 0,
78+
exp: {
79+
index: 0,
80+
x: 263.33,
81+
y: 200,
82+
extraText: 'r: 1<br>θ: 0°',
83+
color: '#1f77b4'
84+
}
85+
}, {
86+
desc: 'works with bars with offsets',
87+
traces: [{
88+
r: [1, 2, 3],
89+
theta: [0, 90, 180],
90+
offset: -90
91+
}],
92+
xval: 1,
93+
yval: 0,
94+
exp: {
95+
index: 1,
96+
x: 302.48,
97+
y: 125.55,
98+
extraText: 'r: 2<br>θ: 90°',
99+
color: '#1f77b4'
100+
}
101+
}, {
102+
desc: 'works on clockwise angular axes',
103+
traces: [{
104+
r: [1, 2, 3],
105+
theta: [0, 90, 180],
106+
marker: {color: 'red'}
107+
}],
108+
layout: {
109+
polar: {
110+
angularaxis: {direction: 'clockwise'}
111+
}
112+
},
113+
xval: 0,
114+
yval: 1,
115+
exp: {
116+
index: 0,
117+
x: 200,
118+
y: 136.67,
119+
extraText: 'r: 1<br>θ: 0°',
120+
color: 'red'
121+
}
122+
}, {
123+
desc: 'works with radians theta coordinates',
124+
traces: [{
125+
r: [1, 2, 3],
126+
theta: [0, 2, 4],
127+
thetaunit: 'radians'
128+
}],
129+
xval: 1,
130+
yval: 0,
131+
exp: {
132+
index: 0,
133+
x: 263.33,
134+
y: 200,
135+
extraText: 'r: 1<br>θ: 0°',
136+
color: '#1f77b4'
137+
}
138+
}, {
139+
desc: 'works on radians angular axes',
140+
traces: [{
141+
r: [1, 2, 3],
142+
theta: [0, 90, 180],
143+
marker: {
144+
color: 'rgba(255,0,0,0)',
145+
line: {width: 2, color: 'green'}
146+
}
147+
}],
148+
layout: {
149+
polar: {
150+
angularaxis: {thetaunit: 'radians'}
151+
}
152+
},
153+
xval: -1,
154+
yval: 0,
155+
exp: {
156+
index: 2,
157+
x: 10,
158+
y: 200,
159+
extraText: 'r: 3<br>θ: 3.141593',
160+
color: 'green'
161+
}
162+
}, {
163+
desc: 'works on category angular axes',
164+
traces: [{
165+
theta: ['a', 'b', 'c', 'd', 'e'],
166+
r0: 1
167+
}],
168+
xval: 1,
169+
yval: 0,
170+
exp: {
171+
index: 0,
172+
x: 238,
173+
y: 200,
174+
extraText: 'r: 1<br>θ: a',
175+
color: '#1f77b4'
176+
}
177+
}, {
178+
desc: 'works on *gridshape:linear* subplots',
179+
traces: [{
180+
theta: ['a', 'b', 'c', 'd', 'e'],
181+
r0: 1
182+
}],
183+
layout: {
184+
polar: {gridshape: 'linear'}
185+
},
186+
xval: 1,
187+
yval: 0,
188+
exp: {
189+
index: 0,
190+
x: 238,
191+
y: 200,
192+
extraText: 'r: 1<br>θ: a',
193+
color: '#1f77b4'
194+
}
195+
}, {
196+
desc: 'works on log radial axes',
197+
traces: [{
198+
r: [100, 200, 50]
199+
}],
200+
layout: {
201+
polar: {
202+
radialaxis: {type: 'log'}
203+
}
204+
},
205+
xval: 0,
206+
yval: 0.3,
207+
exp: {
208+
index: 1,
209+
x: 105,
210+
y: 35.46,
211+
extraText: 'r: 200<br>θ: 120°',
212+
color: '#1f77b4'
213+
}
214+
}, {
215+
desc: 'works on category radial axes',
216+
traces: [{
217+
r: ['a', 'b', 'd', 'f']
218+
}],
219+
xval: -2,
220+
yval: 0,
221+
exp: {
222+
index: 2,
223+
x: 70,
224+
y: 200,
225+
extraText: 'r: d<br>θ: 180°',
226+
color: '#1f77b4'
227+
}
228+
}, {
229+
desc: 'works on date radial axes',
230+
traces: [{
231+
r: ['2018-08-1', '2018-09-5', '2018-10-8', '2018-09-20']
232+
}],
233+
xval: 0,
234+
yval: 3295437499,
235+
exp: {
236+
index: 1,
237+
x: 200,
238+
y: 97.35,
239+
extraText: 'r: Sep 5, 2018<br>θ: 90°',
240+
color: '#1f77b4'
241+
}
242+
}, {
243+
desc: 'works on negative radial coordinates',
244+
traces: [{
245+
base: 10,
246+
r: [-1, -5, 10, -5]
247+
}],
248+
xval: 0,
249+
yval: -6,
250+
exp: {
251+
index: 3,
252+
x: 200,
253+
y: 247.5,
254+
extraText: 'r: −5<br>θ: 270°',
255+
color: '#1f77b4'
256+
}
257+
}, {
258+
desc: 'works on reversed radial axis ranges',
259+
traces: [{
260+
r: [10, 12, 15]
261+
}],
262+
layout: {
263+
polar: {
264+
radialaxis: {range: [20, 0]}
265+
}
266+
},
267+
xval: 8,
268+
yval: -8,
269+
exp: {
270+
index: 1,
271+
x: 160,
272+
y: 130.72,
273+
extraText: 'r: 12<br>θ: 120°',
274+
color: '#1f77b4'
275+
}
276+
}, {
277+
desc: 'on overlapping bars of same size, the narrower wins',
278+
traces: [{
279+
r: [1, 2, 3],
280+
theta: [90, 180, 360],
281+
width: 70
282+
}, {
283+
r: [1, 2, 3],
284+
theta: [90, 180, 360],
285+
width: 90
286+
}],
287+
layout: {polar: {barmode: 'overlay'}},
288+
xval: 1,
289+
yval: 0,
290+
exp: {
291+
curveNumber: 0,
292+
index: 2,
293+
x: 390,
294+
y: 200,
295+
extraText: 'r: 3<br>θ: 360°',
296+
color: '#1f77b4'
297+
}
298+
}, {
299+
desc: 'on overlapping bars of same width, the one will tip closer to cursor wins',
300+
traces: [{
301+
r: [1, 2, 2],
302+
theta: [90, 180, 360],
303+
width: 70
304+
}, {
305+
r: [1, 2, 3],
306+
theta: [90, 180, 360],
307+
width: 70
308+
}],
309+
layout: {polar: {barmode: 'overlay'}},
310+
xval: 1.9,
311+
yval: 0,
312+
exp: {
313+
curveNumber: 0,
314+
index: 2,
315+
x: 326.67,
316+
y: 200,
317+
extraText: 'r: 2<br>θ: 360°',
318+
color: '#1f77b4'
319+
}
320+
}]
321+
.forEach(function(specs) {
322+
it('should generate correct hover labels ' + specs.desc, function(done) {
323+
run(specs).catch(failTest).then(done);
324+
});
325+
});
326+
});

0 commit comments

Comments
 (0)