-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathChartCore.js
More file actions
120 lines (98 loc) · 3.15 KB
/
ChartCore.js
File metadata and controls
120 lines (98 loc) · 3.15 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
import React, { PureComponent } from 'react';
import Highcharts from 'highcharts/highstock.src';
import exporting from 'highcharts/modules/exporting';
import noDataToDisplay from 'highcharts/modules/no-data-to-display';
import initChart from './config/initChart';
import updateChart from './config/updateChart';
import axisIndicators from './plugins/axisIndicators';
import addLoadingFlag from './plugins/addLoadingFlag';
import styles from './styles';
// import winLossIndicators from './plugins/winLossIndicators';
// import tradeMarker from './plugins/tradeMarker';
// workaround for tests to work
if (Object.keys(Highcharts).length > 0) {
exporting(Highcharts);
noDataToDisplay(Highcharts);
axisIndicators();
addLoadingFlag();
// winLossIndicators();
// tradeMarker();
}
export type IndicatorsConfig = {
class: 'sma' | 'ema' | 'bb' | 'rsi' | 'macd',
name?: string, // name to be show in tooltip
period?: number,
}
export type ChartEvent = {
type: string,
handler: () => void,
}
type Props = {
parent: Object,
contract: Object,
id: string,
indicators: IndicatorsConfig[],
symbol: string,
noData: boolean,
pipSize: number,
type: ChartType,
dataType: DataType,
trade: Object,
ticks: Tick[],
events: ChartEvent[],
theme: string,
shiftMode: 'fixed' | 'dynamic', // switch to decide how chart move when data added
};
export default class ChartCore extends PureComponent {
props: Props;
chartDiv: any;
chart: Chart;
eventListeners: Object[];
componentDidMount() {
this.createChart();
updateChart(this.chart, { ticks: [] }, this.props);
}
componentDidUpdate(prevProps) {
if (this.props.symbol !== prevProps.symbol ||
this.props.noData !== prevProps.noData) {
this.destroyChart();
this.createChart(this.props);
}
updateChart(this.chart, prevProps, this.props);
}
componentWillUnmount() {
this.destroyChart();
}
createChart(newProps?: Props) {
const props = newProps || this.props;
const config = initChart(Object.assign({
hideEndButton: (hide) =>
this.props.parent.setState({ endButtonShown: !hide }),
}, props));
this.chart = this.props.parent.chart =
new Highcharts.StockChart(this.chartDiv, config, (chart) => {
if (!props.noData && props.ticks.length === 0) {
chart.showLoading();
}
});
this.eventListeners = props.events.map(e => ({
type: e.type,
handler: ev => e.handler(ev, this.chart),
}));
this.eventListeners.forEach(e => this.chartDiv.addEventListener(e.type, e.handler));
}
destroyChart() {
if (this.eventListeners) {
this.eventListeners.forEach(e => this.chartDiv.removeEventListener(e.type, e.handler));
}
if (this.chart) {
this.chart.destroy();
}
}
render() {
const { id } = this.props;
return (
<div style={styles.chartCore} ref={x => { this.chartDiv = x; }} id={id} />
);
}
}