-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterval.ts
97 lines (90 loc) · 2.68 KB
/
interval.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import { Band } from '@antv/scale';
import { MarkComponent as MC, Vector2 } from '../runtime';
import { IntervalMark } from '../spec';
import { MaybeZeroY1, MaybeZeroX } from '../transform';
import {
IntervalShape,
IntervalHollow,
IntervalFunnel,
IntervalPyramid,
} from '../shape';
import {
baseGeometryChannels,
basePostInference,
basePreInference,
tooltip1d,
} from './utils';
function bandWidth(scale: Band, x: any): number {
return scale.getBandWidth(scale.invert(x));
}
const shape = {
rect: IntervalShape,
hollow: IntervalHollow,
funnel: IntervalFunnel,
pyramid: IntervalPyramid,
};
export type IntervalOptions = Omit<IntervalMark, 'type'>;
/**
* Convert value for each channel to rect shapes.
* p0 p1
* ┌────┐
* │ │
* │ │
* p3 └────┘ p2
*/
export const Interval: MC<IntervalOptions> = () => {
return (index, scale, value, coordinate) => {
const { x: X, y: Y, y1: Y1, series: S, size: SZ } = value;
// Calc width for each interval.
// The scales for x and series channels must be band scale.
const x = scale.x as Band;
const series = scale.series as Band;
const [width] = coordinate.getSize();
const NSZ = SZ ? SZ.map((d) => +d / width) : null;
const x1x2 = !SZ
? (x: number, w: number, i: number) => [x, x + w]
: (x: number, w: number, i: number) => {
const mx = x + w / 2;
const s = NSZ[i];
return [mx - s / 2, mx + s / 2];
};
// Calc the points of bounding box for the interval.
// They are start from left-top corner in clock wise order.
const P = Array.from(index, (i) => {
const groupWidth = bandWidth(x, X[i]);
const ratio = series ? bandWidth(series, S?.[i]) : 1;
const width = groupWidth * ratio;
const offset = (+S?.[i] || 0) * groupWidth;
const x0 = +X[i] + offset;
const [x1, x2] = x1x2(x0, width, i);
const y1 = +Y[i];
const y2 = +Y1[i];
const p1 = [x1, y1];
const p2 = [x2, y1];
const p3 = [x2, y2];
const p4 = [x1, y2];
return [p1, p2, p3, p4].map((d) => coordinate.map(d)) as Vector2[];
});
return [index, P];
};
};
Interval.props = {
defaultShape: 'rect',
defaultLabelShape: 'label',
composite: false,
shape,
channels: [
...baseGeometryChannels({ shapes: Object.keys(shape) }),
{ name: 'x', scale: 'band', required: true },
{ name: 'y', required: true },
{ name: 'series', scale: 'band' },
{ name: 'size' },
],
preInference: [
...basePreInference(),
{ type: MaybeZeroY1 },
{ type: MaybeZeroX },
],
postInference: [...basePostInference(), ...tooltip1d()],
interaction: { shareTooltip: true },
};