Built with blockbuilder.org
Last active
December 5, 2018 19:58
-
-
Save romsson/2f94c1913b81f7fd20c530236934433a to your computer and use it in GitHub Desktop.
Grouped bar chart v4
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
license: mit |
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> | |
<meta charset="utf-8"> | |
<style> | |
.axis text { | |
font: 10px sans-serif; | |
} | |
.axis path, | |
.axis line { | |
fill: none; | |
stroke: #000; | |
shape-rendering: crispEdges; | |
} | |
.x.axis path { | |
display: none; | |
} | |
</style> | |
<body> | |
<script src="https://d3js.org/d3.v4.min.js"></script> | |
<script> | |
// Source: https://bl.ocks.org/mbostock/3887051 | |
var n = 10, // number of samples | |
m = 5; // number of series | |
var data = d3.range(m).map(function() { | |
return d3.range(n).map(Math.random); | |
}); | |
var margin = {top: 20, right: 30, bottom: 30, left: 40}, | |
width = 960 - margin.left - margin.right, | |
height = 500 - margin.top - margin.bottom; | |
var y = d3.scaleLinear() | |
.domain([0, 1]) | |
.range([height, 0]); | |
var x0 = d3.scaleBand() | |
.domain(d3.range(n)) | |
.range([0, width], .2); | |
var x1 = d3.scaleBand() | |
.domain(d3.range(m)) | |
.range([0, x0.bandwidth() - 10]); | |
var z = d3.scaleOrdinal() | |
.range(["#98abc5", "#8a89a6", "#7b6888", "#6b486b", "#a05d56", "#d0743c", "#ff8c00"]); | |
var svg = d3.select("body").append("svg") | |
.attr("width", width + margin.left + margin.right) | |
.attr("height", height + margin.top + margin.bottom) | |
.append("g") | |
.attr("transform", "translate(" + margin.left + "," + margin.top + ")"); | |
svg.append("g").selectAll("g") | |
.data(data) | |
.enter().append("g") | |
.style("fill", function(d, i) { return z(i); }) | |
.attr("transform", function(d, i) { return "translate(" + x1(i) + ",0)"; }) | |
.selectAll("rect") | |
.data(function(d) { return d; }) | |
.enter().append("rect") | |
.attr("width", x1.bandwidth()) | |
.attr("height", y) | |
.attr("x", function(d, i) { return x0(i); }) | |
.attr("y", function(d) { return height - y(d); }); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment