baseddata.io/static/js/bitcoin-price.js

66 lines
1.4 KiB
JavaScript
Raw Normal View History

2024-08-01 14:06:16 +01:00
let dataArr = [];
const myChart = echarts.init(document.getElementById("chart"));
2024-08-06 12:03:42 +01:00
async function fetchDataForChart() {
try {
const apiEndpoint = "https://api.bitlab21.com/price";
const response = await fetch(apiEndpoint);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const dataArr = await response.json();
initEchart(dataArr);
} catch (error) {
console.error("Fetching data failed:", error);
}
}
2024-08-01 14:06:16 +01:00
2024-08-06 12:03:42 +01:00
function initEchart(dataArr) {
2024-08-01 14:06:16 +01:00
const option = {
backgroundColor: backgroundColor,
2024-08-08 11:53:39 +01:00
tooltip: tooltip,
2024-08-01 14:06:16 +01:00
toolbox: toolboxParams,
xAxis: {
data: dataArr.map((row) => row.date),
axisTick: axisTick,
axisLabel: axisLabel,
axisLine: axisLine,
},
grid: grid,
dataZoom: dataZoom(),
yAxis: [
{
type: "value",
name: "Price (USD)",
2024-08-08 11:53:39 +01:00
nameGap: 30,
2024-08-01 14:06:16 +01:00
nameLocation: "middle",
2024-08-08 11:53:39 +01:00
nameTextStyle: textStyleMain,
2024-08-01 14:06:16 +01:00
position: "left",
alignTicks: true,
axisTick: axisTick,
axisLine: axisLine,
splitLine: {
show: false,
},
2024-08-08 11:53:39 +01:00
axisLabel: {
...axisLabel,
formatter(value, index) {
return nFormatter(value, 0);
},
},
2024-08-01 14:06:16 +01:00
},
],
series: [
{
type: "line",
name: "Price (USD)",
data: dataArr.map((row) => row.price),
},
],
};
myChart.setOption(option);
}
2024-08-06 12:03:42 +01:00
fetchDataForChart();