Skip to content

Instantly share code, notes, and snippets.

@badosa
Last active November 22, 2019 08:39
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/8167da90df2f90796932526648c6c2f3 to your computer and use it in GitHub Desktop.
Save badosa/8167da90df2f90796932526648c6c2f3 to your computer and use it in GitHub Desktop.
Bankruptcies in Norway

This is an example of using the Slice() method.

Statistics Norway offers a dataset about bankruptcies by industry, municipality and time (dataset 66000). Slice() allows to freeze the time and industry dimensions and keep only what's relevant to build the choropleth map (municipality).

<!DOCTYPE html>
<!--[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>Slice Example</title>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<!-- DO NOT DO THIS IN PRODUCTION -->
<!-- JS.ORG IS NOT A CDN -->
<link href="https://visual.js.org/visual.css" rel="stylesheet" type="text/css" />
<script src="https://visual.js.org/lazyvisualsetup.js"></script>
<!-- /DO NOT DO THIS IN PRODUCTION -->
<script src="https://cdn.jsdelivr.net/npm/jsonstat@0.13.13"></script>
</head>
<body>
<div id="visual" class="visual">Retrieving a big dataset (around 200,000 data) from Statistics Norway...</div>
<script>
//Norway's bundle ID 66000:
//Bankruptcies, by industry and legal form. Municipalities, latest five quarters
var id="66000";
//Retrieve the bundle in English in the JSON-stat format
JSONstat("https://data.ssb.no/api/v0/dataset/"+id+".json?lang=en", function(){
//1. Parse the info
var
//Select the only dataset available in the bundle
ds=this.Dataset(0),
//Get the time dimension ID
t=ds.role.time[0],
//Store the time dimension info
tdim=ds.Dimension(t),
//Get the latest time period ID
last=tdim.id[tdim.length-1],
//Get the latest time label
lastlabel=tdim.Category(last).label,
//Improve time label (this shouldn't be necessary...)
time="Q"+lastlabel.substring(5,6)+" "+lastlabel.substring(0,4),
//Filter: all industries, last time period available
filter=[ ["NACE2007", "01-99"], [t, last] ],
//Apply filter to create a subset
//All dimensions in the new dataset are constant but "Region" (municipalities)
subset=ds.Slice( filter ),
//Get the municipalities IDs
mun=subset.Dimension( "Region" ).id,
//Get all the municipalities values
//with its IDs (Visual data format)
data=subset.value.map( function( e, i ){
return { id: mun[i], val: e };
})
;
document.getElementById("visual").innerHTML="Building the map. Just a sec...";
//2. Draw map with retrieved and transformed info
visual(
{
lang: "en",
title: "Bankruptcies",
time: time,
geo: "Norway",
footer: "Source: "+subset.source+" (dataset "+id+").",
dec: 0,
type: "cmap",
by: "norwaymun",
data: data
}
);
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment