-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathopenhours-d3.js
218 lines (184 loc) · 6.24 KB
/
openhours-d3.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
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
218
$(function() {
d3.select("#ChartArea").append('svg:svg').attr("id",'ChartSVG');
find_open_restaurants('rest_hours.csv', new Date(), function(openSpots) {
render(openSpots);
});
});
var margin = {top: 20, right: 20, bottom: 30, left: 40},
width = 960 - margin.left - margin.right,
height = 700 - margin.top - margin.bottom;
var d3methods = {
hr_offset: 4,
red_x: undefined
};
d3methods.key = function(d) {
return d.name;
};
d3methods.setY = function(d, i) {
return 'translate(0,'+ (((i+1)*12)+margin.top) +')';
};
d3methods.reverseScale = d3.scale.linear().domain([0, width]).range([d3methods.hr_offset, 29]);
d3methods.xScale = d3.scale.linear().domain([d3methods.hr_offset, 29]).range([0, width]),
d3methods.xValue = function(d) { return d3methods.xScale(d.close - (d.open-d3methods.hr_offset)); };
d3methods.dragmove = function(d) {
d3methods.red_x += d3.event.dx;
d3.select(this).attr("transform", "translate(" + d3methods.red_x + "," + (margin.top+1) + ")");
var hrs = d3methods.reverseScale(d3methods.red_x);
var tmp = new Date();
if(hrs >= 24) {
hrs -= 24;
tmp.setDate(tmp.getDate()-1);
}
var today = new Date(tmp.getFullYear(), tmp.getMonth(), tmp.getDate(), Math.floor(hrs), Math.floor((hrs%1)*60), 0, 0);
find_open_restaurants('rest_hours.csv', today, function(openSpots) {
redraw(openSpots);
});
};
var redraw = function(dataset) {
var vis = d3.select("#ChartSVG");
var gBar = vis.selectAll("g.bar-group");
gBar = gBar.data(dataset, d3methods.key);
gBar.exit().attr("opacity", 0.25)
.transition()
.duration(300)
.attr("transform", function(d, i) {
var x1 = d3methods.xScale(d.open);
var x2 = x1 + d3methods.xValue(d);
var translateX;
// to move left/right depending where the red line is
if(d3methods.red_x >= x2) {
translateX = -700;
} else if(d3methods.red_x <= x1) {
translateX = width + 700;
}
return 'translate('+ translateX +','+ this._y +')';
})
.remove();
gBar.attr("opacity", 0.75)
.transition()
.duration(250)
.attr("transform", d3methods.setY)
.each('end', function() {
d3.select(this).attr("opacity", 1);
var coordsRaw = d3.select(this).attr("transform");
var coordsRegex = /(\d+)/g;
if(coordsRaw) {
var coords = coordsRaw.match(coordsRegex);
this._y = coords[1];
}
});
var group = gBar.enter()
.append("svg:g")
.attr("class", 'bar-group')
.attr("transform", d3methods.setY);
group.append('text')
.attr("class", 'restaurant-names')
.attr("x", function(d){
return d3methods.xScale(d.open)-10;
})
.attr("y", 4)
.attr("text-anchor", "end")
.text(function(d){
return d.name;
});
group.append('rect')
.attr("class", 'rect-rest')
.attr("x", function(d){
return d3methods.xScale(d.open);
})
.attr("width", d3methods.xValue)
.attr("height", 4)
.on("mouseover", function() {
d3.select(this).transition()
.duration(100)
.attr("height", 6)
.attr("transform", "translate(0,-1)");
})
.on("mouseout", function() {
d3.select(this).transition()
.duration(100).attr('height', 4)
.attr("transform", "translate(0,0)");
});
// bring the current time group to the front again
var current = d3.select("g.current-time-group").node();
current.parentNode.appendChild(current);
}
var render = function(dataset) {
var vis = d3.select("#ChartSVG");
// x-axis
var xAxis = d3.svg.axis().scale(d3methods.xScale).orient("top").tickSize(0).tickFormat(function(d){
return helpers.to12Hr(d%24);
});
vis.append("g")
.attr("class", 'x-axis')
.attr("transform", "translate(0,20)")
.call(xAxis);
// rules
var x_rules = vis.append("g").attr("class", 'x-rules');
x_rules.selectAll("line.rule")
.data(d3methods.xScale.ticks(29))
.enter().append("line")
.attr("class", 'rule')
.attr("x1", d3methods.xScale)
.attr("x2", d3methods.xScale)
.attr("y1", margin.top)
.attr("y2", height);
// all the open restaurants
var gBar = vis.selectAll("g.bar-group");
gBar = gBar.data(dataset, d3methods.key);
var group = gBar.enter().append("svg:g")
.attr("class", 'bar-group')
.attr("transform", d3methods.setY);
group.append('text')
.attr("class", 'restaurant-names')
.attr("x", function(d){
return d3methods.xScale(d.open)-10;
})
.attr("y", 4)
.attr("text-anchor", "end")
.text(function(d){
return d.name;
});
group.append('rect')
.attr("class", 'rect-rest')
.attr("x", function(d){
return d3methods.xScale(d.open);
})
.attr("width", d3methods.xValue)
.attr("height", 4)
.on("mouseover", function() {
d3.select(this).transition()
.duration(100)
.attr("height", 6)
.attr("transform", "translate(0,-1)");
})
.on("mouseout", function() {
d3.select(this).transition()
.duration(100).attr('height', 4)
.attr("transform", "translate(0,0)");
});
var currentTime = new Date();
var rightNow = (currentTime.getHours() < helpers._cutoff) ? currentTime.getHours()+24 : currentTime.getHours();
rightNow += Math.round((currentTime.getMinutes()/60)*10000)/10000;
d3methods.red_x = d3methods.xScale(rightNow);
// group representing current time
var gCurrent = vis.append("svg:g")
.attr("class", 'current-time-group')
.attr("transform", 'translate('+ d3methods.red_x +','+ (margin.top+1) +')')
.call(d3.behavior.drag().on("drag", d3methods.dragmove));
// the red line
gCurrent.append("line")
.attr("class", 'current-time')
.attr("x1", 0)
.attr("x2", 0)
.attr("y1", 0)
.attr("y2", height-margin.top-1);
// to make the click area bigger
gCurrent.append('rect')
.attr("class", 'current-clickoverlay')
.attr("x", -8)
.attr("y", 0)
.attr("width", 16)
.attr("height", height-margin.top-1)
.attr("opacity", 0);
};