/
area.tsx
184 lines (167 loc) · 4.57 KB
/
area.tsx
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/* eslint no-magic-numbers: ["error", { "ignore": [-1, 0, 1, 2] }]*/
import React from "react";
import { defaults } from "lodash";
import * as d3Shape from "victory-vendor/d3-shape";
import {
Helpers,
Path,
UserProps,
VictoryCommonPrimitiveProps,
LineHelpers,
VictoryCommonThemeProps,
} from "victory-core";
const defined = (d) => {
const y = d._y1 !== undefined ? d._y1 : d._y;
return y !== null && y !== undefined && d._y0 !== null;
};
const getXAccessor = (scale) => {
return (d) => scale.x(d._x1 !== undefined ? d._x1 : d._x);
};
const getYAccessor = (scale) => {
return (d) => scale.y(d._y1 !== undefined ? d._y1 : d._y);
};
const getY0Accessor = (scale) => {
return (d) => scale.y(d._y0);
};
const getAngleAccessor = (scale) => {
return (d) => {
const x = scale.x(d._x1 !== undefined ? d._x1 : d._x);
return -1 * x + Math.PI / 2;
};
};
const getCartesianArea = (props: AreaProps) => {
const { horizontal, scale } = props;
const interpolationFunction = LineHelpers.getInterpolationFunction(props);
return horizontal
? d3Shape
.area()
.defined(defined)
.curve(interpolationFunction)
.x0(getY0Accessor(scale))
.x1(getYAccessor(scale))
.y(getXAccessor(scale))
: d3Shape
.area()
.defined(defined)
.curve(interpolationFunction)
.x(getXAccessor(scale))
.y1(getYAccessor(scale))
.y0(getY0Accessor(scale));
};
const getAreaFunction = (props: AreaProps) => {
const { polar, scale } = props;
const interpolationFunction = LineHelpers.getInterpolationFunction(props);
return polar
? d3Shape
.radialArea()
.defined(defined)
.curve(interpolationFunction)
.angle(getAngleAccessor(scale))
.outerRadius(getYAccessor(scale))
.innerRadius(getY0Accessor(scale))
: getCartesianArea(props);
};
const evaluateProps = (props: AreaProps) => {
/**
* Potential evaluated props are:
* `ariaLabel`
* `desc`
* `id`
* `style`
* `tabIndex`
*/
const ariaLabel = Helpers.evaluateProp(props.ariaLabel, props);
const desc = Helpers.evaluateProp(props.desc, props);
const id = Helpers.evaluateProp(props.id, props);
const style = Helpers.evaluateStyle(
Object.assign({ fill: "black" }, props.style),
props,
);
const tabIndex = Helpers.evaluateProp(props.tabIndex, props);
return Object.assign({}, props, { ariaLabel, desc, id, style, tabIndex });
};
const defaultProps = {
groupComponent: <g />,
pathComponent: <Path />,
role: "presentation",
shapeRendering: "auto",
};
/**
* The area primitive used by VictoryArea
*/
export const Area: React.FC<AreaProps> = (initialProps) => {
const props = evaluateProps(defaults({}, initialProps, defaultProps));
const {
ariaLabel,
role,
shapeRendering,
className,
polar,
origin,
data,
pathComponent,
events,
groupComponent,
clipPath,
id,
style,
desc,
tabIndex,
} = props;
const userProps = UserProps.getSafeUserProps(props);
const defaultTransform =
polar && origin ? `translate(${origin.x}, ${origin.y})` : undefined;
const transform = props.transform || defaultTransform;
const renderLine =
style.stroke && style.stroke !== "none" && style.stroke !== "transparent";
const areaFunction = getAreaFunction(props);
const lineFunction = renderLine && LineHelpers.getLineFunction(props);
const areaStroke = style.stroke ? "none" : style.fill;
const sharedProps = {
"aria-label": ariaLabel,
className,
role,
shapeRendering,
transform,
...events,
clipPath,
tabIndex,
};
const area = React.cloneElement(
pathComponent!,
Object.assign(
{
key: `${id}-area`,
style: Object.assign({}, style, { stroke: areaStroke }),
d: areaFunction(data),
desc,
tabIndex,
},
sharedProps,
userProps,
),
);
const line = renderLine
? React.cloneElement(
pathComponent!,
Object.assign(
{
key: `${id}-area-stroke`,
style: Object.assign({}, style, { fill: "none" }),
d: lineFunction(data),
},
sharedProps,
),
)
: null;
return renderLine
? React.cloneElement(groupComponent!, userProps, [area, line])
: area;
};
export interface AreaProps extends VictoryCommonPrimitiveProps {
horizontal?: VictoryCommonThemeProps["horizontal"];
groupComponent?: React.ReactElement;
// eslint-disable-next-line @typescript-eslint/ban-types
interpolation?: string | Function;
pathComponent?: React.ReactElement;
}