baseddata.io/static/js/hashrate.js

128 lines
3.0 KiB
JavaScript
Raw Permalink Normal View History

2024-08-01 14:06:16 +01:00
let dataArr = [];
const myChart = echarts.init(document.getElementById("chart"));
2024-08-14 19:48:58 +01:00
const filename = "final__hashrate.json";
const periods = [
"all time",
"last 7 days",
"last 28 days",
"last 365 days",
"last 2 years",
];
2024-08-01 14:06:16 +01:00
2024-08-14 19:48:58 +01:00
async function fetchDataForChart(selectedValue) {
2024-08-06 12:03:42 +01:00
try {
2024-08-14 19:48:58 +01:00
const apiEndpoint = `${apiURL}/get_json/${filename}?period=${selectedValue}`;
2024-08-06 12:03:42 +01:00
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,
tooltip: {
2024-08-08 11:53:39 +01:00
...tooltip,
2024-08-01 14:06:16 +01:00
valueFormatter(value, index) {
return nFormatter(value, 0);
},
},
toolbox: toolboxParams,
xAxis: {
data: dataArr.map((row) => row.date),
axisTick: axisTick,
axisLabel: axisLabel,
axisLine: axisLine,
},
grid: grid,
2024-08-14 19:48:58 +01:00
dataZoom: dataZoom(0),
2024-08-01 14:06:16 +01:00
yAxis: [
{
type: "value",
name: "Hashrate (H/s)",
nameLocation: "middle",
2024-08-08 14:28:01 +01:00
nameGap: 30,
2024-08-08 11:53:39 +01:00
nameTextStyle: textStyleMain,
2024-08-01 14:06:16 +01:00
position: "left",
alignTicks: true,
axisTick: axisTick,
splitLine: {
show: false,
},
axisLine: axisLine,
axisLabel: {
fontSize: 12 * fontScale,
2024-08-08 11:53:39 +01:00
color: textColor,
2024-08-01 14:06:16 +01:00
formatter(value, index) {
return nFormatter(value, 0);
},
},
},
{
type: "value",
name: "Difficulty",
nameLocation: "middle",
2024-08-08 11:53:39 +01:00
nameGap: 30,
nameTextStyle: textStyleMain,
2024-08-01 14:06:16 +01:00
axisTick: axisTick,
position: "right",
alignTicks: true,
axisLine: axisLine,
splitLine: {
show: false,
},
axisLabel: {
fontSize: 12 * fontScale,
2024-08-08 11:53:39 +01:00
color: textColor,
2024-08-01 14:06:16 +01:00
formatter(value, index) {
return nFormatter(value, 0);
},
},
},
],
series: [
{
type: "line",
name: "Hashrate",
barCategoryGap: "0%",
data: dataArr.map((row) => row.hashrate),
},
{
type: "line",
color: "red",
id: "rolling-average",
name: "MA28",
showSymbol: false,
barCategoryGap: "0%",
data: dataArr.map((row) => row.hashrate28),
},
{
type: "line",
yAxisIndex: 1,
name: "Difficulty",
barCategoryGap: "0%",
data: dataArr.map((row) => row.difficulty),
},
],
};
myChart.setOption(option);
}
2024-08-14 19:48:58 +01:00
document.addEventListener("DOMContentLoaded", function () {
let periodIndex = 2;
periodDropdown(periods, periodIndex);
fetchDataForChart(periods[periodIndex]);
const selectElement = document.getElementById("select-period-dropdown");
selectElement.addEventListener("change", function (event) {
const selectedValue = event.target.value;
fetchDataForChart(selectedValue);
});
});