This repository has been archived by the owner on Oct 20, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
/
spark-graph.jsx
175 lines (153 loc) · 4.74 KB
/
spark-graph.jsx
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
import React, { PropTypes } from 'react';
import classNames from 'classnames';
import { debounce } from 'lodash';
import arrayEqual from 'array-equal';
import bezierEasing from 'bezier-easing';
import {
pointInterpolator,
colorInterpolator,
getStrokeColor,
transformPoints,
} from './interpolate';
import omit from '../../utilities/omit';
function constructPathString(points) {
const [first, ...rest] = points;
return rest.reduce((prev, curr) => (
`${prev} L ${curr.x} ${curr.y}`
), `M ${first.x} ${first.y}`);
}
class SparkGraph extends React.PureComponent {
constructor(props) {
super(props);
const width = props.width || 0;
const height = props.height || 0;
this.state = {
width,
height,
shouldResize: !(typeof window === 'undefined') && (!props.width || !props.height),
pointsFrom: null,
pointsTo: null,
strokeFrom: null,
strokeTo: null,
};
this.handleResize = debounce(this.handleResize.bind(this), 150).bind(this);
}
componentDidMount() {
if (this.state.shouldResize) {
this.handleResize();
window.addEventListener('resize', this.handleResize);
}
}
componentWillReceiveProps({ width, height, points, stroke }) {
const state = {};
if (width) state.width = width;
if (height) state.height = height;
state.pointsFrom = this.props.points;
state.pointsTo = points;
state.strokeFrom = this.props.stroke;
state.strokeTo = stroke;
this.setState(state);
}
componentDidUpdate() {
const { strokeWidth, transitionDuration } = this.props;
const { height, width, pointsFrom, pointsTo, strokeFrom, strokeTo } = this.state;
const pInterpolator = pointInterpolator(pointsFrom, pointsTo, width, height, strokeWidth);
const cInterpolator = colorInterpolator(pointsFrom, pointsTo, strokeFrom, strokeTo);
if (pInterpolator || cInterpolator) {
const easing = bezierEasing(0.6, 0.04, 0.98, 0.335);
let startTime;
const draw = (time) => {
if (!startTime) startTime = time;
const progress = time - startTime < transitionDuration ? (time - startTime) / transitionDuration : 1;
if (pInterpolator) {
const values = pInterpolator.map(ip => ip(easing(progress)));
this.path.setAttribute('d', constructPathString(values));
}
if (cInterpolator) {
this.path.setAttribute('stroke', cInterpolator(easing(progress)));
}
if (progress < 1) {
requestAnimationFrame(draw);
}
};
requestAnimationFrame(draw);
}
}
componentWillUnmount() {
if (this.state.shouldResize) {
window.removeEventListener('resize', this.handleResize);
}
}
handleResize() {
const rect = this.svgNode.getBoundingClientRect();
if (rect.height !== this.state.height || rect.width !== this.state.width) {
this.setState({
width: rect.width,
height: rect.height,
pointsFrom: null, // Avoid animation on resize
strokeFrom: null, // Avoid animation on resize
});
}
}
render() {
const {
points,
stroke,
strokeWidth,
className,
style,
...rest
} = this.props;
const { width, height, pointsFrom, pointsTo } = this.state;
const styles = Object.assign({
width: !this.props.width ? '100%' : `${width}px`,
height: !this.props.height ? '100%' : `${height}px`,
}, style);
let pointsToRender;
if (!pointsFrom || arrayEqual(pointsFrom, pointsTo)) {
pointsToRender = transformPoints(points, width, height, strokeWidth);
}
return (
<svg
{...omit(rest, 'transitionDuration')}
className={classNames('spark-graph', className)}
style={styles}
viewBox={`0 0 ${width} ${height}`}
ref={(node) => {
this.svgNode = node;
}}
>
<path
style={{ fill: 'none' }}
d={pointsToRender ? constructPathString(pointsToRender) : ''}
stroke={pointsFrom ? getStrokeColor(pointsFrom, stroke) : getStrokeColor(points, stroke)}
strokeWidth={strokeWidth}
strokeLinecap="square"
strokeLinejoin="round"
ref={(node) => {
this.path = node;
}}
/>
</svg>
);
}
}
SparkGraph.propTypes = {
points: PropTypes.arrayOf(PropTypes.number).isRequired,
/** Stroke color */
stroke: PropTypes.string,
strokeWidth: PropTypes.number,
/** Unitless pixel value */
width: PropTypes.number,
/** Unitless pixel value */
height: PropTypes.number,
/** Transition duration in ms */
transitionDuration: PropTypes.number,
className: PropTypes.string,
style: PropTypes.object,
};
SparkGraph.defaultProps = {
strokeWidth: 1,
transitionDuration: 500,
};
export default SparkGraph;