Skip to content

Instantly share code, notes, and snippets.

@badosa
Last active November 21, 2019 05:14
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/7856244 to your computer and use it in GitHub Desktop.
Save badosa/7856244 to your computer and use it in GitHub Desktop.
Norway's Choropleth Map

This code is based on the Norway JSON-stat API and Idescat Visual. The visualization uses the default Visual "filter" option (0.05) to avoid getting an all-white map (Norwegian population is concentrated in a few municipalities).

<!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>Norway JSON-stat API + Idescat Visual choropleth map example</title>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<!-- DO NOT DO THIS IN PRODUCTION -->
<!-- js.org and json-stat.org ARE NOT CDNs -->
<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>
<div id="visual" class="visual"></div>
<script>
//Select Norway JSON-stat bundle 1108
JSONstat("https://data.ssb.no/api/v0/dataset/1108.json?lang=en", function(){
//1. Get info
var
//Select the only dataset available in the bundle
ds=this.Dataset(0),
//Concept
//Folketallet11 = population at the end of quarter.
//Adapt to your needs. For exampleFolketilvekst10 = population growth
concept="Folketallet11",
//Get title
title=ds.Dimension("ContentsCode").Category(concept).label,
//Get time label for the only (latest) time available
tlabel=ds.Dimension("Tid").Category(0).label,
//Improve label (this shouldn't be necessary...)
time="Q"+tlabel.substring(5,6)+" "+tlabel.substring(0,4),
//Get municipalities IDs
mun=ds.Dimension( "Region" ).id,
//Get the values for the selected concept
//for all municipalities (free dimension).
//Time is a constant dimension.
//Then transform info to the Visual data format
data=ds.Data( { "ContentsCode": concept } ).map( function( e, i ){
return { id: mun[i], val: e.value };
})
;
//2. Draw map with retrieved and transformed info
visual(
{
lang: "en",
title: title,
time: time,
geo: "Norway",
footer: "Statistics Norway (dataset 1108)",
unit: { label: "people" },
legend: false,
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