Skip to content

Instantly share code, notes, and snippets.

@wiredsister
Created December 27, 2013 16:00
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 wiredsister/8148974 to your computer and use it in GitHub Desktop.
Save wiredsister/8148974 to your computer and use it in GitHub Desktop.
Help!
var margin = {top: 20, right: 20, bottom: 30, left: 50},
width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
var x = d3.time.scale()
.range([0, width]);
var y = d3.scale.linear()
.range([height, 0]);
var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom")
.ticks(d3.time.months)
.tickSize(16, 0)
.tickFormat(d3.time.format("%B"));
var yAxis = d3.svg.axis()
.scale(y)
.orient("left");
var line = d3.svg.line()
.x(function(d) { return x(d.month); })
.y(function(d) { return y(d.work); });
var svg = d3.select("#chart").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.tsv("../month.tsv", function(error, data) {
data.forEach(function(d) {
d.month = +d.month;
d.work = +d.work;
});
x.domain(d3.extend(data, function(d) {return d.month }));
y.domain(d3.extent(data, function(d) { return d.work }));
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis)
.selectAll(".tick text")
.style("text-anchor", "start")
.attr("x", 6)
.attr("y", 6);
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("Work");
svg.append("path")
.datum(data)
.attr("class", "line")
.attr("d", line);
});
@wiredsister
Copy link
Author

And here is the .tsv file:

month work
0 24509
1 19466
2 18004
3 18381
4 17312
5 19926
6 24761
7 24815
8 24333
9 29117
10 24527
11 17478

@wiredsister
Copy link
Author

I also saw this example for help:
http://bl.ocks.org/mbostock/1849162

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment