Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

dashboard.js 3.9 KB

You have to be logged in to leave a comment. Sign In
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
  1. let tickers = ["AAPL", "ACN", "AMZN", "GOOGL", "META", "MSFT", "NFLX", "NVDA", "ORCL", "TSLA", "V", "WMT", "XOM", "ZM"];
  2. var lastPrices = {};
  3. var counter = 15;
  4. function startUpdateCycle() {
  5. updatePrices();
  6. setInterval(function() {
  7. counter--;
  8. $("#counter").text(counter);
  9. if (counter <= 0) {
  10. updatePrices();
  11. counter = 15;
  12. }
  13. }, 1000);
  14. }
  15. $(document).ready(function () {
  16. tickers.forEach(function (ticker) {
  17. addTickerToGrid(ticker);
  18. });
  19. updateSearch();
  20. updatePrices();
  21. startUpdateCycle();
  22. });
  23. function addTickerToGrid(ticker) {
  24. let htmlData = `
  25. <div id="${ticker}" class="stock-item">
  26. <h4 id="${ticker}-name" class="stock-name">${ticker}</h4>
  27. <h4 id="${ticker}-pct" class="stock-pct"></h4>
  28. <div class="stock-arrow-container">
  29. <img class="stock-arrow">
  30. </div>
  31. <h4 id="${ticker}-price" class="stock-stock"></h4>
  32. </div>
  33. `;
  34. $("#stocks").append(htmlData);
  35. }
  36. function updatePrices() {
  37. tickers.forEach(function (ticker) {
  38. $.ajax({
  39. url: '/get_stock_data',
  40. type: 'POST',
  41. data: JSON.stringify({ ticker: ticker }),
  42. contentType: 'application/json; charset=utf-8',
  43. dataType: 'json',
  44. success: function (data) {
  45. var changePercent = ((data.currentPrice - data.openPrice) / data.openPrice * 100)
  46. var colorClass;
  47. if (changePercent <= -2) {
  48. colorClass = 'dark-red'
  49. } else if (changePercent < 0) {
  50. colorClass = 'red'
  51. } else if (changePercent == 0) {
  52. colorClass = 'gray'
  53. } else if (changePercent <= 2) {
  54. colorClass = 'green'
  55. } else {
  56. colorClass = 'dark-green'
  57. }
  58. $(`#${ticker}-price`).text(`${data.currentPrice.toFixed(2)}`);
  59. $(`#${ticker}-pct`).text(`${changePercent.toFixed(2)}%`);
  60. // Remove the color classes from the price as it should not change color
  61. $(`#${ticker}-price`).removeClass('dark-red red gray green dark-green');
  62. // Apply the color classes to the percentage and the ticker name
  63. $(`#${ticker}-pct, #${ticker}-name`).removeClass('dark-red red gray green dark-green').addClass(colorClass);
  64. var flashClass, arrowClass;
  65. if (lastPrices[ticker] > data.currentPrice) {
  66. flashClass = 'red-flash';
  67. arrowClass = 'stock-arrow-red';
  68. } else if (lastPrices[ticker] < data.currentPrice) {
  69. flashClass = 'green-flash';
  70. arrowClass = 'stock-arrow-green';
  71. } else {
  72. flashClass = 'gray-flash';
  73. arrowClass = 'stock-arrow-dash';
  74. }
  75. lastPrices[ticker] = data.currentPrice;
  76. $(`#${ticker} img`).removeClass('stock-arrow-red stock-arrow-green stock-arrow-dash').addClass(arrowClass);
  77. $(`#${ticker}`).addClass(flashClass);
  78. setTimeout(function () {
  79. $(`#${ticker}`).removeClass(flashClass);
  80. }, 1000);
  81. }
  82. });
  83. });
  84. }
  85. document.getElementById("chat-input").addEventListener("input", function() {
  86. updateSearch();
  87. })
  88. function updateSearch() {
  89. let items = document.querySelector('.stocks-container');
  90. while (items.firstChild) items.removeChild(items.firstChild);
  91. let search = document.getElementById("chat-input").value;
  92. let newTickers = [];
  93. tickers.forEach(function(ticker) {
  94. if (ticker.toLowerCase().includes(search.toLowerCase())) {
  95. newTickers.push(ticker);
  96. }
  97. });
  98. console.log(newTickers);
  99. newTickers.forEach(function(ticker) {
  100. addTickerToGrid(ticker);
  101. });
  102. updatePrices();
  103. }
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...