Skip to content

Commit

Permalink
Update index.html
Browse files Browse the repository at this point in the history
  • Loading branch information
stelpras authored Oct 20, 2023
1 parent 45cf267 commit cf56d98
Showing 1 changed file with 2 additions and 78 deletions.
80 changes: 2 additions & 78 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -32,84 +32,8 @@ <h2>60 Minutes</h2>
</div>

<div id="chart"></div>
<script>
currencyForm = document.getElementById("currency-form");
bitcoinPriceElement = document.getElementById("bitcoin-price");
average5minElement = document.getElementById("average-5min");
average30minElement = document.getElementById("average-30min");
average60minElement = document.getElementById("average-60min");

prices5min = [];
prices30min = [];
prices60min = [];

function refresh() {
selectedCurrency = document.getElementById("currency").value;

fetch(`https://api.coinbase.com/v2/prices/spot?currency=${selectedCurrency}`)
.then(response => response.json())
.then(data => {
price = parseFloat(data.data.amount);
bitcoinPriceElement.innerHTML = `Prize is now: ${selectedCurrency} ${price.toFixed(2)}`;

prices5min.push(price);
prices30min.push(price);
prices60min.push(price);

// Keep only the last 5, 30, and 60 prices
if (prices5min.length > 5) {
prices5min.shift();
}
if (prices30min.length > 30) {
prices30min.shift();
}
if (prices60min.length > 60) {
prices60min.shift();
}

average5min = calculateAverage(prices5min);
average30min = calculateAverage(prices30min);
average60min = calculateAverage(prices60min);

average5minElement.innerHTML = `${selectedCurrency} ${average5min.toFixed(2)}`;
average30minElement.innerHTML = `${selectedCurrency} ${average30min.toFixed(2)}`;
average60minElement.innerHTML = `${selectedCurrency} ${average60min.toFixed(2)}`;

// Update the chart with the new data
chart.updateOptions(options);
})
.catch(error => {
bitcoinPriceElement.innerHTML = "There was an error!";
console.error(error);
});
}

function calculateAverage(prices) {
sum = prices.reduce((total, price) => total + price, 0);
return sum / prices.length;
}
var options = {
chart: {
type: 'line'
},
series: [{
name: 'bitcoin-prizes',
data: prices60min
}],
xaxis: {
type: 'category',
categories: Array.from({ length: 60 }, (_, i) => String(i + 1))
}

}

var chart = new ApexCharts(document.querySelector("#chart"), options);

chart.render();

// Refresh data every 1 minute
setInterval(refresh, 60000);
</script>
<script src="./index.js"></script>

</body>
</html>
</html>

0 comments on commit cf56d98

Please sign in to comment.