Skip to content

Commit 4d04b9b

Browse files
committed
Spend by month front page chart
1 parent ff60547 commit 4d04b9b

File tree

5 files changed

+73
-6
lines changed

5 files changed

+73
-6
lines changed

.eleventy.js

+13-5
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,30 @@ module.exports = function (eleventyConfig) {
2121
return spendCollection;
2222
});
2323

24+
eleventyConfig.addCollection("spendsOver500_monthly", (collection) => {
25+
return _(collection.getAll()[0].data.spendsOver500).transform(function (result, value, key) {
26+
result.push({
27+
title: key,
28+
displayTitle: (new Date(`${key}-01`)).toLocaleString("en-GB", { month: "long", year: "numeric" }),
29+
total: _(value).sumBy("Transaction Amount")
30+
})
31+
}, []);
32+
})
33+
2434
eleventyConfig.addFilter("jsonify", function (value) { return JSON.stringify(value, null, 4) });
2535

2636
eleventyConfig.addFilter("fixed", function (value, length) { return value?.toFixed(length || 2) });
2737

2838
eleventyConfig.addFilter("sum", function (value, propName) {
29-
return value.reduce((accumulator, currentValue) => {
30-
return accumulator + currentValue[propName]
31-
}, 0);
39+
return _(value).sumBy(propName);
3240
});
3341

3442
const GBPFormat = new Intl.NumberFormat('en-GB', {
3543
style: 'currency',
3644
currency: 'GBP',
3745
});
38-
39-
function toCurrency(value){
46+
47+
function toCurrency(value) {
4048
return GBPFormat.format(value);
4149
}
4250

.vscode/launch.json

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"version": "0.2.0",
3+
"configurations": [
4+
{
5+
"name": "11ty",
6+
"type": "node",
7+
"request": "launch",
8+
"program": "${workspaceRoot}/node_modules/.bin/eleventy",
9+
"stopOnEntry": false,
10+
"args": [],
11+
"cwd": "${workspaceRoot}",
12+
"console": "internalConsole",
13+
}
14+
]
15+
}

_assets/js/charts.js

+31
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,36 @@ export function spendByDirectorate(jsonElementId, chartElementId) {
4141
enabled: false
4242
},
4343
});
44+
}
4445

46+
export function spendByMonth(jsonElementId, chartElementId) {
47+
const jsonData = JSON.parse(document.getElementById(jsonElementId).textContent);
48+
49+
const chart = Highcharts.chart(chartElementId, {
50+
chart: {
51+
type: 'line'
52+
},
53+
title: {
54+
text: 'Spends over £500 by month'
55+
},
56+
xAxis: {
57+
type: "category"
58+
},
59+
yAxis: {
60+
title: {text:"Spend in £"}
61+
},
62+
series: [{
63+
name: "Spend for month by all directorates",
64+
data: jsonData.map(x => [x.displayTitle, x.total])
65+
}],
66+
tooltip: {
67+
useHTML: true,
68+
formatter: function () {
69+
return `<p><strong>${this.point.name}</strong></p><p>${GBPFormat.format(this.y)}</p>`;
70+
}
71+
},
72+
credits: {
73+
enabled: false
74+
},
75+
});
4576
}

index.html index.njk

+13-1
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,15 @@
44
description: "An open data source of Scottish Government spending data"
55
---
66

7+
<script id="spend-data" type="application/json">
8+
{{ collections.spendsOver500_monthly | jsonify | safe }}
9+
</script>
10+
711
<h1>Welcome to Open Data Scotland GovSpend</h1>
812
<p class="lead">An open data source of Scottish Government spending data</p>
913

14+
<div id="chart-spend-by-month" style="width:100%; height:500px;"></div>
15+
1016
<h2>Spends over £500 per month</h2>
1117

1218
<ul>
@@ -15,4 +21,10 @@ <h2>Spends over £500 per month</h2>
1521
<a href="spends/{{spendData.title}}">{{spendData.displayTitle}} - {{spendData.data.length}} entries</a>
1622
</li>
1723
{% endfor %}
18-
</ul>
24+
</ul>
25+
26+
<script>
27+
document.addEventListener('DOMContentLoaded', function () {
28+
charts.spendByMonth("spend-data", "chart-spend-by-month");
29+
});
30+
</script>

spendover500.njk

+1
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ eleventyComputed:
8282
</div>
8383

8484
<div id="chart-spend-by-directorate" style="width:100%; height:600px;"></div>
85+
8586
<div class="table-responsive">
8687
<table class="table table-bordered table-striped">
8788
<thead>

0 commit comments

Comments
 (0)