Skip to content

Instantly share code, notes, and snippets.



Last active Aug 22, 2019
What would you like to do?
Simple example scales
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 {background: #EEE;}
<svg width=900 height=500></svg>
const cookiesThisWeek = [10, 20, 10, 50, 1, 12, 2];
const svg ="svg");
const width = +svg.attr('width');
const height = +svg.attr('height');
const yScale = d3.scaleLinear()
.domain([d3.min(cookiesThisWeek), d3.max(cookiesThisWeek)])
.range([0, height]);
const daysOfTheWeek = [...cookiesThisWeek.keys()]
const xScale = d3.scaleBand()
.range([0, width])
const colorScale = d3.scaleOrdinal(d3.schemeBlues[7])
.attr('x', (_, i) => xScale(i))
.attr('y', (d) => height - yScale(d))
.attr('width', xScale.bandwidth())
.attr('height', (d) => yScale(d))
.attr('fill', (_,i) => colorScale(i))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment