Skip to content

Instantly share code, notes, and snippets.

@golsby
Forked from pearswj/data.tsv
Last active June 8, 2016 00:50
Show Gist options
  • Save golsby/9f3a046a871413f1e4f4baaf31f60345 to your computer and use it in GitHub Desktop.
Save golsby/9f3a046a871413f1e4f4baaf31f60345 to your computer and use it in GitHub Desktop.
Area chart for visualising open/closed issues
date resolved unresolved
20160507 101 28
20160508 102 26
20160509 102 26
20160510 107 23
20160511 113 19
20160512 114 21
20160513 114 21
20160514 114 21
20160515 114 21
20160516 118 17
20160517 121 18
20160518 124 14
20160519 126 13
20160520 126 13
20160521 126 13
20160522 126 15
20160523 127 14
20160524 128 15
20160525 129 15
20160526 130 15
20160527 130 15
20160528 130 15
20160529 131 15
20160530 134 12
20160531 134 12
20160601 134 12
20160602 134 12
20160603 134 12
20160604 134 12
20160605 134 12
<!DOCTYPE html>
<meta charset="utf-8">
<style>
body {
font: 10px sans-serif;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.axis {
font-size: 8pt;
}
.x.axis path {
/* display: none; */
}
.area.unresolved {
fill: rgb(200,200,255);
}
.line.unresolved {
fill: none;
stroke: #f00;
stroke-width: 2px;
}
.area.resolved {
fill: rgb(255,200,200);
}
.line.resolved {
fill: none;
stroke: #00f;
stroke-width: 2px;
}
.line {
fill: none;
stroke: #000;
stroke-width: 2px;
/* stroke-dasharray: 5, 5;
}
</style>
<body>
<script src="https://d3js.org/d3.v3.min.js" charset="utf-8"></script>
<script>
var margin = {top: 5, right: 40, bottom: 5, left: -2},
width = 80 + margin.left + margin.right,
height = 20 + margin.top + margin.bottom;
var parseDate = d3.time.format("%Y%m%d").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");
// TODO: make line out of "ignored" issues (currently an average)
//var line = d3.svg.area()
// .interpolate("basis")
// .x(function(d) { return x(d.date); })
// .y(function(d) { return y((d["resolved"] + d["unresolved"]) * 0.5); });
var line = d3.svg.area().x(function(d) { return x(d.date); }).y(function(d) { return 0; });
var area_unresolved = d3.svg.area()
.interpolate("linear")
.x(function(d) { return x(d.date); })
.y0(height)
.y1(function(d) { return y(d["unresolved"]); });
var line_unresolved = d3.svg.line()
.x(function(d) { return x(d.date); })
.y(function(d) { return y(d["unresolved"]); });
var area_resolved = d3.svg.area()
.interpolate("linear")
.x(function(d) { return x(d.date); })
.y0(0)
.y1(function(d) { return y(d["resolved"]); });
var line_resolved = d3.svg.line()
.x(function(d) { return x(d.date); })
.y(function(d) { return y(d["resolved"]); });
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("data.tsv", function(error, data) {
if (error) throw error;
data.forEach(function(d) {
d.date = parseDate(d.date);
d["unresolved"]= +d["unresolved"];
d["resolved"] = -d["resolved"];
});
x.domain(d3.extent(data, function(d) { return d.date; }));
y.domain([
d3.min(data, function(d) { return Math.min(d["resolved"], d["unresolved"]); }),
d3.max(data, function(d) { return Math.max(d["resolved"], d["unresolved"]); })
]);
var yAxis = d3.svg.axis()
.scale(y)
.tickValues(y.domain())
.orient("right");
svg.datum(data);
// draw the green area
svg.append("path")
.attr("class", "area resolved")
.attr("d", area_unresolved.y0(function(d) { return y(0); }));
// draw the red area
svg.append("path")
.attr("class", "area unresolved")
.attr("d", area_resolved.y0(function(d) { return y(0); }));
svg.append("path")
.attr("class", "line resolved")
.attr("d", line_resolved(data));
svg.append("path")
.attr("class", "line unresolved")
.attr("d", line_unresolved(data));
// draw a dashed line
//svg.append("path")
// .attr("class", "line")
// .attr("d", line);
//svg.append("g")
// .attr("class", "x axis")
// .attr("transform", "translate(0," + height + ")")
// .call(xAxis);
svg.append("g")
.attr("class", "y axis")
.attr("transform", "translate(" + width + " ,0)")
.call(yAxis)
.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", ".71em")
.style("text-anchor", "end")
// .text("Temperature (ºF)");
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment