Repository navigation
Expand file tree
/
Copy pathchart.js
More file actions
204 lines (196 loc) · 5.68 KB
/
Copy pathchart.js
File metadata and controls
204 lines (196 loc) · 5.68 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
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
import { Group } from '@vx/group';
import { AreaClosed, LinePath, Bar } from '@vx/shape';
import { withParentSize } from '@vx/responsive';
import { scaleTime, scaleLinear } from '@vx/scale';
import { LinearGradient } from '@vx/gradient';
import { PatternLines } from '@vx/pattern';
import { AxisBottom } from '@vx/axis';
import { withTooltip } from '@vx/tooltip';
import { localPoint } from '@vx/event';
import { bisector } from 'd3-array';
import Tooltips from './tooltips';
import HoverLine from './hoverline';
import MaxPrice from './maxprice';
import MinPrice from './minprice';
import formatPrice from '../utils/formatPrice';
import formatDate from '../utils/formatDate';
class Chart extends React.Component {
constructor(props) {
super(props);
}
render() {
const {
data,
parentWidth = 600,
parentHeight = 400,
margin = {},
tooltipData,
tooltipLeft,
tooltipTop,
showTooltip,
hideTooltip
} = this.props;
const width = parentWidth - margin.left - margin.right;
const height = parentHeight - margin.top - margin.bottom;
const x = d => new Date(d.time);
const y = d => d.price;
const bisectDate = bisector(d => x(d)).left;
const firstPoint = data[0];
const currentPoint = data[data.length - 1];
const minPrice = Math.min(...data.map(y));
const maxPrice = Math.max(...data.map(y));
const firstPrice = y(firstPoint);
const currentPrice = y(currentPoint);
const maxData = [
{ time: x(firstPoint), price: maxPrice },
{ time: x(currentPoint), price: maxPrice }
];
const minData = [
{ time: x(firstPoint), price: minPrice },
{ time: x(currentPoint), price: minPrice }
];
const xScale = scaleTime({
range: [0, width],
domain: [x(firstPoint), x(currentPoint)]
});
const yScale = scaleLinear({
range: [height, 0],
domain: [minPrice, maxPrice + 100]
});
return (
<div>
<svg
ref={s => (this.svg = s)}
width={parentWidth}
height={parentHeight}
>
<LinearGradient
id="fill"
from="#6086d6"
to="#6086d6"
fromOpacity={0.2}
toOpacity={0}
/>
<PatternLines
id="dLines"
height={6}
width={6}
stroke="#27273f"
strokeWidth={1}
orientation={['diagonal']}
/>
<Group top={margin.top} left={margin.left}>
<AxisBottom
data={data}
scale={xScale}
x={x}
top={height}
left={margin.left + 18}
numTicks={3}
hideTicks
hideAxisLine
tickLabelComponent={
<text
fill="#ffffff"
dy=".33em"
fillOpacity={0.3}
fontSize={11}
textAnchor="middle"
/>
}
/>
<MaxPrice
data={maxData}
yText={yScale(maxPrice)}
label={formatPrice(maxPrice)}
yScale={yScale}
xScale={xScale}
x={x}
y={y}
/>
<AreaClosed
stroke="transparent"
data={data}
yScale={yScale}
xScale={xScale}
x={x}
y={y}
fill="url(#fill)"
/>
<AreaClosed
stroke="transparent"
data={data}
yScale={yScale}
xScale={xScale}
x={x}
y={y}
fill="url(#dLines)"
/>
<LinePath
data={data}
yScale={yScale}
xScale={xScale}
y={y}
x={x}
stroke="#6086d6"
strokeOpacity="0.8"
strokeWidth={1}
/>
<MinPrice
data={minData}
yScale={yScale}
xScale={xScale}
y={y}
x={x}
yText={yScale(minPrice)}
label={formatPrice(minPrice)}
/>
<Bar
data={data}
width={width}
height={height - margin.bottom}
fill="transparent"
onMouseLeave={data => event => hideTooltip()}
onMouseMove={data => event => {
const { x: xPoint } = localPoint(this.svg, event);
const x0 = xScale.invert(xPoint);
const index = bisectDate(data, x0, 1);
const d0 = data[index - 1];
const d1 = data[index];
const d = x0 - xScale(x(d0)) > xScale(x(d1)) - x0 ? d1 : d0;
showTooltip({
tooltipData: d,
tooltipLeft: xScale(x(d)),
tooltipTop: yScale(y(d))
});
}}
/>
</Group>
{tooltipData &&
<HoverLine
from={{
x: tooltipLeft,
y: yScale(y(maxData[0]))
}}
to={{
x: tooltipLeft,
y: yScale(y(minData[0]))
}}
tooltipLeft={tooltipLeft}
tooltipTop={tooltipTop}
/>}
</svg>
{tooltipData &&
<Tooltips
yTop={tooltipTop - 12}
yLeft={tooltipLeft + 12}
yLabel={formatPrice(y(tooltipData))}
xTop={yScale(y(minData[0])) + 4}
xLeft={tooltipLeft}
xLabel={formatDate(x(tooltipData))}
/>}
</div>
);
}
}
export default withTooltip(Chart);