| 1 | import array from "./array.js";
|
|---|
| 2 | import constant from "./constant.js";
|
|---|
| 3 | import offsetNone from "./offset/none.js";
|
|---|
| 4 | import orderNone from "./order/none.js";
|
|---|
| 5 |
|
|---|
| 6 | function stackValue(d, key) {
|
|---|
| 7 | return d[key];
|
|---|
| 8 | }
|
|---|
| 9 |
|
|---|
| 10 | function stackSeries(key) {
|
|---|
| 11 | const series = [];
|
|---|
| 12 | series.key = key;
|
|---|
| 13 | return series;
|
|---|
| 14 | }
|
|---|
| 15 |
|
|---|
| 16 | export default function() {
|
|---|
| 17 | var keys = constant([]),
|
|---|
| 18 | order = orderNone,
|
|---|
| 19 | offset = offsetNone,
|
|---|
| 20 | value = stackValue;
|
|---|
| 21 |
|
|---|
| 22 | function stack(data) {
|
|---|
| 23 | var sz = Array.from(keys.apply(this, arguments), stackSeries),
|
|---|
| 24 | i, n = sz.length, j = -1,
|
|---|
| 25 | oz;
|
|---|
| 26 |
|
|---|
| 27 | for (const d of data) {
|
|---|
| 28 | for (i = 0, ++j; i < n; ++i) {
|
|---|
| 29 | (sz[i][j] = [0, +value(d, sz[i].key, j, data)]).data = d;
|
|---|
| 30 | }
|
|---|
| 31 | }
|
|---|
| 32 |
|
|---|
| 33 | for (i = 0, oz = array(order(sz)); i < n; ++i) {
|
|---|
| 34 | sz[oz[i]].index = i;
|
|---|
| 35 | }
|
|---|
| 36 |
|
|---|
| 37 | offset(sz, oz);
|
|---|
| 38 | return sz;
|
|---|
| 39 | }
|
|---|
| 40 |
|
|---|
| 41 | stack.keys = function(_) {
|
|---|
| 42 | return arguments.length ? (keys = typeof _ === "function" ? _ : constant(Array.from(_)), stack) : keys;
|
|---|
| 43 | };
|
|---|
| 44 |
|
|---|
| 45 | stack.value = function(_) {
|
|---|
| 46 | return arguments.length ? (value = typeof _ === "function" ? _ : constant(+_), stack) : value;
|
|---|
| 47 | };
|
|---|
| 48 |
|
|---|
| 49 | stack.order = function(_) {
|
|---|
| 50 | return arguments.length ? (order = _ == null ? orderNone : typeof _ === "function" ? _ : constant(Array.from(_)), stack) : order;
|
|---|
| 51 | };
|
|---|
| 52 |
|
|---|
| 53 | stack.offset = function(_) {
|
|---|
| 54 | return arguments.length ? (offset = _ == null ? offsetNone : _, stack) : offset;
|
|---|
| 55 | };
|
|---|
| 56 |
|
|---|
| 57 | return stack;
|
|---|
| 58 | }
|
|---|