Skip to content

Instantly share code, notes, and snippets.

@emeeks
Last active March 18, 2016 03:36
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save emeeks/a44c63ccd99fdfe12bdc to your computer and use it in GitHub Desktop.
Ch. 4, Fig. 20 - D3.js in Action

This is the code for Chapter 4, Figure 20 from D3.js in Action demonstrating several d3.svg.line() interpolation options.

<html>
<head>
<title>D3 in Action Chapter 4 - Example 11</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()
.interpolate("cardinal")
.x(function(d) {
return xScale(d.day)
})
.y(function(d) {
return yScale(d.tweets)
})
retweetLine = d3.svg.line()
.interpolate("basis")
.x(function(d) {
return xScale(d.day)
})
.y(function(d) {
return yScale(d.retweets)
})
favLine = d3.svg.line()
.interpolate("step-before")
.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>
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