Skip to content

Instantly share code, notes, and snippets.

@badosa
Last active November 21, 2019 05:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save badosa/5723134 to your computer and use it in GitHub Desktop.
Save badosa/5723134 to your computer and use it in GitHub Desktop.
Unemployment Patterns

Countries are ranked according to the latest unemployment rate (OECD data). The green ones are countries with a lower rate than OECD in the latest year available; the red ones are countries that have a higher rate than OECD. The lastest rate can be displayed placing the cursor on the chart. The color of the sparkline is a trend indicator: orange means that latest unemployment is not significantly higher or lower (10% change) than in 2006; red indicates increases bigger than 10% and green decreases bigger than 10%.

Built with JSON-stat, the JSON-stat Javascript Toolkit and Utilities Suite and Google Visualization API.

<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" media="all" href="/d/5723134/styles.css"/>
<script src="https://cdn.jsdelivr.net/combine/npm/jsonstat@0.13.13,npm/jsonstat-utils@2.5.5"></script>
<script src="https://www.google.com/jsapi"></script>
</head>
<body>
<div id="content"></div>
<script type="text/javascript">
google.load('visualization', '1', {packages:['corechart']});
var
url="https://stats.oecd.org/SDMX-JSON/data/LFS_SEXAGE_I_R/AUS+AUT+BEL+CAN+CHL+CZE+DNK+EST+FIN+FRA+DEU+GRC+HUN+ISL+IRL+ISR+ITA+JPN+KOR+LUX+MEX+NLD+NZL+NOR+POL+PRT+SVK+SVN+ESP+SWE+CHE+TUR+GBR+USA+OECD.MW.900000.UR.A/all?startTime=2006&dimensionAtObservation=allDimensions",
content=document.getElementById("content"),
html='<div class="info text">UN</div><div class="info text">EM</div><div class="info text">PL</div><div class="info text">OY</div><div class="info text">ME</div><div class="info text">NT</div><div class="info text">&nbsp;</div><div class="info text">PA</div><div class="info text">TT</div><div class="info text">ER</div><div class="info text">NS</div><div class="info text">&nbsp;</div>',
gap=0.10,
line=function(data,geo,trend, max){
var
opts={
colors: [trend],
enableInteractivity: false,
chartArea:{
height: 66,
width: 66
},
legend: {
position: 'none'
},
hAxis: {
textPosition: 'none'
},
vAxis: {
textPosition: 'none',
gridlines: {
color: 'none'
},
baselineColor: 'none',
maxValue: max,
minValue: 0
},
lineWidth: 3
},
lineview=new google.visualization.DataView(data)
;
lineview.setRows(lineview.getFilteredRows([{column: 0, value: geo}]));
lineview.setColumns([1, 6]);
new google.visualization.LineChart(document.getElementById(geo)).draw(lineview,opts);
},
init=function(){
fetch(url)
.then(function(resp) {
resp.json().then(function(json){
var
ds=JSONstat(JSONstatUtils.fromSDMX(json)),
max=ds.value.reduce(function(a, b) {
return Math.max(a, b);
}),
time=ds.Dimension("TIME_PERIOD").id,
last=time[time.length-1],
first=time[0],
oecdUR=ds.Data({COUNTRY: "OECD", TIME_PERIOD: last}).value,
area=ds.Dimension("COUNTRY"),
id=area.id,
table=ds.toTable({type:'object', content: 'id'}),
data=new google.visualization.DataTable(table,0.6),
sorted=id.filter(function(e){ return (e!=="OECD"); })
.map(function(e){
var
start=ds.Data({COUNTRY: e, TIME_PERIOD: first}).value,
end=ds.Data({COUNTRY: e, TIME_PERIOD: last}).value,
change=(end-start)/start,
trend=(change>gap) ? "#600610" : (change<(-1*gap)) ? "#004f00" : "orange"
oecd=(end<oecdUR) ? "green" : "red"
;
return {geo: e, end: end, trend: trend, oecd: oecd};
})
.sort(function(a,b){return a.end - b.end})
;
sorted.forEach(function(e){
var label=area.Category(e.geo).label;
html+='<div class="info '+e.oecd+'" title="'+label+'">'+e.geo+'</div><div class="spark" title="'+Math.round(e.end*10)/10+'" id="'+e.geo+'"></div>';
});
for(var c=0, len=last.length; c<len; c++){
html+='<div class="info text">'+last[c]+'</div>';
}
content.innerHTML=html;
sorted.forEach(function(e){
line(data,e.geo,e.trend, max);
});
});
})
;
}
;
google.setOnLoadCallback(init);
</script>
</body>
</html>
body {
width: 830px;
font-family: verdana, sans-serif;
background-color: #f0f0f0;
padding: 50px 65px;
}
#content div.spark, #content div.info {
float: left;
width: 66px;
border: 1px solid #eee;
margin-right: 1px;
}
#content div.spark {
height: 51px;
}
#content div.info {
height: 37px;
padding-top: 14px;
color: white;
text-align: center;
font-weight: bold;
font-size: 140%;
}
#content div.text {
background-color: black;
}
#content div.green {
background-color: #004f00;
}
#content div.red {
background-color: #600610;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment