Compare commits

..

5 Commits

Author SHA1 Message Date
Sam e9cd27d5c4 add protected mangrove content 2024-09-22 14:29:03 +01:00
Sam ce58616cbf add sortable to head 2024-09-22 14:28:52 +01:00
Sam 886abd21c8 add chart shortcode that responds to selectable table 2024-09-22 14:28:25 +01:00
Sam 2d21aa98fc add more routes to backend 2024-09-22 14:27:43 +01:00
Sam 0e6e19d483 selectable tables that generate events 2024-09-22 14:27:13 +01:00
10 changed files with 310 additions and 77 deletions

View File

@ -1,5 +1,29 @@
aggregate_mangrove_by_country = [ def mangrove_by_country_latest():
pipeline = [
{ {
"$match": {"year": "2020"}, "$match": {"year": "2020"},
}, },
] ]
return pipeline
def mangrove_by_country_agg(query):
pipeline = [
{"$match": {"country_with_parent": query["country_with_parent"]}},
{
"$group": {
"_id": {"country_with_parent": "$country_with_parent", "year": "$year"},
"total_pixels": {"$sum": "$total_n_pixels"},
}
},
{
"$project": {
"_id": 0,
"country_with_parent": "$_id.country_with_parent",
"year": "$_id.year",
"total_pixels": 1,
}
},
{"$sort": {"year": 1}},
]
return pipeline

View File

@ -4,18 +4,45 @@ from models.mongodb_handler import MongoDBHandler
import models.pipelines as pipelines import models.pipelines as pipelines
import schema.schemas as schemas import schema.schemas as schemas
from schema.schemas import DataSerializer from schema.schemas import DataSerializer
import ast
router = APIRouter() router = APIRouter()
@router.get("/mangrove_country_data") @router.get("/mangrove_by_country_latest")
async def mangrove_country_data(): async def mangrove_by_country_latest():
db = client.baseddata db = client.baseddata
collection_name = db["final__protected_mangroves_summary_stats_by_country_agg"] collection_name = db["final__protected_mangroves_summary_stats_by_country_agg"]
schema = schemas.mangrove_country_schema schema = schemas.mangrove_by_country_latest_schema
query = pipelines.aggregate_mangrove_by_country pipeline = pipelines.mangrove_by_country_latest()
serializer = DataSerializer(schema) serializer = DataSerializer(schema)
handler = MongoDBHandler(collection_name) handler = MongoDBHandler(collection_name)
rawData = handler.aggregate(query) rawData = handler.aggregate(pipeline)
serializedData = serializer.serialize_many(rawData)
return serializedData
@router.get("/mangrove_by_country_agg")
async def mangrove_by_country_agg(query: str):
query = ast.literal_eval(query)
db = client.baseddata
collection_name = db["final__protected_mangroves_summary_stats_by_country_agg"]
schema = schemas.mangrove_by_country_agg_schema
pipeline = pipelines.mangrove_by_country_agg(query)
serializer = DataSerializer(schema)
handler = MongoDBHandler(collection_name)
rawData = handler.aggregate(pipeline)
serializedData = serializer.serialize_many(rawData)
return serializedData
@router.get("/bitcoin_business_growth")
async def bitcoin_business_growth(query: str):
query = ast.literal_eval(query)
db = client.baseddata
collection_name = db["final__bitcoin_business_growth_by_country"]
schema = schemas.bitcoin_business_growth
pipeline = pipelines.bitcoin_business_growth(query)
serializer = DataSerializer(schema)
handler = MongoDBHandler(collection_name)
rawData = handler.aggregate(pipeline)
serializedData = serializer.serialize_many(rawData) serializedData = serializer.serialize_many(rawData)
return serializedData return serializedData

View File

@ -1,4 +1,4 @@
def mangrove_country_schema(data): def mangrove_by_country_latest_schema(data):
return { return {
"country_with_parent": str(data["country_with_parent"]), "country_with_parent": str(data["country_with_parent"]),
"original_pixels": int(data["original_pixels"]), "original_pixels": int(data["original_pixels"]),
@ -7,6 +7,20 @@ def mangrove_country_schema(data):
"cumulative_pct_diff": float(data["cumulative_pct_diff"]), "cumulative_pct_diff": float(data["cumulative_pct_diff"]),
} }
def mangrove_by_country_agg_schema(data):
return {
"country_with_parent": str(data["country_with_parent"]),
"year": int(data["year"]),
"total_pixels": int(data["total_pixels"])
}
def bitcoin_business_growth_schema(data):
return {
"country_name": str(data["country_name"]),
"cumulative_current_value": str(data["cumulative_current_value"]),
"year": int(data["year"]),
"total_pixels": int(data["total_pixels"])
}
class DataSerializer: class DataSerializer:
def __init__(self, schema_func): def __init__(self, schema_func):

