Skip to content

Instantly share code, notes, and snippets.



Last active Aug 22, 2019
What would you like to do?
Simple example axes
license: mit
<!DOCTYPE html>
<meta charset="utf-8">
<script src=""></script>
body { margin:0;position:fixed;top:0;right:0;bottom:0;left:0; }
<svg width=900 height=500></svg>
const cookiesThisWeek = [
{day: 'Monday', cookies: 10},
{day: 'Tuesday', cookies: 20},
{day: 'Wednesday', cookies: 10},
{day: 'Thursday', cookies: 61},
{day: 'Friday', cookies: 1},
{day: 'Saturday', cookies: 12},
{day: 'Sunday', cookies: 2}];
const svg ="svg");
const width = +svg.attr('width');
const height = +svg.attr('height');
const margin = 40;
const innerHeight = height - 2*margin;
const innerWidth = width - 2*margin;
const yScale = d3.scaleLinear()
.domain([d3.min( => c.cookies)), d3.max( => c.cookies))])
.range([height - 2*margin, 0]);
const daysOfTheWeek = =>
const xScale = d3.scaleBand()
.range([0, width - 2*margin])
const colorScale = d3.scaleOrdinal(d3.schemeBlues[7])
const g = svg.append('g')
.attr('transform', `translate(${margin}, ${margin})`);
.attr('transform', `translate(0, ${innerHeight})`)
.attr('x', (d) => xScale(
.attr('y', (d) => yScale(d.cookies))
.attr('width', xScale.bandwidth())
.attr('height', (d) => innerHeight - yScale(d.cookies))
.attr('fill', (_,i) => colorScale(i));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment