Skip to content

Instantly share code, notes, and snippets.

@ramnathv
Created July 18, 2013 13:36
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ramnathv/6029350 to your computer and use it in GitHub Desktop.
Save ramnathv/6029350 to your computer and use it in GitHub Desktop.
rCharts + DAT.GUI
<!doctype HTML>
<meta charset = 'utf-8'>
<html>
<head>
<link rel='stylesheet' href="http://netdna.bootstrapcdn.com/bootswatch/2.3.1/cosmo/bootstrap.min.css">
<link rel="stylesheet" href="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.1/css/bootstrap-responsive.min.css" >
<link rel='stylesheet' href="http://twitter.github.io/bootstrap/assets/js/google-code-prettify/prettify.css">
<link rel='stylesheet' href="http://aozora.github.io/bootplus/assets/css/docs.css">
<link rel='stylesheet' href='http://nvd3.org/src/nv.d3.css'>
<script src='http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js' type='text/javascript'></script>
<script src='http://d3js.org/d3.v2.min.js' type='text/javascript'></script>
<script src='http://nvd3.org/nv.d3.js' type='text/javascript'></script>
<script src='http://nvd3.org/lib/fisheye.js' type='text/javascript'></script>
<style>
.rChart {
display: block
margin: auto auto;
width: 100%;
height: 400px;
}
</style>
</head>
<body ng-app>
<div class='container'>
<div class='row'>
<div class='span4' id='my-gui-container'>
</div>
<div class='span8'>
<div id='chartdc4b591f5dea' class='rChart nvd3'>
<svg></svg>
</div>
</div>
</div>
</div>
<script type='text/javascript'>
$(document).ready(function(){
var opts = {
"dom": "chartdc4b591f5dea",
"width": 700,
"height": 400,
"x": "Eye",
"y": "Freq",
"group": "Hair",
"type": "multiBarChart",
"id": "chartdc4b591f5dea"
},
data = [
{
"Hair": "Black",
"Eye": "Brown",
"Sex": "Male",
"Freq": 32
},
{
"Hair": "Brown",
"Eye": "Brown",
"Sex": "Male",
"Freq": 53
},
{
"Hair": "Red",
"Eye": "Brown",
"Sex": "Male",
"Freq": 10
},
{
"Hair": "Blond",
"Eye": "Brown",
"Sex": "Male",
"Freq": 3
},
{
"Hair": "Black",
"Eye": "Blue",
"Sex": "Male",
"Freq": 11
},
{
"Hair": "Brown",
"Eye": "Blue",
"Sex": "Male",
"Freq": 50
},
{
"Hair": "Red",
"Eye": "Blue",
"Sex": "Male",
"Freq": 10
},
{
"Hair": "Blond",
"Eye": "Blue",
"Sex": "Male",
"Freq": 30
},
{
"Hair": "Black",
"Eye": "Hazel",
"Sex": "Male",
"Freq": 10
},
{
"Hair": "Brown",
"Eye": "Hazel",
"Sex": "Male",
"Freq": 25
},
{
"Hair": "Red",
"Eye": "Hazel",
"Sex": "Male",
"Freq": 7
},
{
"Hair": "Blond",
"Eye": "Hazel",
"Sex": "Male",
"Freq": 5
},
{
"Hair": "Black",
"Eye": "Green",
"Sex": "Male",
"Freq": 3
},
{
"Hair": "Brown",
"Eye": "Green",
"Sex": "Male",
"Freq": 15
},
{
"Hair": "Red",
"Eye": "Green",
"Sex": "Male",
"Freq": 7
},
{
"Hair": "Blond",
"Eye": "Green",
"Sex": "Male",
"Freq": 8
}
]
var myChart = drawChart(opts, data)
var gui = new dat.GUI({ autoPlace: false }),
controls_json = {
"x": {
"name": "x",
"value": "Eye",
"values": [ "Eye", "Hair" ],
"label": "Select x :"
},
"group": {
"name": "group",
"value": "Hair",
"values": [ "Eye", "Hair" ],
"label": "Select group :"
},
"type": {
"name": "type",
"value": "multiBarChart",
"values": [ "multiBarChart", "multiBarHorizontalChart" ],
"label": "Select type :"
}
}
var customContainer = document.getElementById('my-gui-container');
customContainer.appendChild(gui.domElement);
var x = gui.add(opts, 'x',
controls_json['x'].values)
x.onChange(function(value){
opts['x'] = value;
$('#chartdc4b591f5dea svg').empty();
drawChart(opts, data)
})
var group = gui.add(opts, 'group',
controls_json['group'].values)
group.onChange(function(value){
opts['group'] = value;
$('#chartdc4b591f5dea svg').empty();
drawChart(opts, data)
})
var type = gui.add(opts, 'type',
controls_json['type'].values)
type.onChange(function(value){
opts['type'] = value;
$('#chartdc4b591f5dea svg').empty();
drawChart(opts, data)
})
});
function drawChart(opts, data){
var data = d3.nest()
.key(function(d){
return opts.group === undefined ? 'main' : d[opts.group]
})
.entries(data)
var chart = nv.models[opts.type]()
.x(function(d) { return d[opts.x] })
.y(function(d) { return d[opts.y] })
.width(opts.width)
.height(opts.height)
d3.select("#" + opts.id + ' svg')
.datum(data)
.transition().duration(500)
.call(chart);
nv.utils.windowResize(chart.update);
return chart
};
</script>
<script src='http://dat-gui.googlecode.com/git/build/dat.gui.min.js'></script>
</body>
<!-- Google Prettify -->
<script src="http://cdnjs.cloudflare.com/ajax/libs/prettify/188.0.0/prettify.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js"></script>
<script
src='https://google-code-prettify.googlecode.com/svn-history/r232/trunk/src/lang-r.js'>
</script>
<script>
var pres = document.getElementsByTagName("pre");
for (var i=0; i < pres.length; ++i) {
pres[i].className = "prettyprint linenums";
}
prettyPrint();
</script>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment