This example shows how to use fromCSV() to convert a CSV file into JSON-stat so it can be then processed by the JSON-stat Javascript Toolkit.
Last active
November 21, 2019 05:29
fromCSV() example
This file contains 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> | |
<!--[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>Visual + JSON-stat: line chart from CSV</title> | |
<meta http-equiv="X-UA-Compatible" content="IE=edge" /> | |
<!-- DO NOT DO THIS IN PRODUCTION --> | |
<!-- js.org and json-stat.org ARE NOT CDNs! Link to your own copies or to 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/combine/npm/jsonstat@0.13.13,npm/jsonstat-utils@2.5.5"></script> | |
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script> | |
</head> | |
<body> | |
<section id="visual" class="visual"></section> | |
<script> | |
$.ajax( { url: "https://data.ssb.no/api/v0/dataset/1086.csv?lang=en" } ) | |
.done(function( csv ) { | |
console.log(csv); | |
console.log(JSONstatUtils.fromCSV( csv )); | |
console.log(JSONstat( JSONstatUtils.fromCSV( csv ) )); | |
var | |
ds=JSONstat( JSONstatUtils.fromCSV( csv ) ), | |
cpi=ds.Data( { "contents" : "12-month rate (per cent)" }, false ), | |
time=ds.Dimension( "month" ).id.map( function( t ){ | |
return t.replace( /M/, "" ); | |
}); | |
; | |
visual({ | |
lang: "en", | |
title: "Consumer Price Index", | |
footer: "Source: Statistics Norway. Dataset 1086.", | |
geo: "Norway", | |
unit: { "symbol" : "%" }, | |
grid: { point: 0, shadow: 0 }, | |
axis: { x: false }, | |
dec: 1, | |
type: "tsline", | |
time: time, | |
data: [ { label: "12-month rate", val: cpi } ] | |
}); | |
}); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment