-
Notifications
You must be signed in to change notification settings - Fork 283
/
Index.js
198 lines (174 loc) · 6.17 KB
/
Index.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/**
* Copyright (c) 2016, The Regents of the University of California,
* through Lawrence Berkeley National Laboratory (subject to receipt
* of any required approvals from the U.S. Dept. of Energy).
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/
/* eslint-disable */
import React from "react";
import moment from "moment";
// Pond
import { Collection, TimeSeries, TimeEvent, IndexedEvent, TimeRange } from "pondjs";
// Imports from the charts library
import ChartContainer from "../../../../../components/ChartContainer";
import ChartRow from "../../../../../components/ChartRow";
import Charts from "../../../../../components/Charts";
import YAxis from "../../../../../components/YAxis";
import LineChart from "../../../../../components/LineChart";
import BarChart from "../../../../../components/BarChart";
import Resizable from "../../../../../components/Resizable";
import stockchart_docs from "./stockchart_docs.md";
import stockchart_thumbnail from "./stockchart_thumbnail.png";
import aapl from "./aapl_historical.json";
//
// Price: High, low, open, close
//
const name = "AAPL-price";
const columns = ["time", "open", "close", "low", "high"];
const events = aapl.map(item => {
const timestamp = moment(new Date(item.date));
const { open, close, low, high } = item;
return new TimeEvent(timestamp.toDate(), {
open: +open,
close: +close,
low: +low,
high: +high
});
});
const collection = new Collection(events);
const sortedCollection = collection.sortByTime();
const series = new TimeSeries({ name, columns, collection: sortedCollection });
//
// Volume
//
const volumeEvents = aapl.map(item => {
const index = item.date.replace(/\//g, "-");
const { volume } = item;
return new IndexedEvent(index, { volume: +volume });
});
const volumeCollection = new Collection(volumeEvents);
const sortedVolumeCollection = volumeCollection.sortByTime();
const seriesVolume = new TimeSeries({
name: "AAPL-volume",
utc: false,
collection: sortedVolumeCollection
});
class stockchart extends React.Component {
constructor(props) {
super(props);
this.state = {
mode: "log",
timerange: new TimeRange([1236985288649, 1326654398343])
};
}
handleTimeRangeChange = timerange => {
this.setState({ timerange });
};
setModeLinear = () => {
this.setState({ mode: "linear" });
};
setModeLog = () => {
this.setState({ mode: "log" });
};
renderChart = () => {
const { timerange } = this.state;
const croppedSeries = series.crop(timerange);
const croppedVolumeSeries = seriesVolume.crop(timerange);
return (
<ChartContainer
timeRange={timerange}
hideWeekends={true}
enablePanZoom={true}
onTimeRangeChanged={this.handleTimeRangeChange}
timeAxisStyle={{ axis: { fill: "none", stroke: "none" } }}
>
<ChartRow height="300">
<Charts>
<LineChart
axis="y"
style={{ close: { normal: { stroke: "steelblue" } } }}
columns={["close"]}
series={croppedSeries}
interpolation="curveBasis"
/>
</Charts>
<YAxis
id="y"
label="Price ($)"
min={croppedSeries.min("close")}
max={croppedSeries.max("close")}
format=",.0f"
width="60"
type={this.state.mode}
/>
</ChartRow>
<ChartRow height="200" axisMargin={0}>
<Charts>
<BarChart
axis="y"
style={{ volume: { normal: { stroke: "steelblue" } } }}
columns={["volume"]}
series={croppedVolumeSeries}
/>
</Charts>
<YAxis
id="y"
label="Volume"
min={croppedVolumeSeries.min("volume")}
max={croppedVolumeSeries.max("volume")}
width="60"
/>
</ChartRow>
</ChartContainer>
);
};
render() {
const linkStyle = {
fontWeight: 600,
color: "grey",
cursor: "default"
};
const linkStyleActive = {
color: "steelblue",
cursor: "pointer"
};
return (
<div>
<div className="row">
<div className="col-md-12">
<h3>Apple stock price</h3>
</div>
</div>
<hr />
<div className="row">
<div className="col-md-12" style={{ fontSize: 14, color: "#777" }}>
<span
style={this.state.mode === "log" ? linkStyleActive : linkStyle}
onClick={this.setModeLinear}
>
Linear
</span>
<span> | </span>
<span
style={this.state.mode === "linear" ? linkStyleActive : linkStyle}
onClick={this.setModeLog}
>
Log
</span>
</div>
</div>
<hr />
<div className="row">
<div className="col-md-12">
<Resizable>{this.renderChart()}</Resizable>
</div>
</div>
</div>
);
}
}
// Export example
export default { stockchart, stockchart_docs, stockchart_thumbnail };