Skip to content

Instantly share code, notes, and snippets.

@areologist
Last active July 2, 2017 21:47
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/e272c47ab7a61fa851d9ce6bd8c646fa to your computer and use it in GitHub Desktop.
Save areologist/e272c47ab7a61fa851d9ce6bd8c646fa to your computer and use it in GitHub Desktop.
horizontal bars
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-color: #f6fafc;
border: 1px solid #333;
padding: 15px;
width: 240px;
}
.chart svg {
background: white;
border: 1px solid #ddd;
}
.bar {
fill: #0f6ba8;
}
.axis {
fill: #888;
text-anchor: middle;
font-size: 15px;
font-family: Arial;
}
.axis g:first-of-type {
text-anchor: start;
}
</style>
</head>
<body>
<div class="chart"></div>
<script>
const margin = { top: 25, right: 50, bottom: 25, left: 25 };
const width = 300 - margin.left - margin.right;
const height = 350 - margin.top - margin.bottom;
const fullWidth = width + margin.left + margin.right;
const fullHeight = height + margin.top + margin.bottom;
const data = [
{ amount: 15900, category: 'Travel & Entertainment' },
{ amount: 12090, category: 'Donations & Gifts' },
{ amount: 8670, category: 'COGS' },
{ amount: 2340, category: 'Other Categories' }
];
const 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 + ')');
const xScale = d3.scaleLinear()
.domain([0, 20000])
.range([0, width])
.nice();
const yScale = d3.scaleBand()
.padding(0.2)
.domain(data.map(d => d.category))
.range([0, height]);
svg.selectAll('rect')
.data(data)
.enter()
.append('rect')
.attr('class', 'bar')
.attr('x', 0)
.attr('y', d => yScale(d.category))
.attr('width', d => xScale(d.amount))
.attr('height', yScale.bandwidth());
const topScale = d3.scaleLinear()
.domain([0, 50])
.range([0, width]);
buildTopAxis([0, 25, 50], topScale);
buildBottomAxis([0, 10000, 20000], xScale);
function buildTopAxis(values, scale) {
console.log('scale: ', scale(0));
const enter = svg.append('g')
.attr('class', 'axis')
.selectAll('text')
.data(values)
.enter()
.append('g')
.attr('transform', d => `translate(${scale(d)}, 0)`);
enter.append('text')
.text(d => d3.format('.0%')(d/100))
.attr('x', 0)
.attr('y', 0)
.attr('dy', '0.1em');
}
function buildBottomAxis(values, scale) {
const g = svg.append('g')
.attr('class', 'axis')
.selectAll('g')
.data(values)
.enter()
.append('g')
.append('text')
.text(d => d3.format('$,.0f')(d))
.attr('x', d => scale(d))
.attr('y', 0)
.attr('dy', '0.7em');
g.attr('transform', `translate(0, ${height})`);
}
///////
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