Skip to content

Instantly share code, notes, and snippets.

@wiederkehr
Last active December 16, 2015 19:10
Show Gist options
  • Save wiederkehr/5483069 to your computer and use it in GitHub Desktop.
Save wiederkehr/5483069 to your computer and use it in GitHub Desktop.
Margin conventions for charts with the brush component.

Margin conventions for charts with the brush component. All credits for this go to Mike Bostock.

<!doctype html>
<html lang=en>
<head>
<meta charset=utf-8>
<title>Margin Conventions with Brush</title>
<link type="text/css" rel="stylesheet" href="style.css"/>
</head>
<body>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script src="script.js"></script>
</body>
</html>
(function() {
var xAxisHeight = 20,
margin = {top: 20, right: 20, bottom: 20, left: 20, middle: 20},
focusHeight = 300,
contextHeight = 100,
outerHeight = focusHeight + contextHeight + xAxisHeight + xAxisHeight + margin.top + margin.bottom + margin.middle,
outerWidth = 960,
width = outerWidth - margin.left - margin.right,
height = outerHeight - margin.top - margin.bottom;
var x = d3.scale.identity()
.domain([0, width]);
var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom");
var svg = d3.select("body").append("svg")
.attr("width", outerWidth)
.attr("height", outerHeight)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
var focus = svg.append("g")
.attr("transform", "translate(0,0)");
focus.append("rect")
.attr("class", "focus")
.attr("width", width)
.attr("height", focusHeight);
focus.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + focusHeight + ")")
.call(xAxis);
var context = svg.append("g")
.attr("transform", "translate(0," + (focusHeight + xAxisHeight + margin.middle) + ")");
context.append("rect")
.attr("class", "context")
.attr("width", width)
.attr("height", contextHeight);
context.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + contextHeight + ")")
.call(xAxis);
})()
body {
font: 10px "Helvetica Neue", Helvetica, Arial, sans-serif;
}
svg{
background: #eee;
}
.axis path {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.focus {
fill: rgba(0,0,0,0.1);
}
.context {
fill: rgba(0,0,0,0.2);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment