Skip to content

Commit d7999e2

Browse files
committed
'fill' fx calcdata items instead of 'merging' them
- previously fx attribute arrays with length less than the calcdata trace length were skipped. That is, they did not get filled with their default - this patch here ensures that the defaults values are present in the calcdata so that we don't have to rely on fallbacks in the hover hot code path.
1 parent 3c693a0 commit d7999e2

File tree

3 files changed

+55
-11
lines changed

3 files changed

+55
-11
lines changed

src/components/fx/calc.js

+7-7
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,17 @@ module.exports = function calc(gd) {
3030
// won't match the data array order.
3131
if(Registry.traceIs(trace, 'pie')) continue;
3232

33-
var mergeFn = Registry.traceIs(trace, '2dMap') ? paste : Lib.mergeArray;
33+
var fillFn = Registry.traceIs(trace, '2dMap') ? paste : Lib.fillArray;
3434

35-
mergeFn(trace.hoverinfo, cd, 'hi', makeCoerceHoverInfo(trace));
35+
fillFn(trace.hoverinfo, cd, 'hi', makeCoerceHoverInfo(trace));
3636

3737
if(!trace.hoverlabel) continue;
3838

39-
mergeFn(trace.hoverlabel.bgcolor, cd, 'hbg');
40-
mergeFn(trace.hoverlabel.bordercolor, cd, 'hbc');
41-
mergeFn(trace.hoverlabel.font.size, cd, 'hts');
42-
mergeFn(trace.hoverlabel.font.color, cd, 'htc');
43-
mergeFn(trace.hoverlabel.font.family, cd, 'htf');
39+
fillFn(trace.hoverlabel.bgcolor, cd, 'hbg');
40+
fillFn(trace.hoverlabel.bordercolor, cd, 'hbc');
41+
fillFn(trace.hoverlabel.font.size, cd, 'hts');
42+
fillFn(trace.hoverlabel.font.color, cd, 'htc');
43+
fillFn(trace.hoverlabel.font.family, cd, 'htf');
4444
}
4545
};
4646

src/lib/index.js

+10-4
Original file line numberDiff line numberDiff line change
@@ -350,19 +350,25 @@ lib.noneOrAll = function(containerIn, containerOut, attrList) {
350350
}
351351
};
352352

353-
/** merge data array into calcdata items
353+
lib.mergeArray = function(traceAttr, cd, cdAttr) {
354+
if(Array.isArray(traceAttr)) {
355+
var imax = Math.min(traceAttr.length, cd.length);
356+
for(var i = 0; i < imax; i++) cd[i][cdAttr] = traceAttr[i];
357+
}
358+
};
359+
360+
/** fill data array into calcdata items
354361
*
355362
* @param {array} traceAttr : trace attribute
356363
* @param {object} cd : calcdata trace
357364
* @param {string} cdAttr : calcdata key
358365
* @param {function} [fn] : optional function to apply to each array item
359366
*/
360-
lib.mergeArray = function(traceAttr, cd, cdAttr, fn) {
367+
lib.fillArray = function(traceAttr, cd, cdAttr, fn) {
361368
fn = fn || lib.identity;
362369

363370
if(Array.isArray(traceAttr)) {
364-
var imax = Math.min(traceAttr.length, cd.length);
365-
for(var i = 0; i < imax; i++) {
371+
for(var i = 0; i < cd.length; i++) {
366372
cd[i][cdAttr] = fn(traceAttr[i]);
367373
}
368374
}

test/jasmine/tests/hover_label_test.js

+38
Original file line numberDiff line numberDiff line change
@@ -1187,6 +1187,44 @@ describe('Test hover label custom styling:', function() {
11871187
path: ['rgb(68, 68, 68)', 'rgb(255, 255, 255)'],
11881188
text: [13, 'Arial', 'rgb(255, 255, 255)']
11891189
});
1190+
1191+
// test insufficient arrayOk case
1192+
return Plotly.restyle(gd, 'hoverinfo', [['none']]);
1193+
})
1194+
.then(function() {
1195+
expect(gd.calcdata[0].map(function(o) { return o.hi; })).toEqual(
1196+
['none', 'x+y+z+text', 'x+y+z+text'],
1197+
'should fill calcdata item with correct default'
1198+
);
1199+
1200+
_hover(gd, { xval: gd._fullData[0].x[0] });
1201+
1202+
assertPtLabel(null);
1203+
assertCommonLabel(null);
1204+
})
1205+
.then(function() {
1206+
_hover(gd, { xval: gd._fullData[0].x[1] });
1207+
1208+
assertPtLabel({
1209+
path: ['rgb(0, 0, 0)', 'rgb(255, 255, 255)'],
1210+
text: [13, 'Arial', 'rgb(255, 255, 255)']
1211+
});
1212+
assertCommonLabel({
1213+
path: ['rgb(68, 68, 68)', 'rgb(255, 255, 255)'],
1214+
text: [13, 'Arial', 'rgb(255, 255, 255)']
1215+
});
1216+
})
1217+
.then(function() {
1218+
_hover(gd, { xval: gd._fullData[0].x[2] });
1219+
1220+
assertPtLabel({
1221+
path: ['rgb(0, 255, 255)', 'rgb(68, 68, 68)'],
1222+
text: [13, 'Arial', 'rgb(68, 68, 68)']
1223+
});
1224+
assertCommonLabel({
1225+
path: ['rgb(68, 68, 68)', 'rgb(255, 255, 255)'],
1226+
text: [13, 'Arial', 'rgb(255, 255, 255)']
1227+
});
11901228
})
11911229
.catch(fail)
11921230
.then(done);

0 commit comments

Comments
 (0)