Skip to content

Instantly share code, notes, and snippets.

@hungvietdo
Forked from weiglemc/.block
Last active January 30, 2016 14:31
Show Gist options
  • Save hungvietdo/7bc52597560fc51e1117 to your computer and use it in GitHub Desktop.
Save hungvietdo/7bc52597560fc51e1117 to your computer and use it in GitHub Desktop.
Bar chart practice

My notes

Edit of mbostock's DRY Bar Chart

A variation of the example bar chart using a slightly more D.R.Y. style. The visual encoding is represented by two functions and their composition:

  • The value accessor returns the value (or property) to encode for a given data object.
  • The scale maps this value to a visual display encoding, such as a pixel position.
  • The map function represents the composition valuescale, mapping from data to display.

Inspired by Andrew Winterman’s post, Tooling for the Lazy Programmer: DRYing up D3.

Year Winner DogsEaten Country NewRecord
1980 Paul Siederman & Joe Baldini 9.1 United States 0
1981 Thomas DeBerry 11 United States 0
1982 Steven Abrams 11 United States 0
1983 Luis Llamas 19.5 Mexico 0
1984 Birgit Felden 9.5 Germany 0
1985 Oscar Rodriguez 11.75 United States 0
1986 Mark Heller 15.5 United States 0
1987 Don Wolfman 12 United States 0
1988 Jay Green 14 United States 0
1989 Jay Green 13 United States 0
1990 Mike DeVito 16 United States 0
1991 Frank Dellarosa 21.5 United States 1
1992 Frank Dellarosa 19 United States 0
1993 Mike DeVito 17 United States 0
1994 Mike DeVito 20 United States 0
1995 Edward Krachie 19.5 United States 0
1996 Edward Krachie 22.25 United States 1
1997 Hirofumi Nakajima 24.5 Japan 1
1998 Hirofumi Nakajima 19 Japan 0
1999 Steve Keiner 20.25 United States 0
2000 Kazutoyo "The Rabbit" Arai 25.13 Japan 1
2001 Takeru Kobayashi 50 Japan 1
2002 Takeru Kobayashi 50.5 Japan 1
2003 Takeru Kobayashi 44.5 Japan 0
2004 Takeru Kobayashi 53.5 Japan 1
2005 Takeru Kobayashi 49 Japan 0
2006 Takeru "Tsunami" Kobayashi 53.75 Japan 1
2007 Joey Chestnut 66 United States 1
2008 Joey Chestnut 59 United States 0
2009 Joey Chestnut 68 United States 1
2010 Joey Chestnut 54 United States 0
<!DOCTYPE html>
<meta charset="utf-8">
<style>
body {
font: 10px sans-serif;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.bar {
fill: steelblue;
}
.x.axis path {
display: none;
}
</style>
<body>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script>
var margin = {top: 20, right: 20, bottom: 50, left: 40},
width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
var xValue = function(d) { return d.Year; }, // data -> value
xScale = d3.scale.ordinal().rangeRoundBands([0, width], .1), // value -> display
xMap = function(d) { return xScale(xValue(d)); }, // data -> display
xAxis = d3.svg.axis().scale(xScale).orient("bottom");
var yValue = function(d) { return d.DogsEaten; }, // data -> value
yScale = d3.scale.linear().range([height, 0]), // value -> display
yMap = function(d) { return yScale(yValue(d)); }, // data -> display
yAxis = d3.svg.axis().scale(yScale).orient("left");
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 + ")");
d3.csv("data.csv", type, function(error, data) {
xScale.domain(data.map(xValue)); // data.map(xValue) returns an array of years
yScale.domain([0, d3.max(data, yValue)]);
// Note: domain for ordinal needs to be the whole range, not just min/max
console.log ("data.map(xValue) = ", data.map(xValue));
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
svg.append("g")
.attr("class", "y axis")
.call(yAxis)
.append("text")
.attr("x", margin.left + 20)
.attr("y", 6)
.attr("dy", ".71em")
.style("text-anchor", "end")
.text("Dogs Eaten");
svg.selectAll(".bar")
.data(data)
.enter().append("rect")
.attr("class", "bar")
.attr("x", xMap)
.attr("width", xScale.rangeBand)
.attr("y", yMap)
.attr("height", function(d) { return height - yMap(d); })
.attr("style", function(d) {if (d.NewRecord == 1) return "fill:green";})
.attr("debug", function (d) {console.log(d);
console.log("x data = ", xValue(d));
console.log("x pixel = ", xMap(d));
console.log("y data = ", yValue(d));
console.log("y pixel = ", yMap(d));
console.log("width = ", xScale.rangeBand(d));
});
});
function type(d) {
d.DogsEaten = +d.DogsEaten; // change string into number format
return d;
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment