From 5e9ee8a5e9d1d16d91cdee0790db007a78d593b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philippe=20Rivi=C3=A8re?= Date: Thu, 23 Jun 2022 13:49:55 +0200 Subject: [PATCH] respect thresholds passed to contourDensity --- src/density.js | 5 ++++- test/density-test.js | 22 +++++++++++++++++++++- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/src/density.js b/src/density.js index 55ca469..9a6321c 100644 --- a/src/density.js +++ b/src/density.js @@ -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); diff --git a/test/density-test.js b/test/density-test.js index 8aeb34b..43a3bdb 100644 --- a/test/density-test.js +++ b/test/density-test.js @@ -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); @@ -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); +});