Advertisement
Guest User

Untitled

a guest
Nov 20th, 2019
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.51 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. const closingTime = utilities.validate(snapshot[122]);
  88.  
  89.  
  90. const lineType = stockTime === 1 ? 'line' : 'none';
  91. let previousClosePluginQuoteDisplay;
  92.  
  93. const lastValue = utilities.validate(value[value.length - 1]);
  94. const firstValue = utilities.validate(value[0]);
  95. const priceChange = lastValue - firstValue;
  96.  
  97. const priceChangePerc = priceChange * 100 / firstValue;
  98.  
  99.  
  100. const newTimes = times.length < 50 ? times : times.filter((_, i) => i % 2 === 0);
  101. const newValues = value.length < 50 ? value : value.filter((_, i) => i % 2 === 0);
  102.  
  103.  
  104. getTimes(times[0], $(`${mainWrapper} .time`));
  105. getTimes(times[times.length - 1], $(`${mainWrapper} .time_close`));
  106.  
  107. const date = new Date(times[times.length - 1]);
  108. const formatter = new Intl.DateTimeFormat(vars.country, { month: 'short' });
  109. const closeMonth = formatter.format(new Date(times[times.length - 1]));
  110. const closeDay = date.getDate();
  111. const closeYear = date.getFullYear();
  112. const closeHour = date.getHours() - 1;
  113. const minutes = date.getMinutes() < 10 ? `0${date.getMinutes()}` : date.getMinutes();
  114.  
  115. $(`${mainWrapper} .last__update`).text(`${closeMonth} ${closeDay}, ${closeYear} ${closeHour}:${minutes} `);
  116.  
  117.  
  118. if (value[0] < value[value.length - 1]) {
  119. backgroundColor = 'rgba(0, 200, 120, 0.1)';
  120. borderColor = 'rgb(0, 200, 120)';
  121. } else {
  122. backgroundColor = 'rgba(199,23,0, 0.1)';
  123. borderColor = 'rgb(199,23,0)';
  124. }
  125.  
  126.  
  127. [...] CODE TO CHANGE DOM VALUES WAS HERE [...]
  128.  
  129.  
  130.  
  131.  
  132. [...] BEFORE RENDER FUNCTION WAS HERE, IT COLORS THE GRAPH RED AND GREEN BASED ON THE CLOSING PRICE [...]
  133.  
  134.  
  135. const chartdata = {
  136. labels: [...newTimes],
  137. datasets: [{
  138. label: 'EMA 20',
  139. data: [...newValues],
  140. borderColor: '#868e96',
  141. backgroundColor: 'transparent',
  142. borderWidth: 2,
  143.  
  144. }, {
  145. label: 'EMA 50',
  146. data: [...newValues],
  147. borderColor: '#fab008',
  148. backgroundColor: 'transparent',
  149. borderWidth: 2,
  150.  
  151. }, {
  152. backgroundColor,
  153. borderColor,
  154. data: [...newValues],
  155. lineTension: 0,
  156.  
  157. }],
  158. };
  159.  
  160. const ctx = {};
  161.  
  162. ctx[stockIds] = document.getElementById(`quoteDisplay_chart-${stockId}`).getContext('2d');
  163.  
  164. ctx[stockIds].height = 500;
  165.  
  166.  
  167.  
  168. if (window.chart !== undefined) window.chart.update();
  169.  
  170.  
  171. const quoteChart = new Chart(ctx[stockIds], {
  172. type: 'line',
  173. data: chartdata,
  174. options: {
  175.  
  176. responsive: true,
  177. maintainAspectRatio: false,
  178. scales: {
  179. yAxes: [{
  180. position: 'right',
  181. ticks: {
  182. display: true,
  183. },
  184. }],
  185. xAxes: [{
  186. ticks: {
  187. display: true,
  188. },
  189. }],
  190. },
  191. animation: {
  192. duration: 0,
  193. },
  194. tooltips: {
  195. mode: 'index',
  196. intersect: false,
  197. position: 'custom',
  198. },
  199. filter(tooltipItem) {
  200. return tooltipItem.datasetIndex === 2;
  201. },
  202. },
  203. legend: {
  204.  
  205. display: true,
  206. position: 'top',
  207. padding: 20,
  208. align: 'start',
  209.  
  210. labels: {
  211. filter(legendItem) {
  212. if (legendItem.datasetIndex === 2) {
  213. return false;
  214. }
  215. return true;
  216. },
  217.  
  218. },
  219. },
  220.  
  221.  
  222. },
  223. });
  224.  
  225. if (quoteChart) {
  226. quoteChart.update();
  227. }
  228.  
  229. if ($(quoteDisplayEl).hasClass('quoteDisplay--light')) {
  230. chartHandel.push(quoteChart);
  231. } else {
  232. window.chart = quoteChart;
  233. }
  234. } catch (error) {
  235. window.console.log('Error parsing JSON data', error);
  236. }
  237. },
  238. };
  239.  
  240.  
  241. export default quoteDisplay;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement