This is the code for Chapter 4, Figure 19 from D3.js in Action demonstrating d3.svg.line().
Last active
March 18, 2016 03:36
-
-
Save emeeks/5901fbf392bcc55197b2 to your computer and use it in GitHub Desktop.
Ch. 4, Fig. 19 - D3.js in Action
This file contains 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
<html> | |
<head> | |
<title>D3 in Action Chapter 4 - Example 10</title> | |
<meta charset="utf-8" /> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js"></script> | |
</head> | |
<style> | |
svg { | |
height: 500px; | |
width: 500px; | |
border: 1px solid gray; | |
} | |
line { | |
shape-rendering: crispEdges; | |
stroke: #000000; | |
} | |
line.minor { | |
stroke: #777777; | |
stroke-dasharray: 2,2; | |
} | |
path.domain { | |
fill: none; | |
stroke: black; | |
} | |
</style> | |
<body> | |
<div id="viz"> | |
<svg> | |
</svg> | |
</div> | |
</body> | |
<footer> | |
<script> | |
d3.csv("tweetdata.csv", lineChart) | |
function lineChart(data) { | |
xScale = d3.scale.linear().domain([1,10.5]).range([20,480]); | |
yScale = d3.scale.linear().domain([0,35]).range([480,20]); | |
xAxis = d3.svg.axis() | |
.scale(xScale) | |
.orient("bottom") | |
.tickSize(480) | |
.tickValues([1,2,3,4,5,6,7,8,9,10]); | |
d3.select("svg").append("g").attr("id", "xAxisG").call(xAxis); | |
yAxis = d3.svg.axis() | |
.scale(yScale) | |
.orient("right") | |
.ticks(10) | |
.tickSize(480) | |
.tickSubdivide(true); | |
d3.select("svg").append("g").attr("id", "yAxisG").call(yAxis); | |
d3.select("svg").selectAll("circle.tweets") | |
.data(data) | |
.enter() | |
.append("circle") | |
.attr("class", "tweets") | |
.attr("r", 5) | |
.attr("cx", function(d) {return xScale(d.day)}) | |
.attr("cy", function(d) {return yScale(d.tweets)}) | |
.style("fill", "black") | |
d3.select("svg").selectAll("circle.retweets") | |
.data(data) | |
.enter() | |
.append("circle") | |
.attr("class", "retweets") | |
.attr("r", 5) | |
.attr("cx", function(d) {return xScale(d.day)}) | |
.attr("cy", function(d) {return yScale(d.retweets)}) | |
.style("fill", "lightgray") | |
d3.select("svg").selectAll("circle.favorites") | |
.data(data) | |
.enter() | |
.append("circle") | |
.attr("class", "favorites") | |
.attr("r", 5) | |
.attr("cx", function(d) {return xScale(d.day)}) | |
.attr("cy", function(d) {return yScale(d.favorites)}) | |
.style("fill", "gray"); | |
tweetLine = d3.svg.line() | |
.x(function(d) { | |
return xScale(d.day) | |
}) | |
.y(function(d) { | |
return yScale(d.tweets) | |
}) | |
retweetLine = d3.svg.line() | |
.x(function(d) { | |
return xScale(d.day) | |
}) | |
.y(function(d) { | |
return yScale(d.retweets) | |
}) | |
favLine = d3.svg.line() | |
.x(function(d) { | |
return xScale(d.day) | |
}) | |
.y(function(d) { | |
return yScale(d.favorites) | |
}) | |
d3.select("svg") | |
.append("path") | |
.attr("d", tweetLine(data)) | |
.attr("fill", "none") | |
.attr("stroke", "darkred") | |
.attr("stroke-width", 2) | |
d3.select("svg") | |
.append("path") | |
.attr("d", retweetLine(data)) | |
.attr("fill", "none") | |
.attr("stroke", "gray") | |
.attr("stroke-width", 3) | |
d3.select("svg") | |
.append("path") | |
.attr("d", favLine(data)) | |
.attr("fill", "none") | |
.attr("stroke", "black") | |
.attr("stroke-width", 2) | |
} | |
</script> | |
</footer> | |
</html> |
This file contains 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
day | tweets | retweets | favorites | |
---|---|---|---|---|
1 | 1 | 2 | 5 | |
2 | 6 | 11 | 3 | |
3 | 3 | 8 | 1 | |
4 | 5 | 14 | 6 | |
5 | 10 | 29 | 16 | |
6 | 4 | 22 | 10 | |
7 | 3 | 14 | 1 | |
8 | 5 | 18 | 7 | |
9 | 1 | 30 | 22 | |
10 | 4 | 16 | 15 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment