-
Notifications
You must be signed in to change notification settings - Fork 222
/
BasicLineSeries.tsx
95 lines (82 loc) · 2.51 KB
/
BasicLineSeries.tsx
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
import { scaleTime } from "d3-scale";
import * as React from "react";
import {
CandlestickSeries,
Chart,
ChartCanvas,
CrossHairCursor,
timeFormat,
withDeviceRatio,
withSize,
XAxis,
YAxis,
} from "react-financial-charts";
import { IOHLCData, withUpdatingData, withOHLCData } from "../../data";
interface ChartProps {
readonly data: IOHLCData[];
readonly height: number;
readonly width: number;
readonly ratio: number;
}
class BasicLineSeries extends React.Component<ChartProps> {
private readonly margin = { left: 0, right: 56, top: 0, bottom: 24 };
private readonly padding = { left: 0, right: 32, top: 0, bottom: 0 };
public render() {
const { data, height, ratio, width } = this.props;
const xScale = scaleTime();
const xAccessor = (d: IOHLCData) => d.date;
return (
<ChartCanvas
height={height}
ratio={ratio}
width={width}
margin={this.margin}
padding={this.padding}
data={data}
seriesName="Data"
xScale={xScale}
xAccessor={xAccessor}
>
<Chart id={1} yExtentsCalculator={this.yExtentsCalculator}>
<CandlestickSeries />
<XAxis tickFormat={timeFormat} />
<YAxis />
</Chart>
<CrossHairCursor snapX={false} />
</ChartCanvas>
);
}
private readonly yExtentsCalculator = ({ plotData }: { plotData: IOHLCData[] }) => {
let min: number | undefined;
let max: number | undefined;
for (const { low, high } of plotData) {
if (min === undefined) {
min = low;
}
if (max === undefined) {
max = high;
}
if (low !== undefined) {
if (min! > low) {
min = low;
}
}
if (high !== undefined) {
if (max! < high) {
max = high;
}
}
}
if (min === undefined) {
min = 0;
}
if (max === undefined) {
max = 0;
}
const padding = (max - min) * 0.1;
return [min - padding, max + padding * 2];
};
}
export const Updating = withOHLCData("SECONDS")(
withUpdatingData()(withSize({ style: { minHeight: 600 } })(withDeviceRatio()(BasicLineSeries))),
);