D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
mtranggit
Full window
Github gist
Margin Convention with D3
Built with
blockbuilder.org
<!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>
https://d3js.org/d3.v4.min.js