Skip to content

Instantly share code, notes, and snippets.

@marcdhansen
Last active August 29, 2015 14:04
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 marcdhansen/b1ef8f7a6f935375b684 to your computer and use it in GitHub Desktop.
Save marcdhansen/b1ef8f7a6f935375b684 to your computer and use it in GitHub Desktop.
Simplified Margin Convention Example
<!DOCTYPE html>
<html>
<head>
<style>
.axis line, .axis path {
shape-rendering: crispEdges;
stroke: black;
fill: none;
}
</style>
<script type="text/javascript" src="http://mbostock.github.com/d3/d3.js"></script>
</head>
<body>
<div id="viz" style="position:absolute;width:100%;height:100%"></div>
<script type="text/javascript">
var div = d3.select("#viz");
var divWidth = parseInt(div.style("width"));
var divHeight = parseInt(div.style("height"));
var margin = {top: 41, right: 42, bottom: 63, left: 64};
var width = divWidth - margin.left - margin.right;
var height = divHeight - margin.top - margin.bottom;
var svg = div.append("svg")
.attr("width", divWidth)
.attr("height", divHeight);
svg.append("rect")
.attr("x", 0)
.attr("y", 0)
.attr("width", divWidth)
.attr("height", divHeight)
.attr("fill", "burlywood")
;
// size
svg.append("text")
.attr("x", 5)
.attr("y", margin.top/2)///
.attr("text-anchor", "left")
.style("font-size", "20px")
.text(divWidth + "x" + divHeight);
// margin.top
svg.append("text")
.attr("x", divWidth/2)///
.attr("y", margin.top * 2/3)///
.attr("text-anchor", "center")
.style("font-size", "20px")
.text(margin.top);
// margin.right
svg.append("text")
.attr("x", divWidth - (margin.right * 4/5))///
.attr("y", divHeight/2)///
.attr("text-anchor", "center")
.style("font-size", "20px")
.text(margin.right);
// margin.bottom
svg.append("text")
.attr("x", divWidth/2)///
.attr("y", divHeight - (margin.bottom * 1/3))///
.attr("text-anchor", "center")
.style("font-size", "20px")
.text(margin.bottom);
// margin.left
svg.append("text")
.attr("x", 5)
.attr("y", divHeight/2)///
.attr("text-anchor", "center")
.style("font-size", "20px")
.text(margin.left);
var g = svg.append('g')
.attr("transform",
"translate(" + margin.left + "," + margin.top + ")");
g.append("rect")
.attr("x", 0)
.attr("y", 0)
.attr("width", width)
.attr("height", height)
.attr("fill", "wheat")
;
g.append("text")
.attr("x", 5)
.attr("y", 20)
.attr("text-anchor", "left")
.style("font-size", "20px")
.text(width + "x" + height);
// Define identity (1:1) scales
var x = d3.scale.identity().domain([0, width]);
var y = d3.scale.identity().domain([0, height]).range([height,0]);
// Define stock x and y axis
var xAxis = d3.svg.axis().scale(x).orient('bottom');
var yAxis = d3.svg.axis().scale(y).orient('left').ticks(10);
g.append('g')
.attr('transform', 'translate(0,' + height + ')')
.attr("class", "x axis")
.call(xAxis);
g.append('g')
.attr("class", "y axis")
.call(yAxis);
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment