svelteplot 0.5.0-pr-237.0 → 0.5.1-pr-238.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/transforms/bin.js +36 -17
- package/package.json +1 -1
package/dist/transforms/bin.js
CHANGED
|
@@ -4,6 +4,14 @@ import { bin as d3Bin, extent, thresholdFreedmanDiaconis, thresholdScott, thresh
|
|
|
4
4
|
import { reduceOutputs } from '../helpers/reduce.js';
|
|
5
5
|
import { groupFacetsAndZ } from '../helpers/group.js';
|
|
6
6
|
import { isDate } from '../helpers/typeChecks.js';
|
|
7
|
+
const CHANNELS = {
|
|
8
|
+
x: Symbol('x'),
|
|
9
|
+
x1: Symbol('x1'),
|
|
10
|
+
x2: Symbol('x2'),
|
|
11
|
+
y: Symbol('y'),
|
|
12
|
+
y1: Symbol('y1'),
|
|
13
|
+
y2: Symbol('y2')
|
|
14
|
+
};
|
|
7
15
|
const ThresholdGenerators = {
|
|
8
16
|
auto: thresholdScott,
|
|
9
17
|
scott: thresholdScott,
|
|
@@ -101,11 +109,12 @@ export function bin({ data, ...channels }, options = { thresholds: 'auto', cumul
|
|
|
101
109
|
// channels.x is the input
|
|
102
110
|
binX.value((d) => resolveChannel('x', d, channels));
|
|
103
111
|
binY.value((d) => resolveChannel('y', d, channels));
|
|
112
|
+
let yThresholds = [];
|
|
104
113
|
if (interval) {
|
|
105
114
|
const [xlo, xhi] = extent(data.map((d) => resolveChannel('x', d, channels)));
|
|
106
115
|
const [ylo, yhi] = extent(data.map((d) => resolveChannel('y', d, channels)));
|
|
107
116
|
binX.thresholds(maybeInterval(interval).range(xlo, xhi));
|
|
108
|
-
binY.thresholds(maybeInterval(interval).range(ylo, yhi));
|
|
117
|
+
binY.thresholds((yThresholds = maybeInterval(interval).range(ylo, yhi)));
|
|
109
118
|
}
|
|
110
119
|
else if (thresholds) {
|
|
111
120
|
// when binning in x and y, we need to ensure we are using consistent thresholds
|
|
@@ -114,7 +123,7 @@ export function bin({ data, ...channels }, options = { thresholds: 'auto', cumul
|
|
|
114
123
|
: thresholds;
|
|
115
124
|
binX.thresholds(t);
|
|
116
125
|
binY.thresholds(t);
|
|
117
|
-
|
|
126
|
+
yThresholds = binY(data)
|
|
118
127
|
.slice(1)
|
|
119
128
|
.map((g) => g.x0);
|
|
120
129
|
binY.thresholds(yThresholds);
|
|
@@ -124,12 +133,12 @@ export function bin({ data, ...channels }, options = { thresholds: 'auto', cumul
|
|
|
124
133
|
let newChannels = {
|
|
125
134
|
inset: 0.5,
|
|
126
135
|
...channels,
|
|
127
|
-
x:
|
|
128
|
-
x1:
|
|
129
|
-
x2:
|
|
130
|
-
y:
|
|
131
|
-
y1:
|
|
132
|
-
y2:
|
|
136
|
+
x: CHANNELS.x,
|
|
137
|
+
x1: CHANNELS.x1,
|
|
138
|
+
x2: CHANNELS.x2,
|
|
139
|
+
y: CHANNELS.y,
|
|
140
|
+
y1: CHANNELS.y1,
|
|
141
|
+
y2: CHANNELS.y2,
|
|
133
142
|
__x_origField: typeof channels.x === 'string' ? channels.x : null,
|
|
134
143
|
__y_origField: typeof channels.y === 'string' ? channels.y : null
|
|
135
144
|
};
|
|
@@ -141,20 +150,30 @@ export function bin({ data, ...channels }, options = { thresholds: 'auto', cumul
|
|
|
141
150
|
const newData = [];
|
|
142
151
|
binX(data).forEach((groupX) => {
|
|
143
152
|
const newRecordBaseX = {
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
153
|
+
[CHANNELS.x1]: groupX.x0,
|
|
154
|
+
[CHANNELS.x2]: groupX.x1,
|
|
155
|
+
[CHANNELS.x]: isDate(groupX.x0)
|
|
147
156
|
? new Date(Math.round((groupX.x0.getTime() + groupX.x1.getTime()) * 0.5))
|
|
148
157
|
: (groupX.x0 + groupX.x1) * 0.5
|
|
149
158
|
};
|
|
150
|
-
|
|
159
|
+
const [ylo, yhi] = extent(groupX.map((d) => resolveChannel('y', d, channels)));
|
|
160
|
+
const tExtentLo = yThresholds.filter((d) => d < ylo).at(-1);
|
|
161
|
+
const tExtentHi = yThresholds.filter((d) => d > yhi).at(0);
|
|
162
|
+
binY(groupX).forEach((groupY, i) => {
|
|
163
|
+
if (groupY.length === 0)
|
|
164
|
+
return;
|
|
165
|
+
// The first bin.x0 is always equal to the minimum domain value,
|
|
166
|
+
// and the last bin.x1 is always equal to the maximum domain value,
|
|
167
|
+
// therefore we need to align with the thresholds
|
|
168
|
+
const y1 = groupY.x0 === ylo ? tExtentLo : groupY.x0;
|
|
169
|
+
const y2 = groupY.x1 === yhi ? tExtentHi : groupY.x1;
|
|
151
170
|
const newRecordBaseY = {
|
|
152
171
|
...newRecordBaseX,
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
? new Date(Math.round((
|
|
157
|
-
: (
|
|
172
|
+
[CHANNELS.y1]: y1,
|
|
173
|
+
[CHANNELS.y2]: y2,
|
|
174
|
+
[CHANNELS.y]: isDate(y1)
|
|
175
|
+
? new Date(Math.round((y1.getTime() + y2.getTime()) * 0.5))
|
|
176
|
+
: (y1 + y2) * 0.5
|
|
158
177
|
};
|
|
159
178
|
const newGroupChannels = groupFacetsAndZ(groupY, channels, (items, itemGroupProps) => {
|
|
160
179
|
const newRecord = {
|