Skip to content

Instantly share code, notes, and snippets.

@badosa
Last active October 2, 2022 17:16
Inflation in Spain

The Spanish Statistical Office (INE) has an API that follows the arrobj pattern (an array of objects for data), meaning: it can be converted to JSON-stat with fromTable(). The data is visualized with ChartJS.

This conversion is not strictly necessary because INE also supports JSON-stat. For an example using INE's JSON-stat API see Latest unemployment rate in Spain.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<script src="https://cdn.jsdelivr.net/combine/npm/jsonstat-toolkit@latest,npm/jsonstat-suite@latest"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.2/Chart.js"></script>
</head>
<body>
<div class="chart-container" style="position: relative; height:90vh; width:90vw">
<canvas id="chart"></canvas>
</div>
<script>
var
url="https://servicios.ine.es/wstempus/js/EN/DATOS_SERIE/IPC206448?nult=72",
ctx=document.getElementById("chart").getContext("2d")
;
function main(data){
var
label=data.Nombre,
data=data.Data,
time=data.map(function(e){ return e.Anyo+"M"+e.FK_Periodo; }),
ds=JSONstat(JSONstatUtils.fromTable(data,
{
type: "arrobj",
vlabel: "Valor",
drop: ["FK_TipoDato", "FK_Periodo", "Anyo", "Secreto"]
}
)),
myChart=new Chart(ctx, {
type: "line",
data: {
labels: time,
datasets: [{
label: label,
data: ds.value,
borderColor:"rgb(0, 139, 0)",
lineTension: 0,
fill: false,
pointRadius: 0
}]
},
options: {
title: {
display: true,
fontSize: 20,
text: "Consumer Price Index in Spain"
},
legend: {
display: true,
},
scales: {
yAxes: [{
ticks: {
beginAtZero: true
}
}],
xAxes: [{
display: true
}]
}
}
});
}
fetch( url )
.then(function(resp) {
resp.json().then(main);
})
;
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment