Skip to content

Instantly share code, notes, and snippets.

@darrenjaworski
Last active December 16, 2015 06:29
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 darrenjaworski/5392352 to your computer and use it in GitHub Desktop.
Save darrenjaworski/5392352 to your computer and use it in GitHub Desktop.
Area shaded line chart

The lake elevation of Thunderbird for the last two years. The Line chart is rendered with D3, and has a red area underneath the average elevation.

<!DOCTYPE html>
<html>
<head>
<style>
body {
font: 10px sans-serif;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.x.axis path {
display: none;
}
.area.above {
fill: #E89B9B;
}
.area.below {
fill: blue;
}
.line {
fill: none;
stroke: #000;
stroke-width: 1.5px;
}
</style>
</head>
<body>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script>
var margin = {top: 20, right: 20, bottom: 30, left: 50},
width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
var parseDate = d3.time.format("%b-%y").parse;
var x = d3.time.scale()
.range([0, width]);
var y = d3.scale.linear()
.range([height, 0]);
var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom");
var yAxis = d3.svg.axis()
.scale(y)
.orient("left");
var line = d3.svg.area()
.interpolate("basis")
.x(function(d) { return x(d.date); })
.y(function(d) { return y(d["Thunderbird"]); });
var area = d3.svg.area()
.interpolate("basis")
.x(function(d) { return x(d.date); })
.y1(function(d) { return y(d["Thunderbird"]); });
var svg = d3.select("body").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
d3.tsv("thunderbird.tsv", function(error, data) {
data.forEach(function(d) {
d.date = parseDate(d.date);
d["Thunderbird"]= +d["Thunderbird"];
d["Normal"] = +d["Normal"];
});
x.domain(d3.extent(data, function(d) { return d.date; }));
y.domain([
d3.min(data, function(d) { return Math.min(d["Thunderbird"], d["Normal"]); }),
d3.max(data, function(d) { return (Math.max(d["Thunderbird"], d["Normal"]+1)); })
]);
svg.datum(data);
svg.append("clipPath")
.attr("id", "clip-below")
.append("path")
.attr("d", area.y0(height));
svg.append("clipPath")
.attr("id", "clip-above")
.append("path")
.attr("d", area.y0(0));
svg.append("path")
.attr("class", "area above")
.attr("clip-path", "url(#clip-above)")
.attr("d", area.y0(function(d) { return y(d["Normal"]); }));
svg.append("path")
.attr("class", "area below")
.attr("clip-path", "url(#clip-below)")
.attr("d", area);
svg.append("path")
.attr("class", "line")
.attr("d", line);
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(1," + height + ")")
.call(xAxis);
svg.append("g")
.attr("class", "y axis")
.call(yAxis)
.append("text")
.attr("transform", "rotate(-90)")
.attr("transform", "translate(50, 0)")
.attr("y", 6)
.attr("dy", ".71em")
.style("text-anchor", "end")
.text("Elevation");
var legend = svg.selectAll(".legend")
//.data(ageNames.slice().reverse())
// .enter().append("g")
.attr("class", "legend")
.attr("transform", function(d, i) { return "translate(0," + i * 20 + ")"; });
legend.append("rect")
.attr("x", width - 18)
.attr("width", 18)
.attr("height", 18)
.style("fill", "#E89B9B");
legend.append("text")
.attr("x", width - 24)
.attr("y", 9)
.attr("dy", ".35em")
.style("text-anchor", "end")
.text(function(d) { return d; });
});
</script>
</body>
</html>
date Thunderbird Normal
Feb-11 1036.38 1039
Mar-11 1036.3 1039
Apr-11 1035.88 1039
May-11 1036.57 1039
Jun-11 1037.19 1039
Jul-11 1036.19 1039
Aug-11 1035.23 1039
Sep-11 1034.23 1039
Oct-11 1033.95 1039
Nov-11 1034.04 1039
Dec-11 1034.09 1039
Jan-12 1033.91 1039
Feb-12 1034.02 1039
Mar-12 1035.02 1039
Apr-12 1036.94 1039
May-12 1036.93 1039
Jun-12 1036.41 1039
Jul-12 1035.36 1039
Aug-12 1034.21 1039
Sep-12 1033.44 1039
Oct-12 1032.9 1039
Nov-12 1032.35 1039
Dec-12 1031.81 1039
Jan-13 1031.51 1039
Feb-13 1031.48 1039
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment