Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit a9498bb

Browse filesBrowse files
committed
fix part 1 of plotly#1978 - keep bin width and trim zeros from 1-bin histograms
1 parent f81ca4d commit a9498bb
Copy full SHA for a9498bb

File tree

3 files changed

+40
-3
lines changed
Filter options

3 files changed

+40
-3
lines changed

‎src/traces/bar/sieve.js

Copy file name to clipboardExpand all lines: src/traces/bar/sieve.js
+9-2Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,26 @@ function Sieve(traces, separateNegativeValues, dontMergeOverlappingData) {
3030
this.separateNegativeValues = separateNegativeValues;
3131
this.dontMergeOverlappingData = dontMergeOverlappingData;
3232

33+
// for single-bin histograms - see histogram/calc
34+
var width1 = Infinity;
35+
3336
var positions = [];
3437
for(var i = 0; i < traces.length; i++) {
3538
var trace = traces[i];
3639
for(var j = 0; j < trace.length; j++) {
3740
var bar = trace[j];
3841
if(bar.p !== BADNUM) positions.push(bar.p);
3942
}
43+
if(trace[0] && trace[0].width1) {
44+
width1 = Math.min(trace[0].width1, width1);
45+
}
4046
}
4147
this.positions = positions;
4248

43-
var dv = Lib.distinctVals(this.positions);
49+
var dv = Lib.distinctVals(positions);
4450
this.distinctPositions = dv.vals;
45-
this.minDiff = dv.minDiff;
51+
if(dv.vals.length === 1 && width1) this.minDiff = width1;
52+
else this.minDiff = Math.min(dv.minDiff, width1);
4653

4754
this.binWidth = this.minDiff;
4855

‎src/traces/histogram/calc.js

Copy file name to clipboardExpand all lines: src/traces/histogram/calc.js
+7-1Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ module.exports = function calc(gd, trace) {
135135
break;
136136
}
137137
}
138-
for(i = seriesLen - 1; i > firstNonzero; i--) {
138+
for(i = seriesLen - 1; i >= firstNonzero; i--) {
139139
if(size[i]) {
140140
lastNonzero = i;
141141
break;
@@ -149,6 +149,12 @@ module.exports = function calc(gd, trace) {
149149
}
150150
}
151151

152+
if(cd.length === 1) {
153+
// when we collapse to a single bin, calcdata no longer describes bin size
154+
// so we need to explicitly specify it
155+
cd[0].width1 = Axes.tickIncrement(cd[0].p, binSpec.size, false, calendar) - cd[0].p;
156+
}
157+
152158
arraysToCalcdata(cd, trace);
153159

154160
return cd;

‎test/jasmine/tests/histogram_test.js

Copy file name to clipboardExpand all lines: test/jasmine/tests/histogram_test.js
+24Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,17 @@ describe('Test histogram', function() {
269269
expect(out.length).toEqual(9001);
270270
});
271271

272+
it('handles single-bin data without extra bins', function() {
273+
var out = _calc({
274+
x: [2.1, 3, 3.9],
275+
xbins: {start: 0, end: 10, size: 2}
276+
});
277+
278+
expect(out).toEqual([
279+
{b: 0, p: 3, s: 3, width1: 2}
280+
]);
281+
});
282+
272283
function calcPositions(opts, extraTraces) {
273284
return _calc(opts, extraTraces).map(function(v) { return v.p; });
274285
}
@@ -554,5 +565,18 @@ describe('Test histogram', function() {
554565
.catch(fail)
555566
.then(done);
556567
});
568+
569+
it('give the right bar width for single-bin histograms', function(done) {
570+
Plotly.newPlot(gd, [{
571+
type: 'histogram',
572+
x: [3, 3, 3],
573+
xbins: {start: 0, end: 10, size: 2}
574+
}])
575+
.then(function() {
576+
expect(gd._fullLayout.xaxis.range).toBeCloseToArray([2, 4], 3);
577+
})
578+
.catch(fail)
579+
.then(done);
580+
});
557581
});
558582
});

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.