Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6c42b04

Browse files
author
Ben Cutrell
committed
Adds unhover events to pie graphs with tests
1 parent c4a6d7f commit 6c42b04

File tree

3 files changed

+76
-2
lines changed

3 files changed

+76
-2
lines changed

src/plots/cartesian/graph_interact.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1277,7 +1277,14 @@ function hoverChanged(gd, evt, oldhoverdata) {
12771277
}
12781278

12791279
// remove hover effects on mouse out, and emit unhover event
1280-
function unhover(gd, evt) {
1280+
function unhover(gd, evt, subplot) {
1281+
if (subplot === 'pie') {
1282+
gd.emit('plotly_unhover', {
1283+
points: [evt]
1284+
});
1285+
return;
1286+
}
1287+
12811288
var fullLayout = gd._fullLayout;
12821289
if(!evt) evt = {};
12831290
if(evt.target &&

src/traces/pie/plot.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ var Fx = require('../../plots/cartesian/graph_interact');
1414
var Color = require('../../components/color');
1515
var Drawing = require('../../components/drawing');
1616
var svgTextUtils = require('../../lib/svg_text_utils');
17+
var Plotly = require('../../plotly');
1718

1819
var helpers = require('./helpers');
1920

@@ -129,7 +130,8 @@ module.exports = function plot(gd, cdpie) {
129130
hasHoverData = true;
130131
}
131132

132-
function handleMouseOut() {
133+
function handleMouseOut(evt) {
134+
Plotly.Fx.unhover(gd, evt, 'pie');
133135
if(hasHoverData) {
134136
Fx.loneUnhover(fullLayout._hoverlayer.node());
135137
hasHoverData = false;
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
var Plotly = require('@lib/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 unhovering', 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+
var futureData;
28+
29+
gd.on('plotly_unhover', function(data) {
30+
futureData = data;
31+
});
32+
33+
var x = width / 2
34+
var y = height / 2
35+
mouseEvent('mouseover', x, y);
36+
mouseEvent('mouseout', x, y);
37+
38+
expect(futureData.points.length).toEqual(1);
39+
expect(Object.keys(futureData.points[0])).toEqual([
40+
'v', 'label', 'color', 'i', 'hidden',
41+
'text', 'px1', 'pxmid', 'midangle',
42+
'px0', 'largeArc', 'cxFinal', 'cyFinal'
43+
]);
44+
expect(futureData.points[0].i).toEqual(3);
45+
});
46+
47+
it('should fire when the mouse moves off the graph', function(done) {
48+
var count = 0
49+
futureData = [];
50+
51+
gd.on('plotly_unhover', function(data) {
52+
count++;
53+
});
54+
// // mouseEvent('mouseout', 180, 140);
55+
// expect(count).toEqual(3);
56+
57+
setTimeout(function() {
58+
mouseEvent('mouseover', 180, 140);
59+
mouseEvent('mouseout', 180, 140);
60+
expect(count).toEqual(1);
61+
done();
62+
}, 100);
63+
});
64+
});
65+
});

0 commit comments

Comments
 (0)