View File

@ -9,5 +9,6 @@ tags: ["Bitcoin", "Stats"]
script: "/js/mangrove-map.js" script: "/js/mangrove-map.js"
--- ---
{{< table id="mangrove_countries" endpoint="mangrove_country_data" headers="{'country_with_parent': 'Country', 'original_pixels': '1996 Cover', 'total_n_pixels': '2020 Cover', 'cumulative_pixels_diff': 'Diff', 'cumulative_pct_diff': '% Diff'}" maxHeight="400px" sortable="true">}} {{< table id="mangrove_countries" endpoint="mangrove_by_country_latest" headers="{'country_with_parent': 'Country', 'original_pixels': '1996 Cover', 'total_n_pixels': '2020 Cover', 'cumulative_pixels_diff': 'Diff', 'cumulative_pct_diff': '% Diff'}" maxHeight="400px" sortable="true" valueId="country_with_parent" selectableRows="single" >}}
{{< chart id="mangrove_countries" endpoint="mangrove_by_country_agg" chartType="bar" xAxisField="year" yAxisField="total_pixels" sortField="year" scaleChart=true >}}
{{< map id="map" style="https://tiles.semitamaps.com/styles/maptiler-basic/style.json">}} {{< map id="map" style="https://tiles.semitamaps.com/styles/maptiler-basic/style.json">}}

View File

@ -2,7 +2,95 @@
<section class="chart-container"> <section class="chart-container">
<div id="chart-modifiers"></div> <div id="chart-modifiers"></div>
<div id="chart"> <div id="chart">
<script src="{{ .src }}"></script> <script>
chartData = [];
function createChart(
id,
endpoint,
chartType,
xAxisField,
yAxisField,
sortField = null,
scaleChart = false,
) {
async function fetchDataForChart(query, valueId) {
try {
const apiEndpoint = `${apiURL}/${endpoint}?query=${query}`;
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);
}
}
function updateChart() {
console.log(chartData);
let chartDataMap = new Map();
for (let objectId in chartData) {
chartDataMap.set(objectId, chartData[objectId]);
}
var chartDom = document.getElementById("chart");
var myChart = echarts.init(chartDom);
// // Sort the data by year
// if (sortField) {
// data.sort((a, b) => a[sortField] - b[sortField]);
// }
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> </div>
</section> </section>
<script src="/js/chart-params.js"></script> <script src="/js/chart-params.js"></script>

View File

@ -8,6 +8,7 @@
<link rel="stylesheet" href="/css/toc.css" type="text/css" media="all" /> <link rel="stylesheet" href="/css/toc.css" type="text/css" media="all" />
<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>
<link <link
rel="stylesheet" rel="stylesheet"
href="/css/codeblock.css" href="/css/codeblock.css"

120
layouts/partials/table.html Normal file
View File

@ -0,0 +1,120 @@
<script>
function createTable(
endpoint,
id,
headers,
maxHeight,
sortable,
valueId,
selectableRows,
) {
async function fetchDataForTable() {
try {
const apiEndpoint = `${apiURL}/${endpoint}`;
const response = await fetch(apiEndpoint);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const fetchedData = await response.json();
data = fetchedData;
generateTable(data);
} catch (error) {
console.error("Fetching data failed:", error);
}
}
function generateTable(data) {
const jsonTableContainer = document.getElementById(
`${id}-table-container`,
);
jsonTableContainer.className = "jsonTableContainer";
jsonTableContainer.innerHTML = "";
jsonTableContainer.style.maxHeight = maxHeight;
tableHeaderNames = Object.values(headers);
tableHeaderKeys = Object.keys(headers);
const table = document.createElement("table");
table.id = id;
const thead = document.createElement("thead");
const tbody = document.createElement("tbody");
const headerRow = document.createElement("tr");
tableHeaderNames.forEach((header) => {
const th = document.createElement("th");
th.textContent = header;
headerRow.appendChild(th);
});
thead.appendChild(headerRow);
table.appendChild(thead);
for (const key in data) {
const row = document.createElement("tr");
row.value = data[key][valueId];
tableHeaderKeys.forEach((columnName) => {
const td = document.createElement("td");
const div = document.createElement("div");
div.id = "scrollable";
div.textContent = data[key][columnName];
td.appendChild(div);
row.appendChild(td);
tbody.appendChild(row);
});
}
table.appendChild(thead);
table.appendChild(tbody);
// selectable rows
let currentlySelectedRow = null;
if (selectableRows === "multi" || selectableRows === "single") {
const rows = table.getElementsByTagName("tr");
for (let i = 1; i < rows.length; i++) {
// Skip the header row
rows[i].addEventListener("click", function () {
if (selectableRows === "single") {
// Deselect the currently selected row, if any
if (currentlySelectedRow && currentlySelectedRow !== this) {
currentlySelectedRow.classList.remove("selected");
const deselectEvent = new CustomEvent("rowDeselected", {
detail: { row: currentlySelectedRow },
});
document.dispatchEvent(deselectEvent);
}
// Toggle the selected class on the clicked row
this.classList.toggle("selected");
// Update the currently selected row
currentlySelectedRow = this.classList.contains("selected")
? this
: null;
} else {
// Multi-select behavior
this.classList.toggle("selected");
}
const event = new CustomEvent(
this.classList.contains("selected")
? "rowSelected"
: "rowDeselected",
{
detail: { row: this, selectableRows: selectableRows, valueId: valueId},
},
);
document.dispatchEvent(event);
});
}
}
jsonTableContainer.appendChild(table);
if (sortable == "true") {
table.className = "sortable";
sorttable.makeSortable(document.getElementById(id));
}
}
fetchDataForTable();
}
</script>

View File

@ -1,2 +1,8 @@
{{ $id := .Get "src" | md5 }} {{ partial "chart.html" (dict "src" (.Get "src") {{ partial "chart.html" }}
"id" $id) }} <div id = '{{ .Get "id" }}-chart-container'>
<script>
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" }})
});
</script>
</div>

