-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathd3.punchcard.js
144 lines (133 loc) · 3.86 KB
/
d3.punchcard.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
var pane_left = 120
, pane_right = 800
, width = pane_left + pane_right
, height = 520
, margin = 10
, i
, j
, tx
, ty
, max = 0
, data = [
[1, 0, 0, 0, 1, 1, 4, 5, 5, 1, 1, 1, 1, 1, 1, 2, 5, 5, 4, 1, 1, 1, 1, 0],
[1, 0, 0, 0, 1, 1, 4, 5, 5, 1, 1, 1, 1, 1, 1, 2, 5, 5, 4, 1, 1, 1, 1, 0],
[1, 0, 0, 0, 1, 1, 4, 5, 5, 1, 1, 1, 1, 1, 1, 2, 5, 5, 4, 1, 1, 1, 1, 0],
[1, 0, 0, 0, 1, 1, 4, 5, 5, 1, 1, 1, 1, 1, 1, 2, 5, 5, 4, 1, 1, 1, 1, 0],
[1, 0, 0, 0, 1, 1, 4, 5, 5, 1, 1, 1, 1, 1, 1, 2, 5, 5, 4, 1, 1, 1, 1, 0],
[1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 0, 0]
];
// X-Axis.
var x = d3.scale.linear().domain([0, 23]).
range([pane_left + margin, pane_right - 2 * margin]);
// Y-Axis.
var y = d3.scale.linear().domain([0, 6]).
range([2 * margin, height - 10 * margin]);
// The main SVG element.
var punchcard = d3.
select("#punchcard").
append("svg").
attr("width", width - 2 * margin).
attr("height", height - 2 * margin).
append("g");
// Hour line markers by day.
for (i in y.ticks(7)) {
punchcard.
append("g").
selectAll("line").
data([0]).
enter().
append("line").
attr("x1", margin).
attr("x2", width - 3 * margin).
attr("y1", height - 3 * margin - y(i)).
attr("y2", height - 3 * margin - y(i)).
style("stroke-width", 1).
style("stroke", "#efefef");
punchcard.
append("g").
selectAll(".rule").
data([0]).
enter().
append("text").
attr("x", margin).
attr("y", height - 3 * margin - y(i) - 5).
attr("text-anchor", "left").
text(["Sunday", "Saturday", "Friday", "Thursday", "Wednesday", "Tuesday", "Monday"][i]);
punchcard.
append("g").
selectAll("line").
data(x.ticks(24)).
enter().
append("line").
attr("x1", function(d) { return pane_left - 2 * margin + x(d); }).
attr("x2", function(d) { return pane_left - 2 * margin + x(d); }).
attr("y1", height - 4 * margin - y(i)).
attr("y2", height - 3 * margin - y(i)).
style("stroke-width", 1).
style("stroke", "#ccc");
}
// Hour text markers.
punchcard.
selectAll(".rule").
data(x.ticks(24)).
enter().
append("text").
attr("class", "rule").
attr("x", function(d) { return pane_left - 2 * margin + x(d); }).
attr("y", height - 3 * margin).
attr("text-anchor", "middle").
text(function(d) {
if (d === 0) {
return "12a";
} else if (d > 0 && d < 12) {
return d;
} else if (d === 12) {
return "12p";
} else if (d > 12 && d < 25) {
return d - 12;
}
});
// Data has array where indicy 0 is Monday and 6 is Sunday, however we draw
// from the bottom up.
data = data.reverse();
// Find the max value to normalize the size of the circles.
for (i = 0; i < data.length; i++) {
max = Math.max(max, Math.max.apply(null, data[i]));
}
// Show the circles on the punchcard.
for (i = 0; i < data.length; i++) {
for (j = 0; j < data[i].length; j++) {
punchcard.
append("g").
selectAll("circle").
data([data[i][j]]).
enter().
append("circle").
style("fill", "#888").
on("mouseover", mover).
on("mouseout", mout).
on("mousemove", function() {
return tooltip.
style("top", (d3.event.pageY - 10) + "px").
style("left", (d3.event.pageX + 10) + "px");
}).
attr("r", function(d) { return d / max * 14; }).
attr("transform", function() {
tx = pane_left - 2 * margin + x(j);
ty = height - 7 * margin - y(i);
return "translate(" + tx + ", " + ty + ")";
});
}
function mover(d) {
tooltip = d3.select("body")
.append("div")
.style("position", "absolute")
.style("z-index", "99999")
.attr("class", "vis-tool-tip")
.text(d);
}
function mout(d) {
$(".vis-tool-tip").fadeOut(50).remove();
}
}