-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
74 lines (74 loc) · 2.41 KB
/
index.js
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
// https://api.coingecko.com/api/v3/simple/price?ids=BITCOIN&vs_currencies=USD
function getprice(){
console.log("Called")
fetch('https://api.coingecko.com/api/v3/simple/price?ids=BITCOIN&vs_currencies=USD')
.then(response=>response.json())
.then(result=> {
console.log(result.bitcoin)
document.querySelector('#invField1').value=1
document.querySelector('#invField2').value="$ "+String(result.bitcoin.usd)
var bp=result.bitcoin.usd
document.querySelector('#invField1').onkeyup = () => {
let num=document.querySelector('#invField1').value
if(num!='')
{
document.querySelector('#invField2').value="$ "+String(num*bp)
}
}
})
}
document.addEventListener('DOMContentLoaded',function(){
getprice()
addgraph(1)
setInterval(getprice,20000)
document.querySelector('#numdays').onchange = function(){
addgraph(this.value)
}
})
function addgraph(days){
let xValues=[]
let yValues=[]
fetch(`https://api.coingecko.com/api/v3/coins/bitcoin/market_chart?vs_currency=usd&days=${days}`)
.then(response=>response.json())
.then(data=>{
data.prices.forEach(price=>{
xValues.push(new Date(price[0]).toLocaleDateString())
yValues.push(price[1])
})
document.querySelector('#graph_div').innerHTML=""
let canvas=document.createElement('canvas')
canvas.id="graph"
document.querySelector('#graph_div').append(canvas)
new Chart("graph", {
type: "line",
data: {
labels: xValues,
datasets: [{
fill: false,
lineTension: 0.25,
backgroundColor: "rgba(0,0,255,1.0)",
borderColor: "red",
data: yValues
}]
},
options: {
plugins:{
legend: {
display: false
},
title: {
display: true,
text: "Bitcoin price in USD"
},
},
scales: {
xAxis: {
ticks: {
maxTicksLimit: 10,
},
},
}
}
});
})
}