View File

@ -1,63 +1,8 @@
{{ partial "table.html" }}
<div id = '{{ .Get "id" }}-table-container'>
<script> <script>
async function fetchDataForTable() { document.addEventListener("DOMContentLoaded", function () {
try { createTable({{ .Get "endpoint" }}, {{ .Get "id" }}, {{ .Get "headers" | safeJS }}, {{ .Get "maxHeight" }}, {{ .Get "sortable" }}, {{ .Get "valueId" }}, {{ .Get "selectableRows" }})
const apiEndpoint = `${apiURL}/{{ .Get "endpoint" }}`; });
const response = await fetch(apiEndpoint);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const fetchedData = await response.json();
data = fetchedData;
console.log(data);
createTable(data);
} catch (error) {
console.error("Fetching data failed:", error);
}
}
function createTable(data) {
const jsonTableContainer = document.getElementById("jsonTableContainer");
jsonTableContainer.innerHTML = "";
jsonTableContainer.style.maxHeight = "{{ .Get `maxHeight` }}"
tableHeaderNames = Object.values({{ .Get `headers` | safeJS }});
tableHeaderKeys = Object.keys({{ .Get `headers` | safeJS }});
const table = document.createElement("table");
table.id = "{{ .Get `id` }}"
const thead = document.createElement("thead");
const tbody = document.createElement("tbody");
const headerRow = document.createElement("tr");
tableHeaderNames.forEach((header) => {
const th = document.createElement("th");
th.textContent = header;
headerRow.appendChild(th);
});
thead.appendChild(headerRow);
table.appendChild(thead);
for (const key in data) {
const row = document.createElement('tr');
tableHeaderKeys.forEach((columnName) => {
const td = document.createElement("td");
const div = document.createElement("div");
div.id = "scrollable";
div.textContent = data[key][columnName];
td.appendChild(div);
row.appendChild(td);
tbody.appendChild(row);
});
}
table.appendChild(thead);
table.appendChild(tbody);
jsonTableContainer.appendChild(table)
{{ if eq (.Get "sortable") "true" }}
table.className = "sortable"
sorttable.makeSortable(document.getElementById("{{ .Get `id` }}"));
{{ end }}
table.className = ""
}
</script> </script>
<div id="jsonTableContainer"></div> </div>
<script src="/js/lib/sorttable.js"></script>

View File

@ -1,5 +1,5 @@
/* Tables */ /* Tables */
#jsonTableContainer { .jsonTableContainer {
display: flex; display: flex;
overflow-y: auto; overflow-y: auto;
} }
@ -49,3 +49,10 @@ tr:nth-child(odd) {
background-color: var(--table-odd-row-bg-color); background-color: var(--table-odd-row-bg-color);
font-size: var(--table-row-font-size); font-size: var(--table-row-font-size);
} }
tr:hover {
background-color: #f5f5f5;
}
tr.selected {
background-color: #d1ecf1;
}