|
| 1 | +import React, {useState, useEffect} from 'react'; |
| 2 | +import {View, Text, SafeAreaView, StyleSheet} from 'react-native'; |
| 3 | +import ChartView from 'react-native-highcharts'; |
| 4 | +import axios from 'axios'; |
| 5 | +/** |
| 6 | + * Request data from the server, add it to the graph and set a timeout |
| 7 | + * to request again |
| 8 | + */ |
| 9 | +function processData(list, type, desc) { |
| 10 | + var res = []; |
| 11 | + // Convert to data points |
| 12 | + for (var i = 0; i < list.length; i++) { |
| 13 | + list[i] = { |
| 14 | + value: Number(list[i][0]), |
| 15 | + volume: Number(list[i][1]), |
| 16 | + }; |
| 17 | + } |
| 18 | + |
| 19 | + // Sort list just in case |
| 20 | + list.sort(function (a, b) { |
| 21 | + if (a.value > b.value) { |
| 22 | + return 1; |
| 23 | + } else if (a.value < b.value) { |
| 24 | + return -1; |
| 25 | + } else { |
| 26 | + return 0; |
| 27 | + } |
| 28 | + }); |
| 29 | + |
| 30 | + // Calculate cummulative volume |
| 31 | + if (desc) { |
| 32 | + for (var i = list.length - 1; i >= 0; i--) { |
| 33 | + if (i < list.length - 1) { |
| 34 | + list[i].totalvolume = list[i + 1].totalvolume + list[i].volume; |
| 35 | + } else { |
| 36 | + list[i].totalvolume = list[i].volume; |
| 37 | + } |
| 38 | + // var dp = {}; |
| 39 | + // dp['value'] = list[i].value; |
| 40 | + // dp[type + 'volume'] = list[i].volume; |
| 41 | + // dp[type + 'totalvolume'] = list[i].totalvolume; |
| 42 | + res.unshift([list[i].value, list[i].totalvolume]); |
| 43 | + } |
| 44 | + } else { |
| 45 | + for (var i = 0; i < list.length; i++) { |
| 46 | + if (i > 0) { |
| 47 | + list[i].totalvolume = list[i - 1].totalvolume + list[i].volume; |
| 48 | + } else { |
| 49 | + list[i].totalvolume = list[i].volume; |
| 50 | + } |
| 51 | + // var dp = {}; |
| 52 | + // dp['value'] = list[i].value; |
| 53 | + // dp[type + 'volume'] = list[i].volume; |
| 54 | + // dp[type + 'totalvolume'] = list[i].totalvolume; |
| 55 | + res.push([list[i].value, list[i].totalvolume]); |
| 56 | + } |
| 57 | + } |
| 58 | + return res; |
| 59 | +} |
| 60 | + |
| 61 | +const HighChart2 = () => { |
| 62 | + var Highcharts = 'Highcharts'; |
| 63 | + const apiBack = axios.create({ |
| 64 | + baseURL: |
| 65 | + 'https://poloniex.com/public?command=returnOrderBook¤cyPair=BTC_ETH&depth=50', |
| 66 | + //timeout: 1000, |
| 67 | + }); |
| 68 | + |
| 69 | + const [state, setState] = useState({ |
| 70 | + chartOptions: { |
| 71 | + chart: { |
| 72 | + type: 'area', |
| 73 | + animation: false, // don't animate in old IE |
| 74 | + marginRight: 10, |
| 75 | + center: [0.0], |
| 76 | + threshold: 0, |
| 77 | + // events: { |
| 78 | + // load: function () { |
| 79 | + // // set up the updating of the chart each second |
| 80 | + // // var series = this.series[0], |
| 81 | + // // series1 = this.series[1]; |
| 82 | + // setInterval(function () { |
| 83 | + // alert('runnning'); |
| 84 | + // var y = Math.random(); |
| 85 | + // series.addPoint(y, true, true); |
| 86 | + // series1.addPoint(y, true, true); |
| 87 | + // // apiBack |
| 88 | + // // .get('') |
| 89 | + // // .then((res) => res.json()) |
| 90 | + // // .then((res) => { |
| 91 | + // // setState({ |
| 92 | + // // ...state, |
| 93 | + // // chartOptions: { |
| 94 | + // // ...state.chartOptions, |
| 95 | + // // series: [ |
| 96 | + // // {data: processData(res.data.bids, 'bids', true)}, |
| 97 | + // // {data: processData(res.data.asks, 'asks', false)}, |
| 98 | + // // ], |
| 99 | + // // }, |
| 100 | + // // }); |
| 101 | + // // }); |
| 102 | + // // series.redraw(); |
| 103 | + // // series1.redraw(); |
| 104 | + // }, 5000); |
| 105 | + // }, |
| 106 | + // }, |
| 107 | + }, |
| 108 | + series: [ |
| 109 | + { |
| 110 | + data: [], |
| 111 | + }, |
| 112 | + { |
| 113 | + data: [], |
| 114 | + }, |
| 115 | + ], |
| 116 | + title: { |
| 117 | + text: 'Live random data', |
| 118 | + }, |
| 119 | + xAxis: { |
| 120 | + minPadding: 0, |
| 121 | + maxPadding: 0, |
| 122 | + plotLines: [ |
| 123 | + { |
| 124 | + color: '#888', |
| 125 | + //value: state.asks.length && state.asks[0][0], |
| 126 | + width: 1, |
| 127 | + label: { |
| 128 | + text: 'Actual price', |
| 129 | + rotation: 90, |
| 130 | + }, |
| 131 | + }, |
| 132 | + ], |
| 133 | + title: { |
| 134 | + text: 'Price', |
| 135 | + }, |
| 136 | + }, |
| 137 | + yAxis: [ |
| 138 | + { |
| 139 | + lineWidth: 1, |
| 140 | + gridLineWidth: 1, |
| 141 | + title: null, |
| 142 | + tickWidth: 1, |
| 143 | + tickLength: 5, |
| 144 | + tickPosition: 'inside', |
| 145 | + labels: { |
| 146 | + align: 'left', |
| 147 | + x: 8, |
| 148 | + }, |
| 149 | + }, |
| 150 | + { |
| 151 | + opposite: true, |
| 152 | + linkedTo: 0, |
| 153 | + lineWidth: 1, |
| 154 | + gridLineWidth: 0, |
| 155 | + title: null, |
| 156 | + tickWidth: 1, |
| 157 | + tickLength: 5, |
| 158 | + tickPosition: 'inside', |
| 159 | + labels: { |
| 160 | + align: 'right', |
| 161 | + x: -8, |
| 162 | + }, |
| 163 | + }, |
| 164 | + ], |
| 165 | + tooltip: { |
| 166 | + backgroundColor: '#FCFFC5', |
| 167 | + borderColor: 'black', |
| 168 | + borderRadius: 10, |
| 169 | + borderWidth: 3, |
| 170 | + crosshairs: [true, true], |
| 171 | + formatter: function () { |
| 172 | + return ( |
| 173 | + '<b>' + this.series.name + '</b><br/>' + this.x + '<br/>' + this.y |
| 174 | + ); |
| 175 | + }, |
| 176 | + }, |
| 177 | + legend: { |
| 178 | + enabled: false, |
| 179 | + }, |
| 180 | + plotOptions: { |
| 181 | + area: { |
| 182 | + fillOpacity: 0.2, |
| 183 | + lineWidth: 1, |
| 184 | + step: 'center', |
| 185 | + }, |
| 186 | + }, |
| 187 | + exporting: { |
| 188 | + enabled: false, |
| 189 | + }, |
| 190 | + }, |
| 191 | + }); |
| 192 | + |
| 193 | + useEffect(() => { |
| 194 | + apiBack.get('').then((res) => |
| 195 | + setState({ |
| 196 | + ...state, |
| 197 | + chartOptions: { |
| 198 | + ...state.chartOptions, |
| 199 | + series: [ |
| 200 | + {data: processData(res.data.bids, 'bids', true)}, |
| 201 | + {data: processData(res.data.asks, 'asks', false)}, |
| 202 | + ], |
| 203 | + }, |
| 204 | + }), |
| 205 | + ); |
| 206 | + }, []); |
| 207 | + |
| 208 | + return ( |
| 209 | + <SafeAreaView style={{flex: 1, backgroundColor: 'green'}}> |
| 210 | + <View style={{flex: 1, backgroundColor: 'black'}}> |
| 211 | + <ChartView |
| 212 | + style={{height: 300, overflow: 'hidden'}} |
| 213 | + config={state.chartOptions} |
| 214 | + //options={state.chartOptions} |
| 215 | + originWhitelist={['']} |
| 216 | + javaScriptEnabled={true} |
| 217 | + domStorageEnabled={true} |
| 218 | + useWebKit={true} |
| 219 | + scalesPageToFit={undefined}></ChartView> |
| 220 | + </View> |
| 221 | + </SafeAreaView> |
| 222 | + ); |
| 223 | +}; |
| 224 | + |
| 225 | +export default HighChart2; |
0 commit comments