-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
213 lines (184 loc) · 8.76 KB
/
index.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
<!DOCTYPE html>
<html>
<head>
<?php include 'meta.php'; ?>
<title>DPLA Visualizations</title>
<link href='http://fonts.googleapis.com/css?family=Raleway' rel='stylesheet' type='text/css'>
<link rel="stylesheet" href="style.css" />
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script>
<script type="text/javascript" src="http://d3js.org/d3.v3.min.js"></script>
<script type="text/javascript">
$(function() {
$('#d3-dpla').submit(function(e) {
e.preventDefault();
var q = $('#q').val();
var message = $('#message').text("Your search phrase: " + q);
var hide = $('#hide');
hide.removeClass('hide');
if(q) {
$('svg, #records').detach();
$.getJSON("DplaHistogram.php?q=" + q, function(data) {
var count = 0;
for(var j=0; j<data.length; j++) {
count += data[j].count;
}
if(count > 0) {
var margin = {top: 30, right: 20, bottom: 50, left: 50},
axisPadding = 5,
height = 450 - margin.top - margin.bottom,
width = 1100 - margin.left - margin.right;
var ticks = [];
for(var i=0; i<data.length; i++) {
ticks.push(data[i].decade);
}
var div = d3.select("body").append("div")
.attr("class", "tooltip")
.style("opacity", 0);
var svg = d3.select("body").append("svg")
.attr("height", height + margin.top + margin.bottom)
.attr("width", width);
var xScale = d3.scale.ordinal()
.domain(ticks)
.rangeRoundBands([53, width], 0.05);
var yScale = d3.scale.linear()
.domain([0,d3.max(data, function(d) { return d.count; })])
.range([0, height]);
var yTickScale = d3.scale.linear()
.domain([d3.max(data, function(d) { return d.count; }), 0])
.range([0, height]);
svg.selectAll("rect")
.data(data, function(d) {
return d.decade;
})
.enter()
.append("rect")
.attr("x", function(d) {
return xScale(d.decade);
})
.attr("y", function(d) {
return height - yScale(d.count) + axisPadding;
})
.attr("width", xScale.rangeBand())
.attr("height", function(d) {
return yScale(d.count);
})
.attr("fill", "steelblue")
.on("mouseover", function(d) {
div.transition()
.duration(200)
.style("opacity", .9);
div .html("Your term(s) appeared in <br/>" + d.count + " records in the " + d.decade + "'s"
+ "<br/><br/>Click highlighted bar to view records")
.style("left", (d3.event.pageX - 28) + "px")
.style("top", (d3.event.pageY - 28) + "px");
})
.on("mouseout", function() {
div.transition()
.duration(500)
.style("opacity", 0);
})
.on("click", function(d) {
hide.removeClass('hide');
d3.select("body").append("div")
.attr("id", "records");
var recs = $('#records');
$.get("DplaHistogram.php?q=" + q + "&decade=" + d.decade, function(data) {
hide.addClass('hide');
if(data.length === 0) {
recs.html("<p>There were no records to add.</p>");
} else {
recs.html(data);
}
window.location = '#records';
});
});
var xAxis = d3.svg.axis()
.scale(xScale)
.orient("bottom");
svg.append("g")
.attr("class", "axis")
.attr("transform", "translate(-3," + (height + axisPadding) + ")")
.call(xAxis);
svg.append("text")
.attr("x", width / 2)
.attr("y", height + margin.bottom)
.style("text-anchor", "middle")
.text("Decades")
var yAxis = d3.svg.axis()
.scale(yTickScale)
.orient("left");
svg.append("g")
.attr("class", "axis")
.attr("transform", "translate(" + margin.left + "," + axisPadding +")")
.call(yAxis);
svg.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 15)
.attr("x", 0 - (height/2))
.style("text-anchor", "middle")
.text("Item Count")
} else {
$('#message').text('Your search returned no results');
}
hide.addClass('hide');
});
} else {
hide.addClass('hide');
$('#message').text('Please submit a search term');
}
});
});
</script>
<style type="text/css">
.axis path,
.axis line {
fill: none;
padding: 5px;
stroke: black;
shape-rendering: crispEdges;
}
.axis text {
font-family: Raleway, sans-serif;
font-size: 11px;
}
rect:hover {
fill: orange;
}
#records h2 {
margin: 15px 0 -5px 25px;
}
#records ul {
border: 1px solid lightgray;
padding: 25px;
border-radius: 5px;
width: 50%;
}
#records ul a {
text-decoration: none;
color: steelblue;
}
#results {
margin-bottom: 50px;
}
#full_results {
height: auto;
width: auto;
padding: 10px;
text-decoration: none;
}
</style>
</head>
<body>
<?php include_once 'header.php'; ?>
<h1>DPLA Visualizations - See how your term(s) frequency changes through time</h1>
<p>Note: Depending on your search terms it can take some time to return your graph</p>
<p>Hover over a column to see how many items were returned for that decade</p>
<form action="#" method="post" id="d3-dpla">
<input type="text" name="q" id="q" placeholder="Search the DPLA"/>
<input type="submit" value="Search">
</form>
<p id="message"></p>
<img src="ajax-loader.gif" alt="load indicator" id="hide" class="hide" />
<?php include_once 'google.php'; ?>
</body>
</html>