forked from kimthostrup/flask-dashboard-adminator
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from cryptoniq/master
Improved robustness of charting
- Loading branch information
Showing
3 changed files
with
61 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,3 +8,4 @@ yarn.lock | |
yarn-error.log | ||
*.psd | ||
env/ | ||
.idea/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 += "<input type='text' class='chart_serie_data' id='"+key+chart_styles[styles_index]+"' value='"; | ||
for (var newindex = 0; newindex < market_charts_data[key].length; ++newindex) {var tempts = new Date(parseInt(market_charts_data[key][newindex][0]));array_with_ts.push(tempts.toISOString());array_with_data.push(market_charts_data[key][newindex][1]);} | ||
my_div_with_data_text += array_with_data.toString()+"'>"; | ||
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 += "<input type='text' class='chart_labels' id='chart_labels' value='"+array_with_ts.toString()+"'>"; | ||
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, | ||
}); | ||
}); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters