forked from CliveCarrington/RPi_Temp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
events_tips.php
188 lines (152 loc) · 4.81 KB
/
events_tips.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
<?php
$hostname = 'localhost';
$username = 'pi_select';
$password = 'xxxxxxxxxx';
try {
$dbh = new PDO("mysql:host=$hostname;dbname=measurements",
$username, $password);
/*** The SQL SELECT statement ***/
$sth = $dbh->prepare("
SELECT dtg
FROM `events`
");
$sth->execute();
/* Fetch all of the remaining rows in the result set */
$result = $sth->fetchAll(PDO::FETCH_ASSOC);
/*** close the database connection ***/
$dbh = null;
}
catch(PDOException $e)
{
echo $e->getMessage();
}
$json_data = json_encode($result);
?>
<!DOCTYPE html>
<meta charset="utf-8">
<style>
body { font: 12px sans-serif; }
.axis path,
.axis line {
fill: none;
stroke: grey;
shape-rendering: crispEdges;
}
.dot { stroke: none; fill: steelblue; }
.grid .tick { stroke: lightgrey; opacity: 0.7; }
.grid path { stroke-width: 0;}
div.tooltip {
position: absolute;
text-align: center;
width: 80px;
height: 30px;
padding: 2px;
font: 12px sans-serif;
background: #ddd;
border: solid 0px #aaa;
border-radius: 8px;
pointer-events: none;
}
</style>
<body>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script>
// Get the data
<?php echo "data=".$json_data.";" ?>
// Parse the date / time formats
parseDate = d3.time.format("%Y-%m-%d").parse;
parseTime = d3.time.format("%H:%M:%S").parse;
data.forEach(function(d) {
dtgSplit = d.dtg.split(" "); // split on the space
d.date = parseDate(dtgSplit[0]); // get the date seperatly
d.time = parseTime(dtgSplit[1]); // get the time seperatly
});
// Get the number of days in the date range to calculate height
var oneDay = 24*60*60*1000; // hours*minutes*seconds*milliseconds
var dateStart = d3.min(data, function(d) { return d.date; });
var dateFinish = d3.max(data, function(d) { return d.date; });
var numberDays = Math.round(Math.abs((dateStart.getTime() -
dateFinish.getTime())/(oneDay)));
var margin = {top: 40, right: 20, bottom: 30, left: 100},
width = 600 - margin.left - margin.right,
height = numberDays * 8;
var formatDay_Time = d3.time.format("%H:%M"); // Format tooltip time
var formatWeek_Year = d3.time.format("%d-%m-%Y"); // Format tooltip date
var x = d3.time.scale().range([0, width]);
var y = d3.time.scale().range([0, height]);
var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom")
.ticks(7)
.tickFormat(d3.time.format("%H:%M"));
var yAxis = d3.svg.axis()
.scale(y)
.orient("left")
.ticks(7,0,0);
var svg = d3.select("body")
.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 + ")");
// State the functions for the grid
function make_x_axis() {
return d3.svg.axis()
.scale(x)
.orient("bottom")
.ticks(7)
}
// Set the domains
x.domain([new Date(1899, 12, 01, 0, 0, 1),
new Date(1899, 12, 02, 0, 0, 0)]);
y.domain(d3.extent(data, function(d) { return d.date; }));
// tickSize: Get or set the size of major, minor and end ticks
svg.append("g").classed("grid x_grid", true)
.attr("transform", "translate(0," + height + ")")
.style("stroke-dasharray", ("3, 3, 3"))
.call(make_x_axis()
.tickSize(-height, 0, 0)
.tickFormat(""))
// Draw the Axes and the tick labels
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis)
.selectAll("text")
.style("text-anchor", "middle");
svg.append("g")
.attr("class", "y axis")
.call(yAxis)
.selectAll("text")
.style("text-anchor", "end");
// Tooltip stuff
var div = d3.select("body").append("div")
.attr("class", "tooltip")
.style("opacity", 1e-6);
// draw the plotted circles
svg.selectAll(".dot")
.data(data)
.enter().append("circle")
.attr("class", "dot")
.attr("r", 4.5)
.style("opacity", 0.5)
.attr("cx", function(d) { return x(d.time); })
.attr("cy", function(d) { return y(d.date); })
.on("mouseover", function(d) {
div.transition()
.duration(200)
.style("opacity", 1);
div.html(
formatDay_Time(d.time) + "<br/>" +
formatWeek_Year(d.date) )
.style("left", (d3.event.pageX) + "px")
.style("top", (d3.event.pageY - 36) + "px");
}) // tooltip
.on("mouseout", function(d) {
div.transition()
.duration(500)
.style("opacity", 1e-6);
});
</script>
</body>