Skip to content

Commit ec448f2

Browse files
committed
histogram: in hover labels, harmonize start/end values of shared bins
1 parent 9a75ef0 commit ec448f2

File tree

3 files changed

+46
-3
lines changed

3 files changed

+46
-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

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

105+
if(!gd._fullLayout._roundFnOpts) gd._fullLayout._roundFnOpts = {};
106+
var groupName = trace['_' + mainData + 'bingroup'];
107+
var roundFnOpts = {leftGap: Infinity, rightGap: Infinity};
108+
if(groupName) {
109+
if(!gd._fullLayout._roundFnOpts[groupName]) gd._fullLayout._roundFnOpts[groupName] = roundFnOpts;
110+
roundFnOpts = gd._fullLayout._roundFnOpts[groupName];
111+
}
112+
105113
// bin the data
106114
// and make histogram-specific pt-number-to-cd-index map object
107115
var nMax = size.length;
108116
var uniqueValsPerBin = true;
109-
var leftGap = Infinity;
110-
var rightGap = Infinity;
117+
var leftGap = roundFnOpts.leftGap;
118+
var rightGap = roundFnOpts.rightGap;
111119
var ptNumber2cdIndex = {};
112120
for(i = 0; i < pos0.length; i++) {
113121
var posi = pos0[i];
@@ -124,10 +132,17 @@ function calc(gd, trace) {
124132
rightGap = Math.min(rightGap, binEdges[n + 1] - posi);
125133
}
126134
}
135+
roundFnOpts.leftGap = leftGap;
136+
roundFnOpts.rightGap = rightGap;
127137

128138
var roundFn;
129139
if(!uniqueValsPerBin) {
130-
roundFn = getBinSpanLabelRound(leftGap, rightGap, binEdges, pa, calendar);
140+
roundFn = function(v, isRightEdge) {
141+
return function() {
142+
var roundFnOpts = gd._fullLayout._roundFnOpts[groupName];
143+
return getBinSpanLabelRound(roundFnOpts.leftGap, roundFnOpts.rightGap, binEdges, pa, calendar)(v, isRightEdge);
144+
};
145+
};
131146
}
132147

133148
// average and/or normalize the data, if needed
@@ -173,6 +188,8 @@ function calc(gd, trace) {
173188
if(uniqueValsPerBin) {
174189
cdi.ph0 = cdi.ph1 = (inputPoints[i].length) ? pos0[inputPoints[i][0]] : pos[i];
175190
} else {
191+
// Defer evaluation of ph(0|1) in crossTraceCalc
192+
trace._computePh = true;
176193
cdi.ph0 = roundFn(binEdges[i]);
177194
cdi.ph1 = roundFn(binEdges[i + 1], true);
178195
}

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)