Skip to content

Commit cf6f1c4

Browse files
authored
Merge pull request #264 from VisActor/fix/bin
fix: the lastbin range should be same as binStep, not threshold with …
2 parents ef223f2 + 0f6d9ef commit cf6f1c4

File tree

3 files changed

+31
-5
lines changed

3 files changed

+31
-5
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"changes": [
3+
{
4+
"packageName": "@visactor/vdataset",
5+
"comment": "Fix bin range is limits by max as threshold",
6+
"type": "none"
7+
}
8+
],
9+
"packageName": "@visactor/vdataset"
10+
}

packages/vdataset/__tests__/bin.test.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,22 @@ describe('bin transform', () => {
1717
}
1818
});
1919

20+
test('bins in max value and threshold', () => {
21+
const data = [1, 1, 1, 2, 5, 7, 8, 9, 10].map(v => ({ v }));
22+
const bins = bin(data, { field: 'v', bins: 10 });
23+
expect(bins.length).toBe(10);
24+
expect(bins[9].x0).toBe(10);
25+
expect(bins[9].x1).toBe(11);
26+
});
27+
28+
test('bins in steps', () => {
29+
const data = [1, 1, 1, 2, 5, 7, 8, 9, 10].map(v => ({ v }));
30+
const bins = bin(data, { field: 'v', step: 2, bins: 10 });
31+
expect(bins.length).toBe(6);
32+
expect(bins[5].x0).toBe(10);
33+
expect(bins[5].x1).toBe(12);
34+
});
35+
2036
test('step produces correct bin widths', () => {
2137
const data = [] as any[];
2238
for (let i = 0; i <= 20; i++) {
@@ -168,12 +184,12 @@ describe('bin transform', () => {
168184
const data = [{ v: 1.2 }, { v: 2.5 }, { v: 5.0 }, { v: 10.7 }];
169185
const out: any = bin(data, { field: 'v', bins: 3 });
170186
// expect thresholds to start at floor(min)=1 and use integer step ceil((max-start)/bins)=4
171-
// thresholds -> [1, 5, 9, max]
187+
// thresholds -> [1, 5, 9, 13]
172188
expect(out.length).toBe(3);
173189
expect(out[0].x0).toBeCloseTo(1, 12);
174190
expect(out[1].x0).toBeCloseTo(5, 12);
175191
expect(out[2].x0).toBeCloseTo(9, 12);
176192
// last x1 should be the actual max
177-
expect(out[2].x1).toBeCloseTo(10.7, 12);
193+
expect(out[2].x1).toBeCloseTo(13, 12);
178194
});
179195
});

packages/vdataset/src/transform/bin.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ export const bin: Transform = (data: Array<object>, options?: IBinOptions) => {
9494
}
9595
thresholds = [startMin];
9696

97-
while (startMin < max) {
97+
while (startMin <= max) {
9898
startMin += stepSize;
9999
thresholds.push(startMin);
100100
}
@@ -107,13 +107,13 @@ export const bin: Transform = (data: Array<object>, options?: IBinOptions) => {
107107
const stepSizeInt = Math.ceil((max - start) / bins);
108108
thresholds = new Array(bins + 1);
109109
for (let i = 0; i <= bins; i++) {
110-
thresholds[i] = i === bins ? max : start + stepSizeInt * i;
110+
thresholds[i] = start + stepSizeInt * i;
111111
}
112112
} else {
113113
const stepSize = (max - min) / bins;
114114
thresholds = new Array(bins + 1);
115115
for (let i = 0; i <= bins; i++) {
116-
thresholds[i] = i === bins ? max : min + stepSize * i;
116+
thresholds[i] = min + stepSize * i;
117117
}
118118
}
119119
}

0 commit comments

Comments
 (0)