Skip to content

Commit d352fa5

Browse files
committed
add 'text' attribute to box traces
- associated with each sample pt - only visible on hover (for now)
1 parent aef61ae commit d352fa5

File tree

5 files changed

+53
-0
lines changed

5 files changed

+53
-0
lines changed

src/traces/box/attributes.js

+10
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,16 @@ module.exports = {
6262
'missing and the position axis is categorical'
6363
].join(' ')
6464
},
65+
text: extendFlat({}, scatterAttrs.text, {
66+
description: [
67+
'Sets the text elements associated with each sample value.',
68+
'If a single string, the same string appears over',
69+
'all the data points.',
70+
'If an array of string, the items are mapped in order to the',
71+
'this trace\'s (x,y) coordinates.',
72+
'To be seen, trace `hoverinfo` must contain a *text* flag.'
73+
].join(' ')
74+
}),
6575
whiskerwidth: {
6676
valType: 'number',
6777
min: 0,

src/traces/box/calc.js

+13
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ module.exports = function calc(gd, trace) {
5656
var n = Lib.findBin(pos[i], posBins);
5757
if(n >= 0 && n < pLen) {
5858
var pt = {v: v, i: i};
59+
arraysToCalcdata(pt, trace, i);
5960
ptsPerBin[n].push(pt);
6061
}
6162
}
@@ -176,6 +177,18 @@ function initNestedArray(len) {
176177
return arr;
177178
}
178179

180+
function arraysToCalcdata(pt, trace, i) {
181+
var trace2calc = {
182+
text: 'tx'
183+
};
184+
185+
for(var k in trace2calc) {
186+
if(Array.isArray(trace[k])) {
187+
pt[trace2calc[k]] = trace[k][i];
188+
}
189+
}
190+
}
191+
179192
function sortByVal(a, b) { return a.v - b.v; }
180193

181194
function extractVal(o) { return o.v; }

src/traces/box/defaults.js

+1
Original file line numberDiff line numberDiff line change
@@ -72,5 +72,6 @@ module.exports = function supplyDefaults(traceIn, traceOut, defaultColor, layout
7272
}
7373
}
7474

75+
coerce('text');
7576
coerce('hoveron');
7677
};

src/traces/box/hover.js

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ var Axes = require('../../plots/cartesian/axes');
1212
var Lib = require('../../lib');
1313
var Fx = require('../../components/fx');
1414
var Color = require('../../components/color');
15+
var fillHoverText = require('../scatter/fill_hover_text');
1516

1617
module.exports = function hoverPoints(pointData, xval, yval, hovermode) {
1718
var cd = pointData.cd;
@@ -170,6 +171,7 @@ module.exports = function hoverPoints(pointData, xval, yval, hovermode) {
170171
yLabelVal: pt.y
171172
});
172173

174+
fillHoverText(pt, trace, pointData2);
173175
closeData.push(pointData2);
174176
}
175177
}

test/jasmine/tests/box_test.js

+27
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,33 @@ describe('Test box hover:', function() {
216216
'', '', '', '', '', '', '', '', '', '', 'radishes'
217217
],
218218
axis: 'day 1'
219+
}, {
220+
desc: 'text items on hover',
221+
patch: function(fig) {
222+
fig.data.forEach(function(trace) {
223+
trace.boxpoints = 'all';
224+
trace.hoveron = 'points';
225+
trace.text = trace.y.map(function(v) { return 'look:' + v; });
226+
});
227+
fig.layout.hovermode = 'closest';
228+
return fig;
229+
},
230+
nums: ['(day 1, 0.7)\nlook:0.7', '(day 1, 0.6)\nlook:0.6', '(day 1, 0.6)\nlook:0.6'],
231+
name: ['radishes', 'radishes', 'radishes']
232+
}, {
233+
desc: 'only text items on hover',
234+
patch: function(fig) {
235+
fig.data.forEach(function(trace) {
236+
trace.boxpoints = 'all';
237+
trace.hoveron = 'points';
238+
trace.text = trace.y.map(function(v) { return 'look:' + v; });
239+
trace.hoverinfo = 'text';
240+
});
241+
fig.layout.hovermode = 'closest';
242+
return fig;
243+
},
244+
nums: ['look:0.7', 'look:0.6', 'look:0.6'],
245+
name: ['', '', '']
219246
}].forEach(function(specs) {
220247
it('should generate correct hover labels ' + specs.desc, function(done) {
221248
run(specs).catch(fail).then(done);

0 commit comments

Comments
 (0)