-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex10.html
More file actions
220 lines (194 loc) · 8.57 KB
/
index10.html
File metadata and controls
220 lines (194 loc) · 8.57 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
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
214
215
216
217
218
219
220
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Plot Manager</title>
<link href="css/main.css" rel="stylesheet" type="text/css"/>
<style type="text/css">
.date_axis {
height: 42px;
z-index: 2;
border: 1px solid black;
border-bottom-width: 0;
}
.plot_container {
position: relative;
border: 1px solid black;
}
.plot_container > canvas:focus {
outline: none;
}
.y_axis {
position: relative;
width: 42px;
border: 1px solid black;
border-left-width: 0;
}
.data_point_table {
display: table;
margin-top: 5px;
}
.data_point_row {
display: table-row;
}
.data_point_header_cell {
font-weight: bold;
}
.data_point_cell {
display: table-cell;
padding-right: 20px;
}
</style>
<script src="lib/jquery/jquery-1.11.2.min.js" type="text/javascript"></script>
<script src="js/jquery.mousewheel.min.js"></script>
<script src="lib/superagent/superagent.js" type="text/javascript"></script>
<script src="js/grapher.min.js"></script>
<script src="js/org/bodytrack/grapher/PlotManager.js" type="text/javascript"></script>
<script type="text/javascript">
var ESDR_API_ROOT_URL = 'https://esdr.cmucreatelab.org/api/v1';
var feedIdOrApiKey = 4231;
var channelName = "PM2_5";
var hoverSampleValueAndTimeElement = null;
var cursorSampleValueAndTimeElement = null;
window.grapherLoad = function() {
var maxTimeSecs = Date.now() / 1000;
var minTimeSecs = maxTimeSecs - 7 * 24 * 60 * 60;
var plotManager = new org.bodytrack.grapher.PlotManager("date_axis", minTimeSecs, maxTimeSecs);
plotManager.setWillAutoResizeWidth(true, function() {
return $(window).width() // window width
- $(".y_axis").width() // Y axis width
- 20 // left and right margins
- 3; // grapher and Y axis borders
});
var datasource = function(level, offset, successCallback) {
superagent
.get(ESDR_API_ROOT_URL + "/feeds/" + feedIdOrApiKey + "/channels/" + channelName + "/tiles/" + level + "." + offset)
.end(function(err, res) {
if (err) {
return console.log("Error: " + res.body);
}
switch (res.status) {
case 200:
return successCallback(JSON.stringify(res.body.data));
case 401:
return console.log("Unauthorized");
case 403:
return console.log("Forbidden");
default:
return console.log("Error: " + res.body);
}
});
};
var datasource = ESDR_API_ROOT_URL + "/feeds/" + feedIdOrApiKey + "/channels/" + channelName + "/tiles";
var minValue = 0;
var maxValue = 20;
// add the plot
plotManager.addDataSeriesPlot("my_plot", datasource, "plot_container", "y_axis", minValue, maxValue);
// save references to commonly used objects
var plot = plotManager.getPlotContainer("plot_container").getPlot("my_plot");
hoverSampleValueAndTimeElement = $("#hover_sample_value_and_time");
cursorSampleValueAndTimeElement = $("#cursor_sample_value_and_time");
// Set the default cursor position to the middle of the date axis, then set a timer to fetch the cursor
// value--we need the timer because the plot data loads asynchronously. There's a race condition here, because
// the plot data may not be loaded by the time the timer fires, but, whatever, this is good enough for an
// example.
var initialCursorTime = (maxTimeSecs + minTimeSecs) / 2;
plotManager.getDateAxis().setCursorPosition(initialCursorTime);
setTimeout(function() {
displayCursorTimeAndValue(plot, initialCursorTime);
}, 500);
// Remember the current cursor position so we don't need to do a lot of needless calls to
// getClosestDataPointToTimeWithinWindow() when the axis is panned/zoomed, but the cursor position hasn't
// actually changed.
var currentCursorPosition = null;
// define the axisChangeListener
var axisChangeListener = function(evt) {
if (evt) {
var cursorPosition = evt['cursorPosition'];
// don't do anything if the cursor position hasn't changed (e.g. the user is zooming/panning the axis)
if (currentCursorPosition != cursorPosition) {
currentCursorPosition = cursorPosition;
displayCursorTimeAndValue(plot, cursorPosition);
}
}
else {
cursorSampleValueAndTimeElement.empty().hide();
}
};
// add a listener to the date axis so we can get the current cursor position when the user moves the cursor
plotManager.getDateAxis().addAxisChangeListener(axisChangeListener);
// add the data point listener to handle mouse hover events
plot.addDataPointListener(function(dataPoint) {
renderValueAndTimeInElement(dataPoint, hoverSampleValueAndTimeElement);
});
// configure checkbox
$("#is_listener_attached").click(function() {
if ($(this).prop("checked")) {
displayCursorTimeAndValue(plot, plotManager.getDateAxis().getCursorPosition());
plotManager.getDateAxis().addAxisChangeListener(axisChangeListener);
}
else {
$("#cursor_sample_value_and_time").html("listener not attached").show();
plotManager.getDateAxis().removeAxisChangeListener(axisChangeListener);
}
});
};
function displayCursorTimeAndValue(plot, timeInSecs) {
// I'm using a window of [timeInSecs - 3600, timeInSecs + 0] here because I know that the data samples are
// recorded every hour (3600 seconds), and I want the closest data point *before* the given timeInSecs.
var dataPoint = plot.getClosestDataPointToTimeWithinWindow(timeInSecs, 3600, 0);
renderValueAndTimeInElement(dataPoint, cursorSampleValueAndTimeElement);
}
function renderValueAndTimeInElement(dataPoint, element) {
if (dataPoint) {
element.html(dataPoint.valueString + " μg/m<sup>3</sup> at " + dataPoint.dateString).show();
}
else {
element.empty().hide();
}
}
</script>
</head>
<body>
<p>
An auto-resizing plot which has two listeners attached. Hover over the plot data points to see the value and
timestamp appear below the plot. Drag the red triangle cursor to see the value and timestamp of the most recent
data point with respect to the cursor. You can toggle whether the cursor listener is attached by toggling the checkbox.
</p>
<div id="grapher_container" class="noselect">
<table id="grapher" border="0" cellpadding="0" cellspacing="0">
<tr>
<td>
<div id="date_axis" class="date_axis" style="width:400px;"></div>
</td>
<td></td>
</tr>
<tr>
<td>
<div id="plot_container" class="plot_container" style="width:400px;height:300px;"></div>
</td>
<td>
<div id="y_axis" class="y_axis" style="height:300px"></div>
</td>
</tr>
<tr>
<td colspan="2">
<div style="float:right; margin-top:5px">
<input type="checkbox" id="is_listener_attached" value="true" checked="checked"><label for="is_listener_attached">Is cursor listener attached?</label>
</div>
<div class="data_point_table">
<div class="data_point_row">
<div class="data_point_cell data_point_header_cell">Cursor</div>
<div class="data_point_cell data_point_header_cell">Hover</div>
</div>
<div class="data_point_row">
<div class="data_point_cell" id="cursor_sample_value_and_time"></div>
<div class="data_point_cell" id="hover_sample_value_and_time"></div>
</div>
</div>
</td>
</tr>
</table>
</div>
</body>
</html>