Skip to content

Instantly share code, notes, and snippets.

@fdaudens
Created April 12, 2015 19:55
Show Gist options
  • Save fdaudens/992502bf6118da4b7b6b to your computer and use it in GitHub Desktop.
Save fdaudens/992502bf6118da4b7b6b to your computer and use it in GitHub Desktop.
var margin = {top: 20, right: 10, bottom: 20, left: 10},
width = 635 - margin.left - margin.right,
height = 357 - margin.top - margin.bottom;
var x = d3.time.scale()
// .domain([1985-01-01, 2016-01-01])
.range([0, width]);
var y = d3.scale.linear()
.range([height, 0]);
var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom")
.tickFormat(d3.time.format("%Y"))
.ticks(5);
var yAxis = d3.svg.axis()
.scale(y)
.orient("left")
.ticks(3);
var parseDate = d3.time.format("%y-%m-%d").parse;
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 + ")");
var color = d3.scale.category20c()
.range(['#B00000', '#0174DF']);
d3.csv("file.csv", function(error, data) {
data.forEach(function(d) {
d.date = parseDate(d.date);
d.nombre = +d.nombre;
});
// x.domain(data.map(function(d) { return d.date; }));
// x.domain([1985-01-01, 2016-01-01]);
x.domain(d3.extent(data, function(d) { return d.date; }));
y.domain([0, d3.max(data, function(d) { return d.nombre; })]);
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis)
.selectAll("text")
.style("text-anchor", "end")
.attr("dx", "-.8em")
.attr("dy", "-.55em")
.attr("transform", "rotate(-90)" );
svg.append("g")
.attr("class", "y axis")
.call(yAxis)
.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", ".71em")
.style("text-anchor", "end")
.text("Nombre");
svg.selectAll("bar")
.data(data)
.enter().append("rect")
.style("fill", function(d) { return color(d.parti); })
.attr("x", function(d) { return x(d.date); })
.attr("width", 5)
.attr("y", function(d) { return y(d.nombre); })
.attr("height", function(d) { return height - y(d.nombre); });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment