Skip to content

Commit 7b6911f

Browse files
committed
add 'hovertext' to bar traces
1 parent 2b02c5f commit 7b6911f

File tree

5 files changed

+45
-2
lines changed

5 files changed

+45
-2
lines changed

src/traces/bar/arrays_to_calcdata.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ var mergeArray = require('../../lib').mergeArray;
1515
// arrayOk attributes, merge them into calcdata array
1616
module.exports = function arraysToCalcdata(cd, trace) {
1717
mergeArray(trace.text, cd, 'tx');
18+
mergeArray(trace.hovertext, cd, 'htx');
1819

1920
var marker = trace.marker;
2021
if(marker) {

src/traces/bar/attributes.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ module.exports = {
4949
dy: scatterAttrs.dy,
5050

5151
text: scatterAttrs.text,
52+
hovertext: scatterAttrs.hovertext,
5253

5354
textposition: {
5455
valType: 'enumerated',

src/traces/bar/defaults.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ module.exports = function supplyDefaults(traceIn, traceOut, defaultColor, layout
3737
coerce('width');
3838

3939
coerce('text');
40+
coerce('hovertext');
4041

4142
var textPosition = coerce('textposition');
4243

src/traces/bar/hover.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,10 @@ module.exports = function hoverPoints(pointData, xval, yval, hovermode) {
8383
pointData.xLabelVal = di.p;
8484
}
8585

86-
if(di.tx) pointData.text = di.tx;
86+
if(di.htx) pointData.text = di.htx;
87+
else if(trace.hovertext) pointData.text = trace.hovertext;
88+
else if(di.tx) pointData.text = di.tx;
89+
else if(trace.text) pointData.text = trace.text;
8790

8891
ErrorBars.hoverInfo(di, trace, pointData);
8992

test/jasmine/tests/bar_test.js

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ var Axes = PlotlyInternal.Axes;
99

1010
var createGraphDiv = require('../assets/create_graph_div');
1111
var destroyGraphDiv = require('../assets/destroy_graph_div');
12+
var fail = require('../assets/fail_test');
1213
var customMatchers = require('../assets/custom_matchers');
1314

1415
describe('Bar.supplyDefaults', function() {
@@ -1197,7 +1198,8 @@ describe('bar hover', function() {
11971198

11981199
return {
11991200
style: [pt.index, pt.color, pt.xLabelVal, pt.yLabelVal],
1200-
pos: [pt.x0, pt.x1, pt.y0, pt.y1]
1201+
pos: [pt.x0, pt.x1, pt.y0, pt.y1],
1202+
text: pt.text
12011203
};
12021204
}
12031205

@@ -1274,6 +1276,41 @@ describe('bar hover', function() {
12741276
});
12751277
});
12761278

1279+
describe('text labels', function() {
1280+
1281+
it('should show \'hovertext\' items when present, \'text\' if not', function(done) {
1282+
gd = createGraphDiv();
1283+
1284+
var mock = Lib.extendDeep({}, require('@mocks/text_chart_arrays'));
1285+
mock.data.forEach(function(t) { t.type = 'bar'; });
1286+
1287+
Plotly.plot(gd, mock).then(function() {
1288+
var out = _hover(gd, -0.25, 0.5, 'closest');
1289+
expect(out.text).toEqual('Hover text\nA', 'hover text');
1290+
1291+
return Plotly.restyle(gd, 'hovertext', null);
1292+
})
1293+
.then(function() {
1294+
var out = _hover(gd, -0.25, 0.5, 'closest');
1295+
expect(out.text).toEqual('Text\nA', 'hover text');
1296+
1297+
return Plotly.restyle(gd, 'text', ['APPLE', 'BANANA', 'ORANGE']);
1298+
})
1299+
.then(function() {
1300+
var out = _hover(gd, -0.25, 0.5, 'closest');
1301+
expect(out.text).toEqual('APPLE', 'hover text');
1302+
1303+
return Plotly.restyle(gd, 'hovertext', ['apple', 'banana', 'orange']);
1304+
})
1305+
.then(function() {
1306+
var out = _hover(gd, -0.25, 0.5, 'closest');
1307+
expect(out.text).toEqual('apple', 'hover text');
1308+
})
1309+
.catch(fail)
1310+
.then(done);
1311+
});
1312+
});
1313+
12771314
});
12781315

12791316
function mockBarPlot(dataWithoutTraceType, layout) {

0 commit comments

Comments
 (0)