Skip to content
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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Specify histogram thresholds in fixed-length array #1086

Merged
merged 2 commits into from
Jan 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 9 additions & 1 deletion client/__tests__/util/range.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { range, rangeFill } from "../../src/util/range";
import { range, rangeFill, linspace } from "../../src/util/range";

describe("range", () => {
test("no defaults", () => {
Expand Down Expand Up @@ -40,3 +40,11 @@ describe("rangefill", () => {
);
});
});

describe("linspace", () => {
test("linspace(arr, start, step)", () => {
expect(linspace(0.0, 2.0, 5)).toMatchObject(
[0.0, 0.5, 1.0, 1.5, 2.0]
);
});
});
8 changes: 7 additions & 1 deletion client/src/components/brushableHistogram/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import * as d3 from "d3";
import memoize from "memoize-one";
import * as globals from "../../globals";
import actions from "../../actions";
import { linspace } from "../../util/range";
import { makeContinuousDimensionName } from "../../util/nameCreators";

@connect((state, ownProps) => {
Expand Down Expand Up @@ -53,15 +54,20 @@ class HistogramBrush extends React.PureComponent {
const values = col.asArray();
const summary = col.summarize();
const { min: domainMin, max: domainMax } = summary;
const numBins = 40;

histogramCache.x = d3
.scaleLinear()
.domain([domainMin, domainMax])
.range([this.marginLeft, this.marginLeft + this.width]);

const [xStart, xStop] = histogramCache.x.domain();
const histThresholds = linspace(xStart, xStop, numBins + 1);

histogramCache.bins = d3
.histogram()
.domain(histogramCache.x.domain())
.thresholds(40)(values);
.thresholds(histThresholds)(values);

const yMax = histogramCache.bins
.map(b => b.length)
Expand Down
5 changes: 5 additions & 0 deletions client/src/util/range.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,8 @@ export function range(start, stop, step) {
const len = Math.max(Math.ceil((stop - start) / step), 0);
return _doFill(new Array(len), start, step, len);
}

export function linspace(start, stop, nsteps) {
const delta = (stop - start) / (nsteps - 1).toFixed();
return range(0, nsteps, 1).map(i => start + i * delta);
}