-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcrossfilter-practice.html
173 lines (144 loc) · 5.93 KB
/
crossfilter-practice.html
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
<!DOCTYPE html>
<html>
<head>
<title>crossfilter practice</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/dc/2.0.0-beta.32/dc.css">
</head>
<body>
<div id="chart-line-hitsperday"></div>
<div id="heat-map"></div>
<div id="chart-ring-year"></div>
<div style='clear:both;'>
<table id="dc-data-table">
<thead>
<tr class="header">
<th>Day</th>
</tr>
</thead>
</table>
</div>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/crossfilter/1.3.12/crossfilter.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.17/d3.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/dc/2.0.0-beta.32/dc.min.js"></script>
<script>
var data = [
{date: "2011-11-14T16:17:54Z", quantity: 2, total: 190, tip: 100, type: "tab"},
{date: "2011-11-14T16:20:19Z", quantity: 2, total: 190, tip: 100, type: "tab"},
{date: "2011-11-14T16:28:54Z", quantity: 1, total: 300, tip: 200, type: "visa"},
{date: "2011-11-14T16:30:43Z", quantity: 2, total: 90, tip: 0, type: "tab"},
{date: "2011-11-14T16:48:46Z", quantity: 2, total: 90, tip: 0, type: "tab"},
{date: "2011-11-14T16:53:41Z", quantity: 2, total: 90, tip: 0, type: "tab"},
{date: "2011-11-14T16:54:06Z", quantity: 1, total: 100, tip: 0, type: "cash"},
{date: "2011-11-14T16:58:03Z", quantity: 2, total: 90, tip: 0, type: "tab"},
{date: "2011-11-14T17:07:21Z", quantity: 2, total: 90, tip: 0, type: "tab"},
{date: "2011-11-14T17:22:59Z", quantity: 2, total: 90, tip: 0, type: "tab"},
{date: "2011-11-14T17:25:45Z", quantity: 2, total: 200, tip: 0, type: "cash"},
{date: "2011-11-14T17:29:52Z", quantity: 1, total: 200, tip: 100, type: "visa"}
];
function print_filter(filter){
var f=eval(filter);
if (typeof(f.length) != "undefined") {}else{}
if (typeof(f.top) != "undefined") {f=f.top(Infinity);}else{}
if (typeof(f.dimension) != "undefined") {f=f.dimension(function(d) { return "";}).top(Infinity);}else{}
console.log(filter+"("+f.length+") = "+JSON.stringify(f).replace("[","[\n\t").replace(/}\,/g,"},\n\t").replace("]","\n]"));
}
var ndx = crossfilter(data);
var typeDim = ndx.dimension(function(d) {return d.type;});
var cash_total = ndx.groupAll().reduceSum(function(d) {return d.total;});
console.log(cash_total);
console.log(cash_total.value());
var cash_and_visa_filter = typeDim.filter(function(d) { if (d ==="visa" || d==="cash") {return d;} });
console.log(cash_and_visa_filter);
// console.log(JSON.stringify(cash_and_visa_filter));
// print_filter("cash_and_visa_filter");
// console.log(JSON.stringify(eval("cash_and_visa_filter")));
// console.log(JSON.stringify(cash_and_visa_filter).replace("[","[\n\t").replace(/}\,/g,"},\n\t").replace("]","\n]"));
var a = cash_and_visa_filter.top();console.log(a);
data = [
{date: "12/27/2012", http_404: 2, http_200: 190, http_302: 100},
{date: "12/28/2012", http_404: 2, http_200: 10, http_302: 100},
{date: "12/29/2012", http_404: 1, http_200: 300, http_302: 200},
{date: "12/30/2012", http_404: 2, http_200: 90, http_302: 0},
{date: "12/31/2012", http_404: 2, http_200: 90, http_302: 0},
{date: "01/01/2013", http_404: 2, http_200: 90, http_302: 0},
{date: "01/02/2013", http_404: 1, http_200: 10, http_302: 1},
{date: "01/03/2013", http_404: 2, http_200: 90, http_302: 0},
{date: "01/04/2013", http_404: 2, http_200: 90, http_302: 0},
{date: "01/05/2013", http_404: 2, http_200: 90, http_302: 0},
{date: "01/06/2013", http_404: 2, http_200: 200, http_302: 1},
{date: "01/07/2013", http_404: 1, http_200: 200, http_302: 100}
];
ndx = crossfilter(data);
var parseDate = d3.time.format("%m/%d/%Y").parse;
data.forEach(function(d) {
d.date = parseDate(d.date);
d.total= d.http_404+d.http_200+d.http_302;
});
print_filter("data");
var dateDim = ndx.dimension(function(d) {return d.date;});
var hits = dateDim.group().reduceSum(function(d) {return d.total;})
// var hits = dateDim.group().reduceSum(dc.pluck('total'));
print_filter("hits");
var minDate = dateDim.bottom(1)[0].date;
var maxDate = dateDim.top(1)[0].date;
console.log(minDate, "\n",maxDate);
var hitslineChart = dc.lineChart("#chart-line-hitsperday");
var heatMapChart = dc.heatMap('#heat-map');
// hitslineChart
// .width(500)
// .height(500)
// .dimension(dateDim)
// .group(hits)
// .yAxisLabel("Hits per day")
// .brushOn(true)
// .x(d3.time.scale().domain([minDate,maxDate]));
heatMapChart
.width(500)
.height(200)
.dimension(dateDim)
.group(hits);
data.forEach(function(d) {
// d.date = parseDate(d.date);
// d.total= d.http_404+d.http_200+d.http_302;
d.Year=d.date.getFullYear();
});
var yearDim = ndx.dimension(function(d) {return +d.Year;});
var year_total = yearDim.group().reduceSum(function(d) {return d.total;});
var yearChart = dc.pieChart("#chart-ring-year");
yearChart
.width(200)
.height(200)
.dimension(yearDim)
.innerRadius(50)
.controlsUseVisibility(true)
.group(year_total);
var status_200=dateDim.group().reduceSum(function(d) {return d.http_200;});
var status_302=dateDim.group().reduceSum(function(d) {return d.http_302;});
var status_404=dateDim.group().reduceSum(function(d) {return d.http_404;});
hitslineChart
.width(500).height(200)
.dimension(dateDim)
.group(status_200,"200")
.stack(status_302,"302")
.stack(status_404,"404")
.renderArea(true)
.x(d3.time.scale().domain([minDate,maxDate]))
.brushOn(false)
.legend(dc.legend().x(50).y(10).itemHeight(13).gap(5))
.yAxisLabel("Hits per day");
var datatable = dc.dataTable("#dc-data-table");
datatable
.dimension(dateDim)
.group(function(d) {return d.Year;})
// dynamic columns creation using an array of closures
.columns([
function(d) { return d.date.getDate() + "/" + (d.date.getMonth() + 1) + "/" + d.date.getFullYear(); },
function(d) {return d.http_200;},
function(d) {return d.http_302;},
function(d) {return d.http_404;},
function(d) {return d.total;}
]);
dc.renderAll();
</script>
</body>
</html>