From 9d7c3c2adbf9eccb9e4bab4cd375186de90a1c11 Mon Sep 17 00:00:00 2001 From: Mike Bostock Date: Tue, 19 Nov 2019 09:37:17 -0800 Subject: [PATCH 01/16] Allow stack.{keys,order} to be iterable. --- src/stack.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/stack.js b/src/stack.js index ab4008e..756163c 100644 --- a/src/stack.js +++ b/src/stack.js @@ -1,4 +1,3 @@ -import {slice} from "./array.js"; import constant from "./constant.js"; import offsetNone from "./offset/none.js"; import orderNone from "./order/none.js"; @@ -38,7 +37,7 @@ export default function() { } stack.keys = function(_) { - return arguments.length ? (keys = typeof _ === "function" ? _ : constant(slice.call(_)), stack) : keys; + return arguments.length ? (keys = typeof _ === "function" ? _ : constant(Array.from(_)), stack) : keys; }; stack.value = function(_) { @@ -46,7 +45,7 @@ export default function() { }; stack.order = function(_) { - return arguments.length ? (order = _ == null ? orderNone : typeof _ === "function" ? _ : constant(slice.call(_)), stack) : order; + return arguments.length ? (order = _ == null ? orderNone : typeof _ === "function" ? _ : constant(Array.from(_)), stack) : order; }; stack.offset = function(_) { From 5a4e7929c4ea8865e4d7591274a16cdd2416008d Mon Sep 17 00:00:00 2001 From: Mike Bostock Date: Tue, 19 Nov 2019 09:49:56 -0800 Subject: [PATCH 02/16] Accept iterable for stack. --- src/stack.js | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/src/stack.js b/src/stack.js index 756163c..68815ef 100644 --- a/src/stack.js +++ b/src/stack.js @@ -6,6 +6,12 @@ function stackValue(d, key) { return d[key]; } +function stackSeries(key) { + const series = []; + series.key = key; + return series; +} + export default function() { var keys = constant([]), order = orderNone, @@ -14,18 +20,14 @@ export default function() { function stack(data) { var kz = keys.apply(this, arguments), - i, - m = data.length, - n = kz.length, - sz = new Array(n), + sz = kz.map(stackSeries), + i, n = sz.length, m = -1, oz; - for (i = 0; i < n; ++i) { - for (var ki = kz[i], si = sz[i] = new Array(m), j = 0, sij; j < m; ++j) { - si[j] = sij = [0, +value(data[j], ki, j, data)]; - sij.data = data[j]; + for (const d of data) { + for (i = 0, ++m; i < n; ++i) { + (sz[i][m] = [0, +value(d, kz[i], m, data)]).data = d; } - si.key = ki; } for (i = 0, oz = order(sz); i < n; ++i) { From 32221b2fca3da201bd18adfb288c7952af826a6d Mon Sep 17 00:00:00 2001 From: Mike Bostock Date: Tue, 19 Nov 2019 09:51:20 -0800 Subject: [PATCH 03/16] Rename. --- src/stack.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/stack.js b/src/stack.js index 68815ef..e5a849b 100644 --- a/src/stack.js +++ b/src/stack.js @@ -21,12 +21,12 @@ export default function() { function stack(data) { var kz = keys.apply(this, arguments), sz = kz.map(stackSeries), - i, n = sz.length, m = -1, + i, n = sz.length, j = -1, oz; for (const d of data) { - for (i = 0, ++m; i < n; ++i) { - (sz[i][m] = [0, +value(d, kz[i], m, data)]).data = d; + for (i = 0, ++j; i < n; ++i) { + (sz[i][j] = [0, +value(d, kz[i], j, data)]).data = d; } } From 3c9bd9ea7318f4aa416f622219826fba774cf9f8 Mon Sep 17 00:00:00 2001 From: Mike Bostock Date: Tue, 19 Nov 2019 09:57:05 -0800 Subject: [PATCH 04/16] Allow stack.keys function to return an iterable. --- src/stack.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/stack.js b/src/stack.js index e5a849b..592429f 100644 --- a/src/stack.js +++ b/src/stack.js @@ -19,14 +19,13 @@ export default function() { value = stackValue; function stack(data) { - var kz = keys.apply(this, arguments), - sz = kz.map(stackSeries), + var sz = Array.from(keys.apply(this, arguments), stackSeries), i, n = sz.length, j = -1, oz; for (const d of data) { for (i = 0, ++j; i < n; ++i) { - (sz[i][j] = [0, +value(d, kz[i], j, data)]).data = d; + (sz[i][j] = [0, +value(d, sz[i].key, j, data)]).data = d; } } From e3337dd212a18586f881e1427e03d0870ecadb36 Mon Sep 17 00:00:00 2001 From: Mike Bostock Date: Tue, 19 Nov 2019 10:02:31 -0800 Subject: [PATCH 05/16] Allow stack.order function to return an iterable. --- src/stack.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/stack.js b/src/stack.js index 592429f..6c3742d 100644 --- a/src/stack.js +++ b/src/stack.js @@ -29,7 +29,7 @@ export default function() { } } - for (i = 0, oz = order(sz); i < n; ++i) { + for (i = 0, oz = Array.from(order(sz)); i < n; ++i) { sz[oz[i]].index = i; } From 061a6a58f012251f504201a6bfc957aefde6ec05 Mon Sep 17 00:00:00 2001 From: Mike Bostock Date: Tue, 19 Nov 2019 10:06:03 -0800 Subject: [PATCH 06/16] Allow pie, line, and area to take iterables. --- src/area.js | 3 ++- src/array.js | 6 ++++++ src/line.js | 3 ++- src/pie.js | 3 ++- src/stack.js | 3 ++- 5 files changed, 14 insertions(+), 4 deletions(-) diff --git a/src/area.js b/src/area.js index a048a01..9fe3db8 100644 --- a/src/area.js +++ b/src/area.js @@ -1,4 +1,5 @@ import {path} from "d3-path"; +import array from "./array.js"; import constant from "./constant.js"; import curveLinear from "./curve/linear.js"; import line from "./line.js"; @@ -18,7 +19,7 @@ export default function() { var i, j, k, - n = data.length, + n = (data = array(data)).length, d, defined0 = false, buffer, diff --git a/src/array.js b/src/array.js index 8eeac16..90808ff 100644 --- a/src/array.js +++ b/src/array.js @@ -1 +1,7 @@ export var slice = Array.prototype.slice; + +export default function(x) { + return typeof x === "object" && "length" in x + ? x // Array, TypedArray, NodeList, array-like + : Array.from(x); // Map, Set, iterable, string, or anything else +} diff --git a/src/line.js b/src/line.js index 3ba2ab3..ba3ae15 100644 --- a/src/line.js +++ b/src/line.js @@ -1,4 +1,5 @@ import {path} from "d3-path"; +import array from "./array.js"; import constant from "./constant.js"; import curveLinear from "./curve/linear.js"; import {x as pointX, y as pointY} from "./point.js"; @@ -13,7 +14,7 @@ export default function() { function line(data) { var i, - n = data.length, + n = (data = array(data)).length, d, defined0 = false, buffer; diff --git a/src/pie.js b/src/pie.js index 1108722..6748f45 100644 --- a/src/pie.js +++ b/src/pie.js @@ -1,3 +1,4 @@ +import array from "./array.js"; import constant from "./constant.js"; import descending from "./descending.js"; import identity from "./identity.js"; @@ -13,7 +14,7 @@ export default function() { function pie(data) { var i, - n = data.length, + n = (data = array(data)).length, j, k, sum = 0, diff --git a/src/stack.js b/src/stack.js index 6c3742d..c4d30b1 100644 --- a/src/stack.js +++ b/src/stack.js @@ -1,3 +1,4 @@ +import array from "./array.js"; import constant from "./constant.js"; import offsetNone from "./offset/none.js"; import orderNone from "./order/none.js"; @@ -29,7 +30,7 @@ export default function() { } } - for (i = 0, oz = Array.from(order(sz)); i < n; ++i) { + for (i = 0, oz = array(order(sz)); i < n; ++i) { sz[oz[i]].index = i; } From 3672c0112fa2597e52cc942cf95386558fb0af1f Mon Sep 17 00:00:00 2001 From: Mike Bostock Date: Tue, 19 Nov 2019 10:06:48 -0800 Subject: [PATCH 07/16] Test for pie(iterable). --- test/pie-test.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/test/pie-test.js b/test/pie-test.js index 0a4aaa8..e952e60 100644 --- a/test/pie-test.js +++ b/test/pie-test.js @@ -24,6 +24,16 @@ tape("pie(data) returns arcs in input order", function(test) { test.end(); }); +tape("pie(data) accepts an iterable", function(test) { + var p = shape.pie(); + test.deepEqual(p(new Set([1, 3, 2])), [ + {data: 1, value: 1, index: 2, startAngle: 5.235987755982988, endAngle: 6.283185307179585, padAngle: 0}, + {data: 3, value: 3, index: 0, startAngle: 0.000000000000000, endAngle: 3.141592653589793, padAngle: 0}, + {data: 2, value: 2, index: 1, startAngle: 3.141592653589793, endAngle: 5.235987755982988, padAngle: 0} + ]); + test.end(); +}); + tape("pie(data) coerces the specified value to a number", function(test) { var p = shape.pie(), three = {valueOf: function() { return 3; }}; test.deepEqual(p(["1", three, "2"]), [ From 7651a1aba6b9e3fa7d1d202a8c4f5e84e1f6d50f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philippe=20Rivi=C3=A8re?= Date: Fri, 10 Jul 2020 21:57:38 +0200 Subject: [PATCH 08/16] links to d3v6 versions --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 70783d0..9645d84 100644 --- a/README.md +++ b/README.md @@ -26,11 +26,11 @@ For more, read [Introducing d3-shape](https://medium.com/@mbostock/introducing-d ## Installing -If you use NPM, `npm install d3-shape`. Otherwise, download the [latest release](https://github.com/d3/d3-shape/releases/latest). You can also load directly from [d3js.org](https://d3js.org), either as a [standalone library](https://d3js.org/d3-shape.v1.min.js) or as part of [D3](https://github.com/d3/d3). AMD, CommonJS, and vanilla environments are supported. In vanilla, a `d3` global is exported: +If you use NPM, `npm install d3-shape`. Otherwise, download the [latest release](https://github.com/d3/d3-shape/releases/latest). You can also load directly from [d3js.org](https://d3js.org), either as a [standalone library](https://d3js.org/d3-shape.v2.min.js) or as part of [D3](https://github.com/d3/d3). AMD, CommonJS, and vanilla environments are supported. In vanilla, a `d3` global is exported: ```html - - + +