Advertisement
Guest User

Untitled

a guest
Nov 20th, 2019
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.07 KB | None | 0 0
  1. import Chart from 'chart.js';
  2. [...] I removed the rest of the imports [...]
  3.  
  4. const { Promise } = require('es6-promise');
  5.  
  6. const quoteDisplay = {
  7.  
  8. init() {
  9. if ($('.quoteDisplay').length > 0) {
  10. $('.quoteDisplay').each((_, elementObj) => {
  11. const stockId = $(elementObj).data('stock_id');
  12. this.setup(stockId, elementObj);
  13. });
  14. }
  15. },
  16.  
  17.  
  18. setup(stockId, element) {
  19. let stockTime = 1;
  20. const grab = (id, time) => {
  21. /* Promise to make sure data loads */
  22.  
  23. // eslint-disable-next-line
  24. new Promise((resolve, reject) => {
  25. const self = this;
  26.  
  27. $.ajax({
  28. url: `${vars.baseUrl}/${vars.country}/components/quoteDisplay/${time}/${stockId}`,
  29. method: 'GET',
  30. dataType: 'JSON',
  31. beforeSend() {
  32. $('.loading-image').show();
  33. },
  34. success(data) {
  35. resolve(data);
  36. self.vals(data, stockId, element);
  37. $('.loading-image').hide();
  38. },
  39. error(error) {
  40. reject(error);
  41. },
  42. });
  43. });
  44. };
  45.  
  46. [...] UNNECESSARY CODE WAS HERE [...]
  47. /**
  48. * Changes graph when the user chooses a different time frame
  49. */
  50. $('.quoteDisplay__subheader__timing__url').on('click', (e) => {
  51. e.preventDefault();
  52. const self = $(e.currentTarget);
  53. self.siblings().removeClass('active');
  54. self.addClass('active');
  55. const largeStockId = $('.quoteDisplay').data('stock_id');
  56.  
  57. stockTime = self.data('timing_id');
  58. grab(largeStockId, stockTime);
  59. });
  60. },
  61.  
  62.  
  63. vals(data, stockId, quoteDisplayEl) {
  64. [...] CONST FOR DOM ELEMENTS WERE HERE [...]
  65. const chartHandel = [];
  66.  
  67. const times = [];
  68. const value = [];
  69.  
  70.  
  71. let backgroundColor;
  72. let borderColor;
  73.  
  74.  
  75.  
  76. [...] EPOCH TIME -> Normal time function was here [...]
  77.  
  78.  
  79. try {
  80. data.historic.forEach((item) => {
  81. times.push(item[0]);
  82. value.push(item[1]);
  83. });
  84.  
  85. const { snapshot } = data;
  86.  
  87.  
  88. [...] VARS OF THE DOM VALUES WERE HERE [...]
  89.  
  90. const newTimes = times.length < 50 ? times : times.filter((_, i) => i % 2 === 0);
  91. const newValues = value.length < 50 ? value : value.filter((_, i) => i % 2 === 0);
  92.  
  93.  
  94.  
  95.  
  96.  
  97. [...] CODE TO CHANGE DOM VALUES WAS HERE [...]
  98.  
  99.  
  100.  
  101.  
  102. [...] BEFORE RENDER FUNCTION WAS HERE, IT COLORS THE GRAPH RED AND GREEN BASED ON THE CLOSING PRICE [...]
  103.  
  104.  
  105. const chartdata = {
  106. labels: [...newTimes],
  107. datasets: [{
  108. label: 'EMA 20',
  109. data: [...newValues],
  110. borderColor: '#868e96',
  111. backgroundColor: 'transparent',
  112. borderWidth: 2,
  113.  
  114. }, {
  115. label: 'EMA 50',
  116. data: [...newValues],
  117. borderColor: '#fab008',
  118. backgroundColor: 'transparent',
  119. borderWidth: 2,
  120.  
  121. }, {
  122. backgroundColor,
  123. borderColor,
  124. data: [...newValues],
  125. lineTension: 0,
  126.  
  127. }],
  128. };
  129.  
  130. const ctx = {};
  131.  
  132. ctx[stockIds] = document.getElementById(`quoteDisplay_chart-${stockId}`).getContext('2d');
  133.  
  134. ctx[stockIds].height = 500;
  135.  
  136.  
  137.  
  138. if (window.chart !== undefined) window.chart.update();
  139.  
  140.  
  141. const quoteChart = new Chart(ctx[stockIds], {
  142. type: 'line',
  143. data: chartdata,
  144. options: {
  145.  
  146. responsive: true,
  147. maintainAspectRatio: false,
  148. scales: {
  149. yAxes: [{
  150. position: 'right',
  151. ticks: {
  152. display: true,
  153. },
  154. }],
  155. xAxes: [{
  156. ticks: {
  157. display: true,
  158. },
  159. }],
  160. },
  161. animation: {
  162. duration: 0,
  163. },
  164. tooltips: {
  165. mode: 'index',
  166. intersect: false,
  167. position: 'custom',
  168. },
  169. filter(tooltipItem) {
  170. return tooltipItem.datasetIndex === 2;
  171. },
  172. },
  173. legend: {
  174.  
  175. display: true,
  176. position: 'top',
  177. padding: 20,
  178. align: 'start',
  179.  
  180. labels: {
  181. filter(legendItem) {
  182. if (legendItem.datasetIndex === 2) {
  183. return false;
  184. }
  185. return true;
  186. },
  187.  
  188. },
  189. },
  190.  
  191.  
  192. },
  193. });
  194.  
  195. if (quoteChart) {
  196. quoteChart.update();
  197. }
  198.  
  199. if ($(quoteDisplayEl).hasClass('quoteDisplay--light')) {
  200. chartHandel.push(quoteChart);
  201. } else {
  202. window.chart = quoteChart;
  203. }
  204. } catch (error) {
  205. window.console.log('Error parsing JSON data', error);
  206. }
  207. },
  208. };
  209.  
  210.  
  211. export default quoteDisplay;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement