Skip to content

Commit 6e128e8

Browse files
committed
Merge pull request #150 from plotly/hover-pie
Pie-chart hover events
2 parents ecb476d + 66eade7 commit 6e128e8

File tree

4 files changed

+94
-1
lines changed

4 files changed

+94
-1
lines changed

src/plots/cartesian/graph_interact.js

+7
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,13 @@ fx.unhover = function (gd, evt, subplot) {
309309
// The actual implementation is here:
310310

311311
function hover(gd, evt, subplot){
312+
if(subplot === 'pie'){
313+
gd.emit('plotly_hover', {
314+
points: [evt]
315+
});
316+
return;
317+
}
318+
312319
if(!subplot) subplot = 'xy';
313320

314321
var fullLayout = gd._fullLayout,

src/traces/pie/plot.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ module.exports = function plot(gd, cdpie) {
8282
slicePath = sliceTop.selectAll('path.surface').data([pt]),
8383
hasHoverData = false;
8484

85-
function handleMouseOver() {
85+
function handleMouseOver(evt) {
8686
// in case fullLayout or fullData has changed without a replot
8787
var fullLayout2 = gd._fullLayout,
8888
trace2 = gd._fullData[trace.index],
@@ -123,6 +123,8 @@ module.exports = function plot(gd, cdpie) {
123123
}
124124
);
125125

126+
Plotly.Fx.hover(gd, evt, 'pie');
127+
126128
hasHoverData = true;
127129
}
128130

test/jasmine/assets/mouse_event.js

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module.exports = function (type, x, y, opts) {
2+
var el = document.elementFromPoint(x,y);
3+
var options = opts || { bubbles: true };
4+
var ev = new window.MouseEvent(type, options);
5+
el.dispatchEvent(ev);
6+
};

test/jasmine/tests/hover_pie_test.js

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
var Plotly = require('@src/index');
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 mouseEvent = require('../assets/mouse_event');
7+
8+
describe('pie hovering', function () {
9+
var mock = require('@mocks/pie_simple.json');
10+
11+
describe('event data', function () {
12+
var mockCopy = Lib.extendDeep({}, mock),
13+
width = mockCopy.layout.width,
14+
height = mockCopy.layout.height,
15+
gd;
16+
17+
beforeEach(function (done) {
18+
gd = createGraphDiv();
19+
20+
Plotly.plot(gd, mockCopy.data, mockCopy.layout)
21+
.then(done);
22+
});
23+
24+
afterEach(destroyGraphDiv);
25+
26+
it('should contain the correct fields', function () {
27+
28+
var expected = [{
29+
v: 4,
30+
label: '3',
31+
color: '#ff7f0e',
32+
i: 3,
33+
hidden: false,
34+
text: '26.7%',
35+
px1: [0,-60],
36+
pxmid: [-44.588689528643656,-40.14783638153149],
37+
midangle: -0.8377580409572781,
38+
px0: [-59.67131372209641,6.2717077960592],
39+
largeArc: 0,
40+
cxFinal: 200,
41+
cyFinal: 160
42+
}],
43+
futureData;
44+
45+
46+
gd.on('plotly_hover', function (data) {
47+
futureData = data;
48+
});
49+
50+
mouseEvent('mouseover', width / 2, height / 2);
51+
expect(futureData.points.length).toEqual(1);
52+
expect(Object.keys(futureData.points[0])).toEqual([
53+
'v', 'label', 'color', 'i', 'hidden',
54+
'text', 'px1', 'pxmid', 'midangle',
55+
'px0', 'largeArc', 'cxFinal', 'cyFinal'
56+
]);
57+
expect(futureData.points[0].i).toEqual(3);
58+
});
59+
60+
it('should fire when moving from one slice to another', function (done) {
61+
var count = 0
62+
futureData = [];
63+
64+
gd.on('plotly_hover', function (data) {
65+
count++;
66+
futureData.push(data);
67+
});
68+
69+
mouseEvent('mouseover', 180, 140);
70+
setTimeout(function () {
71+
mouseEvent('mouseover', 240, 200);
72+
expect(count).toEqual(2);
73+
expect(futureData[0]).not.toEqual(futureData[1])
74+
done();
75+
}, 100);
76+
});
77+
});
78+
});

0 commit comments

Comments
 (0)