-
Notifications
You must be signed in to change notification settings - Fork 194
Expand file tree
/
Copy pathSparklinesLine.js
More file actions
70 lines (60 loc) · 1.78 KB
/
SparklinesLine.js
File metadata and controls
70 lines (60 loc) · 1.78 KB
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
import PropTypes from 'prop-types';
import React from 'react';
export default class SparklinesLine extends React.Component {
static propTypes = {
color: PropTypes.string,
style: PropTypes.object,
};
static defaultProps = {
style: {},
onMouseMove: () => {},
};
render() {
const { data, points, width, height, margin, color, style, onMouseMove } = this.props;
const fillInvert = style.fillInvert ? style.fillInvert : false
const linePoints = points.map(p => [p.x, p.y]).reduce((a, b) => a.concat(b));
const closePolyPoints = [
points[points.length - 1].x,
fillInvert ? margin : height - margin,
margin,
fillInvert ? margin : height - margin,
margin,
points[0].y,
];
const fillPoints = linePoints.concat(closePolyPoints);
const lineStyle = {
stroke: color || style.stroke || 'slategray',
strokeWidth: style.strokeWidth || '1',
strokeLinejoin: style.strokeLinejoin || 'round',
strokeLinecap: style.strokeLinecap || 'round',
fill: 'none',
};
const fillStyle = {
stroke: style.stroke || 'none',
strokeWidth: '0',
fillOpacity: style.fillOpacity || '.1',
fill: style.fill || color || 'slategray',
pointerEvents: 'auto',
};
const tooltips = points.map((p, i) => {
return (
<circle
key={i}
cx={p.x}
cy={p.y}
r={2}
style={fillStyle}
onMouseEnter={e => onMouseMove('enter', data[i], p)}
onClick={e => onMouseMove('click', data[i], p)}
/>
);
});
return (
<g>
{tooltips}
<polyline points={fillPoints.join(' ')} style={fillStyle} />
<polyline points={linePoints.join(' ')} style={lineStyle} />
</g>
);
}
}