Skip to content

Instantly share code, notes, and snippets.

@areologist
Last active June 27, 2017 19:19
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 areologist/3313b6ebc328380c3947899091a11b35 to your computer and use it in GitHub Desktop.
Save areologist/3313b6ebc328380c3947899091a11b35 to your computer and use it in GitHub Desktop.
axes and margins
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 {
border: 1px solid hotpink;
}
svg {
background-color: #f3f9fc;
border: 1px solid blue;
}
</style>
</head>
<body>
<div class="chart"></div>
<script>
const margin = { top: 25, right: 35, bottom: 60, left: 35 };
const width = 550 - margin.left - margin.right;
const height = 425 - margin.top - margin.bottom;
const 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)
.attr('height', height)
.attr('fill', 'lightblue')
.attr('stroke', 'green');
const yScale = d3.scaleLinear()
.domain([0, 100])
.range([height, 0]);
const yAxis = d3.axisLeft(yScale)
.ticks(10);
//.tickValues([0, 100]);
// add the axis by passing it into d3.call (and wrap it in a <g>)
svg.append('g')
.call(yAxis);
// let's make a date-based x axis
const xScale = d3.scaleTime()
.domain([new Date(2017, 0, 1), new Date(2017, 1, 1)])
.range([0, width]);
const xAxis = d3.axisBottom(xScale)
.ticks(5)
.tickSize(20)
.tickPadding(10);
svg.append('g')
.attr('transform', `translate(0, ${height})`)
.call(xAxis);
</script>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment