You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

95 lines
2.6 KiB

  1. $(function() {
  2. var heads = '<th>Official Rate</th><th>Official Value</th><th>Market Value</th>';
  3. $("thead tr").append(heads);
  4. ajax = function(options, callback) {
  5. var xhr;
  6. xhr = new XMLHttpRequest();
  7. xhr.open(options.type, options.url, options.async || true);
  8. xhr.onreadystatechange = function() {
  9. if (xhr.readyState === 4) {
  10. return callback(xhr.responseText);
  11. }
  12. };
  13. return xhr.send();
  14. };
  15. ajax({
  16. type: "GET",
  17. url: "http://api.fairplayground.info/rawdata/faircoin_prices.csv",
  18. }, processData);
  19. function processData(data) {
  20. if(data) priceData = csvToArray(data);
  21. // $(".inner-cont").append("priceData: ");
  22. // $(".inner-cont").append(priceData);
  23. if (priceData) {
  24. $('tbody > tr').each(function() {
  25. var dateVar = $(this).find('td:first').text();
  26. var d = new Date(dateVar);
  27. var dateISO = d.toISOString().slice(0, 10);
  28. var faircoins = $(this).find('td:nth-child(3)').text();
  29. // $(this).append('<td>'+faircoins+'</td>');
  30. calculateEuros(dateISO, faircoins, this);
  31. });
  32. }
  33. };
  34. function calculateEuros(date, faircoins, el) {
  35. if (!faircoins) {
  36. console.log("please, fill the faircoin input box");
  37. return;
  38. }
  39. if (!date) {
  40. console.log("no date");
  41. return;
  42. }
  43. //let's find the date in the priceData
  44. var selectedPriceData = [];
  45. for (var i = 0; i < priceData.length; i++) {
  46. if (priceData[i][0] == date) {
  47. selectedPriceData = priceData[i];
  48. }
  49. }
  50. if (selectedPriceData.length < 1) {
  51. console.log("selected date not found in the dataset");
  52. return;
  53. }
  54. // console.log(selectedPriceData);
  55. var euros = faircoins * selectedPriceData[3];
  56. var market = faircoins * selectedPriceData[2];
  57. $(el).append('<td>' + parseFloat(selectedPriceData[3]).toString() + '</td>');
  58. $(el).append('<td>€ ' + (Math.round(euros * 100) / 100) + '</td>');
  59. $(el).append('<td>€ ' + (Math.round(market * 100) / 100) + '</td>');
  60. }
  61. function csvToArray(csv) {
  62. var a1 = csv.split("\n");
  63. var r = [];
  64. for (var i = 0; i < a1.length; i++) {
  65. var a2 = a1[i].split(",");
  66. r.push(a2);
  67. }
  68. return (r);
  69. }
  70. });