-
Notifications
You must be signed in to change notification settings - Fork 525
/
Copy pathvictory-axis.tsx
257 lines (236 loc) · 7.41 KB
/
victory-axis.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
import React from "react";
import { isEmpty } from "lodash";
import {
VictoryLabel,
VictoryContainer,
VictoryTheme,
LineSegment,
TextSize,
addEvents,
Axis,
UserProps,
DomainPropType,
EventPropTypeInterface,
OrientationTypes,
VictoryAxisCommonProps,
VictoryCommonProps,
VictorySingleLabelableProps,
EventsMixinClass,
} from "victory-core";
import { getBaseProps, getStyles } from "./helper-methods";
const fallbackProps = {
width: 450,
height: 300,
padding: 50,
};
const options = {
components: [
{ name: "axis", index: 0 },
{ name: "axisLabel", index: 0 },
{ name: "grid" },
{ name: "parent", index: "parent" },
{ name: "ticks" },
{ name: "tickLabels" },
],
};
export type VictoryAxisTTargetType =
| "axis"
| "axisLabel"
| "grid"
| "ticks"
| "tickLabels"
| "parent";
export interface VictoryAxisProps
extends VictoryAxisCommonProps,
VictoryCommonProps,
VictorySingleLabelableProps {
crossAxis?: boolean;
domain?: DomainPropType;
events?: EventPropTypeInterface<VictoryAxisTTargetType, number | string>[];
fixLabelOverlap?: boolean;
offsetX?: number;
offsetY?: number;
orientation?: OrientationTypes;
}
// eslint-disable-next-line @typescript-eslint/no-empty-interface
interface VictoryAxisBase extends EventsMixinClass<VictoryAxisProps> {}
class VictoryAxisBase extends React.Component<VictoryAxisProps> {
static animationWhitelist: Array<keyof VictoryAxisProps> = [
"style",
"domain",
"range",
"tickCount",
"tickValues",
"offsetX",
"offsetY",
"padding",
"width",
"height",
];
static displayName = "VictoryAxis";
static role = "axis";
static defaultTransitions = {
onExit: {
duration: 500,
},
onEnter: {
duration: 500,
},
};
static defaultProps = {
axisComponent: <LineSegment />,
axisLabelComponent: <VictoryLabel />,
tickLabelComponent: <VictoryLabel />,
tickComponent: <LineSegment />,
gridComponent: <LineSegment />,
standalone: true,
theme: VictoryTheme.grayscale,
containerComponent: <VictoryContainer />,
groupComponent: <g role="presentation" />,
fixLabelOverlap: false,
};
static getDomain = Axis.getDomain;
static getAxis = Axis.getAxis;
static getStyles(props) {
return getStyles(props);
}
static getBaseProps(props) {
return getBaseProps(props, fallbackProps);
}
static expectedComponents: Array<keyof VictoryAxisProps> = [
"axisComponent",
"axisLabelComponent",
"groupComponent",
"containerComponent",
"tickComponent",
"tickLabelComponent",
"gridComponent",
];
renderLine(props) {
const { axisComponent } = props;
const axisProps = this.getComponentProps(axisComponent, "axis", 0);
return React.cloneElement(axisComponent, axisProps);
}
renderLabel(props) {
const { axisLabelComponent, label } = props;
if (!label) {
return null;
}
const axisLabelProps = this.getComponentProps(
axisLabelComponent,
"axisLabel",
0,
);
return React.cloneElement(axisLabelComponent, axisLabelProps);
}
renderGridAndTicks(props) {
const { tickComponent, tickLabelComponent, gridComponent, name } = props;
const shouldRender = (componentProps) => {
const { style = {}, events = {} } = componentProps;
const visible =
style.stroke !== "transparent" &&
style.stroke !== "none" &&
style.strokeWidth !== 0;
return visible || !isEmpty(events);
};
return this.dataKeys.map((key, index) => {
const tickProps = this.getComponentProps(tickComponent, "ticks", index);
const BaseTickComponent = React.cloneElement(tickComponent, tickProps);
const TickComponent = shouldRender(BaseTickComponent.props)
? BaseTickComponent
: undefined;
const gridProps = this.getComponentProps(gridComponent, "grid", index);
const BaseGridComponent = React.cloneElement(gridComponent, gridProps);
const GridComponent = shouldRender(BaseGridComponent.props)
? BaseGridComponent
: undefined;
const tickLabelProps = this.getComponentProps(
tickLabelComponent,
"tickLabels",
index,
);
const TickLabel = React.cloneElement(tickLabelComponent, tickLabelProps);
const children = [GridComponent, TickComponent, TickLabel].filter(
Boolean,
);
return React.cloneElement(
props.groupComponent,
{ key: `${name}-tick-group-${key}` },
children,
);
});
}
fixLabelOverlap(gridAndTicks, props) {
const isVertical = Axis.isVertical(props);
const size = isVertical ? props.height : props.width;
const isVictoryLabel = (child) => child.type && child.type.role === "label";
const labels = gridAndTicks
.map((gridAndTick) => gridAndTick.props.children)
.reduce((accumulator, childArr) => accumulator.concat(childArr), [])
.filter(isVictoryLabel)
.map((child) => child.props);
const paddingToObject = (padding) =>
typeof padding === "object"
? Object.assign({}, { top: 0, right: 0, bottom: 0, left: 0 }, padding)
: { top: padding, right: padding, bottom: padding, left: padding };
const labelsSumSize = labels.reduce((sum, label) => {
const padding = paddingToObject(label.style.padding);
const labelSize = TextSize.approximateTextSize(label.text, {
angle: label.angle,
fontSize: label.style.fontSize,
letterSpacing: label.style.letterSpacing,
fontFamily: label.style.fontFamily,
});
return (
sum +
(isVertical
? labelSize.height + padding.top + padding.bottom
: labelSize.width + padding.right + padding.left)
);
}, 0);
const availiableLabelCount = Math.floor(
(size * gridAndTicks.length) / labelsSumSize,
);
const divider = Math.ceil(gridAndTicks.length / availiableLabelCount) || 1;
const getLabelCoord = (gridAndTick) =>
gridAndTick.props.children
.filter(isVictoryLabel)
.reduce(
(prev, child) => (isVertical ? child.props.y : child.props.x) || 0,
0,
);
const sorted = gridAndTicks.sort(
(a, b) =>
isVertical
? getLabelCoord(b) - getLabelCoord(a) // ordinary axis has top-bottom orientation
: getLabelCoord(a) - getLabelCoord(b), // ordinary axis has left-right orientation
);
return sorted.filter((gridAndTick, index) => index % divider === 0);
}
// Overridden in native versions
shouldAnimate() {
return !!this.props.animate;
}
render(): React.ReactElement {
const { animationWhitelist } = VictoryAxis;
const props = Axis.modifyProps(this.props, fallbackProps);
const userProps = UserProps.getSafeUserProps(this.props);
if (this.shouldAnimate()) {
return this.animateComponent(props, animationWhitelist);
}
const gridAndTicks = this.renderGridAndTicks(props);
const modifiedGridAndTicks = props.fixLabelOverlap
? this.fixLabelOverlap(gridAndTicks, props)
: gridAndTicks;
const children = [
this.renderLine(props),
this.renderLabel(props),
...modifiedGridAndTicks,
];
const container = React.cloneElement(props.containerComponent, userProps);
return props.standalone
? this.renderContainer(container, children)
: React.cloneElement(props.groupComponent, userProps, children);
}
}
export const VictoryAxis = addEvents(VictoryAxisBase, options);