Skip to content

Instantly share code, notes, and snippets.

@mtranggit
Created September 20, 2017 08:01
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 mtranggit/3e68b786e5b587afea6ce6359f8616bd to your computer and use it in GitHub Desktop.
Save mtranggit/3e68b786e5b587afea6ce6359f8616bd to your computer and use it in GitHub Desktop.
Margin Convention with D3
license: mit
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<script src="https://d3js.org/d3.v4.min.js"></script>
<style>
body { margin:0;position:fixed;top:0;right:0;bottom:0;left:0; }
.chart {
background: lightgray;
border: 1px solid black;
width: 425px;
height: 625px;
}
</style>
</head>
<body>
<div class="chart"></div>
<script>
// Feel free to change or delete any of the code you see in this editor!
var margin = { top: 10, right: 10, bottom: 25, left: 25}
var width = 425 - margin.left - margin.right
var height = 625 - margin.top - margin.bottom
var svg = d3.select('.chart')
.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('rect')
.attr('width', width/2)
.attr('height', height)
.style('fill', 'lightblue')
.style('stroke', 'lightgreen')
svg.append('rect')
.attr('x', width/2)
.attr('width', width/2)
.attr('height', height)
.style('fill', 'lightyellow')
.style('stroke', 'lightgreen')
</script>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment