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
useEffect(() => { | |
fetchData().then((chartData) => { | |
initChart(chartData); | |
setLatestPrice(parseFloat(chartData.price[chartData.price.length - 1]).toFixed(2)); | |
}); | |
const timerID = setInterval(() => { | |
fetchData().then((chartData) => { | |
updateChart(chartData); | |
setLatestPrice(parseFloat(chartData.price[chartData.price.length - 1]).toFixed(2)); | |
}); |
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
const updateChart = (data) => { | |
let trace_price = { | |
x: [data.index.map((t) => new Date(t))], | |
y: [data.price], | |
}; | |
let trace_volumes = { | |
x: [data.index.map((t) => new Date(t))], | |
y: [data.volumes], | |
}; |
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
import React, { useState, useEffect } from "react"; | |
import callAPI from "./utils"; | |
function App() { | |
const [latestPrice, setLatestPrice] = useState(0); | |
useEffect(() => { | |
fetchData().then((chartData) => { | |
initChart(chartData); | |
setLatestPrice(parseFloat(chartData.price[chartData.price.length - 1]).toFixed(2)); |
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
const [latestPrice, setLatestPrice] = useState(0); | |
useEffect(() => { | |
fetchData().then((chartData) => { | |
initChart(chartData); | |
setLatestPrice(parseFloat(chartData.price[chartData.price.length - 1]).toFixed(2)); | |
}); | |
}, []); |
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
const fetchData = async () => { | |
let data = { index: [], price: [], volumes: [] }; | |
let result = await callAPI("https://api.coingecko.com/api/v3/coins/ethereum/market_chart?vs_currency=usd&days=1&interval=1m"); | |
for (const item of result.prices) { | |
data.index.push(item[0]); | |
data.price.push(item[1]); | |
} | |
for (const item of result.total_volumes) data.volumes.push(item[1]); | |
return data; | |
}; |
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
const initChart = (data) => { | |
let trace_price = { | |
name: "Price ($)", | |
x: data.index.map((t) => new Date(t)), | |
y: data.price, | |
xaxis: "x", | |
yaxis: "y1", | |
type: "scatter", | |
mode: "lines+markers", | |
marker: { color: "blue", size: 3 }, |
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
const callAPI = async (url) => { | |
let response = await fetch(url, { | |
headers: { | |
"Content-Type": "application/json", | |
Accept: "application/json", | |
}, | |
}); | |
if (!response.ok) { | |
const message = `An error has occured: ${response.status}`; | |
throw new Error(message); |
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
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8" /> | |
<meta http-equiv="X-UA-Compatible" content="IE=edge" /> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | |
<title>Realtime Chart</title> | |
<link | |
href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta3/dist/css/bootstrap.min.css" |
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
created() { | |
if (!("topic" in localStorage)) localStorage.topic = "top"; | |
this.loadStories(localStorage.topic); | |
if (!("theme" in localStorage)) localStorage.theme = "dark"; | |
}, | |
mounted() { | |
this.toggleDarkMode(localStorage.theme); | |
this.$refs[`topic-${localStorage.topic}`].classList.add("text-green-500"); | |
}, |
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
toggleDarkMode(theme, evt) { | |
let htmlElm = document.querySelector("html"); | |
const setLight = () => { | |
htmlElm.classList.remove("dark"); | |
localStorage.theme = "light"; | |
this.$refs.toggleDark.textContent = "☀️"; | |
}; | |
const setDark = () => { | |
htmlElm.classList.add("dark"); | |
localStorage.theme = "dark"; |
NewerOlder