Created
December 29, 2011 19:54
-
-
Save michaelaguiar/1535916 to your computer and use it in GitHub Desktop.
D3.js Line Chart Test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function() { | |
$(function() { | |
var data, h, max, 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]; | |
data2 = [1, 4, 7, 0, 2, 2, 4, 1, 3]; | |
_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); | |
x = d3.scale.linear().domain([0, data.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 + ")"); | |
vis.selectAll('path.line').data([data, data2]).enter().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:text').text(function(d) { | |
return d; | |
}).attr('text-anchor', 'end').attr('dy', 2).attr('dx', -4); | |
ticks = vis.selectAll('.tickx').data(x.ticks(data.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); | |
vis.selectAll('.point').data(data).enter() | |
.append("svg:circle").attr("class", 'point') | |
.attr('r', '3') | |
.attr("cx", function(d, i) { return x(i); }) | |
.attr("cy", function(d) { return y(d); }) | |
.on('mouseover', function() { return d3.select(this).attr('r', 5); }) | |
.on('mouseout', function() { return d3.select(this).attr('r', 3); }) | |
.on('click', function(d, i) { return console.log(d, i); }); | |
}); | |
}).call(this); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
See my fork. It needed a little bit of refactoring because paths can't have nested elements. By adding paths to
<g>
elements, you can easily add as many lines and points as you want and eachg
element represents it's own little chunk of data.