Skip to content

Instantly share code, notes, and snippets.

@caged
Forked from michaelaguiar/d3_line.js
Created December 29, 2011 21:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save caged/1536332 to your computer and use it in GitHub Desktop.
Save caged/1536332 to your computer and use it in GitHub Desktop.
D3.js Line Chart Test
(function() {
$(function() {
var data, h, max, paths, pb, pl, pr, pt, ticks, version, vis, w, x, y, _ref;
version = Number(document.location.hash.replace('#', ''));
data = [[3, 7, 9, 1, 4, 6, 8, 2, 5], [5, 2, 3, 4, 9, 6, 4, 6, 8]];
_ref = [20, 20, 20, 20], pt = _ref[0], pl = _ref[1], pr = _ref[2], pb = _ref[3];
w = 800 - (pl + pr);
h = 300 - (pt + pb);
max = d3.max(data, function(d) {
return d3.max(d);
});
x = d3.scale.linear().domain([0, data[0].length - 1]).range([0, w]);
y = d3.scale.linear().domain([0, max]).range([h, 0]);
vis = d3.select('#chart').style('margin', '20px auto').style('width', "" + w + "px").append('svg:svg').attr('width', w + (pl + pr)).attr('height', h + pt + pb).attr('class', 'viz').append('svg:g').attr('transform', "translate(" + pl + "," + pt + ")");
paths = vis.selectAll('path.line').data(data).enter().append("svg:g");
paths.append('svg:path').attr("d", d3.svg.line().x(function(d, i) {
return x(i);
}).y(y));
ticks = vis.selectAll('.ticky').data(y.ticks(7)).enter().append('svg:g').attr('transform', function(d) {
return "translate(0, " + (y(d)) + ")";
}).attr('class', 'ticky');
ticks.append('svg:line').attr('y1', 0).attr('y2', 0).attr('x1', 0).attr('x2', w);
ticks.append('svg:text').text(function(d) {
return d;
}).attr('text-anchor', 'end').attr('dy', 2).attr('dx', -4);
ticks = vis.selectAll('.tickx').data(x.ticks(data[0].length)).enter().append('svg:g').attr('transform', function(d, i) {
return "translate(" + (x(i)) + ", 0)";
}).attr('class', 'tickx');
ticks.append('svg:line').attr('y1', h).attr('y2', 0).attr('x1', 0).attr('x2', 0);
ticks.append('svg:text').text(function(d, i) {
return i;
}).attr('y', h).attr('dy', 15).attr('dx', -2);
return paths.selectAll('.point').data(function(d) {
return d;
}).enter().append("svg:circle").attr("class", 'point').attr("r", 4).attr("cx", function(d, i) {
return x(i);
}).attr("cy", function(d) {
return y(d);
}).on('mouseover', function() {
return d3.select(this).attr('r', 8);
}).on('mouseout', function() {
return d3.select(this).attr('r', 4);
}).on('click', function(d, i) {
return console.log(d, i);
});
});
}).call(this);
<!DOCTYPE html>
<html>
<head>
<title>Line Chart</title>
<link type="text/css" rel="stylesheet" href="line.css"/>
<script type="text/javascript" src="../../d3.js"></script>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
</head>
<body>
<style>
text {
fill: #ccc;
}
path {
stroke-width: 2px;
stroke: #fcaf30;
}
.tickx line,
.ticky line {
stroke-width: 1px;
stroke: #b2b2b2;
stroke-opacity: 0.4;
shape-rendering: crispedges;
}
.tickx text,
.ticky text {
fill: #ccc;
font-size: 10px;
}
.point {
fill: #fcaf30;
}
</style>
<div id="chart"><script type="text/javascript" src="line.js"></script></div>
</body>
</html>
@michaelaguiar
Copy link

Works great!! I appreciate it man, this looks awesome.

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