-
Notifications
You must be signed in to change notification settings - Fork 715
/
D3ShapeFactories.ts
114 lines (99 loc) · 3.35 KB
/
D3ShapeFactories.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import {
arc as d3Arc,
area as d3Area,
line as d3Line,
pie as d3Pie,
radialLine as d3RadialLine,
stack as d3Stack,
} from 'd3-shape';
import setNumberOrNumberAccessor from './setNumberOrNumberAccessor';
import {
ArcPathConfig,
AreaPathConfig,
LinePathConfig,
PiePathConfig,
RadialLinePathConfig,
StackPathConfig,
} from '../types';
import stackOrder from './stackOrder';
import stackOffset from './stackOffset';
export function arc<Datum>({
innerRadius,
outerRadius,
cornerRadius,
startAngle,
endAngle,
padAngle,
padRadius,
}: ArcPathConfig<Datum> = {}) {
const path = d3Arc<Datum>();
if (innerRadius != null) setNumberOrNumberAccessor(path.innerRadius, innerRadius);
if (outerRadius != null) setNumberOrNumberAccessor(path.outerRadius, outerRadius);
if (cornerRadius != null) setNumberOrNumberAccessor(path.cornerRadius, cornerRadius);
if (startAngle != null) setNumberOrNumberAccessor(path.startAngle, startAngle);
if (endAngle != null) setNumberOrNumberAccessor(path.endAngle, endAngle);
if (padAngle != null) setNumberOrNumberAccessor(path.padAngle, padAngle);
if (padRadius != null) setNumberOrNumberAccessor(path.padRadius, padRadius);
return path;
}
export function area<Datum>({ x, x0, x1, y, y0, y1, defined, curve }: AreaPathConfig<Datum> = {}) {
const path = d3Area<Datum>();
if (x) setNumberOrNumberAccessor(path.x, x);
if (x0) setNumberOrNumberAccessor(path.x0, x0);
if (x1) setNumberOrNumberAccessor(path.x1, x1);
if (y) setNumberOrNumberAccessor(path.y, y);
if (y0) setNumberOrNumberAccessor(path.y0, y0);
if (y1) setNumberOrNumberAccessor(path.y1, y1);
if (defined) path.defined(defined);
if (curve) path.curve(curve);
return path;
}
export function line<Datum>({ x, y, defined, curve }: LinePathConfig<Datum> = {}) {
const path = d3Line<Datum>();
if (x) setNumberOrNumberAccessor(path.x, x);
if (y) setNumberOrNumberAccessor(path.y, y);
if (defined) path.defined(defined);
if (curve) path.curve(curve);
return path;
}
export function pie<Datum>({
startAngle,
endAngle,
padAngle,
value,
sort,
sortValues,
}: PiePathConfig<Datum> = {}) {
const path = d3Pie<Datum>();
// ts can't distinguish between these method overloads
if (sort === null) path.sort(sort);
else if (sort != null) path.sort(sort);
if (sortValues === null) path.sortValues(sortValues);
else if (sortValues != null) path.sortValues(sortValues);
if (value != null) path.value(value);
if (padAngle != null) setNumberOrNumberAccessor(path.padAngle, padAngle);
if (startAngle != null) setNumberOrNumberAccessor(path.startAngle, startAngle);
if (endAngle != null) setNumberOrNumberAccessor(path.endAngle, endAngle);
return path;
}
export function radialLine<Datum>({
angle,
radius,
defined,
curve,
}: RadialLinePathConfig<Datum> = {}) {
const path = d3RadialLine<Datum>();
if (angle) setNumberOrNumberAccessor(path.angle, angle);
if (radius) setNumberOrNumberAccessor(path.radius, radius);
if (defined) path.defined(defined);
if (curve) path.curve(curve);
return path;
}
export function stack<Datum, Key>({ keys, value, order, offset }: StackPathConfig<Datum, Key>) {
const path = d3Stack<Datum, Key>();
if (keys) path.keys(keys);
if (value) setNumberOrNumberAccessor(path.value, value);
if (order) path.order(stackOrder(order));
if (offset) path.offset(stackOffset(offset));
return path;
}