Skip to content

Instantly share code, notes, and snippets.

@badosa
Last active November 21, 2019 05:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save badosa/a7f539bd1f367f8d5d3f to your computer and use it in GitHub Desktop.
Save badosa/a7f539bd1f367f8d5d3f to your computer and use it in GitHub Desktop.
Dano-Norwegian Inflation

Statistics Denmark and Statistics Norway support the JSON-stat format. In this example, CPI (12-month rate) is retrieved from both sources, processed with the JSON-stat Javascript Toolkit and finally visualized with Idescat Visual.

Because data are retrieved asynchronously and the chart must wait to have all data, in this example, JSONstat() is called inside JSONstat(). This works in this simple scenario because only two different sources of data are used. For a more generalized approach, see Inflation in different countries.

<!--[if lt IE 7]><html class="lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
<!--[if IE 7]><html class="lt-ie9 lt-ie8"> <![endif]-->
<!--[if IE 8]><html class="lt-ie9"> <![endif]-->
<!--[if gt IE 8]><!--> <html> <!--<![endif]-->
<head>
<title>Dano-Norwegian Inflation</title>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<!-- DO NOT DO THIS IN PRODUCTION -->
<!-- js.org and json-stat.org ARE NOT CDNs! Link to your own copies or to a CDN. -->
<link href="https://visual.js.org/visual.css" rel="stylesheet" type="text/css" />
<script src="https://cdn.jsdelivr.net/npm/jsonstat@0.13.13"></script>
<script src="https://visual.js.org/lazyvisualsetup.js"></script>
<!-- /DO NOT DO THIS IN PRODUCTION -->
</head>
<body>
<section id="visual" class="visual"></section>
<script>
var
years=5,
months=years*12,
dk={
label: "Denmark",
url: "https://api.statbank.dk/v1/data/PRIS111/JSONSTAT?lang=en&VAREGR=000000&ENHED=300&Tid=(-n%2B"+months+")",
},
no={
label: "Norway",
url: "https://data.ssb.no/api/v0/dataset/1086.json?lang=en",
},
message=function(s){
document.getElementById("visual").innerHTML="Travelling to <strong>"+s+"</strong> to get some data...";
}
;
message(dk.label);
JSONstat(
dk.url,
function(){
dk.ds=this.Dataset(0);
message(no.label);
JSONstat(
no.url,
function(){
no.ds=this.Dataset(0);
dk.time=dk.ds.Dimension(dk.ds.role.time[0]).id;
no.time=no.ds.Dimension(no.ds.role.time[0]).id;
var
/* Time criterion: Denmark's time periods */
time=dk.time.map(function(e){ return e.replace(/M/,""); }),
first=no.time.indexOf(dk.time[0]),
count=0
;
/* dk: dataset has all dimensions constant but time */
dk.data=dk.ds.value;
/* no: we need to select indicador "Tolvmanedersendring" (12-month rate) */
no.data=no.ds.Data({"ContentsCode": "Tolvmanedersendring"}, false)
/* Time criterion: Denmark's time periods */
.filter(function(e,i){ return i>=first && count++<months; })
;
/* If Norway is not so up-to-date as Denmark, add nulls */
for(var miss=months-no.data.length; miss--;){
no.data.push(null);
}
/* Line chart */
visual({
lang: "en",
title: "Consumer Price Index. 12-month rate",
geo: dk.label+" and "+no.label,
time: time,
footer: "Source: "+dk.ds.source+", "+no.ds.source+".",
unit: {label: "%"},
dec: 1,
grid: {
line: 5,
shadow: 6,
point: 0
},
type: "tsline",
data: [
{ label: dk.label, val: dk.data },
{ label: no.label, val: no.data }
]
});
}
);
}
);
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment