-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcalendar-chart.js
More file actions
217 lines (187 loc) · 5.4 KB
/
calendar-chart.js
File metadata and controls
217 lines (187 loc) · 5.4 KB
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
dc.calendarChart = function (parent, chartGroup) {
var thisYear = new Date().getFullYear();
var _chart = dc.marginMixin(dc.baseMixin({}));
var color;
var width = 900,
height = 120,
cellSize = 16; // cell size
_chart.SELECTED_CLASS = "day-selected";
_chart.DESELECTED_CLASS = "day-deselected";
_chart.range = d3.range(thisYear, thisYear + 1);
var day = d3.time.format("%w"),
week = d3.time.format("%U"),
percent = d3.format(".1%"),
format = d3.time.format("%Y-%m-%d"),
fullMonth = d3.time.format("%b");
var dowMap = {
"M" : 2,
"W" : 4,
"F" : 6
};
_chart._doRedraw = function() {
_chart._doRender();
_highlightFilters();
return _chart;
};
_chart._doRender = function () {
d3.select("#"+ _chart.anchorName())
.selectAll("svg")
.remove();
var svg = d3.select("#"+ _chart.anchorName())
.selectAll("svg")
.data(_chart.range)
.enter().append("svg")
.style("padding",'3px')
.attr("width", width + _chart.margins().left + (cellSize * 3))
.attr("height", height + _chart.margins().top + cellSize)
.attr("class", "RdYlGn")
.append("g")
.attr("transform", "translate(" + _chart.margins().left + "," + _chart.margins().top + ")");
svg.append("text")
.attr("transform", "translate(-16," + cellSize * 3.5 + ")rotate(-90)")
.style("text-anchor", "middle")
.text(function(d) { return d; });
if(_chart.renderTitle()){
var dowLabel = svg.selectAll('.dowLabel')
.data(function(d){return ["M","W","F"];})
.enter().append("text")
.attr('transform', function(d){
return "translate(-15," + parseInt((cellSize * dowMap[d]) - 3) + ")";})
.text(function(d) { return d; })
.attr("style","font-weight : bold");
}
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);
// .attr("data-ot",function(d){
// return d;
// });
if(_chart.renderTitle()){
rect.append("title")
.text(function(d) { return d; });
}
var monthLabel = svg.selectAll(".monthLabel")
.data(function(d) { return d3.time.months(new Date(d, 0, 1), new Date(d + 1, 0, 1)); })
.enter().append("text")
.text(function(d){ return fullMonth(d);})
.attr("x", function(d){return week(d) * cellSize /*+ cellSize*/;})
.attr("y", -3)
.attr("class","monthLabel");
var data = d3.nest()
.key(function(d) { return d.key; })
.rollup(function(d) {
return _chart.valueAccessor()(d);
})
.map(_chart.data());
if(!color){
color = d3.scale.quantize()
.domain([-4,4])
.range(d3.range(11).map(function(d) {
return "q" + d + "-11";
}));
}
rect.filter(function(d) {
var date = simpleDate(d);
return date in data;
})
.attr("class", function(d) {
var date = simpleDate(d);
return "day " + color(data[date]); })
.on('click', onClick);
// .attr("data-ot",function(d){
// var date = simpleDate(d);
// return d + ": " + data[date].toFixed(2) + " Daily Average";
// });
if(_chart.renderTitle()){
rect.filter(function(d) {
var date = simpleDate(d);
return date in data; })
.select("title")
.text(function(d) {
var date = simpleDate(d);
return date + ": " + data[date].toFixed(2) + " Daily Average";
});
}
return _chart;
};
function onClick(d, i) {
var dateClicked = simpleDate(d);
_chart.group().all().forEach(function(datum){
if(datum.key === dateClicked){
_chart.onClick(datum, i);
}
});
}
function prefixZero(value) {
var s = value + "";
if(s.length === 1){
return "0" + value;
}else{
return value;
}
}
function simpleDate(date){
return date.getFullYear() + "-" + prefixZero(date.getMonth() + 1) + "-" + prefixZero(date.getDate());
}
_chart.legendables = function () {
// do nothing in base, should be overridden by sub-function
return [];
};
_chart.legendHighlight = function (d) {
// do nothing in base, should be overridden by sub-function
};
_chart.legendReset = function (d) {
// do nothing in base, should be overridden by sub-function
};
_chart.legendToggle = function (d) {
// do nothing in base, should be overriden by sub-function
};
_chart.isLegendableHidden = function (d) {
// do nothing in base, should be overridden by sub-function
return false;
};
//custom overrides for calendarChart since standard selected and deselected
//classes for DC make the chart look bad
_chart.highlightSelected = function (e) {
d3.select(e).classed(_chart.SELECTED_CLASS, true);
d3.select(e).classed(_chart.DESELECTED_CLASS, false);
};
_chart.fadeDeselected = function (e) {
d3.select(e).classed(_chart.SELECTED_CLASS, false);
d3.select(e).classed(_chart.DESELECTED_CLASS, true);
};
_chart.resetHighlight = function (e) {
d3.select(e).classed(_chart.SELECTED_CLASS, false);
d3.select(e).classed(_chart.DESELECTED_CLASS, false);
};
_chart.rangeYears = function(range){
_chart.range = d3.range(range[0], range[1]);
return _chart;
}
function _highlightFilters() {
if (_chart.hasFilter()) {
var chartData = _chart.group().all();
_chart.root().selectAll('.day').each(function (d) {
if (_chart.hasFilter(simpleDate(d))) {
_chart.highlightSelected(this);
}
else {
_chart.fadeDeselected(this);
}
});
}
else {
_chart.root().selectAll('.day').each(function (d) {
_chart.resetHighlight(this);
});
}
}
return _chart.anchor(parent, chartGroup);
};