Skip to content

Commit 0a77239

Browse files
committedOct 3, 2017
fixes #2044 - add hoverinfo arrayOk support to ...
... scattergeo, choropleth, scatterternary and scattermapbox - these are all the trace type that rely on `extraText` built in their _module.hoverPoints method - this commit should've been part of PR #1761
1 parent b24759d commit 0a77239

File tree

10 files changed

+114
-41
lines changed

10 files changed

+114
-41
lines changed
 

‎src/traces/choropleth/hover.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -53,17 +53,17 @@ module.exports = function hoverPoints(pointData, xval, yval) {
5353
};
5454

5555
function makeHoverInfo(pointData, trace, pt, axis) {
56-
var hoverinfo = trace.hoverinfo;
56+
var hoverinfo = pt.hi || trace.hoverinfo;
5757

5858
var parts = (hoverinfo === 'all') ?
5959
attributes.hoverinfo.flags :
6060
hoverinfo.split('+');
6161

62-
var hasName = (parts.indexOf('name') !== -1),
63-
hasLocation = (parts.indexOf('location') !== -1),
64-
hasZ = (parts.indexOf('z') !== -1),
65-
hasText = (parts.indexOf('text') !== -1),
66-
hasIdAsNameLabel = !hasName && hasLocation;
62+
var hasName = (parts.indexOf('name') !== -1);
63+
var hasLocation = (parts.indexOf('location') !== -1);
64+
var hasZ = (parts.indexOf('z') !== -1);
65+
var hasText = (parts.indexOf('text') !== -1);
66+
var hasIdAsNameLabel = !hasName && hasLocation;
6767

6868
var text = [];
6969

‎src/traces/scattercarpet/hover.js

+8-7
Original file line numberDiff line numberDiff line change
@@ -46,18 +46,19 @@ module.exports = function hoverPoints(pointData, xval, yval, hovermode) {
4646
newPointData.yLabelVal = undefined;
4747
// TODO: nice formatting, and label by axis title, for a, b, and c?
4848

49-
var trace = newPointData.trace,
50-
carpet = trace._carpet,
51-
hoverinfo = trace.hoverinfo.split('+'),
52-
text = [];
49+
var trace = newPointData.trace;
50+
var carpet = trace._carpet;
51+
var hoverinfo = cdi.hi || trace.hoverinfo;
52+
var parts = hoverinfo.split('+');
53+
var text = [];
5354

5455
function textPart(ax, val) {
5556
text.push(((ax.labelprefix && ax.labelprefix.length > 0) ? ax.labelprefix : (ax._hovertitle + ': ')) + val.toFixed(3) + ax.labelsuffix);
5657
}
5758

58-
if(hoverinfo.indexOf('all') !== -1) hoverinfo = ['a', 'b'];
59-
if(hoverinfo.indexOf('a') !== -1) textPart(carpet.aaxis, cdi.a);
60-
if(hoverinfo.indexOf('b') !== -1) textPart(carpet.baxis, cdi.b);
59+
if(parts.indexOf('all') !== -1) parts = ['a', 'b'];
60+
if(parts.indexOf('a') !== -1) textPart(carpet.aaxis, cdi.a);
61+
if(parts.indexOf('b') !== -1) textPart(carpet.baxis, cdi.b);
6162

6263
var ij = carpet.ab2ij([cdi.a, cdi.b]);
6364
var i0 = Math.floor(ij[0]);

‎src/traces/scattergeo/hover.js

+13-11
Original file line numberDiff line numberDiff line change
@@ -71,29 +71,31 @@ module.exports = function hoverPoints(pointData, xval, yval) {
7171
};
7272

7373
function getExtraText(trace, pt, axis) {
74-
var hoverinfo = trace.hoverinfo;
74+
var hoverinfo = pt.hi || trace.hoverinfo;
7575

76-
var parts = (hoverinfo === 'all') ?
76+
var parts = hoverinfo === 'all' ?
7777
attributes.hoverinfo.flags :
7878
hoverinfo.split('+');
7979

80-
var hasLocation = parts.indexOf('location') !== -1 && Array.isArray(trace.locations),
81-
hasLon = (parts.indexOf('lon') !== -1),
82-
hasLat = (parts.indexOf('lat') !== -1),
83-
hasText = (parts.indexOf('text') !== -1);
84-
80+
var hasLocation = parts.indexOf('location') !== -1 && Array.isArray(trace.locations);
81+
var hasLon = (parts.indexOf('lon') !== -1);
82+
var hasLat = (parts.indexOf('lat') !== -1);
83+
var hasText = (parts.indexOf('text') !== -1);
8584
var text = [];
8685

8786
function format(val) {
8887
return Axes.tickText(axis, axis.c2l(val), 'hover').text + '\u00B0';
8988
}
9089

91-
if(hasLocation) text.push(pt.loc);
92-
else if(hasLon && hasLat) {
90+
if(hasLocation) {
91+
text.push(pt.loc);
92+
} else if(hasLon && hasLat) {
9393
text.push('(' + format(pt.lonlat[0]) + ', ' + format(pt.lonlat[1]) + ')');
94+
} else if(hasLon) {
95+
text.push('lon: ' + format(pt.lonlat[0]));
96+
} else if(hasLat) {
97+
text.push('lat: ' + format(pt.lonlat[1]));
9498
}
95-
else if(hasLon) text.push('lon: ' + format(pt.lonlat[0]));
96-
else if(hasLat) text.push('lat: ' + format(pt.lonlat[1]));
9799

98100
if(hasText) {
99101
var tx;

‎src/traces/scattermapbox/hover.js

+12-9
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,14 @@ module.exports = function hoverPoints(pointData, xval, yval) {
6666
};
6767

6868
function getExtraText(trace, di) {
69-
var hoverinfo = trace.hoverinfo.split('+'),
70-
isAll = (hoverinfo.indexOf('all') !== -1),
71-
hasLon = (hoverinfo.indexOf('lon') !== -1),
72-
hasLat = (hoverinfo.indexOf('lat') !== -1);
69+
var hoverinfo = di.hi || trace.hoverinfo;
70+
var parts = hoverinfo.split('+');
71+
var isAll = parts.indexOf('all') !== -1;
72+
var hasLon = parts.indexOf('lon') !== -1;
73+
var hasLat = parts.indexOf('lat') !== -1;
7374

74-
var lonlat = di.lonlat,
75-
text = [];
75+
var lonlat = di.lonlat;
76+
var text = [];
7677

7778
// TODO should we use a mock axis to format hover?
7879
// If so, we'll need to make precision be zoom-level dependent
@@ -82,11 +83,13 @@ function getExtraText(trace, di) {
8283

8384
if(isAll || (hasLon && hasLat)) {
8485
text.push('(' + format(lonlat[0]) + ', ' + format(lonlat[1]) + ')');
86+
} else if(hasLon) {
87+
text.push('lon: ' + format(lonlat[0]));
88+
} else if(hasLat) {
89+
text.push('lat: ' + format(lonlat[1]));
8590
}
86-
else if(hasLon) text.push('lon: ' + format(lonlat[0]));
87-
else if(hasLat) text.push('lat: ' + format(lonlat[1]));
8891

89-
if(isAll || hoverinfo.indexOf('text') !== -1) {
92+
if(isAll || parts.indexOf('text') !== -1) {
9093
var tx;
9194

9295
if(di.htx) tx = di.htx;

‎src/traces/scatterternary/hover.js

+9-8
Original file line numberDiff line numberDiff line change
@@ -49,19 +49,20 @@ module.exports = function hoverPoints(pointData, xval, yval, hovermode) {
4949
newPointData.yLabelVal = undefined;
5050
// TODO: nice formatting, and label by axis title, for a, b, and c?
5151

52-
var trace = newPointData.trace,
53-
ternary = trace._ternary,
54-
hoverinfo = trace.hoverinfo.split('+'),
55-
text = [];
52+
var trace = newPointData.trace;
53+
var ternary = trace._ternary;
54+
var hoverinfo = cdi.hi || trace.hoverinfo;
55+
var parts = hoverinfo.split('+');
56+
var text = [];
5657

5758
function textPart(ax, val) {
5859
text.push(ax._hovertitle + ': ' + Axes.tickText(ax, val, 'hover').text);
5960
}
6061

61-
if(hoverinfo.indexOf('all') !== -1) hoverinfo = ['a', 'b', 'c'];
62-
if(hoverinfo.indexOf('a') !== -1) textPart(ternary.aaxis, cdi.a);
63-
if(hoverinfo.indexOf('b') !== -1) textPart(ternary.baxis, cdi.b);
64-
if(hoverinfo.indexOf('c') !== -1) textPart(ternary.caxis, cdi.c);
62+
if(parts.indexOf('all') !== -1) parts = ['a', 'b', 'c'];
63+
if(parts.indexOf('a') !== -1) textPart(ternary.aaxis, cdi.a);
64+
if(parts.indexOf('b') !== -1) textPart(ternary.baxis, cdi.b);
65+
if(parts.indexOf('c') !== -1) textPart(ternary.caxis, cdi.c);
6566

6667
newPointData.extraText = text.join('<br>');
6768

‎test/jasmine/tests/carpet_test.js

+11
Original file line numberDiff line numberDiff line change
@@ -603,4 +603,15 @@ describe('scattercarpet hover labels', function() {
603603
)
604604
.then(done);
605605
});
606+
607+
it('should generate hover label with arrayOk \'hoverinfo\' settings', function(done) {
608+
var fig = Lib.extendDeep({}, require('@mocks/scattercarpet.json'));
609+
fig.data[5].hoverinfo = ['a+b', 'a+b', 'a+b', 'b+y'];
610+
611+
run(
612+
[200, 200], fig,
613+
[['b = 3.500', 'y = 2.900'], null]
614+
)
615+
.then(done);
616+
});
606617
});

‎test/jasmine/tests/choropleth_test.js

+12
Original file line numberDiff line numberDiff line change
@@ -128,4 +128,16 @@ describe('Test choropleth hover:', function() {
128128
)
129129
.then(done);
130130
});
131+
132+
it('should generate hover label with arrayOk \'hoverinfo\' settings', function(done) {
133+
var fig = Lib.extendDeep({}, require('@mocks/geo_first.json'));
134+
fig.data[1].hoverinfo = ['location', 'z', 'location+name'];
135+
136+
run(
137+
[400, 160],
138+
fig,
139+
['RUS', 'trace 1']
140+
)
141+
.then(done);
142+
});
131143
});

‎test/jasmine/tests/scattergeo_test.js

+6
Original file line numberDiff line numberDiff line change
@@ -315,4 +315,10 @@ describe('Test scattergeo hover', function() {
315315
.then(done);
316316
});
317317

318+
it('should generate hover label with arrayOk \'hoverinfo\' settings', function(done) {
319+
Plotly.restyle(gd, 'hoverinfo', [['lon', null, 'lat+name']]).then(function() {
320+
check([381, 221], ['lon: 10°', null]);
321+
})
322+
.then(done);
323+
});
318324
});

‎test/jasmine/tests/scattermapbox_test.js

+26
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ var convert = require('@src/traces/scattermapbox/convert');
77

88
var createGraphDiv = require('../assets/create_graph_div');
99
var destroyGraphDiv = require('../assets/destroy_graph_div');
10+
var fail = require('../assets/fail_test');
1011

1112
var mouseEvent = require('../assets/mouse_event');
1213
var click = require('../assets/click');
@@ -604,6 +605,31 @@ describe('@noCI scattermapbox hover', function() {
604605
})
605606
.then(done);
606607
});
608+
609+
it('should generate hover label (\'hoverinfo\' array case)', function(done) {
610+
function check(expected) {
611+
var out = hoverPoints(getPointData(gd), 11, 11)[0];
612+
expect(out.extraText).toEqual(expected);
613+
}
614+
615+
Plotly.restyle(gd, 'hoverinfo', [['lon', 'lat', 'lon+lat+name']]).then(function() {
616+
check('lon: 10°');
617+
return Plotly.restyle(gd, 'hoverinfo', [['lat', 'lon', 'name']]);
618+
})
619+
.then(function() {
620+
check('lat: 10°');
621+
return Plotly.restyle(gd, 'hoverinfo', [['text', 'lon', 'name']]);
622+
})
623+
.then(function() {
624+
check('Apple');
625+
return Plotly.restyle(gd, 'hoverinfo', [[null, 'lon', 'name']]);
626+
})
627+
.then(function() {
628+
check('(10°, 10°)<br>Apple');
629+
})
630+
.catch(fail)
631+
.then(done);
632+
});
607633
});
608634

609635
describe('@noCI Test plotly events on a scattermapbox plot:', function() {

‎test/jasmine/tests/ternary_test.js

+11
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,17 @@ describe('ternary plots', function() {
145145
fontSize: 13,
146146
fontFamily: 'Gravitas'
147147
}, 'after hoverlabel styling restyle call');
148+
149+
return Plotly.restyle(gd, 'hoverinfo', [['a', 'b+c', 'b']]);
150+
})
151+
.then(function() {
152+
check('Component A: 0.5', {
153+
bgcolor: 'rgb(31, 119, 180)',
154+
bordercolor: 'rgb(0, 0, 255)',
155+
fontColor: 'rgb(0, 0, 255)',
156+
fontSize: 13,
157+
fontFamily: 'Gravitas'
158+
}, 'after hoverlabel styling restyle call');
148159
})
149160
.catch(fail)
150161
.then(done);

0 commit comments

Comments
 (0)
Please sign in to comment.