-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroamhistogram.js
More file actions
162 lines (133 loc) · 4.09 KB
/
roamhistogram.js
File metadata and controls
162 lines (133 loc) · 4.09 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
var barSpacing = 1;
var barPadding = 20;
var barWidth = 300;
var barHeight = 100;
var roamBarData;
var barYScale, barXScale, barAxis2, svghist, labels, xAxis;
Array.max = function( array ){
return Math.max.apply( Math, array );
};
Array.min = function( array ){
return Math.min.apply( Math, array );
};
function init()
{
svghist = d3.select("#roamHist")
.append("svg")
.attr("class", "barchart")
.attr("width", barWidth)
.attr("height", barHeight);
svghist.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + (barHeight - barPadding) + ")")
.call(xAxis);
}
function makeScales(roamBarData)
{
var numBars = roamBarData.length;
//create bar scales
var countMin = Array.min(roamBarData.map(function(o){return o.count;}));
var countMax = Array.max(roamBarData.map(function(o){return o.count;}));
barYScale = d3.scale.log()
.domain([countMin, countMax])
.range([barPadding,barHeight-barPadding]);
var roamMin = Array.min(roamBarData.map(function(o){return o.duration;}));
var roamMax = Array.max(roamBarData.map(function(o){return o.duration;}));
barXScale = d3.scale.linear()
.domain([roamMin, roamMax])
.range([barPadding,barWidth-barPadding]);
xAxis = d3.svg.axis()
.scale(barXScale)
.ticks(numBars)
.tickSubdivide(true)
.orient("bottom");
}
init();
function histRefresh()
{
var histquery = 'SELECT floor(duration/10)*10 as duration, count(*) as count FROM roams WHERE ' + filter.roams.where + ' GROUP BY floor(duration/10)*10;';
var histurl = "jsonSQL.php?db=amz_bfi1&q=" + histquery;
//console.log(histurl);
var roamBarData= [];
d3.json(histurl, function(error, roamBarData) {
console.log("Histogram received with " + roamBarData.length + " data");
makeScales(roamBarData);
//Create Bar element
bars = svghist.selectAll(".bar").data(roamBarData);
labels = svghist.selectAll("text.label").data(roamBarData);
axis = svghist.selectAll(".x.axis");
numBars = roamBarData.length;
//BARS
bars.enter()
.append("rect")
.attr("class", "bar")
.attr("x", function(d,i){
return i*(barWidth/roamBarData.length);
})
.attr("y", function(d){
//return barHeight - barYScale(d.count) - barPadding;
return barHeight - barPadding;
})
.attr("width", barWidth/roamBarData.length - barSpacing)
.attr("height", function(d){
//return barYScale(d.count);
return 1;
})
.attr("fill", "red");
bars.transition()
.duration(1000)
.attr("x", function(d,i){
return i*(barWidth/roamBarData.length);
})
.attr("y", function(d){
return barHeight - barYScale(d.count) - barPadding;
})
.attr("width", barWidth/roamBarData.length - barSpacing)
.attr("height", function(d){
return barYScale(d.count);
});
bars.exit().transition()
.duration(1000)
//.attr("fill-opacity", 0)
.attr("x", function(d, i) {
var rectWidth = (barWidth / roamBarData.length);
return (i * rectWidth) + rectWidth*.5 - barSpacing + numBars;
})
.remove();
//LABELS
labels.enter()
.append("text")
.attr("class", "label")
.attr("font-family", "helvetica")
.attr("font-size", "8px")
.attr("fill", "black")
.attr("text-anchor", "middle")
.text(function(d) {
return d.count.toString();
})
.attr("x", function(d, i) {
var rectWidth = (barWidth / roamBarData.length);
return (i * rectWidth) + rectWidth*.5 - barSpacing;
})
.attr("y", function(d) {
return barHeight - barYScale(d.count) - 6; //spacing above bar
});
labels.transition()
.duration(1000)
.text(function(d) {
return d.count.toString();
})
.attr("x", function(d, i) {
var rectWidth = (barWidth / roamBarData.length);
return (i * rectWidth) + rectWidth*.5 - barSpacing;
})
.attr("y", function(d) {
return barHeight - barYScale(d.count) - 6; //spacing above bar
});
labels.exit()
.remove();
axis.transition()
.duration(1000)
.call(xAxis);
});
}