Skip to content

Histogram edge cases #2028

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Sep 22, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
fix part 1 of #1978 - keep bin width and trim zeros from 1-bin histog…
…rams
  • Loading branch information
alexcjohnson committed Sep 20, 2017
commit a9498bb1e89e5702dd1e60f66e93e189e9c3c86d
11 changes: 9 additions & 2 deletions src/traces/bar/sieve.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,26 @@ function Sieve(traces, separateNegativeValues, dontMergeOverlappingData) {
this.separateNegativeValues = separateNegativeValues;
this.dontMergeOverlappingData = dontMergeOverlappingData;

// for single-bin histograms - see histogram/calc
var width1 = Infinity;

var positions = [];
for(var i = 0; i < traces.length; i++) {
var trace = traces[i];
for(var j = 0; j < trace.length; j++) {
var bar = trace[j];
if(bar.p !== BADNUM) positions.push(bar.p);
}
if(trace[0] && trace[0].width1) {
width1 = Math.min(trace[0].width1, width1);
}
}
this.positions = positions;

var dv = Lib.distinctVals(this.positions);
var dv = Lib.distinctVals(positions);
this.distinctPositions = dv.vals;
this.minDiff = dv.minDiff;
if(dv.vals.length === 1 && width1) this.minDiff = width1;
else this.minDiff = Math.min(dv.minDiff, width1);

this.binWidth = this.minDiff;

Expand Down
8 changes: 7 additions & 1 deletion src/traces/histogram/calc.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ module.exports = function calc(gd, trace) {
break;
}
}
for(i = seriesLen - 1; i > firstNonzero; i--) {
for(i = seriesLen - 1; i >= firstNonzero; i--) {
if(size[i]) {
lastNonzero = i;
break;
Expand All @@ -149,6 +149,12 @@ module.exports = function calc(gd, trace) {
}
}

if(cd.length === 1) {
// when we collapse to a single bin, calcdata no longer describes bin size
// so we need to explicitly specify it
cd[0].width1 = Axes.tickIncrement(cd[0].p, binSpec.size, false, calendar) - cd[0].p;
}

arraysToCalcdata(cd, trace);

return cd;
Expand Down
24 changes: 24 additions & 0 deletions test/jasmine/tests/histogram_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,17 @@ describe('Test histogram', function() {
expect(out.length).toEqual(9001);
});

it('handles single-bin data without extra bins', function() {
var out = _calc({
x: [2.1, 3, 3.9],
xbins: {start: 0, end: 10, size: 2}
});

expect(out).toEqual([
{b: 0, p: 3, s: 3, width1: 2}
]);
});

function calcPositions(opts, extraTraces) {
return _calc(opts, extraTraces).map(function(v) { return v.p; });
}
Expand Down Expand Up @@ -554,5 +565,18 @@ describe('Test histogram', function() {
.catch(fail)
.then(done);
});

it('give the right bar width for single-bin histograms', function(done) {
Plotly.newPlot(gd, [{
type: 'histogram',
x: [3, 3, 3],
xbins: {start: 0, end: 10, size: 2}
}])
.then(function() {
expect(gd._fullLayout.xaxis.range).toBeCloseToArray([2, 4], 3);
})
.catch(fail)
.then(done);
});
});
});