victory-chart 18.2.0 → 19.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +28 -0
- package/README.md +9 -5
- package/dist/victory-chart.js +17058 -24378
- package/dist/victory-chart.min.js +13 -14
- package/lib/components/containers/brush-helpers.js +7 -4
- package/lib/components/containers/create-container.js +40 -32
- package/lib/components/containers/cursor-helpers.js +48 -0
- package/lib/components/containers/victory-brush-container.js +7 -3
- package/lib/components/containers/victory-cursor-container.js +145 -0
- package/lib/components/containers/victory-selection-container.js +5 -2
- package/lib/components/containers/victory-voronoi-container.js +40 -15
- package/lib/components/containers/victory-zoom-container.js +9 -2
- package/lib/components/containers/zoom-helpers.js +7 -2
- package/lib/components/victory-area/helper-methods.js +2 -1
- package/lib/components/victory-area/victory-area.js +9 -105
- package/lib/components/victory-axis/helper-methods.js +5 -2
- package/lib/components/victory-axis/victory-axis.js +11 -53
- package/lib/components/victory-bar/helper-methods.js +3 -1
- package/lib/components/victory-bar/victory-bar.js +8 -114
- package/lib/components/victory-candlestick/helper-methods.js +2 -1
- package/lib/components/victory-candlestick/victory-candlestick.js +10 -111
- package/lib/components/victory-chart/helper-methods.js +4 -3
- package/lib/components/victory-chart/victory-chart.js +12 -67
- package/lib/components/victory-errorbar/helper-methods.js +1 -1
- package/lib/components/victory-errorbar/victory-errorbar.js +8 -113
- package/lib/components/victory-group/victory-group.js +12 -86
- package/lib/components/victory-line/helper-methods.js +2 -1
- package/lib/components/victory-line/victory-line.js +9 -105
- package/lib/components/victory-scatter/helper-methods.js +1 -1
- package/lib/components/victory-scatter/victory-scatter.js +8 -111
- package/lib/components/victory-stack/victory-stack.js +11 -59
- package/lib/components/victory-voronoi/helper-methods.js +1 -1
- package/lib/components/victory-voronoi/victory-voronoi.js +7 -112
- package/lib/helpers/common-props.js +87 -0
- package/lib/helpers/wrapper.js +2 -1
- package/lib/index.js +9 -7
- package/package.json +14 -14
- package/src/components/containers/brush-helpers.js +37 -34
- package/src/components/containers/create-container.js +52 -43
- package/src/components/containers/cursor-helpers.js +49 -0
- package/src/components/containers/selection-helpers.js +13 -13
- package/src/components/containers/victory-brush-container.js +33 -29
- package/src/components/containers/victory-cursor-container.js +145 -0
- package/src/components/containers/victory-selection-container.js +14 -11
- package/src/components/containers/victory-voronoi-container.js +50 -25
- package/src/components/containers/victory-zoom-container.js +17 -10
- package/src/components/containers/voronoi-helpers.js +9 -9
- package/src/components/containers/zoom-helpers.js +18 -13
- package/src/components/victory-area/helper-methods.js +7 -6
- package/src/components/victory-area/victory-area.js +14 -110
- package/src/components/victory-axis/helper-methods.js +22 -19
- package/src/components/victory-axis/victory-axis.js +15 -57
- package/src/components/victory-bar/helper-methods.js +7 -5
- package/src/components/victory-bar/victory-bar.js +14 -120
- package/src/components/victory-candlestick/helper-methods.js +10 -9
- package/src/components/victory-candlestick/victory-candlestick.js +20 -121
- package/src/components/victory-chart/helper-methods.js +7 -6
- package/src/components/victory-chart/victory-chart.js +24 -79
- package/src/components/victory-errorbar/helper-methods.js +7 -7
- package/src/components/victory-errorbar/victory-errorbar.js +15 -120
- package/src/components/victory-group/victory-group.js +26 -100
- package/src/components/victory-line/helper-methods.js +5 -4
- package/src/components/victory-line/victory-line.js +16 -112
- package/src/components/victory-scatter/helper-methods.js +8 -8
- package/src/components/victory-scatter/victory-scatter.js +11 -114
- package/src/components/victory-stack/victory-stack.js +21 -69
- package/src/components/victory-voronoi/helper-methods.js +3 -3
- package/src/components/victory-voronoi/victory-voronoi.js +10 -115
- package/src/helpers/axis.js +6 -6
- package/src/helpers/common-props.js +89 -0
- package/src/helpers/wrapper.js +10 -9
- package/src/index.js +7 -5
- package/lib/components/victory-voronoi-tooltip/helper-methods.js +0 -100
- package/lib/components/victory-voronoi-tooltip/victory-voronoi-tooltip.js +0 -180
- package/lib/components/victory-zoom/helper-methods.js +0 -48
- package/lib/components/victory-zoom/victory-zoom.js +0 -221
- package/src/components/victory-voronoi-tooltip/helper-methods.js +0 -101
- package/src/components/victory-voronoi-tooltip/victory-voronoi-tooltip.js +0 -180
- package/src/components/victory-zoom/helper-methods.js +0 -49
- package/src/components/victory-zoom/victory-zoom.js +0 -221
|
@@ -1,101 +0,0 @@
|
|
|
1
|
-
import { assign, omit, defaults, without } from "lodash";
|
|
2
|
-
import { Helpers, Data, Domain, Scale } from "victory-core";
|
|
3
|
-
import { voronoi as d3Voronoi } from "d3-voronoi";
|
|
4
|
-
|
|
5
|
-
export default {
|
|
6
|
-
getBaseProps(props, fallbackProps) {
|
|
7
|
-
props = Helpers.modifyProps(props, fallbackProps, "voronoi");
|
|
8
|
-
const { data, style, scale, polygons, domain } = this.getCalculatedValues(props);
|
|
9
|
-
const { width, height, standalone, theme, events, sharedEvents } = props;
|
|
10
|
-
const initialChildProps = { parent: {
|
|
11
|
-
style: style.parent, scale, domain, data, standalone, height, width, theme
|
|
12
|
-
}};
|
|
13
|
-
|
|
14
|
-
return data.reduce((childProps, datum, index) => {
|
|
15
|
-
const polygon = without(polygons[index], "data");
|
|
16
|
-
const eventKey = datum.eventKey;
|
|
17
|
-
const x = scale.x(datum._x1 !== undefined ? datum._x1 : datum._x);
|
|
18
|
-
const y = scale.y(datum._y1 !== undefined ? datum._y1 : datum._y);
|
|
19
|
-
const dataProps = {
|
|
20
|
-
x, y, datum, data, index, scale, polygon,
|
|
21
|
-
size: props.size,
|
|
22
|
-
style: this.getDataStyles(datum, style.data)
|
|
23
|
-
};
|
|
24
|
-
|
|
25
|
-
childProps[eventKey] = { data: dataProps };
|
|
26
|
-
const text = this.getLabelText(props, datum, index);
|
|
27
|
-
if (text !== undefined && text !== null || events || sharedEvents) {
|
|
28
|
-
childProps[eventKey].labels = assign(
|
|
29
|
-
{},
|
|
30
|
-
this.getFlyoutProps(dataProps, text, style),
|
|
31
|
-
);
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
return childProps;
|
|
35
|
-
}, initialChildProps);
|
|
36
|
-
},
|
|
37
|
-
|
|
38
|
-
getFlyoutProps(dataProps, text, style) {
|
|
39
|
-
const { x, y, index, scale, datum, data } = dataProps;
|
|
40
|
-
return {
|
|
41
|
-
x, y, text, index, scale, datum, data,
|
|
42
|
-
flyoutStyle: style.flyout,
|
|
43
|
-
style: style.labels
|
|
44
|
-
};
|
|
45
|
-
},
|
|
46
|
-
|
|
47
|
-
getStyles(style, styleObject) {
|
|
48
|
-
style = style || {};
|
|
49
|
-
styleObject = styleObject || {};
|
|
50
|
-
const parentStyleProps = { height: "auto", width: "100%" };
|
|
51
|
-
return {
|
|
52
|
-
parent: defaults(parentStyleProps, style.parent, styleObject.parent),
|
|
53
|
-
data: defaults({}, style.data, styleObject.data),
|
|
54
|
-
labels: defaults({}, style.labels, styleObject.labels),
|
|
55
|
-
flyout: defaults({}, style.flyout, styleObject.flyout)
|
|
56
|
-
};
|
|
57
|
-
},
|
|
58
|
-
|
|
59
|
-
getCalculatedValues(props) {
|
|
60
|
-
const defaultStyles = props.theme && props.theme.voronoi && props.theme.voronoi.style ?
|
|
61
|
-
props.theme.voronoi.style : {};
|
|
62
|
-
const style = this.getStyles(props.style, defaultStyles, "auto", "100%");
|
|
63
|
-
const data = Data.getData(props);
|
|
64
|
-
const range = {
|
|
65
|
-
x: Helpers.getRange(props, "x"),
|
|
66
|
-
y: Helpers.getRange(props, "y")
|
|
67
|
-
};
|
|
68
|
-
const domain = {
|
|
69
|
-
x: Domain.getDomain(props, "x"),
|
|
70
|
-
y: Domain.getDomain(props, "y")
|
|
71
|
-
};
|
|
72
|
-
const scale = {
|
|
73
|
-
x: Scale.getBaseScale(props, "x").domain(domain.x).range(range.x),
|
|
74
|
-
y: Scale.getBaseScale(props, "y").domain(domain.y).range(range.y)
|
|
75
|
-
};
|
|
76
|
-
const polygons = props.polygons || this.getPolygons(range, scale, data);
|
|
77
|
-
return {domain, data, scale, style, polygons};
|
|
78
|
-
},
|
|
79
|
-
|
|
80
|
-
getPolygons(range, scale, data) {
|
|
81
|
-
const minRange = [Math.min(...range.x), Math.min(...range.y)];
|
|
82
|
-
const maxRange = [Math.max(...range.x), Math.max(...range.y)];
|
|
83
|
-
const voronoi = d3Voronoi()
|
|
84
|
-
.x((d) => scale.x(d._x1 !== undefined ? d._x1 : d._x))
|
|
85
|
-
.y((d) => scale.y(d._y1 !== undefined ? d._y1 : d._y))
|
|
86
|
-
.extent([minRange, maxRange]);
|
|
87
|
-
return voronoi.polygons(data);
|
|
88
|
-
},
|
|
89
|
-
|
|
90
|
-
getDataStyles(datum, style) {
|
|
91
|
-
const stylesFromData = omit(datum, [
|
|
92
|
-
"_x", "_y", "name", "label"
|
|
93
|
-
]);
|
|
94
|
-
return defaults({}, stylesFromData, style);
|
|
95
|
-
},
|
|
96
|
-
|
|
97
|
-
getLabelText(props, datum, index) {
|
|
98
|
-
return datum.label || (Array.isArray(props.labels) ?
|
|
99
|
-
props.labels[index] : props.labels);
|
|
100
|
-
}
|
|
101
|
-
};
|
|
@@ -1,180 +0,0 @@
|
|
|
1
|
-
import React, { PropTypes } from "react";
|
|
2
|
-
import { partialRight } from "lodash";
|
|
3
|
-
import {
|
|
4
|
-
PropTypes as CustomPropTypes, Helpers, VictoryTransition, VictoryTooltip, addEvents,
|
|
5
|
-
VictoryContainer, VictoryTheme, DefaultTransitions, Voronoi, Data, Domain, Log
|
|
6
|
-
} from "victory-core";
|
|
7
|
-
import TooltipHelpers from "./helper-methods";
|
|
8
|
-
|
|
9
|
-
const fallbackProps = {
|
|
10
|
-
width: 450,
|
|
11
|
-
height: 300,
|
|
12
|
-
padding: 50
|
|
13
|
-
};
|
|
14
|
-
|
|
15
|
-
const animationWhitelist = [
|
|
16
|
-
"data", "domain", "height", "padding", "samples", "size", "style", "width"
|
|
17
|
-
];
|
|
18
|
-
|
|
19
|
-
class VictoryVoronoiTooltip extends React.Component {
|
|
20
|
-
static displayName = "VictoryVoronoiTooltip";
|
|
21
|
-
static role = "tooltip";
|
|
22
|
-
static defaultTransitions = DefaultTransitions.discreteTransitions();
|
|
23
|
-
|
|
24
|
-
static propTypes = {
|
|
25
|
-
animate: PropTypes.object,
|
|
26
|
-
categories: PropTypes.oneOfType([
|
|
27
|
-
PropTypes.arrayOf(PropTypes.string),
|
|
28
|
-
PropTypes.shape({
|
|
29
|
-
x: PropTypes.arrayOf(PropTypes.string), y: PropTypes.arrayOf(PropTypes.string)
|
|
30
|
-
})
|
|
31
|
-
]),
|
|
32
|
-
containerComponent: PropTypes.element,
|
|
33
|
-
data: PropTypes.array,
|
|
34
|
-
domainPadding: PropTypes.oneOfType([
|
|
35
|
-
PropTypes.shape({
|
|
36
|
-
x: PropTypes.oneOfType([ PropTypes.number, CustomPropTypes.domain ]),
|
|
37
|
-
y: PropTypes.oneOfType([ PropTypes.number, CustomPropTypes.domain ])
|
|
38
|
-
}),
|
|
39
|
-
PropTypes.number
|
|
40
|
-
]),
|
|
41
|
-
dataComponent: PropTypes.element,
|
|
42
|
-
domain: PropTypes.oneOfType([
|
|
43
|
-
CustomPropTypes.domain,
|
|
44
|
-
PropTypes.shape({ x: CustomPropTypes.domain, y: CustomPropTypes.domain })
|
|
45
|
-
]),
|
|
46
|
-
events: PropTypes.arrayOf(PropTypes.shape({
|
|
47
|
-
target: PropTypes.oneOf(["data", "labels", "parent"]),
|
|
48
|
-
eventKey: PropTypes.oneOfType([
|
|
49
|
-
PropTypes.array,
|
|
50
|
-
CustomPropTypes.allOfType([CustomPropTypes.integer, CustomPropTypes.nonNegative]),
|
|
51
|
-
PropTypes.string
|
|
52
|
-
]),
|
|
53
|
-
eventHandlers: PropTypes.object
|
|
54
|
-
})),
|
|
55
|
-
eventKey: PropTypes.oneOfType([
|
|
56
|
-
PropTypes.func,
|
|
57
|
-
CustomPropTypes.allOfType([CustomPropTypes.integer, CustomPropTypes.nonNegative]),
|
|
58
|
-
PropTypes.string
|
|
59
|
-
]),
|
|
60
|
-
groupComponent: PropTypes.element,
|
|
61
|
-
height: CustomPropTypes.nonNegative,
|
|
62
|
-
labels: PropTypes.oneOfType([ PropTypes.func, PropTypes.array ]),
|
|
63
|
-
labelComponent: PropTypes.element,
|
|
64
|
-
name: PropTypes.string,
|
|
65
|
-
padding: PropTypes.oneOfType([
|
|
66
|
-
PropTypes.number,
|
|
67
|
-
PropTypes.shape({
|
|
68
|
-
top: PropTypes.number, bottom: PropTypes.number,
|
|
69
|
-
left: PropTypes.number, right: PropTypes.number
|
|
70
|
-
})
|
|
71
|
-
]),
|
|
72
|
-
samples: CustomPropTypes.nonNegative,
|
|
73
|
-
scale: PropTypes.oneOfType([
|
|
74
|
-
CustomPropTypes.scale,
|
|
75
|
-
PropTypes.shape({ x: CustomPropTypes.scale, y: CustomPropTypes.scale })
|
|
76
|
-
]),
|
|
77
|
-
sharedEvents: PropTypes.shape({
|
|
78
|
-
events: PropTypes.array,
|
|
79
|
-
getEventState: PropTypes.func
|
|
80
|
-
}),
|
|
81
|
-
sortKey: PropTypes.oneOfType([
|
|
82
|
-
PropTypes.func,
|
|
83
|
-
CustomPropTypes.allOfType([CustomPropTypes.integer, CustomPropTypes.nonNegative]),
|
|
84
|
-
PropTypes.string,
|
|
85
|
-
PropTypes.arrayOf(PropTypes.string)
|
|
86
|
-
]),
|
|
87
|
-
size: CustomPropTypes.nonNegative,
|
|
88
|
-
standalone: PropTypes.bool,
|
|
89
|
-
style: PropTypes.shape({
|
|
90
|
-
parent: PropTypes.object,
|
|
91
|
-
data: PropTypes.object,
|
|
92
|
-
labels: PropTypes.object,
|
|
93
|
-
flyout: PropTypes.object
|
|
94
|
-
}),
|
|
95
|
-
theme: PropTypes.object,
|
|
96
|
-
width: CustomPropTypes.nonNegative,
|
|
97
|
-
x: PropTypes.oneOfType([
|
|
98
|
-
PropTypes.func,
|
|
99
|
-
CustomPropTypes.allOfType([CustomPropTypes.integer, CustomPropTypes.nonNegative]),
|
|
100
|
-
PropTypes.string,
|
|
101
|
-
PropTypes.arrayOf(PropTypes.string)
|
|
102
|
-
]),
|
|
103
|
-
y: PropTypes.oneOfType([
|
|
104
|
-
PropTypes.func,
|
|
105
|
-
CustomPropTypes.allOfType([CustomPropTypes.integer, CustomPropTypes.nonNegative]),
|
|
106
|
-
PropTypes.string,
|
|
107
|
-
PropTypes.arrayOf(PropTypes.string)
|
|
108
|
-
])
|
|
109
|
-
};
|
|
110
|
-
|
|
111
|
-
static defaultProps = {
|
|
112
|
-
samples: 50,
|
|
113
|
-
scale: "linear",
|
|
114
|
-
standalone: true,
|
|
115
|
-
dataComponent: <Voronoi/>,
|
|
116
|
-
labelComponent: <VictoryTooltip/>,
|
|
117
|
-
containerComponent: <VictoryContainer/>,
|
|
118
|
-
groupComponent: <g role="presentation"/>,
|
|
119
|
-
theme: VictoryTheme.grayscale
|
|
120
|
-
};
|
|
121
|
-
|
|
122
|
-
static getDomain = Domain.getDomain.bind(Domain);
|
|
123
|
-
static getData = Data.getData.bind(Data);
|
|
124
|
-
static getBaseProps = partialRight(
|
|
125
|
-
TooltipHelpers.getBaseProps.bind(TooltipHelpers), fallbackProps);
|
|
126
|
-
static expectedComponents = [
|
|
127
|
-
"dataComponent", "labelComponent", "groupComponent", "containerComponent"
|
|
128
|
-
];
|
|
129
|
-
|
|
130
|
-
componentDidMount() {
|
|
131
|
-
Log.warn(
|
|
132
|
-
"`VictoryVoronoiTooltip` will be deprecated in victory@0.20.0, use `VictoryVoronoiContainer`"
|
|
133
|
-
);
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
renderData(props) {
|
|
137
|
-
const { dataComponent, labelComponent, groupComponent } = props;
|
|
138
|
-
|
|
139
|
-
const dataComponents = this.dataKeys.map((_dataKey, index) => {
|
|
140
|
-
const dataProps = this.getComponentProps(dataComponent, "data", index);
|
|
141
|
-
return React.cloneElement(dataComponent, dataProps);
|
|
142
|
-
});
|
|
143
|
-
|
|
144
|
-
const labelComponents = this.dataKeys.map((_dataKey, index) => {
|
|
145
|
-
const labelProps = this.getComponentProps(labelComponent, "labels", index);
|
|
146
|
-
if (labelProps.text !== undefined && labelProps.text !== null) {
|
|
147
|
-
return React.cloneElement(labelComponent, labelProps);
|
|
148
|
-
}
|
|
149
|
-
}).filter(Boolean);
|
|
150
|
-
|
|
151
|
-
return labelComponents.length > 0 ?
|
|
152
|
-
React.cloneElement(groupComponent, {}, ...dataComponents, ...labelComponents) :
|
|
153
|
-
dataComponents;
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
shouldAnimate() {
|
|
157
|
-
return !!this.props.animate;
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
renderContainer(props, children) {
|
|
161
|
-
const {containerComponent} = props;
|
|
162
|
-
const parentProps = this.getComponentProps(containerComponent, "parent", "parent");
|
|
163
|
-
return React.cloneElement(containerComponent, parentProps, children);
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
render() {
|
|
167
|
-
const {role} = this.constructor;
|
|
168
|
-
const props = Helpers.modifyProps(this.props, fallbackProps, role);
|
|
169
|
-
if (this.shouldAnimate()) {
|
|
170
|
-
return (
|
|
171
|
-
<VictoryTransition animate={props.animate} animationWhitelist={animationWhitelist}>
|
|
172
|
-
{React.createElement(this.constructor, props)}
|
|
173
|
-
</VictoryTransition>
|
|
174
|
-
);
|
|
175
|
-
}
|
|
176
|
-
const children = this.renderData(props);
|
|
177
|
-
return this.renderContainer(props, children);
|
|
178
|
-
}
|
|
179
|
-
}
|
|
180
|
-
export default addEvents(VictoryVoronoiTooltip);
|
|
@@ -1,49 +0,0 @@
|
|
|
1
|
-
import { Collection } from "victory-core";
|
|
2
|
-
export default {
|
|
3
|
-
/**
|
|
4
|
-
* Generates a new domain scaled by factor and constrained by the original domain.
|
|
5
|
-
* @param {[Number, Number]} currentDomain The domain to be scaled.
|
|
6
|
-
* @param {[Number, Number]} originalDomain The original domain for the data set.
|
|
7
|
-
* @param {Number} factor The delta to translate by
|
|
8
|
-
* @return {[Number, Number]} The scale domain
|
|
9
|
-
*/
|
|
10
|
-
scale: (currentDomain, originalDomain, factor) => {
|
|
11
|
-
const [fromBound, toBound] = originalDomain;
|
|
12
|
-
const [from, to] = currentDomain;
|
|
13
|
-
const range = Math.abs(from - to);
|
|
14
|
-
const midpoint = +from + (range / 2);
|
|
15
|
-
const newRange = (range * factor) / 2;
|
|
16
|
-
return [
|
|
17
|
-
Collection.getMaxValue([midpoint - newRange, fromBound]),
|
|
18
|
-
Collection.getMinValue([midpoint + newRange, toBound])
|
|
19
|
-
];
|
|
20
|
-
},
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* Generate a new domain translated by the delta and constrained by the original domain.
|
|
24
|
-
* @param {[Number, Number]} currentDomain The domain to be translated.
|
|
25
|
-
* @param {[Number, Number]} originalDomain The original domain for the data set.
|
|
26
|
-
* @param {Number} delta The delta to translate by
|
|
27
|
-
* @return {[Number, Number]} The translated domain
|
|
28
|
-
*/
|
|
29
|
-
pan: (currentDomain, originalDomain, delta) => {
|
|
30
|
-
const [fromCurrent, toCurrent] = currentDomain.map((val) => +val);
|
|
31
|
-
const [fromOriginal, toOriginal] = originalDomain.map((val) => +val);
|
|
32
|
-
const lowerBound = fromCurrent + delta;
|
|
33
|
-
const upperBound = toCurrent + delta;
|
|
34
|
-
let newDomain;
|
|
35
|
-
if (lowerBound > fromOriginal && upperBound < toOriginal) {
|
|
36
|
-
newDomain = [lowerBound, upperBound];
|
|
37
|
-
} else if (lowerBound < fromOriginal) { // Clamp to lower limit
|
|
38
|
-
const dx = toCurrent - fromCurrent;
|
|
39
|
-
newDomain = [fromOriginal, fromOriginal + dx];
|
|
40
|
-
} else if (upperBound > toOriginal) { // Clamp to upper limit
|
|
41
|
-
const dx = toCurrent - fromCurrent;
|
|
42
|
-
newDomain = [toOriginal - dx, toOriginal];
|
|
43
|
-
} else {
|
|
44
|
-
newDomain = currentDomain;
|
|
45
|
-
}
|
|
46
|
-
return Collection.containsDates(currentDomain) || Collection.containsDates(originalDomain) ?
|
|
47
|
-
newDomain.map((val) => new Date(val)) : newDomain;
|
|
48
|
-
}
|
|
49
|
-
};
|
|
@@ -1,221 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
This component is being temporarily re-added to suppprt an upgrade to `victory-native`
|
|
3
|
-
*/
|
|
4
|
-
import React, {Component, PropTypes} from "react";
|
|
5
|
-
import { assign, isEqual } from "lodash";
|
|
6
|
-
import ChartHelpers from "../victory-chart/helper-methods";
|
|
7
|
-
import ZoomHelpers from "./helper-methods";
|
|
8
|
-
import {
|
|
9
|
-
VictoryClipContainer, Helpers, PropTypes as CustomPropTypes, Timer
|
|
10
|
-
} from "victory-core";
|
|
11
|
-
|
|
12
|
-
const fallbackProps = {
|
|
13
|
-
width: 450,
|
|
14
|
-
height: 300,
|
|
15
|
-
padding: 50
|
|
16
|
-
};
|
|
17
|
-
|
|
18
|
-
class VictoryZoom extends Component {
|
|
19
|
-
static displayName = "VictoryZoom";
|
|
20
|
-
static role = "zoom";
|
|
21
|
-
|
|
22
|
-
static propTypes = {
|
|
23
|
-
children: PropTypes.node,
|
|
24
|
-
zoomDomain: PropTypes.shape({
|
|
25
|
-
x: CustomPropTypes.domain,
|
|
26
|
-
y: CustomPropTypes.domain
|
|
27
|
-
}),
|
|
28
|
-
onDomainChange: PropTypes.func,
|
|
29
|
-
clipContainerComponent: PropTypes.element.isRequired,
|
|
30
|
-
allowZoom: PropTypes.bool
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
static childContextTypes = {
|
|
34
|
-
getTimer: React.PropTypes.func
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
static defaultProps = {
|
|
38
|
-
clipContainerComponent: <VictoryClipContainer/>,
|
|
39
|
-
allowZoom: true
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
constructor(props) {
|
|
43
|
-
super(props);
|
|
44
|
-
|
|
45
|
-
const chart = React.Children.only(this.props.children);
|
|
46
|
-
const [rangex1, rangex0] = Helpers.getRange(
|
|
47
|
-
Helpers.modifyProps(chart.props, {}, "chart"), // TODO: Don't presume chart role
|
|
48
|
-
"x"
|
|
49
|
-
);
|
|
50
|
-
|
|
51
|
-
this.plottableWidth = rangex0 - rangex1;
|
|
52
|
-
this.width = chart.props.width || fallbackProps.width;
|
|
53
|
-
this.state = { domain: props.zoomDomain || this.getDataDomain() };
|
|
54
|
-
|
|
55
|
-
this.events = this.getEvents(props.allowZoom);
|
|
56
|
-
this.clipDataComponents = this.clipDataComponents.bind(this);
|
|
57
|
-
this.getTimer = this.getTimer.bind(this);
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
getChildContext() {
|
|
61
|
-
return {
|
|
62
|
-
getTimer: this.getTimer
|
|
63
|
-
};
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
getTimer() {
|
|
67
|
-
if (!this.timer) {
|
|
68
|
-
this.timer = new Timer();
|
|
69
|
-
}
|
|
70
|
-
return this.timer;
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
componentWillMount() {
|
|
74
|
-
this.getChartRef = (chart) => { this.chartRef = chart; };
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
componentWillUnmount() {
|
|
78
|
-
this.getTimer().stop();
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
componentWillReceiveProps({allowZoom: nextAllowZoom, zoomDomain: nextDomain}) {
|
|
82
|
-
const {allowZoom, zoomDomain} = this.props;
|
|
83
|
-
if (!isEqual(zoomDomain, nextDomain)) {
|
|
84
|
-
this.setState({domain: nextDomain});
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
if (allowZoom !== nextAllowZoom) {
|
|
88
|
-
this.events = this.getEvents(nextAllowZoom);
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
getDataDomain() {
|
|
93
|
-
const chart = React.Children.only(this.props.children);
|
|
94
|
-
const chartChildren = React.Children.toArray(chart);
|
|
95
|
-
|
|
96
|
-
return {
|
|
97
|
-
x: ChartHelpers.getDomain(chart.props, "x", chartChildren)
|
|
98
|
-
};
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
getEvents(allowZoom) {
|
|
102
|
-
const standardEvents = {
|
|
103
|
-
onMouseDown: (evt) => {
|
|
104
|
-
this.targetBounds = this.chartRef.getSvgBounds();
|
|
105
|
-
const x = evt.clientX - this.targetBounds.left;
|
|
106
|
-
this.isPanning = true;
|
|
107
|
-
this.startX = x;
|
|
108
|
-
this.lastDomain = this.state.domain;
|
|
109
|
-
},
|
|
110
|
-
onMouseUp: () => { this.isPanning = false; },
|
|
111
|
-
onMouseLeave: () => { this.isPanning = false; },
|
|
112
|
-
onMouseMove: (evt) => {
|
|
113
|
-
const clientX = evt.clientX;
|
|
114
|
-
if (this.isPanning) {
|
|
115
|
-
requestAnimationFrame(() => { // eslint-disable-line no-undef
|
|
116
|
-
const domain = this.getDataDomain();
|
|
117
|
-
const delta = this.startX - (clientX - this.targetBounds.left);
|
|
118
|
-
const calculatedDx = delta / this.getDomainScale();
|
|
119
|
-
const nextXDomain = ZoomHelpers.pan(this.lastDomain.x, domain.x, calculatedDx);
|
|
120
|
-
this.setDomain({x: nextXDomain});
|
|
121
|
-
this.setState({domain: {x: nextXDomain}});
|
|
122
|
-
});
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
};
|
|
126
|
-
const wheelEvent = {
|
|
127
|
-
onWheel: (evt) => {
|
|
128
|
-
evt.preventDefault();
|
|
129
|
-
const deltaY = evt.deltaY;
|
|
130
|
-
requestAnimationFrame(() => { // eslint-disable-line no-undef
|
|
131
|
-
const {x} = this.state.domain;
|
|
132
|
-
const xBounds = this.getDataDomain().x;
|
|
133
|
-
|
|
134
|
-
// TODO: Check scale factor
|
|
135
|
-
const nextXDomain = ZoomHelpers.scale(x, xBounds, 1 + (deltaY / 300));
|
|
136
|
-
this.setDomain({x: nextXDomain});
|
|
137
|
-
});
|
|
138
|
-
}
|
|
139
|
-
};
|
|
140
|
-
|
|
141
|
-
return [{
|
|
142
|
-
target: "parent",
|
|
143
|
-
eventHandlers: allowZoom ? assign({}, standardEvents, wheelEvent) : standardEvents
|
|
144
|
-
}];
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
setDomain(domain) {
|
|
148
|
-
const {onDomainChange} = this.props;
|
|
149
|
-
this.getTimer().bypassAnimation();
|
|
150
|
-
this.setState({domain}, () => this.getTimer().resumeAnimation());
|
|
151
|
-
if (onDomainChange) { onDomainChange(domain); }
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
getDomainScale() {
|
|
155
|
-
const {x: [from, to]} = this.lastDomain;
|
|
156
|
-
const ratio = this.targetBounds.width / this.width;
|
|
157
|
-
const absoluteAxisWidth = ratio * this.plottableWidth;
|
|
158
|
-
return absoluteAxisWidth / (to - from);
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
clipDataComponents(children, props) { //eslint-disable-line max-statements
|
|
162
|
-
const [rangex0, rangex1] = Helpers.getRange(props, "x");
|
|
163
|
-
const childComponents = [];
|
|
164
|
-
let group = [];
|
|
165
|
-
let groupNumber = 0;
|
|
166
|
-
|
|
167
|
-
const makeGroup = (arr, index) => {
|
|
168
|
-
return React.cloneElement(this.props.clipContainerComponent, {
|
|
169
|
-
key: `ZoomClipContainer-${index}`,
|
|
170
|
-
clipWidth: rangex1 - rangex0,
|
|
171
|
-
clipHeight: fallbackProps.height,
|
|
172
|
-
translateX: rangex0,
|
|
173
|
-
children: arr
|
|
174
|
-
});
|
|
175
|
-
};
|
|
176
|
-
|
|
177
|
-
const findNextAxis = (start) => {
|
|
178
|
-
const subset = children.slice(start);
|
|
179
|
-
return subset.findIndex((child) => child.type.displayName === "VictoryAxis") + start;
|
|
180
|
-
};
|
|
181
|
-
|
|
182
|
-
let axisIndex = findNextAxis(0);
|
|
183
|
-
|
|
184
|
-
if (axisIndex === -1) {
|
|
185
|
-
return makeGroup(children, groupNumber);
|
|
186
|
-
}
|
|
187
|
-
for (let i = 0, len = children.length; i < len; i++) {
|
|
188
|
-
if (i === axisIndex) {
|
|
189
|
-
childComponents.push(makeGroup(group, groupNumber), children[i]);
|
|
190
|
-
axisIndex = findNextAxis(i + 1);
|
|
191
|
-
group = [];
|
|
192
|
-
groupNumber++;
|
|
193
|
-
}
|
|
194
|
-
group.push(children[i]);
|
|
195
|
-
}
|
|
196
|
-
childComponents.push(makeGroup(group, groupNumber));
|
|
197
|
-
return childComponents;
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
renderChart(chartElement, props) {
|
|
201
|
-
return React.cloneElement(chartElement, props);
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
render() {
|
|
205
|
-
const chart = React.Children.only(this.props.children);
|
|
206
|
-
const events = chart.props.events
|
|
207
|
-
? (this.events || []).concat(chart.props.events)
|
|
208
|
-
: this.events;
|
|
209
|
-
|
|
210
|
-
const nextProps = assign({}, chart.props, {
|
|
211
|
-
events,
|
|
212
|
-
domain: this.state.domain,
|
|
213
|
-
ref: this.getChartRef,
|
|
214
|
-
modifyChildren: this.clipDataComponents
|
|
215
|
-
});
|
|
216
|
-
|
|
217
|
-
return this.renderChart(chart, nextProps);
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
}
|
|
221
|
-
export default VictoryZoom;
|