app.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. const graph = new Vue({
  2. el: "#graph",
  3. data: {
  4. candles: [],
  5. latest: undefined
  6. },
  7. methods: {
  8. addCandle: (candle) => {
  9. graph.candles.push({
  10. date: candle[0],
  11. value: candle[2]
  12. });
  13. graph.latest = new Date(candle[0]);
  14. }
  15. },
  16. watch: {
  17. candles: (values) => {
  18. sorted = values.slice();
  19. sorted.sort((a, b) => {
  20. if (a.date < b.date) return -1;
  21. if (a.date > b.date) return 1;
  22. return 0;
  23. });
  24. dates = sorted.map((d) => d.date);
  25. for (let i=0; i<30; i++) {
  26. last = dates[dates.length - 1];
  27. dates.push(last + 60000);
  28. }
  29. d3.select("#data").select("g").remove();
  30. element = document.querySelector("#chart-space");
  31. margin = {
  32. top: 10,
  33. right: 30,
  34. bottom: 100,
  35. left: 60
  36. }
  37. height = element.clientHeight - margin.top - margin.bottom;
  38. width = element.clientWidth - margin.left - margin.right;
  39. svg = d3.select("#data")
  40. .append("g")
  41. .attr("transform", `translate(${margin.left}, ${margin.top})`);
  42. x = d3.scaleTime()
  43. .domain(d3.extent(dates, (d) => { return d }))
  44. .range([ 0, width ]);
  45. svg.append("g")
  46. .attr("transform", `translate(0, ${height})`)
  47. .call(d3.axisBottom(x));
  48. y = d3.scaleLinear()
  49. .domain([0, d3.max(sorted, (d) => { return +d.value })])
  50. .range([ height, 0 ]);
  51. svg.append("g")
  52. .call(d3.axisLeft(y));
  53. svg.append("path")
  54. .datum(sorted)
  55. .attr("fill", "none")
  56. .attr("stroke", "black")
  57. .attr("stroke-width", 2)
  58. .attr("d", d3.line().x((d) => {
  59. return x(d.date);
  60. }).y((d) => {
  61. return y(d.value);
  62. }));
  63. }
  64. }
  65. });
  66. const websocket = new Vue({
  67. el: "#websocket",
  68. created: () => {
  69. this.socket = new WebSocket("wss://api-pub.bitfinex.com/ws/2")
  70. this.socket.onopen = () => {
  71. websocket.connected = true
  72. };
  73. this.socket.onclose = () => {
  74. websocket.info = "";
  75. websocket.connected = false;
  76. };
  77. this.socket.onmessage = (message) => {
  78. json = JSON.parse(message.data);
  79. if (json.event == "info") websocket.info = `Server ${json.serverId} v${json.version}`;
  80. if (Array.isArray(json) && json[1] !== "hb") websocket.processValues(json);
  81. };
  82. },
  83. data: {
  84. connected: false,
  85. info: "",
  86. socket: undefined
  87. },
  88. methods: {
  89. processCandle: (candle) => {
  90. graph.addCandle(candle);
  91. },
  92. processValues: (json) => {
  93. if (Array.isArray(json[1][0])) {
  94. json[1].forEach((data) => websocket.processCandle(data));
  95. } else {
  96. websocket.processCandle(json[1]);
  97. }
  98. }
  99. },
  100. watch: {
  101. connected: (connected) => {
  102. if (connected === true) this.socket.send('{"event":"subscribe","channel":"candles","key":"trade:1m:tBTCUSD"}');
  103. }
  104. }
  105. });