This commit is contained in:
Sam 2024-09-23 17:46:59 +01:00
parent f279f5ade4
commit 5930fe90a1
8 changed files with 122 additions and 123 deletions

View File

@ -1,96 +1,97 @@
<script src="https://cdn.jsdelivr.net/npm/echarts@5.5.1/dist/echarts.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/echarts@5.5.1/dist/echarts.min.js"></script>
<section class="chart-container"> <script>
<div id="chart-modifiers"></div> chartData = [];
<div id="chart"> function createChart(
<script> id,
chartData = []; endpoint,
function createChart( chartType,
id, xAxisField,
endpoint, yAxisField,
chartType, sortField = null,
xAxisField, scaleChart = false,
yAxisField, ) {
sortField = null, async function fetchDataForChart(query, valueId) {
scaleChart = false, try {
) { const apiEndpoint = `${apiURL}/${endpoint}?${query}`;
async function fetchDataForChart(query, valueId) { const response = await fetch(apiEndpoint);
try { if (!response.ok) {
const apiEndpoint = `${apiURL}/${endpoint}?query=${query}`; throw new Error(`HTTP error! status: ${response.status}`);
const response = await fetch(apiEndpoint);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const fetchedData = await response.json();
const newData = fetchedData.reduce((acc, item) => {
const objectId = item[valueId];
if (!acc[objectId]) {
acc[objectId] = [];
}
acc[objectId].push([item[xAxisField], item[yAxisField]]);
return acc;
}, {});
chartData = { ...chartData, ...newData };
updateChart();
} catch (error) {
console.error("Fetching data failed:", error);
}
} }
const fetchedData = await response.json();
function updateChart() { const newData = fetchedData.reduce((acc, item) => {
console.log(chartData); const objectId = item[valueId];
let chartDataMap = new Map(); if (!acc[objectId]) {
for (let objectId in chartData) { acc[objectId] = [];
chartDataMap.set(objectId, chartData[objectId]);
} }
var chartDom = document.getElementById("chart"); acc[objectId].push([item[xAxisField], item[yAxisField]]);
var myChart = echarts.init(chartDom); return acc;
}, {});
// // Sort the data by year chartData = { ...chartData, ...newData };
// if (sortField) { updateChart();
// data.sort((a, b) => a[sortField] - b[sortField]); } catch (error) {
// } console.error("Fetching data failed:", error);
var option = {
xAxis: {
type: "category",
},
yAxis: {
scale: scaleChart,
type: "value",
},
series: Array.from(chartDataMap.entries()).map(([name, data]) => ({
name,
type: chartType,
data,
showSymbol: false,
})),
};
myChart.setOption(option, true);
}
document.addEventListener("rowSelected", (event) => {
console.log(
"Row selected:",
event.detail.row.offsetParent.id,
event.detail.row.value,
event.detail.valueId,
event.detail.selectableRows,
);
query = `{'country_with_parent': '${event.detail.row.value}'}`;
fetchDataForChart(query, event.detail.valueId);
});
document.addEventListener("rowDeselected", (event) => {
console.log(
"Row deselected:",
event.detail.row.offsetParent.id,
event.detail.row.value,
);
delete chartData[event.detail.row.value];
updateChart()
});
} }
</script> }
</div>
</section> function updateChart() {
console.log(chartData);
let chartDataMap = new Map();
for (let objectId in chartData) {
chartDataMap.set(objectId, chartData[objectId]);
}
var chartDom = document.getElementById(`${id}--chart`);
var myChart = echarts.init(chartDom);
var option = {
tooltip: {
...tooltip,
valueFormatter(value, index) {
return nFormatter(value, 0);
},
},
xAxis: {
type: "time",
},
yAxis: {
scale: scaleChart,
type: "value",
},
series: Array.from(chartDataMap.entries()).map(([name, data]) => ({
name,
type: chartType,
data,
showSymbol: false,
})),
};
myChart.setOption(option, true);
}
document.addEventListener("rowSelected", (event) => {
console.log(
"Row selected:",
event.detail.row.offsetParent.id,
event.detail.row.value,
event.detail.valueId,
event.detail.selectableRows,
);
valueId = event.detail.valueId
let selectedRow = {
[valueId]: event.detail.row.value,
};
query = queryConstructor(selectedRow);
fetchDataForChart(query, valueId);
});
document.addEventListener("rowDeselected", (event) => {
console.log(
"Row deselected:",
event.detail.row.offsetParent.id,
event.detail.row.value,
);
delete chartData[event.detail.row.value];
updateChart();
});
}
</script>
<script src="/js/chart-params.js"></script> <script src="/js/chart-params.js"></script>

View File

@ -9,6 +9,7 @@
<link rel="stylesheet" href="/css/articles.css" type="text/css" media="all" /> <link rel="stylesheet" href="/css/articles.css" type="text/css" media="all" />
<link rel="stylesheet" href="/css/charts.css" type="text/css" media="all" /> <link rel="stylesheet" href="/css/charts.css" type="text/css" media="all" />
<script src="/js/lib/sorttable.js"></script> <script src="/js/lib/sorttable.js"></script>
<script src="/js/lib/helper-functions.js"></script>
<link <link
rel="stylesheet" rel="stylesheet"
href="/css/codeblock.css" href="/css/codeblock.css"

View File

