Skip to content

Instantly share code, notes, and snippets.

@sdawson
Created April 11, 2013 08:20
Show Gist options
  • Save sdawson/5361671 to your computer and use it in GitHub Desktop.
Save sdawson/5361671 to your computer and use it in GitHub Desktop.
line_chart1
{"description":"line_chart1","endpoint":"","display":"svg","public":true,"require":[],"fileconfigs":{"inlet.js":{"default":true,"vim":false,"emacs":false,"fontSize":12},"_.md":{"default":true,"vim":false,"emacs":false,"fontSize":12},"config.json":{"default":true,"vim":false,"emacs":false,"fontSize":12},"test_data.csv":{"default":true,"vim":false,"emacs":false,"fontSize":12},"style.css":{"default":true,"vim":false,"emacs":false,"fontSize":12},"freq.tsv":{"default":true,"vim":false,"emacs":false,"fontSize":12}},"fullscreen":false,"play":false,"loop":false,"restart":false,"autoinit":true,"pause":true,"loop_type":"period","bv":false,"nclones":15,"clone_opacity":0.4,"duration":3000,"ease":"linear","dt":0.01}
// Testing out mbostock's simple line chart eg.
var data = tributary.test_data;
data.forEach(function(d) {
d.x = +d.x;
d.y = +d.y;
});
console.log(data);
var svg = d3.select("svg");
var margin = {top: 20, right: 20, bottom: 30, left: 50},
width = 950 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
var x = d3.scale.linear()
.range([0, width]);
var y = d3.scale.linear()
.range([height, 0]);
var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom");
var yAxis = d3.svg.axis()
.scale(y)
.orient("left");
var line = d3.svg.line()
.x(function(d) {
//console.log(d);
return x(d.x); })
.y(function(d) {
console.log(d);
return y(d.y); });
svg.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
//x.domain(d3.extent(data, function(d) { return d.x; }));
x.domain([0, d3.max(data, function(d) { return d.x; })]);
//y.domain(d3.extent(data, function(d) { return d.y; }));
y.domain([0, d3.max(data, function(d) { return d.y; })]);
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(40," + height + ")")
.call(xAxis);
svg.append("g")
.attr("class", "y axis")
.attr("transform", "translate(40, 0)")
.call(yAxis)
.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", ".71em")
.style("text-anchor", "end")
.text("Price ($)");
svg.append("path")
.datum(data)
.attr("class", "line")
.attr("d", line);
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.line {
fill: none;
stroke: steelblue;
stroke-width = 1.5px;
}
.x.axis path {
display: none;
}
x y
1 5
2 60
3 42
4 109
5 27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment