Skip to content

Instantly share code, notes, and snippets.

@walkerke
Created August 18, 2013 02:24
Show Gist options
  • Save walkerke/6259621 to your computer and use it in GitHub Desktop.
Save walkerke/6259621 to your computer and use it in GitHub Desktop.
Example interactive chart
<!doctype HTML>
<meta charset = 'utf-8'>
<html>
<head>
<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-left: auto;
margin-right: auto;
width: 1200px;
height: 600px;
}
</style>
</head>
<body>
<div id='chart10b091d1a21' class='rChart nvd3'></div>
<script type='text/javascript'>
$(document).ready(function(){
drawchart10b091d1a21()
});
function drawchart10b091d1a21(){
var opts = {
"dom": "chart10b091d1a21",
"width": 1200,
"height": 600,
"x": "month",
"y": "value",
"group": "consumption",
"type": "multiBarChart",
"id": "chart10b091d1a21"
},
data = [
{
"month": "Jan",
"variable": "y2011",
"value": 779,
"consumption": "2011 1k gals"
},
{
"month": "Feb",
"variable": "y2011",
"value": 803,
"consumption": "2011 1k gals"
},
{
"month": "Mar",
"variable": "y2011",
"value": 1232,
"consumption": "2011 1k gals"
},
{
"month": "Apr",
"variable": "y2011",
"value": 1118,
"consumption": "2011 1k gals"
},
{
"month": "May",
"variable": "y2011",
"value": 655,
"consumption": "2011 1k gals"
},
{
"month": "Jun",
"variable": "y2011",
"value": 860,
"consumption": "2011 1k gals"
},
{
"month": "Jul",
"variable": "y2011",
"value": 772,
"consumption": "2011 1k gals"
},
{
"month": "Aug",
"variable": "y2011",
"value": 992,
"consumption": "2011 1k gals"
},
{
"month": "Sep",
"variable": "y2011",
"value": 741,
"consumption": "2011 1k gals"
},
{
"month": "Oct",
"variable": "y2011",
"value": 750,
"consumption": "2011 1k gals"
},
{
"month": "Nov",
"variable": "y2011",
"value": 530,
"consumption": "2011 1k gals"
},
{
"month": "Dec",
"variable": "y2011",
"value": 1326,
"consumption": "2011 1k gals"
},
{
"month": "Jan",
"variable": "y2012",
"value": 983,
"consumption": "2012 1k gals"
},
{
"month": "Feb",
"variable": "y2012",
"value": 965,
"consumption": "2012 1k gals"
},
{
"month": "Mar",
"variable": "y2012",
"value": 938,
"consumption": "2012 1k gals"
},
{
"month": "Apr",
"variable": "y2012",
"value": 778,
"consumption": "2012 1k gals"
},
{
"month": "May",
"variable": "y2012",
"value": 693,
"consumption": "2012 1k gals"
},
{
"month": "Jun",
"variable": "y2012",
"value": 956,
"consumption": "2012 1k gals"
},
{
"month": "Jul",
"variable": "y2012",
"value": 888,
"consumption": "2012 1k gals"
},
{
"month": "Aug",
"variable": "y2012",
"value": 816,
"consumption": "2012 1k gals"
},
{
"month": "Sep",
"variable": "y2012",
"value": 829,
"consumption": "2012 1k gals"
},
{
"month": "Oct",
"variable": "y2012",
"value": 720,
"consumption": "2012 1k gals"
},
{
"month": "Nov",
"variable": "y2012",
"value": 789,
"consumption": "2012 1k gals"
},
{
"month": "Dec",
"variable": "y2012",
"value": 766,
"consumption": "2012 1k gals"
},
{
"month": "Jan",
"variable": "y2013",
"value": 750,
"consumption": "2013 1k gals"
},
{
"month": "Feb",
"variable": "y2013",
"value": 635,
"consumption": "2013 1k gals"
},
{
"month": "Mar",
"variable": "y2013",
"value": 851,
"consumption": "2013 1k gals"
},
{
"month": "Apr",
"variable": "y2013",
"value": 1333,
"consumption": "2013 1k gals"
},
{
"month": "May",
"variable": "y2013",
"value": 1074,
"consumption": "2013 1k gals"
},
{
"month": "Jun",
"variable": "y2013",
"value": 822,
"consumption": "2013 1k gals"
},
{
"month": "Jul",
"variable": "y2013",
"value": null,
"consumption": "2013 1k gals"
},
{
"month": "Aug",
"variable": "y2013",
"value": null,
"consumption": "2013 1k gals"
},
{
"month": "Sep",
"variable": "y2013",
"value": null,
"consumption": "2013 1k gals"
},
{
"month": "Oct",
"variable": "y2013",
"value": null,
"consumption": "2013 1k gals"
},
{
"month": "Nov",
"variable": "y2013",
"value": null,
"consumption": "2013 1k gals"
},
{
"month": "Dec",
"variable": "y2013",
"value": null,
"consumption": "2013 1k gals"
},
{
"month": "Jan",
"variable": "avg1112",
"value": 587,
"consumption": "2011-2012 Avg"
},
{
"month": "Feb",
"variable": "avg1112",
"value": 589,
"consumption": "2011-2012 Avg"
},
{
"month": "Mar",
"variable": "avg1112",
"value": 723,
"consumption": "2011-2012 Avg"
},
{
"month": "Apr",
"variable": "avg1112",
"value": 632,
"consumption": "2011-2012 Avg"
},
{
"month": "May",
"variable": "avg1112",
"value": 449,
"consumption": "2011-2012 Avg"
},
{
"month": "Jun",
"variable": "avg1112",
"value": 605,
"consumption": "2011-2012 Avg"
},
{
"month": "Jul",
"variable": "avg1112",
"value": 553,
"consumption": "2011-2012 Avg"
},
{
"month": "Aug",
"variable": "avg1112",
"value": 603,
"consumption": "2011-2012 Avg"
},
{
"month": "Sep",
"variable": "avg1112",
"value": 523,
"consumption": "2011-2012 Avg"
},
{
"month": "Oct",
"variable": "avg1112",
"value": 490,
"consumption": "2011-2012 Avg"
},
{
"month": "Nov",
"variable": "avg1112",
"value": 440,
"consumption": "2011-2012 Avg"
},
{
"month": "Dec",
"variable": "avg1112",
"value": 697,
"consumption": "2011-2012 Avg"
}
]
var data = d3.nest()
.key(function(d){
return opts.group === undefined ? 'main' : d[opts.group]
})
.entries(data)
nv.addGraph(function() {
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)
chart
.color([ "996633", "66ff33", "003399", "cc3300" ])
.reduceXTicks(false)
.tooltipContent( function(key, x, y, e, graph){
return '<h3>' + key + '</h3>' +
'<p>' + y + ' in ' + x + '</p>'
} )
chart.xAxis
.axisLabel("Month")
chart.yAxis
.axisLabel("Consumption")
.width( 62)
d3.select("#" + opts.id)
.append('svg')
.datum(data)
.transition().duration(500)
.call(chart);
nv.utils.windowResize(chart.update);
return chart;
});
};
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment