Last active
November 30, 2020 10:03
-
-
Save fawcett/b9a3a0cc0112ec8d717c to your computer and use it in GitHub Desktop.
d3.js Area Chart Example
This file contains hidden or 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
<!DOCTYPE html> | |
<meta charset="utf-8"> | |
<style> | |
body { | |
font: 10px sans-serif; | |
} | |
.axis path, | |
.axis line { | |
fill: none; | |
stroke: #000; | |
shape-rendering: crispEdges; | |
} | |
.x.axis path { | |
display: none; | |
} | |
.area { | |
fill: steelblue; | |
} | |
</style> | |
<body> | |
<script src="//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 formatDate = d3.time.format("%Y-%m-%d %H:%M:%S"); | |
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 area = d3.svg.area() | |
.x(function(d) { return x(d.date); }) | |
.y0(height) | |
.y1(function(d) { return y(d.session_rate); }); | |
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("io.tsv", type, function(error, data) { | |
if (error) throw error; | |
x.domain(d3.extent(data, function(d) { return d.date; })); | |
y.domain(d3.extent(data, function(d) { return d.session_rate; })); | |
svg.append("g") | |
.attr("class", "x axis") | |
.attr("transform", "translate(0," + height + ")") | |
.call(xAxis); | |
svg.append("g") | |
.attr("class", "y axis") | |
.call(yAxis) | |
.append("text") | |
.attr("transform", "rotate(-90)") | |
.attr("y", 6) | |
.attr("dy", ".71em") | |
.style("text-anchor", "end") | |
.text("Logical IO"); | |
svg.append("path") | |
.datum(data) | |
.attr("class", "area") | |
.attr("d", area); | |
}); | |
function type(d) { | |
d.date = formatDate.parse(d.date); | |
d.session_rate = +d.session_rate; | |
return d; | |
} | |
</script> |
This file contains hidden or 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
date | session_rate | |
---|---|---|
2016-02-19 14:50:03 | 1445 | |
2016-02-19 14:51:03 | 2963 | |
2016-02-19 14:52:02 | 5342 | |
2016-02-19 14:53:03 | 40 | |
2016-02-19 14:54:02 | 8 | |
2016-02-19 14:55:03 | 120 | |
2016-02-19 14:56:02 | 1912 | |
2016-02-19 14:57:03 | 1995 | |
2016-02-19 14:58:03 | 26 | |
2016-02-19 14:59:03 | 2290 | |
2016-02-19 15:00:03 | 1809 | |
2016-02-19 15:01:02 | 971 | |
2016-02-19 15:02:02 | 55 | |
2016-02-19 15:03:03 | 10 | |
2016-02-19 15:04:03 | 640 | |
2016-02-19 15:05:02 | 2072 | |
2016-02-19 15:06:03 | 703 | |
2016-02-19 15:07:02 | 8581 | |
2016-02-19 15:08:03 | 6 | |
2016-02-19 15:09:03 | 14 | |
2016-02-19 15:10:03 | 3115 | |
2016-02-19 15:11:03 | 71 | |
2016-02-19 15:12:02 | 7 | |
2016-02-19 15:13:04 | 41 | |
2016-02-19 15:14:02 | 47 | |
2016-02-19 15:15:02 | 8 | |
2016-02-19 15:16:03 | 464 | |
2016-02-19 15:19:03 | 30 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
d3.svg.area throws an error - not a function. Can you please help to identify how to plot an area in this case? Thank you very much for your help.