This repository has been archived by the owner on Jun 2, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 77
/
Copy pathTrend.js
180 lines (158 loc) Β· 4.72 KB
/
Trend.js
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
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { omit } from '../../utils';
import {
buildSmoothPath,
buildLinearPath,
injectStyleTag,
} from '../../helpers/DOM.helpers';
import { normalize } from '../../helpers/math.helpers';
import { generateId } from '../../helpers/misc.helpers';
import { normalizeDataset, generateAutoDrawCss } from './Trend.helpers';
const propTypes = {
data: PropTypes.arrayOf(
PropTypes.oneOfType([
PropTypes.number,
PropTypes.shape({
value: PropTypes.number,
}),
]).isRequired
).isRequired,
smooth: PropTypes.bool,
autoDraw: PropTypes.bool,
autoDrawDuration: PropTypes.number,
autoDrawEasing: PropTypes.string,
width: PropTypes.number,
height: PropTypes.number,
padding: PropTypes.number,
radius: PropTypes.number,
gradient: PropTypes.arrayOf(PropTypes.string),
};
const defaultProps = {
radius: 10,
stroke: 'black',
padding: 8,
strokeWidth: 1,
autoDraw: false,
autoDrawDuration: 2000,
autoDrawEasing: 'ease',
};
class Trend extends Component {
constructor(props) {
super(props);
// Generate a random ID. This is important for distinguishing between
// Trend components on a page, so that they can have different keyframe
// animations.
this.trendId = generateId();
this.gradientId = `react-trend-vertical-gradient-${this.trendId}`;
}
componentDidMount() {
const { autoDraw, autoDrawDuration, autoDrawEasing } = this.props;
if (autoDraw) {
this.lineLength = this.path.getTotalLength();
const css = generateAutoDrawCss({
id: this.trendId,
lineLength: this.lineLength,
duration: autoDrawDuration,
easing: autoDrawEasing,
});
injectStyleTag(css);
}
}
getDelegatedProps() {
return omit(this.props, Object.keys(propTypes));
}
renderGradientDefinition() {
const { gradient } = this.props;
return (
<defs>
<linearGradient
id={this.gradientId}
x1="0%"
y1="0%"
x2="0%"
y2="100%"
>
{gradient.slice().reverse().map((c, index) => (
<stop
key={index}
offset={normalize({
value: index,
min: 0,
// If we only supply a single colour, it will try to normalize
// between 0 and 0, which will create NaN. By making the `max`
// at least 1, we ensure single-color "gradients" work.
max: gradient.length - 1 || 1,
})}
stopColor={c}
/>
))}
</linearGradient>
</defs>
);
}
render() {
const {
data,
smooth,
width,
height,
padding,
radius,
gradient,
} = this.props;
// We need at least 2 points to draw a graph.
if (!data || data.length < 2) {
return null;
}
// `data` can either be an array of numbers:
// [1, 2, 3]
// or, an array of objects containing a value:
// [ { value: 1 }, { value: 2 }, { value: 3 }]
//
// For now, we're just going to convert the second form to the first.
// Later on, if/when we support tooltips, we may adjust.
const plainValues = data.map(point => (
typeof point === 'number' ? point : point.value
));
// Our viewbox needs to be in absolute units, so we'll default to 300x75
// Our SVG can be a %, though; this is what makes it scalable.
// By defaulting to percentages, the SVG will grow to fill its parent
// container, preserving a 1/4 aspect ratio.
const viewBoxWidth = width || 300;
const viewBoxHeight = height || 75;
const svgWidth = width || '100%';
const svgHeight = height || '25%';
const normalizedValues = normalizeDataset(plainValues, {
minX: padding,
maxX: viewBoxWidth - padding,
// NOTE: Because SVGs are indexed from the top left, but most data is
// indexed from the bottom left, we're inverting the Y min/max.
minY: viewBoxHeight - padding,
maxY: padding,
});
const path = smooth
? buildSmoothPath(normalizedValues, { radius })
: buildLinearPath(normalizedValues);
return (
<svg
width={svgWidth}
height={svgHeight}
viewBox={`0 0 ${viewBoxWidth} ${viewBoxHeight}`}
{...this.getDelegatedProps()}
>
{gradient && this.renderGradientDefinition()}
<path
ref={(elem) => { this.path = elem; }}
id={`react-trend-${this.trendId}`}
d={path}
fill="none"
stroke={gradient ? `url(#${this.gradientId})` : undefined}
/>
</svg>
);
}
}
Trend.propTypes = propTypes;
Trend.defaultProps = defaultProps;
export default Trend;