Skip to content

Instantly share code, notes, and snippets.

@romsson
Last active October 10, 2017 05:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save romsson/00733297a7d852d377301d55ed8a4226 to your computer and use it in GitHub Desktop.
Save romsson/00733297a7d852d377301d55ed8a4226 to your computer and use it in GitHub Desktop.
[d3-gridding] simple bar chart
license: mit
<!DOCTYPE html>
<meta charset="utf-8">
<style>
body {
font-family: Helvetica;
font-size: 10px;
}
.point {
fill: black;
}
rect {
fill: none;
stroke: black;
stroke-width: 1;
}
</style>
<body>
<script src="http://d3js.org/d3.v4.js"></script>
<script src="http://romsson.github.io/d3-gridding/build/d3-gridding.js"></script>
<script>
var width = 400,
height = 300;
var gridding = d3.gridding()
.size([width, height])
.valueHeight("v")
.orient("up")
.mode("vertical");
var data = d3.range(10).map(function(d, i) {
return {v: d * Math.random()};
});
var griddingData = gridding(data);
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);
svg.selectAll(".square")
.data(griddingData)
.enter().append("rect")
.attr("class", "square")
.attr("width", function(d) { return d.width; })
.attr("height", function(d) { return d.height; })
.attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; })
svg.selectAll(".index")
.data(griddingData)
.enter().append("text")
.attr("class", "index")
.style('text-anchor', 'middle')
.style('dominant-baseline', 'central')
.attr("transform", function(d) { return "translate(" + d.cx + "," + d.cy + ")"; })
.text(function(d, i) { return d.v; });
d3.selectAll(".index").remove();
</script>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment