-
Notifications
You must be signed in to change notification settings - Fork 1
/
Chart1.html
151 lines (135 loc) · 5.11 KB
/
Chart1.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<style>
#chartdiv {
width: 100%;
height: 500px;
}
</style>
<!-- Resources -->
<script src="https://www.amcharts.com/lib/4/core.js"></script>
<script src="https://www.amcharts.com/lib/4/charts.js"></script>
<script src="https://www.amcharts.com/lib/4/themes/animated.js"></script>
<!-- Chart code -->
<script>
am4core.ready(function () {
// Themes begin
am4core.useTheme(am4themes_animated);
// Themes end
// Create chart instance
var chart = am4core.create('chartdiv', am4charts.XYChart);
// Add data
chart.dataSource.url =
'https://poloniex.com/public?command=returnOrderBook¤cyPair=BTC_ETH&depth=50';
chart.dataSource.reloadFrequency = 30000;
chart.dataSource.adapter.add('parsedData', function (data) {
// Function to process (sort and calculate cummulative volume)
function processData(list, type, desc) {
// Convert to data points
for (var i = 0; i < list.length; i++) {
list[i] = {
value: Number(list[i][0]),
volume: Number(list[i][1]),
};
}
// Sort list just in case
list.sort(function (a, b) {
if (a.value > b.value) {
return 1;
} else if (a.value < b.value) {
return -1;
} else {
return 0;
}
});
// Calculate cummulative volume
if (desc) {
for (var i = list.length - 1; i >= 0; i--) {
if (i < list.length - 1) {
list[i].totalvolume =
list[i + 1].totalvolume + list[i].volume;
} else {
list[i].totalvolume = list[i].volume;
}
var dp = {};
dp['value'] = list[i].value;
dp[type + 'volume'] = list[i].volume;
dp[type + 'totalvolume'] = list[i].totalvolume;
res.unshift(dp);
}
} else {
for (var i = 0; i < list.length; i++) {
if (i > 0) {
list[i].totalvolume =
list[i - 1].totalvolume + list[i].volume;
} else {
list[i].totalvolume = list[i].volume;
}
var dp = {};
dp['value'] = list[i].value;
dp[type + 'volume'] = list[i].volume;
dp[type + 'totalvolume'] = list[i].totalvolume;
res.push(dp);
}
}
}
// Init
var res = [];
processData(data.bids, 'bids', true);
processData(data.asks, 'asks', false);
return res;
});
// Set up precision for numbers
chart.numberFormatter.numberFormat = '#,###.####';
// Create axes
var xAxis = chart.xAxes.push(new am4charts.CategoryAxis());
xAxis.dataFields.category = 'value';
//xAxis.renderer.grid.template.location = 0;
xAxis.renderer.minGridDistance = 50;
xAxis.title.text = 'Price (BTC/ETH)';
var yAxis = chart.yAxes.push(new am4charts.ValueAxis());
yAxis.title.text = 'Volume';
// Create series
var series = chart.series.push(new am4charts.StepLineSeries());
series.dataFields.categoryX = 'value';
series.dataFields.valueY = 'bidstotalvolume';
series.strokeWidth = 2;
series.stroke = am4core.color('#0f0');
series.fill = series.stroke;
series.fillOpacity = 0.1;
series.tooltipText =
'Ask: [bold]{categoryX}[/]\nTotal volume: [bold]{valueY}[/]\nVolume: [bold]{bidsvolume}[/]';
var series2 = chart.series.push(new am4charts.StepLineSeries());
series2.dataFields.categoryX = 'value';
series2.dataFields.valueY = 'askstotalvolume';
series2.strokeWidth = 2;
series2.stroke = am4core.color('#f00');
series2.fill = series2.stroke;
series2.fillOpacity = 0.1;
series2.tooltipText =
'Ask: [bold]{categoryX}[/]\nTotal volume: [bold]{valueY}[/]\nVolume: [bold]{asksvolume}[/]';
var series3 = chart.series.push(new am4charts.ColumnSeries());
series3.dataFields.categoryX = 'value';
series3.dataFields.valueY = 'bidsvolume';
series3.strokeWidth = 0;
series3.fill = am4core.color('#000');
series3.fillOpacity = 0.2;
var series4 = chart.series.push(new am4charts.ColumnSeries());
series4.dataFields.categoryX = 'value';
series4.dataFields.valueY = 'asksvolume';
series4.strokeWidth = 0;
series4.fill = am4core.color('#000');
series4.fillOpacity = 0.2;
// Add cursor
chart.cursor = new am4charts.XYCursor();
}); // end am4core.ready()
</script>
</head>
<body>
<div id="chartdiv"></div>
</body>
</html>