Skip to content

Commit e40c807

Browse files
authored
Merge pull request #3213 from plotly/finance-hoverinfo-arrayOk
Finance traces hoverinfo arrayOk
2 parents ceb119f + 12e57bc commit e40c807

File tree

3 files changed

+113
-3
lines changed

3 files changed

+113
-3
lines changed

src/traces/ohlc/hover.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,9 @@ function hoverSplit(pointData, xval, yval, hovermode) {
101101
// skip the rest (for this trace) if we didn't find a close point
102102
if(!closestPoint) return [];
103103

104-
var hoverinfo = trace.hoverinfo;
104+
var cdIndex = closestPoint.index;
105+
var di = cd[cdIndex];
106+
var hoverinfo = di.hi || trace.hoverinfo;
105107
var hoverParts = hoverinfo.split('+');
106108
var isAll = hoverinfo === 'all';
107109
var hasY = isAll || hoverParts.indexOf('y') !== -1;
@@ -166,7 +168,7 @@ function hoverOnPoints(pointData, xval, yval, hovermode) {
166168
return t.labels[attr] + Axes.hoverLabelText(ya, trace[attr][i]);
167169
}
168170

169-
var hoverinfo = trace.hoverinfo;
171+
var hoverinfo = di.hi || trace.hoverinfo;
170172
var hoverParts = hoverinfo.split('+');
171173
var isAll = hoverinfo === 'all';
172174
var hasY = isAll || hoverParts.indexOf('y') !== -1;

src/traces/parcats/attributes.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ module.exports = {
4141
domain: domainAttrs({name: 'parcats', trace: true, editType: 'calc'}),
4242
hoverinfo: extendFlat({}, plotAttrs.hoverinfo, {
4343
flags: ['count', 'probability'],
44-
editType: 'plot'
44+
editType: 'plot',
45+
arrayOk: false
4546
// plotAttrs.hoverinfo description is appropriate
4647
}),
4748
hoveron: {

test/jasmine/tests/finance_test.js

+107
Original file line numberDiff line numberDiff line change
@@ -1046,5 +1046,112 @@ describe('finance charts *special* handlers:', function() {
10461046
.catch(failTest)
10471047
.then(done);
10481048
});
1049+
});
1050+
1051+
describe('finance trace hover:', function() {
1052+
var gd;
1053+
1054+
afterEach(destroyGraphDiv);
1055+
1056+
function run(specs) {
1057+
gd = createGraphDiv();
1058+
1059+
var data = specs.traces.map(function(t) {
1060+
return Lib.extendFlat({
1061+
type: specs.type,
1062+
open: [1, 2],
1063+
close: [2, 3],
1064+
high: [3, 4],
1065+
low: [0, 5]
1066+
}, t);
1067+
});
1068+
1069+
var layout = Lib.extendFlat({
1070+
showlegend: false,
1071+
width: 400,
1072+
height: 400,
1073+
margin: {t: 0, b: 0, l: 0, r: 0, pad: 0}
1074+
}, specs.layout || {});
1075+
1076+
var xval = 'xval' in specs ? specs.xvals : 0;
1077+
var yval = 'yval' in specs ? specs.yvals : 1;
1078+
var hovermode = layout.hovermode || 'x';
1079+
1080+
return Plotly.plot(gd, data, layout).then(function() {
1081+
var results = gd.calcdata.map(function(cd) {
1082+
var trace = cd[0].trace;
1083+
var pointData = {
1084+
index: false,
1085+
distance: 20,
1086+
cd: cd,
1087+
trace: trace,
1088+
xa: gd._fullLayout.xaxis,
1089+
ya: gd._fullLayout.yaxis,
1090+
maxHoverDistance: 20
1091+
};
1092+
var pts = trace._module.hoverPoints(pointData, xval, yval, hovermode);
1093+
return pts ? pts[0] : {distance: Infinity};
1094+
});
1095+
1096+
var actual = results[0];
1097+
var exp = specs.exp;
1098+
1099+
1100+
for(var k in exp) {
1101+
var msg = '- key ' + k;
1102+
expect(actual[k]).toBe(exp[k], msg);
1103+
}
1104+
});
1105+
}
10491106

1107+
['ohlc', 'candlestick'].forEach(function(type) {
1108+
[{
1109+
type: type,
1110+
desc: 'basic',
1111+
traces: [{}],
1112+
exp: {
1113+
extraText: 'open: 1<br>high: 3<br>low: 0<br>close: 2 ▲'
1114+
}
1115+
}, {
1116+
type: type,
1117+
desc: 'with scalar text',
1118+
traces: [{text: 'SCALAR'}],
1119+
exp: {
1120+
extraText: 'open: 1<br>high: 3<br>low: 0<br>close: 2 ▲<br>SCALAR'
1121+
}
1122+
}, {
1123+
type: type,
1124+
desc: 'with array text',
1125+
traces: [{text: ['A', 'B']}],
1126+
exp: {
1127+
extraText: 'open: 1<br>high: 3<br>low: 0<br>close: 2 ▲<br>A'
1128+
}
1129+
}, {
1130+
type: type,
1131+
desc: 'just scalar text',
1132+
traces: [{hoverinfo: 'text', text: 'SCALAR'}],
1133+
exp: {
1134+
extraText: 'SCALAR'
1135+
}
1136+
}, {
1137+
type: type,
1138+
desc: 'just array text',
1139+
traces: [{hoverinfo: 'text', text: ['A', 'B']}],
1140+
exp: {
1141+
extraText: 'A'
1142+
}
1143+
}, {
1144+
type: type,
1145+
desc: 'just array text with array hoverinfo',
1146+
traces: [{hoverinfo: ['text', 'text'], text: ['A', 'B']}],
1147+
exp: {
1148+
extraText: 'A'
1149+
}
1150+
}]
1151+
.forEach(function(specs) {
1152+
it('should generate correct hover labels ' + type + ' - ' + specs.desc, function(done) {
1153+
run(specs).catch(failTest).then(done);
1154+
});
1155+
});
1156+
});
10501157
});

0 commit comments

Comments
 (0)