-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprice_chart.html
94 lines (85 loc) · 2.71 KB
/
price_chart.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="styles1.css">
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<title>Price Charts</title>
<style>
canvas {
width: 400px;
height: 300px;
margin-bottom: 20px;
}
</style>
</head>
<body>
<div id="priceChartsContainer"></div>
<script>
// Function to fetch price data from CoinGecko API
async function fetchPriceData(currency) {
const response = await fetch(`https://api.coingecko.com/api/v3/coins/${currency}/market_chart?vs_currency=usd&days=7`);
const data = await response.json();
return data;
}
// Function to create and render a price chart for a given currency
async function renderPriceChart(currency) {
const priceData = await fetchPriceData(currency);
const labels = priceData.prices.map(entry => new Date(entry[0]).toLocaleDateString());
const prices = priceData.prices.map(entry => entry[1]);
const chartContainer = document.createElement('div');
chartContainer.className = 'chartContainer';
document.getElementById('priceChartsContainer').appendChild(chartContainer);
const canvas = document.createElement('canvas');
chartContainer.appendChild(canvas);
const ctx = canvas.getContext('2d');
new Chart(ctx, {
type: 'line',
data: {
labels: labels,
datasets: [
{
label: currency.toUpperCase(),
data: prices,
borderColor: getRandomColor(),
fill: false
}
]
},
options: {
responsive: true,
scales: {
x: {
display: true,
title: {
display: true,
text: 'Date'
}
},
y: {
display: true,
title: {
display: true,
text: 'Price (USD)'
}
}
}
}
});
}
// Generate random color for each chart
function getRandomColor() {
const letters = '0123456789ABCDEF';
let color = '#';
for (let i = 0; i < 6; i++) {
color += letters[Math.floor(Math.random() * 16)];
}
return color;
}
// Render the price charts when the page loads
window.addEventListener('load', () => {
const currencies = ['bitcoin', 'tether', 'ripple', 'cardano', 'binancecoin', 'usd-coin', 'dogecoin','solana','litecoin','tron','polkadot','polygon','shiba-inu'];
currencies.forEach(currency => renderPriceChart(currency));
});
</script>
</body>
</html>