An example of how to connect a JSON-stat response with Google Visualization API using the toTable() method.
Last active
November 21, 2019 05:08
OECD Unemployment Choropleth
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<script src="https://cdn.jsdelivr.net/npm/jsonstat@0.13.13" type="text/javascript"></script> | |
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script> | |
<script type='text/javascript' src='https://www.google.com/jsapi'></script> | |
<style type="text/css"> | |
body { | |
font-family: verdana; | |
font-size: 80%; | |
} | |
strong { | |
color: green; | |
} | |
</style> | |
</head> | |
<body> | |
<div id="sel"></div> | |
<div id="dataviz"></div> | |
<script type="text/javascript"> | |
google.load('visualization', '1', {packages:['geochart']}); | |
google.setOnLoadCallback(init); | |
function init(){ | |
JSONstat("https://json-stat.org/samples/oecd.json", function(){ | |
if (this.length>0){ | |
var | |
deftime="2012", | |
ds=this.Dataset("oecd"), //select the OECD dataset | |
time=ds.Dimension({role: "time"})[0], //get the time dimension information | |
tbl=ds.toTable({type:"object"}), //convert to object table | |
data=new google.visualization.DataTable(tbl,0.6), | |
map=function(data,defvalue){ | |
var mapview=new google.visualization.DataView(data); | |
mapview.setRows(mapview.getFilteredRows([{column: 2, value: defvalue}])); | |
mapview.setColumns([1,3]); | |
new google.visualization.GeoChart(document.getElementById("dataviz")).draw(mapview); | |
}, | |
selec=function(dim, selected){ | |
var o=''; | |
for(var i=0, len=dim.length; i<len; i++){ | |
var | |
l=dim.Category(dim.id[i]).label, | |
s=(selected!=l) ? "" : 'selected="selected"' | |
; | |
o+="<option "+s+">"+l+"</option>"; | |
} | |
return '<strong>Year</strong> <select id="time">'+o+"</select>"; | |
} | |
; | |
$("#sel").html(selec(time, deftime)); | |
map(data,deftime); | |
$("#time").change(function() { | |
map(data,$(this).val()); | |
}); | |
}else{ | |
$("#dataviz").html("No valid JSON-stat!"); | |
} | |
}); | |
} | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment