From efdb2ee3bbe194221a67d3a87d8f3be2ad0b44d0 Mon Sep 17 00:00:00 2001 From: Cryptoniq Date: Sun, 19 Jan 2020 09:53:49 +0100 Subject: [PATCH] Improved robustness of charting --- .gitignore | 1 + app/static/assets/myartiqox.js | 83 ++++++++++++++++++++++++---------- app/templates/pages/index.html | 3 +- 3 files changed, 61 insertions(+), 26 deletions(-) diff --git a/.gitignore b/.gitignore index beba493..0e5afd3 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,4 @@ yarn.lock yarn-error.log *.psd env/ +.idea/ diff --git a/app/static/assets/myartiqox.js b/app/static/assets/myartiqox.js index fbd5121..e3f807c 100644 --- a/app/static/assets/myartiqox.js +++ b/app/static/assets/myartiqox.js @@ -1,27 +1,62 @@ -var coingeckocharts = document.getElementsByClassName("coingecko-line-chart"); +Array.from(document.getElementsByClassName('coingecko-line-chart')).forEach(chart => { + const coinName = chart.dataset.coin; + const vs_currency = coinName === 'bitcoin' ? 'usd' : "btc"; -for (var ii = 0; ii < coingeckocharts.length; ii++) { -let coinname = coingeckocharts[ii]["id"]; -var vs_currency = "btc"; -if (coinname == "bitcoin") {vs_currency = "usd"; } -const fetchPromise = fetch('https://api.coingecko.com/api/v3/coins/'+coinname+'/market_chart?vs_currency='+vs_currency+'&days=7'); -const datadiv = document.getElementById("div-data-for-coingecko-line-chart-"+coinname); -fetchPromise.then(response => { - return response.json(); -}).then(market_charts_data => { - var chart_styles = [";rgba(237, 231, 246, 0.5);deep-purple-500;deep-purple-700;2", ";rgba(232, 245, 233, 0.5);blue-500;blue-700;2",";rgba(233, 241, 243, 0.5);red-500;red-700;2"]; - var styles_index = 0; - var my_div_with_data_text = ""; - for (var key in market_charts_data) { - var array_with_ts = []; - var array_with_data = []; - my_div_with_data_text += ""; + fetch(`https://api.coingecko.com/api/v3/coins/${coinName}/market_chart?vs_currency=${vs_currency}&days=7`) + .then(response => response.json()) + .then(({prices, total_volumes, market_caps}) => { + const data = { + datasets: [ + { + label: 'Prices', + data: prices.map(x => x[1]), + backgroundColor: ['rgba(233, 241, 243, 0.5)'], + borderColor: ['rgb(0, 0, 255, 0.5)'], + borderWidth: 2 + }, + { + label: 'Total Volumes', + data: total_volumes.map(x => x[1]), + backgroundColor: ['rgba(233, 241, 243, 0.5)'], + borderColor: ['rgb(255, 0, 0, 0.5)'], + borderWidth: 2 + }, + { + label: 'Market caps', + data: market_caps.map(x => x[1]), + backgroundColor: ['rgba(233, 241, 243, 0.5)'], + borderColor: ['rgb(0, 255, 0, 0.5)'], + borderWidth: 2 + }, + ], + labels: prices.map(x => x[0]) // assume all three data buckets have the same time-values; just pick those from the 1st one + }; - styles_index++; - } - my_div_with_data_text += ""; - datadiv.innerHTML = my_div_with_data_text; + const options = { + scales: { + xAxes: [{ + type: 'time', + time: { + displayFormats: { + hour: 'DDD MMM - h a' + } + } + }], + yAxes: [ + { + ticks: { + beginAtZero: true + } + } + ] + } + }; + + const ctx = document.getElementById(`coingecko-line-chart-${coinName}`).getContext('2d'); + new Chart(ctx, { + type: 'line', + data, + options, + }); + }); }); -} diff --git a/app/templates/pages/index.html b/app/templates/pages/index.html index 2fc2164..a5c801d 100644 --- a/app/templates/pages/index.html +++ b/app/templates/pages/index.html @@ -32,8 +32,7 @@
{{ coin }}
- - +