Mercurial > ~darius > hgwebdir.cgi > iwws
annotate static/iwws.js @ 9:6edb93c20971
Fix stretching the graph limits for temperature.
author | Daniel O'Connor <darius@dons.net.au> |
---|---|
date | Tue, 03 Jan 2012 19:05:55 +1030 |
parents | 4b9d1b47ca17 |
children | bfe6ed563ffc |
rev | line source |
---|---|
0 | 1 /* |
2 * Copyright 2011 Daniel O'Connor <darius@dons.net.au> | |
3 * | |
4 * Redistribution and use in source and binary forms, with or without modification, are | |
5 * permitted provided that the following conditions are met: | |
6 * | |
7 * 1. Redistributions of source code must retain the above copyright notice, this list of | |
8 * conditions and the following disclaimer. | |
9 * | |
10 * 2. Redistributions in binary form must reproduce the above copyright notice, this list | |
11 * of conditions and the following disclaimer in the documentation and/or other materials | |
12 * provided with the distribution. | |
13 * | |
14 * THIS SOFTWARE IS PROVIDED BY <COPYRIGHT HOLDER> ''AS IS'' AND ANY EXPRESS OR IMPLIED | |
15 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND | |
16 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> OR | |
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | |
19 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON | |
20 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | |
21 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF | |
22 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
23 * | |
24 * The views and conclusions contained in the software and documentation are those of the | |
25 * authors and should not be interpreted as representing official policies, either expressed | |
26 * or implied, of Daniel O'Connor. | |
27 */ | |
28 | |
7
24e9b717722d
- Update every 10 minutes.
Daniel O'Connor <darius@dons.net.au>
parents:
0
diff
changeset
|
29 var timer = null; |
24e9b717722d
- Update every 10 minutes.
Daniel O'Connor <darius@dons.net.au>
parents:
0
diff
changeset
|
30 var datacache = null; |
24e9b717722d
- Update every 10 minutes.
Daniel O'Connor <darius@dons.net.au>
parents:
0
diff
changeset
|
31 |
0 | 32 $.jQTouch({ |
33 icon: 'icon.png', | |
34 startupScreen: 'img/startup.png' | |
35 }); | |
36 | |
7
24e9b717722d
- Update every 10 minutes.
Daniel O'Connor <darius@dons.net.au>
parents:
0
diff
changeset
|
37 function draw_graph() { |
0 | 38 var temp_out = []; |
8 | 39 var dewpt = []; |
0 | 40 var wavg = []; |
41 var wgust = []; | |
42 var rain = []; | |
8 | 43 var pressure = []; |
0 | 44 var i, mint = 5, maxt = 35; |
7
24e9b717722d
- Update every 10 minutes.
Daniel O'Connor <darius@dons.net.au>
parents:
0
diff
changeset
|
45 var l = datacache['idx'].length - 1; |
0 | 46 var d = new Date(); |
47 var tzofs = d.getTimezoneOffset() * 60; | |
48 for (i = 0; i < l; i++) { | |
49 // Convert time from UTC to browser LT | |
7
24e9b717722d
- Update every 10 minutes.
Daniel O'Connor <darius@dons.net.au>
parents:
0
diff
changeset
|
50 t = datacache['idx'][i] - tzofs; |
9
6edb93c20971
Fix stretching the graph limits for temperature.
Daniel O'Connor <darius@dons.net.au>
parents:
8
diff
changeset
|
51 if (datacache['temp_out'][i] < mint) |
7
24e9b717722d
- Update every 10 minutes.
Daniel O'Connor <darius@dons.net.au>
parents:
0
diff
changeset
|
52 mint = datacache['temp_out'] |
9
6edb93c20971
Fix stretching the graph limits for temperature.
Daniel O'Connor <darius@dons.net.au>
parents:
8
diff
changeset
|
53 if (datacache['temp_out'][i] > maxt) |
6edb93c20971
Fix stretching the graph limits for temperature.
Daniel O'Connor <darius@dons.net.au>
parents:
8
diff
changeset
|
54 maxt = datacache['temp_out'][i] |
7
24e9b717722d
- Update every 10 minutes.
Daniel O'Connor <darius@dons.net.au>
parents:
0
diff
changeset
|
55 temp_out.push([t * 1000.0, datacache['temp_out'][i]]); |
8 | 56 if (datacache['hum_out'][i] != null && datacache['temp_out'][i] != null) { |
57 dewpt.push([t * 1000.0, hum2dp(datacache['hum_out'][i], datacache['temp_out'][i])]); | |
58 } | |
7
24e9b717722d
- Update every 10 minutes.
Daniel O'Connor <darius@dons.net.au>
parents:
0
diff
changeset
|
59 wavg.push([t * 1000.0, datacache['wind_ave'][i], wind2angle(datacache['wind_dir'][i])]); |
24e9b717722d
- Update every 10 minutes.
Daniel O'Connor <darius@dons.net.au>
parents:
0
diff
changeset
|
60 wgust.push([t * 1000.0, datacache['wind_gust'][i], wind2angle(datacache['wind_dir'][i])]); |
8 | 61 pressure.push([t * 1000.0, datacache['abs_pressure'][i]]); |
7
24e9b717722d
- Update every 10 minutes.
Daniel O'Connor <darius@dons.net.au>
parents:
0
diff
changeset
|
62 if (datacache['rain'][i] > 0) |
24e9b717722d
- Update every 10 minutes.
Daniel O'Connor <darius@dons.net.au>
parents:
0
diff
changeset
|
63 rain.push([t * 1000.0, datacache['rain'][i]]); |
0 | 64 } |
9
6edb93c20971
Fix stretching the graph limits for temperature.
Daniel O'Connor <darius@dons.net.au>
parents:
8
diff
changeset
|
65 |
0 | 66 $.plot($("#graph1"), [ |
8 | 67 { data : pressure, label: "Baro", yaxis : 1, points : { show : true }, lines : { show : true } }, |
68 { data : temp_out, label: "Temp.", yaxis : 2, points : { show : true }, lines : { show : true } }, | |
69 { data : dewpt, label: "Dew point", yaxis : 2, points : { show : true }, lines : { show : true } }, | |
0 | 70 ], |
71 { xaxis : { mode : 'time' }, | |
72 legend : { backgroundOpacity : 0, position : 'nw' }, | |
8 | 73 yaxis : { min : 950, max : 1050, tickFormatter : baroFormatter }, |
74 y2axis : { min : mint, max : maxt, tickFormatter : degCFormatter }, | |
0 | 75 }); |
76 $.plot($("#graph2"), [ | |
77 { data : wavg, label: "Wind (Avg)", yaxis : 1, points : { show : true }, lines : { show : true }, direction : true }, | |
78 { data : wgust, label: "Wind (Gust)", yaxis : 1, points : { show : true }, lines : { show : true }, direction : true }, | |
8 | 79 { data : rain, label: "Rain", yaxis : 2, bars : { show : true, barWidth : 0.5 * 60 * 60 * 1000, align : "centre" } }, |
0 | 80 ], |
81 { xaxis : { mode : 'time' }, | |
82 legend : { backgroundOpacity : 0, position : 'nw' }, | |
83 yaxis : { tickFormatter : spdFormatter }, | |
84 y2axis : { min : 0, tickFormatter : mmFormatter } | |
85 }); | |
86 } | |
87 | |
88 function wind2angle(dir) { | |
89 var a = dir * (360.0 / 16.0); | |
90 return a; | |
91 } | |
92 | |
8 | 93 // Formula from http://www.paroscientific.com/dewpoint.htm |
94 function alpha(temp, rh, a, b) { | |
95 return (((a * temp) / (b + temp)) + Math.log(rh / 100.0)); | |
96 } | |
97 | |
98 function hum2dp(rh, temp) { | |
99 var a = 17.27; | |
100 var b = 237.7; | |
101 var Td = (b * alpha(temp, rh, a, b)) / (a - alpha(temp, rh, a, b)); | |
102 return Td; | |
103 } | |
104 | |
0 | 105 function degCFormatter(v, axis) { |
106 return v.toFixed(axis.tickDecimals) +"°C"; | |
107 } | |
108 | |
109 function pctFormatter(v, axis) { | |
110 return v.toFixed(axis.tickDecimals) +"%"; | |
111 } | |
112 | |
113 function spdFormatter(v, axis) { | |
114 return v.toFixed(axis.tickDecimals) + "kph"; | |
115 } | |
116 | |
117 function mmFormatter(v, axis) { | |
118 return v.toFixed(axis.tickDecimals) + "mm"; | |
119 } | |
120 | |
8 | 121 function baroFormatter(v, axis) { |
122 return v.toFixed(axis.tickDecimals) + "hPa"; | |
123 } | |
124 | |
7
24e9b717722d
- Update every 10 minutes.
Daniel O'Connor <darius@dons.net.au>
parents:
0
diff
changeset
|
125 function got_data(data, status) { |
24e9b717722d
- Update every 10 minutes.
Daniel O'Connor <darius@dons.net.au>
parents:
0
diff
changeset
|
126 if (status != "success") { |
24e9b717722d
- Update every 10 minutes.
Daniel O'Connor <darius@dons.net.au>
parents:
0
diff
changeset
|
127 $.log("Couldn't load data. status = " + status); |
24e9b717722d
- Update every 10 minutes.
Daniel O'Connor <darius@dons.net.au>
parents:
0
diff
changeset
|
128 return; |
24e9b717722d
- Update every 10 minutes.
Daniel O'Connor <darius@dons.net.au>
parents:
0
diff
changeset
|
129 } |
24e9b717722d
- Update every 10 minutes.
Daniel O'Connor <darius@dons.net.au>
parents:
0
diff
changeset
|
130 |
24e9b717722d
- Update every 10 minutes.
Daniel O'Connor <darius@dons.net.au>
parents:
0
diff
changeset
|
131 datacache = data; |
24e9b717722d
- Update every 10 minutes.
Daniel O'Connor <darius@dons.net.au>
parents:
0
diff
changeset
|
132 draw_graph(); |
24e9b717722d
- Update every 10 minutes.
Daniel O'Connor <darius@dons.net.au>
parents:
0
diff
changeset
|
133 } |
24e9b717722d
- Update every 10 minutes.
Daniel O'Connor <darius@dons.net.au>
parents:
0
diff
changeset
|
134 |
0 | 135 function update_data() { |
7
24e9b717722d
- Update every 10 minutes.
Daniel O'Connor <darius@dons.net.au>
parents:
0
diff
changeset
|
136 /* Cancel any pending timeout (eg if the user pressed update) */ |
24e9b717722d
- Update every 10 minutes.
Daniel O'Connor <darius@dons.net.au>
parents:
0
diff
changeset
|
137 if (timer != null) |
24e9b717722d
- Update every 10 minutes.
Daniel O'Connor <darius@dons.net.au>
parents:
0
diff
changeset
|
138 clearTimeout(timeout); |
24e9b717722d
- Update every 10 minutes.
Daniel O'Connor <darius@dons.net.au>
parents:
0
diff
changeset
|
139 jQuery.getJSON('iwws/getdata.json', got_data); |
24e9b717722d
- Update every 10 minutes.
Daniel O'Connor <darius@dons.net.au>
parents:
0
diff
changeset
|
140 /* Set to refresh in 10 minutes */ |
24e9b717722d
- Update every 10 minutes.
Daniel O'Connor <darius@dons.net.au>
parents:
0
diff
changeset
|
141 timeout = setTimeout(update_data, 10 * 60 * 60); |
0 | 142 } |
143 | |
144 $(document).ready(function(){ | |
145 update_data(); | |
7
24e9b717722d
- Update every 10 minutes.
Daniel O'Connor <darius@dons.net.au>
parents:
0
diff
changeset
|
146 |
24e9b717722d
- Update every 10 minutes.
Daniel O'Connor <darius@dons.net.au>
parents:
0
diff
changeset
|
147 $('body').bind('turn', function(event, info) { |
24e9b717722d
- Update every 10 minutes.
Daniel O'Connor <darius@dons.net.au>
parents:
0
diff
changeset
|
148 draw_graph(); |
24e9b717722d
- Update every 10 minutes.
Daniel O'Connor <darius@dons.net.au>
parents:
0
diff
changeset
|
149 }); |
0 | 150 }); |