Skip to content

Instantly share code, notes, and snippets.

@badosa
Last active November 21, 2019 05:12
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/6482294 to your computer and use it in GitHub Desktop.
Save badosa/6482294 to your computer and use it in GitHub Desktop.
Traversing the Cube Tree

This sample code allows you to connect to a JSON-stat provider, get a JSON-stat response and show the datasets, dimensions and categories in that response. Three examples are provided but you can type your own JSON-stat address (for instance, try any of the Statistics Norway datasets).

Important: This sample code must be run in its own window.

This sample code uses .Dataset(), .Dimension() and .Category() without parameters for generalization's sake.

<!DOCTYPE html>
<html>
<head>
<title>Traversing the Cube Tree</title>
<link href="/d/6482294/style.css" type="text/css" rel="stylesheet" />
<script src="https://cdn.jsdelivr.net/npm/jsonstat@0.13.13"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js" ></script>
</head>
<body>
<div class="ds">Dataset</div> <div class="dim">Dimension</div> <div class="cat">Category</div>
<form>
<input type="text" value="https://json-stat.org/samples/oecd-canada-col.json" />
<button>connect</button>
<a title="Labor Force Data by County, 2012 Annual Averages (USA)" href="https://json-stat.org/samples/us-labor.json">us-labor.json</a> |
<a title="Persons by region, age and time (Norway)" href="https://data.ssb.no/api/v0/dataset/1080.json?lang=en">1080.json</a> |
<a title="Unemployment rate in the OECD and Population by sex and age group in Canada" href="https://json-stat.org/samples/oecd-canada-col.json">oecd-canada-col.json</a>
</form>
<div id="container"></div>
<script>
function load(e){
e.preventDefault();
if( e.target.tagName==="A" ){
$( "input" ).val( e.target.href );
}
var url = $( "input" ).val();
if( url.indexOf( "http" )===0 ){
read( url );
}
}
function read( o ){
document.getElementById( "container" ).innerHTML = '<ul><li><img src="https://json-stat.org/i/load.gif" alt="Loading..." /></li></ul>';
JSONstat( o, function(){
if( this.length ){
document.getElementById( "container" ).innerHTML = "<ul>" + list( "ds", this.Dataset(), "r" ) + "</ul>";
hijack( "ds", this.Dataset(), "r" ); //using () in JSON-stat for generalization's sake
}else{
window.alert( "A JSON-stat response was not returned." );
document.getElementById( "container" ).innerHTML = "";
}
})
}
function list( cl, e, tree ){
var ret = "";
for(var i=0, len=e.length; i<len; i++){
ret += '<li class="' + cl + ' ' + tree + '"><strong data-id="'+i+'">'+e[i].label+'</strong></li>';
}
return ret;
}
function hijack( cl, i, tree ){
var
next = ( cl === "ds" ) ? "dim" : "cat",
n = "." + cl + "." + tree + " strong"
;
$( n ).click( function(){
var
id = $( this ).data( "id" ),
c = tree + "-" + id
;
switch( cl ){
case "ds":
var e = i[id].Dimension(); //using () in JSON-stat for generalization's sake
break;
case "dim":
var e = i[id].Category(); //using () in JSON-stat for generalization's sake
break;
}
if( cl !== "cat" ){
if( !$( this ).parent().children("ul").length ){
$( this ).parent().append( "<ul>" + list( next, e, c ) + "</ul>" );
hijack( next, e, c );
}else{
$( this ).parent().children( "ul" ).toggle();
}
}
});
}
$("button, a").click( function(e){ load(e); } );
$("input").submit( function(e){ load(e); } );
</script>
</body>
</html>
body {
background-color: #f0f0f0;
}
ul {
list-style-type: none;
margin: 10px 0 0 20px;
padding: 0;
}
li {
margin: 2px;
padding: 4px;
}
strong, div {
color: white;
text-shadow: 0 -1px 0 rgba(0,0,0,.25);
}
strong, div, a {
white-space: nowrap;
}
strong, input, button, a, div {
padding: 4px 6px;
font-family: verdana, sans-serif;
font-size: 90%;
}
strong, input, div {
border-radius: 4px;
}
.ds > strong, .dim > strong {
cursor: pointer;
}
.ds > strong, div.ds {
background-color: #00b000;
}
.dim > strong, div.dim {
background-color: #c00000;
}
.cat > strong, div.cat {
background-color: #a0a0a0;
}
div, .cat {
display: inline-block;
}
div {
font-weight: bold;
}
form, div {
margin: 10px 0 0 26px;
}
input {
width: 400px;
}
#container {
display: block;
margin: 0;
padding: 0;
white-space: normal;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment