Created
October 7, 2013 18:03
-
-
Save kirantemojo/6872293 to your computer and use it in GitHub Desktop.
Simple Small Bar Chart - D3
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> | |
<html> | |
<head> | |
<title>Dsnap - Charts</title> | |
<style> | |
#xaxis .domain { | |
fill:none; | |
stroke:#000; | |
} | |
#xaxis text, #yaxis text { | |
font-size: 10px; | |
} | |
#xaxis .domain{ | |
display:none; | |
} | |
</style> | |
</head> | |
<body> | |
<div id="wrapper"> | |
</div> | |
<script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script> | |
<script> | |
var dollars= [54,35,67,47,83,28,27,49,49,69,77,92,49,57,63,54,87,93,84,29,58]; | |
var categories = ['GenX','GenY','Newly weds','Mille nials','Recent Grads','Big Families','College','Single Parents','Yuppies','Retired','Demo 10','Demo 11','Demo 12','Demo 13','Demo 14','Demo 15','Demo 16','Demo 17','Demo 18','Demo 19','Demo 20']; | |
var colors = ['#0000b4','#0082ca','#0094ff','#0d4bcf','#0066AE','#074285','#00187B','#285964','#405F83','#416545','#4D7069','#6E9985','#7EBC89','#0283AF','#79BCBF','#99C19E']; | |
var grid = d3.range(10).map(function(i){ | |
return {'x1':0,'y1':0,'x2':1020,'y2':0}; | |
}); | |
var xscale = d3.scale.linear() | |
.domain([0,20]) | |
.range([0,1000]); | |
var yscale = d3.scale.linear() | |
.domain([100,10]) | |
.range([0,420]); | |
var colorScale = d3.scale.quantize() | |
.domain([0,categories.length]) | |
.range(colors); | |
var canvas = d3.select('#wrapper') | |
.append('svg') | |
.attr({'width':1100,'height':550}); | |
var grids = canvas.append('g') | |
.attr('id','grid') | |
.attr('transform','translate(50,50)') | |
.selectAll('line') | |
.data(grid) | |
.enter() | |
.append('line') | |
.attr({'x1':function(d){ return d.x1; }, | |
'y1':function(d,i){ return i*46; }, | |
'x2':function(d){ return d.x2; }, | |
'y2':function(d,i){ return i*46; }, | |
}) | |
.style({'stroke':'#adadad','stroke-width':'1px'}); | |
var xAxis = d3.svg.axis(); | |
xAxis | |
.orient('bottom') | |
.scale(xscale) | |
.tickFormat(function(d,i){ return categories[i]; }) | |
.tickValues(d3.range(21)); | |
var yAxis = d3.svg.axis(); | |
yAxis | |
.orient('left') | |
.scale(yscale) | |
.tickSize(2) | |
.tickFormat(function(d,i){ console.log(d); return d+"K"; }) | |
.tickValues([10,20,30,40,50,60,70,80,90,100]); | |
var y_xis = canvas.append('g') | |
.attr("transform", "translate(45,50)") | |
.attr('id','yaxis') | |
.call(yAxis); | |
var x_xis = canvas.append('g') | |
.attr("transform", "translate(55,466)") | |
.attr('id','xaxis') | |
.call(xAxis); | |
var chart = canvas.append('g') | |
.attr("transform", "translate(55,50)") | |
.attr('id','bars') | |
.selectAll('rect') | |
.data(dollars) | |
.enter() | |
.append('rect') | |
.attr('height',function(d){ return 0; }) | |
.attr({'x':function(d,i){ if(i>0){ return xscale(i); } },'y':function(d){ return 413-yscale(d); } }) | |
.style('fill',function(d,i){ return colorScale(i); }) | |
.attr('width',function(d){ return 12; }); | |
var transit = d3.select("svg").selectAll("rect") | |
.data(dollars) | |
.transition() | |
.duration(1000) | |
.attr("height", function(d) {return yscale(d); }); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment