Skip to content

Commit b312392

Browse files
authored
Merge pull request #4662 from plotly/fix-4648
histogram: in hover labels, harmonize start/end values of shared bins
2 parents b52e990 + c387f9b commit b312392

File tree

3 files changed

+51
-3
lines changed

3 files changed

+51
-3
lines changed

src/traces/bar/cross_trace_calc.js

+8
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,14 @@ function crossTraceCalc(gd, plotinfo) {
4747
} else {
4848
calcTracesVert.push(calcTraces[i]);
4949
}
50+
51+
if(fullTrace._computePh) {
52+
var cd = gd.calcdata[i];
53+
for(var j = 0; j < cd.length; j++) {
54+
if(typeof cd[j].ph0 === 'function') cd[j].ph0 = cd[j].ph0();
55+
if(typeof cd[j].ph1 === 'function') cd[j].ph1 = cd[j].ph1();
56+
}
57+
}
5058
}
5159
}
5260

src/traces/histogram/calc.js

+25-3
Original file line numberDiff line numberDiff line change
@@ -102,12 +102,21 @@ function calc(gd, trace) {
102102
};
103103
}
104104

105+
// stash left and right gaps by group
106+
if(!gd._fullLayout._roundFnOpts) gd._fullLayout._roundFnOpts = {};
107+
var groupName = trace['_' + mainData + 'bingroup'];
108+
var roundFnOpts = {leftGap: Infinity, rightGap: Infinity};
109+
if(groupName) {
110+
if(!gd._fullLayout._roundFnOpts[groupName]) gd._fullLayout._roundFnOpts[groupName] = roundFnOpts;
111+
roundFnOpts = gd._fullLayout._roundFnOpts[groupName];
112+
}
113+
105114
// bin the data
106115
// and make histogram-specific pt-number-to-cd-index map object
107116
var nMax = size.length;
108117
var uniqueValsPerBin = true;
109-
var leftGap = Infinity;
110-
var rightGap = Infinity;
118+
var leftGap = roundFnOpts.leftGap;
119+
var rightGap = roundFnOpts.rightGap;
111120
var ptNumber2cdIndex = {};
112121
for(i = 0; i < pos0.length; i++) {
113122
var posi = pos0[i];
@@ -124,10 +133,21 @@ function calc(gd, trace) {
124133
rightGap = Math.min(rightGap, binEdges[n + 1] - posi);
125134
}
126135
}
136+
roundFnOpts.leftGap = leftGap;
137+
roundFnOpts.rightGap = rightGap;
127138

128139
var roundFn;
129140
if(!uniqueValsPerBin) {
130-
roundFn = getBinSpanLabelRound(leftGap, rightGap, binEdges, pa, calendar);
141+
roundFn = function(v, isRightEdge) {
142+
return function() {
143+
var roundFnOpts = gd._fullLayout._roundFnOpts[groupName];
144+
return getBinSpanLabelRound(
145+
roundFnOpts.leftGap,
146+
roundFnOpts.rightGap,
147+
binEdges, pa, calendar
148+
)(v, isRightEdge);
149+
};
150+
};
131151
}
132152

133153
// average and/or normalize the data, if needed
@@ -173,6 +193,8 @@ function calc(gd, trace) {
173193
if(uniqueValsPerBin) {
174194
cdi.ph0 = cdi.ph1 = (inputPoints[i].length) ? pos0[inputPoints[i][0]] : pos[i];
175195
} else {
196+
// Defer evaluation of ph(0|1) in crossTraceCalc
197+
trace._computePh = true;
176198
cdi.ph0 = roundFn(binEdges[i]);
177199
cdi.ph1 = roundFn(binEdges[i + 1], true);
178200
}

test/jasmine/tests/histogram_test.js

+18
Original file line numberDiff line numberDiff line change
@@ -585,6 +585,7 @@ describe('Test histogram', function() {
585585
// even though -0 === 0
586586
out.forEach(function(cdi) {
587587
for(var key in cdi) {
588+
if(typeof cdi[key] === 'function') cdi[key] = cdi[key]();
588589
if(cdi[key] === 0) cdi[key] = 0;
589590
}
590591
});
@@ -861,6 +862,23 @@ describe('Test histogram', function() {
861862
expect(calcPositions(trace2, [trace1])).toEqual([5, 7]);
862863
});
863864

865+
it('harmonizes start/end value of bins when all traces are autobinned', function(done) {
866+
var mock = require('@mocks/histogram_overlay-bingroup');
867+
var gd = createGraphDiv();
868+
Plotly.newPlot(gd, mock)
869+
.then(function(gd) {
870+
destroyGraphDiv();
871+
var cd0 = gd.calcdata[0];
872+
var cd1 = gd.calcdata[1];
873+
for(var i = 0; i < cd0.length && i < cd1.length; i++) {
874+
expect(cd0[i].ph0).toBe(cd1[i].ph0);
875+
expect(cd0[i].ph1).toBe(cd1[i].ph1);
876+
}
877+
})
878+
.catch(failTest)
879+
.then(done);
880+
});
881+
864882
it('autobins all data as one', function() {
865883
// all integers, so all autobins should get shifted to start 0.5 lower
866884
// than they otherwise would.

0 commit comments

Comments
 (0)