|
| 1 | +<!DOCTYPE html> |
| 2 | +<html lang="en"> |
| 3 | +<head> |
| 4 | + <title>dc.js - Bar Chart Example</title> |
| 5 | + <meta charset="UTF-8"> |
| 6 | + <link rel="stylesheet" type="text/css" href="../css/dc.css"/> |
| 7 | +</head> |
| 8 | +<body> |
| 9 | + |
| 10 | + <table id="test"> |
| 11 | + <thead> |
| 12 | + <tr> |
| 13 | + <th>Expt</th> |
| 14 | + <th>#Run</th> |
| 15 | + <th>Avg(Speed)</th> |
| 16 | + </tr> |
| 17 | + </thead> |
| 18 | + </table> |
| 19 | + |
| 20 | +<script type="text/javascript" src="../js/d3.js"></script> |
| 21 | +<script type="text/javascript" src="../js/crossfilter.js"></script> |
| 22 | +<script type="text/javascript" src="../js/dc.js"></script> |
| 23 | +<script type="text/javascript"> |
| 24 | + |
| 25 | +var chart = dc.dataTable("#test"); |
| 26 | +d3.csv("morley.csv", function(error, experiments) { |
| 27 | + /* We will create a table that sorts the experiments |
| 28 | + from the one that has the highest average speed |
| 29 | + to the one that has the lowest average speed |
| 30 | + */ |
| 31 | + /* To do so, we need: |
| 32 | + - that the .group() is defined, and returns the same value for all elements |
| 33 | + - the .order() is defined & defined to something else than the default value which seems to |
| 34 | + be either d3.ascending or descending. (a method that is undefined for .reduce()-d |
| 35 | + dimension groups is called else). |
| 36 | + */ |
| 37 | + var ndx = crossfilter(experiments), |
| 38 | + exptDimension = ndx.dimension(function(d) {return +d.Expt;}), |
| 39 | + groupedDimension = exptDimension.group().reduce( |
| 40 | + function (p, v) { |
| 41 | + ++p.number; |
| 42 | + p.total += +v.Speed; |
| 43 | + p.avg = Math.round(p.total / p.number); |
| 44 | + return p; |
| 45 | + }, |
| 46 | + function (p, v) { |
| 47 | + --p.number; |
| 48 | + p.total -= +v.Speed; |
| 49 | + p.avg = (p.number == 0) ? 0 : Math.round(p.total / p.number); |
| 50 | + return p; |
| 51 | + }, |
| 52 | + function () { |
| 53 | + return {number: 0, total: 0, avg: 0} |
| 54 | + }), |
| 55 | + rank = function (p) { return "rank" }; |
| 56 | + |
| 57 | + chart |
| 58 | + .width(768) |
| 59 | + .height(480) |
| 60 | + .dimension(groupedDimension) |
| 61 | + .group(rank) |
| 62 | + .columns([function (d) { return d.key }, |
| 63 | + function (d) { return d.value.number }, |
| 64 | + function (d) { return d.value.avg }]) |
| 65 | + .sortBy(function (d) { return d.value.avg }) |
| 66 | + .order(d3.descending) |
| 67 | + chart.render(); |
| 68 | +}); |
| 69 | + |
| 70 | +</script> |
| 71 | + |
| 72 | +</body> |
| 73 | +</html> |
0 commit comments