- use keyword
constinstead of variable if the variable for WIDTH and HEIGHT do not change - use ALL_CAPS convention for the const name wich as WIDTH
- review SVG Viewport margin conventions
- review chart area position translation to get X and Y axis aligned properly
-
create constants for WIDTH and HEIGHT of chart area 800x600
-
create const MARGIN as a JavaScript object with properties LEFT, RIGHT, TOP, BOTTOM
-
create variables for SVG_WIDTH and SVG_HEIGHT by adding chart areas to margins
-
use d3.select to select the HTML body element
-
append svg
-
add attributes for width and height to the new svg element
-
append group
gto the svg HTML tag.append("g") -
translate
gby MARGIN.LEFT and MARGIN.TOP.attr("transform", "translate(" + ...
const WIDTH = 800;
const HEIGHT = 600;
const MARGIN = {TOP: 50, BOTTOM: 100, RIGHT: 50, LEFT: 100};
const SVG_HEIGHT = HEIGHT + MARGIN.TOP + MARGIN.BOTTOM;
const SVG_WIDTH = WIDTH + MARGIN.RIGHT + MARGIN.LEFT;
var svg = d3.select("body").append("svg")
.attr("width", SVG_WIDTH)
.attr("height", SVG_HEIGHT)
.append("g")
.attr("transform", "translate(" +
MARGIN.LEFT + "," + MARGIN.TOP + ")");
Use CodePen for exercises