@ -8,9 +8,9 @@
valueId, valueId,
selectableRows, selectableRows,
) { ) {
async function fetchDataForTable() { async function fetchDataForTable(query) {
try { try {
const apiEndpoint = `${apiURL}/${endpoint}`; const apiEndpoint = `${apiURL}/${endpoint}?${query}`;
const response = await fetch(apiEndpoint); const response = await fetch(apiEndpoint);
if (!response.ok) { if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`); throw new Error(`HTTP error! status: ${response.status}`);
@ -25,7 +25,7 @@
function generateTable(data) { function generateTable(data) {
const jsonTableContainer = document.getElementById( const jsonTableContainer = document.getElementById(
`${id}-table-container`, `${id}--table-container`,
); );
jsonTableContainer.className = "jsonTableContainer"; jsonTableContainer.className = "jsonTableContainer";
jsonTableContainer.innerHTML = ""; jsonTableContainer.innerHTML = "";
@ -35,7 +35,7 @@
tableHeaderKeys = Object.keys(headers); tableHeaderKeys = Object.keys(headers);
const table = document.createElement("table"); const table = document.createElement("table");
table.id = id; table.id = `${id}--table`;
const thead = document.createElement("thead"); const thead = document.createElement("thead");
const tbody = document.createElement("tbody"); const tbody = document.createElement("tbody");
const headerRow = document.createElement("tr"); const headerRow = document.createElement("tr");
@ -89,7 +89,6 @@
currentlySelectedRow = this.classList.contains("selected") currentlySelectedRow = this.classList.contains("selected")
? this ? this
: null; : null;
} else { } else {
// Multi-select behavior // Multi-select behavior
this.classList.toggle("selected"); this.classList.toggle("selected");
@ -100,7 +99,11 @@
? "rowSelected" ? "rowSelected"
: "rowDeselected", : "rowDeselected",
{ {
detail: { row: this, selectableRows: selectableRows, valueId: valueId}, detail: {
row: this,
selectableRows: selectableRows,
valueId: valueId,
},
}, },
); );
@ -108,13 +111,21 @@
}); });
} }
} }
jsonTableContainer.appendChild(table); jsonTableContainer.appendChild(table);
// sortable
if (sortable == "true") { if (sortable == "true") {
table.className = "sortable"; table.className = "sortable";
sorttable.makeSortable(document.getElementById(id)); sorttable.makeSortable(document.getElementById(`${id}--table`));
} }
} }
fetchDataForTable();
document.addEventListener("filterChange", function (event) {
query = queryConstructor()
fetchDataForTable(query);
});
query = queryConstructor()
fetchDataForTable(query);
} }
</script> </script>

View File

@ -1,8 +1,10 @@
{{ partial "chart.html" }} {{ partial "chart.html" }}
<div id = '{{ .Get "id" }}-chart-container'> <section class = 'chart-container'>
<div class = "chart" id='{{ .Get "id" }}--chart'>
<script> <script>
document.addEventListener("DOMContentLoaded", function () { document.addEventListener("DOMContentLoaded", function () {
createChart(id={{ .Get "id" }}, endpoint={{ .Get "endpoint" }}, chartType={{ .Get "chartType" }}, xAxisField={{ .Get "xAxisField" }}, yAxisField={{ .Get "yAxisField" }}, sortField={{ .Get "sortField" }}, scaleChart={{ .Get "scaleChart" }}) createChart(id={{ .Get "id" }}, endpoint={{ .Get "endpoint" }}, chartType={{ .Get "chartType" }}, xAxisField={{ .Get "xAxisField" }}, yAxisField={{ .Get "yAxisField" }}, sortField={{ .Get "sortField" }}, scaleChart={{ .Get "scaleChart" }})
}); });
</script> </script>
</div> </div>
</section>

View File

@ -1,5 +1,5 @@
{{ partial "table.html" }} {{ partial "table.html" }}
<div id = '{{ .Get "id" }}-table-container'> <div id = '{{ .Get "id" }}--table-container'>
<script> <script>
document.addEventListener("DOMContentLoaded", function () { document.addEventListener("DOMContentLoaded", function () {
createTable({{ .Get "endpoint" }}, {{ .Get "id" }}, {{ .Get "headers" | safeJS }}, {{ .Get "maxHeight" }}, {{ .Get "sortable" }}, {{ .Get "valueId" }}, {{ .Get "selectableRows" }}) createTable({{ .Get "endpoint" }}, {{ .Get "id" }}, {{ .Get "headers" | safeJS }}, {{ .Get "maxHeight" }}, {{ .Get "sortable" }}, {{ .Get "valueId" }}, {{ .Get "selectableRows" }})

View File

@ -25,7 +25,7 @@ pkgs.mkShell
tmux new-session -d -s baseddata tmux new-session -d -s baseddata
tmux split-window -h tmux split-window -h
tmux send-keys -t 0 "hugo server" C-m tmux send-keys -t 0 "hugo server" C-m
tmux send-keys -t 1 "python backend/main.py" C-m tmux send-keys -t 1 "cd backend && uvicorn main:app --reload" C-m
echo "Baseddata running in dev tmux shell" echo "Baseddata running in dev tmux shell"
fi fi
''; '';

View File

@ -1,24 +1,11 @@
/* Charts */ /* Charts */
.chart-flex-container {
display: flex;
}
.chart-flex-container article {
flex: 1;
}
#chart {
width: 100%;
aspect-ratio: 16 / 9;
}
.chart-container { .chart-container {
margin-top: 20px;
display: flex; display: flex;
justify-content: center; height: 300px;
flex-direction: column;
} }
#chart-modifiers { .chart {
display: flex; display: flex;
height: 100%;
width: 100%;
} }

View File

@ -15,6 +15,3 @@ map.on("load", () => {
}); });
}); });
document.addEventListener("DOMContentLoaded", function () {
fetchDataForTable();
});