Skip to content

Instantly share code, notes, and snippets.

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

This is the code for Chapter 4, Figure 22 from D3.js in Action which loads data and creates several interpolated lines using d3.svg.line().

<html>
<head>
<title>D3 in Action Chapter 4 - Example 12</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("movies.csv", areaChart)
function areaChart(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);
for (x in data[0]) {
if (x != "day") {
movieArea = d3.svg.line()
.x(function(d) {
return xScale(d.day)
})
.y(function(d) {
return yScale(d[x])
})
.interpolate("cardinal")
d3.select("svg")
.append("path")
.style("id", x + "Area")
.attr("d", movieArea(data))
.attr("fill", "none")
.attr("stroke", "black")
.attr("stroke-width", 3)
.style("opacity", .75)
}
}
}
</script>
</footer>
</html>
day movie1 movie2 movie3 movie4 movie5 movie6
1 20 8 3 0 0 0
2 18 5 1 13 0 0
3 14 3 1 10 0 0
4 7 3 0 5 27 15
5 4 3 0 2 20 14
6 3 1 0 0 10 13
7 2 0 0 0 8 12
8 0 0 0 0 6 11
9 0 0 0 0 3 9
10 0 0 0 0 1 8
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment