Skip to content

Instantly share code, notes, and snippets.

@michaelaguiar
Created December 29, 2011 19:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save michaelaguiar/1535916 to your computer and use it in GitHub Desktop.
Save michaelaguiar/1535916 to your computer and use it in GitHub Desktop.
D3.js Line Chart Test
(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);
<!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>
@caged
Copy link

caged commented Dec 29, 2011

You should move data2 to the array on line 16. No need to duplicate that code. Also, I'm not seeing black fill areas, but I remember adding something like that to a similar chart I was working on. rect's and paths might have a default fill color of black that you can remove with fill: none. Other than that, can you post a screenshot?

@michaelaguiar
Copy link
Author

Thanks! It turned out to be the default fill of the path, fill:none did the trick! One more question, if you don't mind. Line 35 where you are setting the points, how can I add data2 to that as well, so that points are applied to the second line? An even better solution would be a way to combine the data and data2 variables. I tried doing something like:

data = [
{data1 values},
{data2 values}
];

But that didn't work...

@caged
Copy link

caged commented Dec 29, 2011

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 each g element represents it's own little chunk of data.

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