Skip to content

Instantly share code, notes, and snippets.

@enjoylife
Created May 22, 2016 19:20
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 enjoylife/6b14aa0dfc0efc0c612670f323c891aa to your computer and use it in GitHub Desktop.
Save enjoylife/6b14aa0dfc0efc0c612670f323c891aa to your computer and use it in GitHub Desktop.
import d3 from 'd3/build/d3';
var margin = {top: 20, right: 60, bottom: 30, left: 20},
width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
// We need a scale for laying out the bars of our barcharts
var x = d3.scaleBand()
.rangeRound([0, width])
.paddingInner(.1)
.paddingOuter(1)
// Need this time scale to work with our x axis values
var xTime = d3.scaleTime().rangeRound([0, width]);
var xAxis = d3.axisBottom(xTime).tickFormat(d3.timeFormat("%b %d"));
// Need a linear scale for working with our counts and sums of our datapoints
var y = d3.scaleLinear().rangeRound([height, 0]).domain([0,1]);
var yAxis = d3.axisRight(y).tickFormat(d3.format(".2s"))
// Choosing two appealing colors for our measure types
var colorForMeasures = d3.scaleOrdinal()
.range(["#8a89a6", "#6B486B"]);
// Don't care what colors these are
var colorForCountry = d3.scaleCategory20b();
// Following the standard d3 margin conventions
var svg = d3.select("body").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("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis)
svg.append("g")
.attr("class", "y axis")
.attr("transform", "translate(" + width + ",0)")
.call(yAxis)
.append("text")
.attr("transform", "rotate(-90)")
.attr("y", -12)
.attr("dy", ".71em")
.style("text-anchor", "end")
.text("Number of Cases");
updateAxis();
function updateAxis() {
svg.select(".x.axis").call(xAxis);
svg.select(".y.axis").call(yAxis);
}
y.domain([0, 16000])
updateAxis()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment