Skip to content

Instantly share code, notes, and snippets.

@mtranggit
Last active September 22, 2017 07:46
Show Gist options
  • Save mtranggit/ba8da21287b9810deafb5096d98ba12e to your computer and use it in GitHub Desktop.
Save mtranggit/ba8da21287b9810deafb5096d98ba12e to your computer and use it in GitHub Desktop.
Build a column chart
license: mit
<!DOCTYPE html>
<head>
<title>Make D3 Responsive</title>
<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>
var margin = {top: 10, right: 25, bottom: 35, left: 40 }
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)
.call(responsivefy)
.append('g')
.attr('transform', `translate(${margin.left}, ${margin.top})`)
var data = [
{score: 63, subject: 'Mathematics'},
{score: 82, subject: 'Geography'},
{score: 74, subject: 'Spelling'},
{score: 97, subject: 'Reading'},
{score: 52, subject: 'Science'},
{score: 74, subject: 'Chemistry'},
{score: 97, subject: 'Physics'},
{score: 52, subject: 'ASL'}
];
// draw xAxis
var xScale = d3.scaleBand()
.domain(data.map(x => x.subject))
.range([0, width])
.padding('0.2')
var xAxis = d3.axisBottom(xScale)
.ticks(5)
.tickSize(10)
.tickPadding(5)
svg.append('g')
.attr('transform', `translate(${0}, ${height})`)
.call(xAxis)
.selectAll('text')
.style('text-anchor', 'end')
.attr('transform', `rotate(-45)`)
// draw yAxis
var yScale = d3.scaleLinear()
.domain([0, 100])
.range([height, 0])
var yAxis = d3.axisLeft(yScale)
.ticks(10)
.tickSize(10)
.tickPadding(10)
svg.append('g')
.attr('transform', `transform(${margin.left}, ${margin.top})`)
.call(yAxis)
// Draw column
svg.selectAll('.column')
.data(data)
.enter()
.append('g')
.attr('class', '.column')
.append('rect')
.attr('fill', 'steelblue')
.attr('x', d => xScale(d.subject))
.attr('y', d => yScale(d.score))
.attr('width', d => xScale.bandwidth())
.attr('height', d => height - yScale(d.score))
function responsivefy(svg) {
// get container + svg aspect ratio
var container = d3.select(svg.node().parentNode),
width = parseInt(svg.style("width")),
height = parseInt(svg.style("height")),
aspect = width / height;
// add viewBox and preserveAspectRatio properties,
// and call resize so that svg resizes on inital page load
svg.attr("viewBox", "0 0 " + width + " " + height)
.attr("preserveAspectRatio", "xMinYMid")
.call(resize);
// to register multiple listeners for same event type,
// you need to add namespace, i.e., 'click.foo'
// necessary if you call invoke this function for multiple svgs
// api docs: https://github.com/mbostock/d3/wiki/Selections#on
d3.select(window).on("resize." + container.attr("id"), resize);
// get width of container and resize svg to fit it
function resize() {
var targetWidth = parseInt(container.style("width"));
svg.attr("width", targetWidth);
svg.attr("height", Math.round(targetWidth / aspect));
}
}
</script>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment