Upgrade to jQM 1.1.0
[busui.git] / labs / busdelay.php
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
<!DOCTYPE html>
<meta charset="utf-8">
<title>Tesseract</title>
<style>
 
 
#charts {
  padding: 10px 0;
}
 
.chart {
  display: inline-block;
  height: 151px;
  margin-bottom: 20px;
}
 
.reset {
  padding-left: 1em;
  font-size: smaller;
  color: #ccc;
}
 
.background.bar {
  fill: #ccc;
}
 
.foreground.bar {
  fill: steelblue;
}
 
.axis path, .axis line {
  fill: none;
  stroke: #000;
  shape-rendering: crispEdges;
}
 
.axis text {
  font: 10px sans-serif;
}
 
.brush rect.extent {
  fill: steelblue;
  fill-opacity: .125;
}
 
.brush .resize path {
  fill: #eee;
  stroke: #666;
}
 
#hour-chart {
  width: 260px;
}
 
#delay-chart {
  width: 230px;
}
 
#distance-chart {
  width: 420px;
}
 
#date-chart {
  width: 920px;
}
 
#flight-list {
  min-height: 1024px;
}
 
#flight-list .date,
#flight-list .day {
  margin-bottom: .4em;
}
 
#flight-list .flight {
  line-height: 1.5em;
  background: #eee;
  width: 640px;
  margin-bottom: 1px;
}
 
#flight-list .time {
  color: #999;
}
 
#flight-list .flight div {
  display: inline-block;
  width: 100px;
}
 
#flight-list div.distance,
#flight-list div.delay {
  width: 160px;
  padding-right: 10px;
  text-align: right;
}
 
#flight-list .early {
  color: green;
}
 
aside {
  position: absolute;
  left: 740px;
  font-size: smaller;
  width: 220px;
}
 
</style>
 
 
<div id="charts">
  <div id="hour-chart" class="chart">
    <div class="title">Time of Day</div>
  </div>
  <div id="delay-chart" class="chart">
    <div class="title">Arrival Delay (min.)</div>
  </div>
  <div id="distance-chart" class="chart">
    <div class="title">Distance (mi.)</div>
  </div>
  <div id="date-chart" class="chart">
    <div class="title">Date</div>
  </div>
</div>
 
<aside id="totals"><span id="active">-</span> of <span id="total">-</span> flights selected.</aside>
 
<div id="lists">
  <div id="flight-list" class="list"></div>
</div>
 
 
</div>
 
 
<script src="../js/tesseract/tesseract.min.js"></script>
<script src="../js/d3/d3.v2.min.js"></script>
<script>
 
d3.csv("busdelay.csv.php", function(flights) {
 
  // Various formatters.
  var formatNumber = d3.format(",d"),
      formatChange = d3.format("+,d"),
      formatDate = d3.time.format("%B %d, %Y"),
      formatTime = d3.time.format("%I:%M %p");
 
  // A nest operator, for grouping the flight list.
  var nestByDate = d3.nest()
      .key(function(d) { return d3.time.day(d.date); });
 
  // A little coercion, since the CSV is untyped.
  flights.forEach(function(d, i) {
    d.index = i;
    d.date = parseDate(d.date);
    d.delay = +d.delay;
    d.distance = +d.distance;
  });
 
  // Create the tesseract and relevant dimensions and groups.
  flight = tesseract(flights),
  all = flight.groupAll(),
  date = flight.dimension(function(d) { return d3.time.day(d.date); }),
  dates = date.group(),
  hour = flight.dimension(function(d) { return d.date.getHours() + d.date.getMinutes() / 60; }),
  hours = hour.group(Math.floor),
  //delay = flight.dimension(function(d) { return Math.max(-60, Math.min(149, d.delay)); }),
  delay = flight.dimension(function(d) { return d.delay; }),
  delays = delay.group(function(d) { return Math.floor(d / 10) * 10; }),
  distance = flight.dimension(function(d) { return Math.min(60, d.distance); }),
  distances = distance.group(function(d) { return Math.floor(d / 50) * 50; });
 
  var charts = [
 
    barChart()
        .dimension(hour)
        .group(hours)
      .x(d3.scale.linear()
        .domain([0, 24])
        .rangeRound([0, 10 * 24])),
 
    barChart()
        .dimension(delay)
        .group(delays)
      .x(d3.scale.linear()
        .domain([-650, 650])
        .rangeRound([0, 10 * 21])),
 
    barChart()
        .dimension(distance)
        .group(distances)
      .x(d3.scale.linear()
        .domain([0, 60])
        .rangeRound([0, 10 * 40])),
 
    barChart()
        .dimension(date)
        .group(dates)
        .round(d3.time.day.round)
      .x(d3.time.scale()
        .domain([new Date(2011, 4, 1), new Date(2012, 1, 4)])
        .rangeRound([0, 10 * 90]))
        .filter([new Date(2011, 4, 4), new Date(2012, 4, 4)])
 
  ];
 
  // Given our array of charts, which we assume are in the same order as the
  // .chart elements in the DOM, bind the charts to the DOM and render them.
  // We also listen to the chart's brush events to update the display.
  var chart = d3.selectAll(".chart")
      .data(charts)
      .each(function(chart) { chart.on("brush", renderAll).on("brushend", renderAll); });
 
  // Render the initial lists.
  var list = d3.selectAll(".list")
      .data([flightList]);
 
  // Render the total.
  d3.selectAll("#total")
      .text(formatNumber(flight.size()));
 
  renderAll();
 
  // Renders the specified chart or list.
  function render(method) {
    d3.select(this).call(method);
  }
 
  // Whenever the brush moves, re-rendering everything.
  function renderAll() {
    chart.each(render);
    list.each(render);
    d3.select("#active").text(formatNumber(all.value()));
  }
 
  // Like d3.time.format, but faster.
  function parseDate(d) {
    return new Date(d);
  }
 
  window.filter = function(filters) {
    filters.forEach(function(d, i) { charts[i].filter(d); });
    renderAll();
  };
 
  window.reset = function(i) {
    charts[i].filter(null);
    renderAll();
  };
 
  function flightList(div) {
    var flightsByDate = nestByDate.entries(date.top(40));
 
    div.each(function() {
      var date = d3.select(this).selectAll(".date")
          .data(flightsByDate, function(d) { return d.key; });
 
      date.enter().append("div")
          .attr("class", "date")
        .append("div")
          .attr("class", "day")
          .text(function(d) { return formatDate(d.values[0].date); });
 
      date.exit().remove();
 
      var flight = date.order().selectAll(".flight")
          .data(function(d) { return d.values; }, function(d) { return d.index; });
 
      var flightEnter = flight.enter().append("div")
          .attr("class", "flight");
 
      flightEnter.append("div")
          .attr("class", "time")
          .text(function(d) { return formatTime(d.date); });
 
      flightEnter.append("div")
          .attr("class", "origin")
          .text(function(d) { return d.origin; });
 
      flightEnter.append("div")
          .attr("class", "destination")
          .text(function(d) { return d.destination; });
 
      flightEnter.append("div")
          .attr("class", "distance")
          .text(function(d) { return formatNumber(d.distance) + " mi."; });
 
      flightEnter.append("div")
          .attr("class", "delay")
          .classed("early", function(d) { return d.delay < 0; })
          .text(function(d) { return formatChange(d.delay) + " min."; });
 
      flight.exit().remove();
 
      flight.order();
    });
  }
 
  function barChart() {
    if (!barChart.id) barChart.id = 0;
 
    var margin = {top: 10, right: 10, bottom: 20, left: 10},
        x,
        y = d3.scale.linear().range([100, 0]),
        id = barChart.id++,
        axis = d3.svg.axis().orient("bottom"),
        brush = d3.svg.brush(),
        brushDirty,
        dimension,
        group,
        round;
 
    function chart(div) {
      var width = x.range()[1],
          height = y.range()[0];
 
      y.domain([0, group.top(1)[0].value]);
 
      div.each(function() {
        var div = d3.select(this),
            g = div.select("g");
 
        // Create the skeletal chart.
        if (g.empty()) {
          div.select(".title").append("a")
              .attr("href", "javascript:reset(" + id + ")")
              .attr("class", "reset")
              .text("reset")
              .style("display", "none");
 
          g = div.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 + ")");
 
          g.append("clipPath")
              .attr("id", "clip-" + id)
            .append("rect")
              .attr("width", width)
              .attr("height", height);
 
          g.selectAll(".bar")
              .data(["background", "foreground"])
            .enter().append("path")
              .attr("class", function(d) { return d + " bar"; })
              .datum(group.all());
 
          g.selectAll(".foreground.bar")
              .attr("clip-path", "url(#clip-" + id + ")");
 
          g.append("g")
              .attr("class", "axis")
              .attr("transform", "translate(0," + height + ")")
              .call(axis);
 
          // Initialize the brush component with pretty resize handles.
          var gBrush = g.append("g").attr("class", "brush").call(brush);
          gBrush.selectAll("rect").attr("height", height);
          gBrush.selectAll(".resize").append("path").attr("d", resizePath);
        }
 
        // Only redraw the brush if set externally.
        if (brushDirty) {
          brushDirty = false;
          g.selectAll(".brush").call(brush);
          div.select(".title a").style("display", brush.empty() ? "none" : null);
          if (brush.empty()) {
            g.selectAll("#clip-" + id + " rect")
                .attr("x", 0)
                .attr("width", width);
          } else {
            var extent = brush.extent();
            g.selectAll("#clip-" + id + " rect")
                .attr("x", x(extent[0]))
                .attr("width", x(extent[1]) - x(extent[0]));
          }
        }
 
        g.selectAll(".bar").attr("d", barPath);
      });
 
      function barPath(groups) {
        var path = [],
            i = -1,
            n = groups.length,
            d;
        while (++i < n) {
          d = groups[i];
          path.push("M", x(d.key), ",", height, "V", y(d.value), "h9V", height);
        }
        return path.join("");
      }
 
      function resizePath(d) {
        var e = +(d == "e"),
            x = e ? 1 : -1,
            y = height / 3;
        return "M" + (.5 * x) + "," + y
            + "A6,6 0 0 " + e + " " + (6.5 * x) + "," + (y + 6)
            + "V" + (2 * y - 6)
            + "A6,6 0 0 " + e + " " + (.5 * x) + "," + (2 * y)
            + "Z"
            + "M" + (2.5 * x) + "," + (y + 8)
            + "V" + (2 * y - 8)
            + "M" + (4.5 * x) + "," + (y + 8)
            + "V" + (2 * y - 8);
      }
    }
 
    brush.on("brushstart.chart", function() {
      var div = d3.select(this.parentNode.parentNode.parentNode);
      div.select(".title a").style("display", null);
    });
 
    brush.on("brush.chart", function() {
      var g = d3.select(this.parentNode),
          extent = brush.extent();
      if (round) g.select(".brush")
          .call(brush.extent(extent = extent.map(round)))
        .selectAll(".resize")
          .style("display", null);
      g.select("#clip-" + id + " rect")
          .attr("x", x(extent[0]))
          .attr("width", x(extent[1]) - x(extent[0]));
      dimension.filterRange(extent);
    });
 
    brush.on("brushend.chart", function() {
      if (brush.empty()) {
        var div = d3.select(this.parentNode.parentNode.parentNode);
        div.select(".title a").style("display", "none");
        div.select("#clip-" + id + " rect").attr("x", null).attr("width", "100%");
        dimension.filterAll();
      }
    });
 
    chart.margin = function(_) {
      if (!arguments.length) return margin;
      margin = _;
      return chart;
    };
 
    chart.x = function(_) {
      if (!arguments.length) return x;
      x = _;
      axis.scale(x);
      brush.x(x);
      return chart;
    };
 
    chart.y = function(_) {
      if (!arguments.length) return y;
      y = _;
      return chart;
    };
 
    chart.dimension = function(_) {
      if (!arguments.length) return dimension;
      dimension = _;
      return chart;
    };
 
    chart.filter = function(_) {
      if (_) {
        brush.extent(_);
        dimension.filterRange(_);
      } else {
        brush.clear();
        dimension.filterAll();
      }
      brushDirty = true;
      return chart;
    };
 
    chart.group = function(_) {
      if (!arguments.length) return group;
      group = _;
      return chart;
    };
 
    chart.round = function(_) {
      if (!arguments.length) return round;
      round = _;
      return chart;
    };
 
    return d3.rebind(chart, brush, "on");
  }
});
 
</script>