Skip to content

Instantly share code, notes, and snippets.

@akrusz
Created August 10, 2012 20:23
Show Gist options
  • Save akrusz/3317560 to your computer and use it in GitHub Desktop.
Save akrusz/3317560 to your computer and use it in GitHub Desktop.
playin around with dee three
<html>
<head>
<title>D3 Demo: Making a bar chart with value labels!</title>
<script type="text/javascript" src="d3.js"></script>
<style type="text/css">
/* No style rules here yet */
</style>
</head>
<body>
<script type="text/javascript">
//Width and height
var w = 800;
var h = 500;
var barPadding = 1;
var dataset = [ 5, 10, 13, 19, 21, 25, 22, 18, 15, 13,
11, 12, 15, 20, 18, 17, 16, 18, 23, 25 ];
var maxValue = Math.max.apply(Math, dataset);
//Create SVG element
var svg = d3.select("body")
.append("svg")
.attr("width", w)
.attr("height", h);
var gradient = svg.append("svg:defs")
.append("svg:linearGradient")
.attr("id", "gradient")
.attr("x1", "0%")
.attr("y1", "0%")
.attr("x2", "100%")
.attr("y2", "100%")
.attr("spreadMethod", "pad");
gradient.append("svg:stop")
.attr("offset", "0%")
.attr("stop-color", "#0c0")
.attr("stop-opacity", 1);
gradient.append("svg:stop")
.attr("offset", "100%")
.attr("stop-color", "#c00")
.attr("stop-opacity", 1);
svg.selectAll("rect")
.data(dataset)
.enter()
.append("rect")
.attr("x", function(d, i) {
return i * (w / dataset.length);
})
.attr("y", 0)
.attr("width", w / dataset.length - barPadding)
.attr("height", h - 22)
.attr("fill", "url(#gradient)");
//.attr("fill", function(d) {
// return "rgb(0, 0, " + Math.ceil(255 * d / maxValue) + ")";
});
svg.selectAll("text")
.data(dataset)
.enter()
.append("text")
.text(function(d, i) {
return i + 1990;
})
.attr("text-anchor", "middle")
.attr("x", function(d, i) {
return i * (w / dataset.length) + (w / dataset.length - barPadding) / 2;
})
.attr("y", function(d) {
return h - 10;
})
.attr("font-family", "sans-serif")
.attr("font-size", "11px")
.attr("fill", "black");
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment