function createChart( chart, endpoint, xAxisType, scaleChart = false, ) { async function fetchDataForChart(query) { try { const apiEndpoint = `${apiURL}/${endpoint}?${query}`; const response = await fetch(apiEndpoint); if (!response.ok) { throw new Error(`HTTP error! status: ${response.status}`); } const chartData = await response.json(); console.log(chartData) updateChart(chartData); } catch (error) { console.error("Fetching data failed:", error); } } function updateChart(chartData) { var option = { tooltip: { ...tooltip, }, xAxis: { type: xAxisType, data: chartData.map((item) => item.date), }, yAxis: [ { type: "value", name: "Rewards (USD)", nameLocation: "middle", nameGap: 30, nameTextStyle: textStyleMain, position: "left", alignTicks: true, axisTick: axisTick, axisLine: axisLine, splitLine: { show: false, }, axisLabel: { ...axisLabel, formatter(value, index) { return nFormatter(value, 0); }, }, }, { type: "value", name: "Subsidy (BTC)", nameLocation: "middle", nameGap: 20, nameTextStyle: { fontSize: 12 * fontScale, color: textColor, }, axisTick: axisTick, position: "right", alignTicks: true, axisLine: axisLine, splitLine: { show: false, }, axisLabel: { fontSize: 12 * fontScale, color: textColor, }, }, ], series: [ { type: "line", name: "Subsidy (USD)", stack: "Total", areaStyle: {}, symbol: "none", lineStyle: { width: 0, }, data: chartData.map((row) => row.subsidy_usd), }, { type: "line", name: "Fees (USD)", stack: "Total", areaStyle: {}, symbol: "none", lineStyle: { width: 0, }, data: chartData.map((row) => row.totalfee_usd), }, { type: "line", name: "Total (USD)", symbol: "none", lineStyle: { width: 1, color: textColor, }, data: chartData.map((row) => row.total_reward_usd), }, { type: "line", yAxisIndex: 1, name: "Block Subsidy (BTC)", symbol: "none", lineStyle: { width: 3, }, data: chartData.map((row) => row.block_subsidy), }, ], }; chart.setOption(option, true); } // listen for filter events for this target document.addEventListener("filterChange", function(event) { eventDetail = event.detail; if (eventDetail.filterActions.includes("refresh")) { chartData = []; query = queryConstructor(); fetchDataForChart(query); updateChart(chartData); } }); let query = queryConstructor(); fetchDataForChart(query); } document.addEventListener("DOMContentLoaded", function() { chartData = []; let chartDom = document.getElementById("miner-rewards-chart"); let myChart = echarts.init(chartDom); const filterPeriods = { "7 day": 7, "28 day": 28, "365 day": 365, "2 years": 730, "5 years": 1826, "10 years": 3652, "all time": 10000 }; dropDownFilter("days_ago_dropdown_filter", "days_ago", ["miner-rewards-chart"], filterPeriods, "365 day") createChart( chart = myChart, endpoint = "miner_rewards", xAxisType = "category", ); });