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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

respect thresholds passed to contourDensity #57

Merged
merged 1 commit into from
Jun 23, 2022
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/density.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,10 @@ export default function() {
var tz = threshold(values0);

// Convert number of thresholds into uniform thresholds.
if (!Array.isArray(tz)) {
if (Array.isArray(tz)) {
const pow4k = Math.pow(2, 2 * k);
tz = tz.map(d => d * pow4k);
} else {
var stop = max(values0);
tz = tickStep(0, stop, tz);
tz = range(0, Math.floor(stop / tz) * tz, tz);
Expand Down
22 changes: 21 additions & 1 deletion test/density-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ it("contourDensity(data) returns the expected result for empty data", () => {
});

it("contourDensity(data) returns contours centered on a point", () => {
const c = contourDensity().thresholds([0.0001, 0.001]);
const c = contourDensity().thresholds([0.00001, 0.0001]);
for (const p of [[100, 100], [100.5, 102]]) {
const contour = c([p]);
assert.strictEqual(contour.length, 2);
Expand All @@ -27,3 +27,23 @@ it("contourDensity(data) returns contours centered on a point", () => {
}
}
});

it("contourDensity.thresholds(values[])(data) returns contours for the given values", () => {
const points = [[1, 0], [0, 1], [1, 1]];
const c = contourDensity();
const c1 = c(points);
const values1 = c1.map(d => d.value);
const c2 = c.thresholds(values1)(points);
const values2 = c2.map(d => d.value);
assert.deepStrictEqual(values1, values2);
});

it("contourDensity.thresholds(values[])(data) returns contours for the given values at a different cellSize", () => {
const points = [[1, 0], [0, 1], [1, 1]];
const c = contourDensity().cellSize(16);
const c1 = c(points);
const values1 = c1.map(d => d.value);
const c2 = c.thresholds(values1)(points);
const values2 = c2.map(d => d.value);
assert.deepStrictEqual(values1, values2);
});