Last active
August 29, 2015 14:05
-
-
Save rinx/2d0c365e2737eac722b9 to your computer and use it in GitHub Desktop.
visualize a timecard data using d3.js
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; | |
shape-rendering: crispEdges; | |
} | |
.day { | |
fill: #eee; | |
stroke: #fff; | |
stroke-width: 3px; | |
} | |
.month { | |
fill: none; | |
stroke: #fff; | |
stroke-width: 5px; | |
} | |
.RdYlGn .q0-11{fill:rgb(255,255,229)} | |
.RdYlGn .q1-11{fill:rgb(247,252,185)} | |
.RdYlGn .q2-11{fill:rgb(217,240,163)} | |
.RdYlGn .q3-11{fill:rgb(173,221,142)} | |
.RdYlGn .q4-11{fill:rgb(120,198,121)} | |
.RdYlGn .q5-11{fill:rgb(65,171,93)} | |
.RdYlGn .q6-11{fill:rgb(35,132,67)} | |
.RdYlGn .q7-11{fill:rgb(0,104,55)} | |
.RdYlGn .q8-11{fill:rgb(0,69,41)} | |
.RdYlGn .q9-11{fill:rgb(0,69,41)} | |
.RdYlGn .q10-11{fill:rgb(0,69,41)} | |
</style> | |
<body> | |
<h1>lab_life</h1> | |
<script src="http://d3js.org/d3.v3.min.js"></script> | |
<script> | |
var width = 960, | |
height = 136, | |
cellSize = 17; // cell size | |
var targetYear = 2014; | |
var day = d3.time.format("%w"), | |
week = d3.time.format("%U"), | |
hour = d3.time.format("%H"), | |
percent = d3.format(".1%"), | |
format = d3.time.format("%Y/%m/%d"); | |
var color = d3.scale.quantize() | |
.domain([0, 1.0]) | |
.range(d3.range(11).map(function(d) { return "q" + d + "-11"; })); | |
var svg = d3.select("body").selectAll("svg") | |
.data(d3.range(targetYear, targetYear + 1)) | |
.enter().append("svg") | |
.attr("width", width) | |
.attr("height", height) | |
.attr("class", "RdYlGn") | |
.append("g") | |
.attr("transform", "translate(" + ((width - cellSize * 53) / 2) + "," + (height - cellSize * 7 - 1) + ")"); | |
svg.append("text") | |
.attr("transform", "translate(-6," + cellSize * 3.5 + ")rotate(-90)") | |
.style("text-anchor", "middle") | |
.text(function(d) { return d; }); | |
var rect = svg.selectAll(".day") | |
.data(function(d) { return d3.time.days(new Date(d, 0, 1), new Date(d + 1, 0, 1)); }) | |
.enter().append("rect") | |
.attr("class", "day") | |
.attr("width", cellSize) | |
.attr("height", cellSize) | |
.attr("x", function(d) { return week(d) * cellSize; }) | |
.attr("y", function(d) { return day(d) * cellSize; }) | |
.datum(format); | |
rect.append("title") | |
.text(function(d) { return d; }); | |
svg.selectAll(".month") | |
.data(function(d) { return d3.time.months(new Date(d, 0, 1), new Date(d + 1, 0, 1)); }) | |
.enter().append("path") | |
.attr("class", "month") | |
.attr("d", monthPath); | |
var calcHours = function(d) { | |
var endTime = Date.parse('2000/01/01 ' + String(d).split(' ')[2]), | |
startTime = Date.parse('2000/01/01 ' + String(d).split(' ')[1]); | |
return (endTime - startTime) / 3600000 / 24; | |
} | |
d3.text("timecard", "text/plane", function(error, txt) { | |
var data = d3.nest() | |
.key(function(d) { return d.split(' ')[0]; }) | |
.rollup(function(d) { return d; }) | |
.map(txt.split('\n')); | |
rect.filter(function(d) { return d in data; }) | |
.attr("class", function(d) { return "day " + color(calcHours(data[d])); }) | |
.select("title") | |
.text(function(d) { return d + "\n" + String(data[d]).split(' ')[1] + "->" + String(data[d]).split(' ')[2]; }); | |
}); | |
function monthPath(t0) { | |
var t1 = new Date(t0.getFullYear(), t0.getMonth() + 1, 0), | |
d0 = +day(t0), w0 = +week(t0), | |
d1 = +day(t1), w1 = +week(t1); | |
return "M" + (w0 + 1) * cellSize + "," + d0 * cellSize | |
+ "H" + w0 * cellSize + "V" + 7 * cellSize | |
+ "H" + w1 * cellSize + "V" + (d1 + 1) * cellSize | |
+ "H" + (w1 + 1) * cellSize + "V" + 0 | |
+ "H" + (w0 + 1) * cellSize + "Z"; | |
} | |
d3.select(self.frameElement).style("height", "2910px"); | |
</script> |
Author
rinx
commented
Aug 30, 2014
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment