-
Notifications
You must be signed in to change notification settings - Fork 584
/
Copy path2.5.frontend.md.html
293 lines (285 loc) · 26.4 KB
/
2.5.frontend.md.html
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="Content-Style-Type" content="text/css" />
<meta name="generator" content="pandoc" />
<title></title>
<style type="text/css">code{white-space: pre;}</style>
<style type="text/css">
div.sourceCode { overflow-x: auto; }
table.sourceCode, tr.sourceCode, td.lineNumbers, td.sourceCode {
margin: 0; padding: 0; vertical-align: baseline; border: none; }
table.sourceCode { width: 100%; line-height: 100%; }
td.lineNumbers { text-align: right; padding-right: 4px; padding-left: 4px; color: #aaaaaa; border-right: 1px solid #aaaaaa; }
td.sourceCode { padding-left: 5px; }
code > span.kw { color: #007020; font-weight: bold; } /* Keyword */
code > span.dt { color: #902000; } /* DataType */
code > span.dv { color: #40a070; } /* DecVal */
code > span.bn { color: #40a070; } /* BaseN */
code > span.fl { color: #40a070; } /* Float */
code > span.ch { color: #4070a0; } /* Char */
code > span.st { color: #4070a0; } /* String */
code > span.co { color: #60a0b0; font-style: italic; } /* Comment */
code > span.ot { color: #007020; } /* Other */
code > span.al { color: #ff0000; font-weight: bold; } /* Alert */
code > span.fu { color: #06287e; } /* Function */
code > span.er { color: #ff0000; font-weight: bold; } /* Error */
code > span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } /* Warning */
code > span.cn { color: #880000; } /* Constant */
code > span.sc { color: #4070a0; } /* SpecialChar */
code > span.vs { color: #4070a0; } /* VerbatimString */
code > span.ss { color: #bb6688; } /* SpecialString */
code > span.im { } /* Import */
code > span.va { color: #19177c; } /* Variable */
code > span.cf { color: #007020; font-weight: bold; } /* ControlFlow */
code > span.op { color: #666666; } /* Operator */
code > span.bu { } /* BuiltIn */
code > span.ex { } /* Extension */
code > span.pp { color: #bc7a00; } /* Preprocessor */
code > span.at { color: #7d9029; } /* Attribute */
code > span.do { color: #ba2121; font-style: italic; } /* Documentation */
code > span.an { color: #60a0b0; font-weight: bold; font-style: italic; } /* Annotation */
code > span.cv { color: #60a0b0; font-weight: bold; font-style: italic; } /* CommentVar */
code > span.in { color: #60a0b0; font-weight: bold; font-style: italic; } /* Information */
</style>
<link rel="stylesheet" href="css/vendor.css" type="text/css" />
</head>
<body>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:wb="http://open.weibo.com/wb" >
<head>
<title>一步步搭建物联网系统(教你设计物联网系统)</title>
<meta name="keywords" content="设计物联网系统,物联网系统设计">
<meta name="description" content="一份关于如何设计物联网系统的文档">
<link rel="stylesheet" href="../css/vendor.css" />
</head>
<body class="container inner wrap">
<h1>一步步搭建物联网系统(教你设计物联网系统)</h1>
</body>
</html>
<div id="TOC">
<ul>
<li><a href="#前端显示"><span class="toc-section-number">1</span> 前端显示</a><ul>
<li><a href="#库与车轮子"><span class="toc-section-number">1.1</span> 库与车轮子</a></li>
<li><a href="#库"><span class="toc-section-number">1.2</span> 库</a><ul>
<li><a href="#jquery"><span class="toc-section-number">1.2.1</span> jQuery</a></li>
<li><a href="#jquery-mobile"><span class="toc-section-number">1.2.2</span> jQuery Mobile</a></li>
</ul></li>
<li><a href="#网站前台显示"><span class="toc-section-number">1.3</span> 网站前台显示</a><ul>
<li><a href="#highcharts"><span class="toc-section-number">1.3.1</span> Highcharts</a></li>
<li><a href="#实时数据"><span class="toc-section-number">1.3.2</span> 实时数据</a></li>
</ul></li>
</ul></li>
</ul>
</div>
<h1 id="前端显示"><span class="header-section-number">1</span> 前端显示</h1>
<h2 id="库与车轮子"><span class="header-section-number">1.1</span> 库与车轮子</h2>
<p>在多数的情况下我们都没有理由也没有必要去重新发明我们的车轮,在这时使用库会是一个比较好的做法。</p>
<h2 id="库"><span class="header-section-number">1.2</span> 库</h2>
<h3 id="jquery"><span class="header-section-number">1.2.1</span> jQuery</h3>
<blockquote>
<p>Jquery是继prototype之后又一个优秀的Javascript库。它是轻量级的js库 ,它兼容CSS3,还兼容各种浏览器(IE 6.0+, FF 1.5+, Safari 2.0+, Opera 9.0+),jQuery2.0及后续版本将不再支持IE6/7/8浏览器。jQuery使用户能更方便地处理HTML(标准通用标记语言下的一个应用)、events、实现动画效果,并且方便地为网站提供AJAX交互。jQuery还有一个比较大的优势是,它的文档说明很全,而且各种应用也说得很详细,同时还有许多成熟的插件可供选择。jQuery能够使用户的html页面保持代码和html内容分离,也就是说,不用再在html里面插入一堆js来调用命令了,只需要定义id即可。</p>
</blockquote>
<p>在我们的代码里用到了jQuery,然而这是一种简单而且快速有速地方法。</p>
<h3 id="jquery-mobile"><span class="header-section-number">1.2.2</span> jQuery Mobile</h3>
<blockquote>
<p>jQuery Mobile是jQuery 在手机上和平板设备上的版本。jQuery Mobile不仅会给主流移动平台带来jQuery核心库,而且会发布一个完整统一的jQuery移动UI框架。支持全球主流的移动平台。jQuery Mobile开发团队说:能开发这个项目,我们非常兴奋。移动Web太需要一个跨浏览器的框架,让开发人员开发出真正的移动Web网站。</p>
</blockquote>
<p>整个展示页面由三部分组成,即<code>header</code>,<code>content</code>,<code>footer</code>。而我们主要需要关心的是content,也就是真下的内容。</p>
<p>我们只需要结合<code>HighChart</code>来设计我们的content就可以了,下面是一个简单地<code>HighChart</code>示例:</p>
<pre><code><div id="Tchart" style="min-width:800px;width:100%;height:300px"></div>
</div></code></pre>
<p>剩下的事就由<code>HighChart</code>来做。</p>
<p>最后代码如下所示</p>
<div class="sourceCode"><pre class="sourceCode html"><code class="sourceCode html"><span class="kw"><body></span>
<span class="kw"><div</span><span class="ot"> data-role=</span><span class="st">"page"</span><span class="kw">></span>
<span class="kw"><div</span><span class="ot"> data-role=</span><span class="st">"header"</span><span class="ot"> class=</span><span class="st">"ui-bar ui-bar-b"</span><span class="ot"> class=</span><span class="st">"jqm-header"</span><span class="kw">></span>
<span class="kw"><h1></span>基础控制<span class="kw"></h1></span>
<span class="kw"></div></span>
<span class="kw"><div</span><span class="ot"> data-role=</span><span class="st">"collapsible"</span><span class="ot"> data-collapsed=</span><span class="st">"false"</span><span class="kw">></span>
<span class="kw"><h3></span>查看温度情况<span class="kw"></h3></span>
<span class="kw"><div></span>
<span class="kw"><div</span><span class="ot"> id=</span><span class="st">"Tchart"</span><span class="ot"> style=</span><span class="st">"min-width:800px;width:100%;height:300px"</span><span class="kw">></div></span>
<span class="kw"></div></span>
<span class="kw"></div></span>
<span class="kw"><div</span><span class="ot"> data-role=</span><span class="st">"footer"</span><span class="ot"> class=</span><span class="st">"ui-bar ui-bar-b"</span><span class="ot"> data-position=</span><span class="st">"fixed"</span><span class="kw">></span>
<span class="kw"><h4><a</span><span class="ot"> href=</span><span class="st">"http://www.phodal.com"</span><span class="kw">></span>Power by Phodal<span class="kw"></a></h4></span>
<span class="kw"></div></span>
<span class="kw"></body></span></code></pre></div>
<p>可以看到上面的代码与一般的HTML不同的地方是<code>data-role</code>,<code>data-collapsed</code>,<code>data-position</code>,而这些是jQuery Mobile所拥有的data属性。</p>
<p><strong>data-role参数表</strong>: rold | 详细 ————–|—- page | 页面容器,其内部的mobile元素将会继承这个容器上所设置的属性 header | 页面标题容器,这个容器内部可以包含文字、返回按钮、功能按钮等元素 footer | 页面页脚容器,这个容器内部也可以包含文字、返回按钮、功能按钮等元素 content | 页面内容容器,这是一个很宽容的容器,内部可以包含标准的html元素和jQueryMobile元素 controlgroup | 将几个元素设置成一组,一般是几个相同的元素类型 fieldcontain | 区域包裹容器,用增加边距和分割线的方式将容器内的元素和容器外的元素明显分隔 navbar |功能导航容器,通俗的讲就是工具条 listview |列表展示容器,类似手机中联系人列表的展示方式 list-divider | 列表展示容器的表头,用来展示一组列表的标题,内部不可包含链接 button | 按钮,将链接和普通按钮的样式设置成为jQueryMobile的风格 none | 阻止框架对元素进行渲染,使元素以html原生的状态显示,主要用于form元素。</p>
<p>同上,我们也可以在网上找到其他相对就的属性。</p>
<h2 id="网站前台显示"><span class="header-section-number">1.3</span> 网站前台显示</h2>
<h3 id="highcharts"><span class="header-section-number">1.3.1</span> Highcharts</h3>
<p>Highcharts有以下的特点</p>
<ul>
<li>兼容性:兼容当今所有的浏览器,包括 iPhone、IE 和火狐等等;</li>
<li>对个人用户完全免费;</li>
<li>纯JS,无BS;</li>
<li>支持大部分的图表类型:直线图,曲线图、区域图、区域曲线图、柱状图、饼装图、散布图;</li>
<li>跨语言:不管是 PHP、Asp.net 还是 Java 都可以使用,它只需要三个文件:一个是Highcharts 的核心文件 highcharts.js,还有 a canvas emulator for IE 和 Jquery类库或者 MooTools 类库;</li>
<li>提示功能:鼠标移动到图表的某一点上有提示信息;</li>
<li>放大功能:选中图表部分放大,近距离观察图表;</li>
<li>易用性:无需要特殊的开发技能,只需要设置一下选项就可以制作适合自己的图表;</li>
<li>时间轴:可以精确到毫秒;</li>
</ul>
<p>在这里只需将需要处理的数据存储到数组中,便可以将其渲染成为图形,下面的温度走势图便是基于Highcharts的结果:</p>
<div class="figure">
<img src="./images/temperture.png" alt="Temperture" />
<p class="caption">Temperture</p>
</div>
<p>先看看最后代码如下所示:</p>
<div class="sourceCode"><pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="kw">var</span> dataLength <span class="op">=</span> []<span class="op">;</span>
<span class="kw">function</span> <span class="at">drawTemp</span>() <span class="op">{</span>
<span class="kw">var</span> zero <span class="op">=</span> []<span class="op">;</span>
<span class="va">$</span>.<span class="at">getJSON</span>(<span class="st">'/athome/'</span><span class="op">,</span> <span class="kw">function</span>(json) <span class="op">{</span>
<span class="kw">var</span> items <span class="op">=</span> []<span class="op">;</span>
<span class="va">dataLength</span>.<span class="at">push</span>(<span class="va">json</span>.<span class="at">length</span>)<span class="op">;</span>
<span class="va">$</span>.<span class="at">each</span>(json<span class="op">,</span> <span class="kw">function</span>(key<span class="op">,</span> val) <span class="op">{</span>
<span class="va">zero</span>.<span class="at">push</span>(<span class="va">val</span>.<span class="at">temperature</span>)<span class="op">;</span>
<span class="op">}</span>)<span class="op">;</span>
chart <span class="op">=</span> <span class="kw">new</span> <span class="va">Highcharts</span>.<span class="at">Chart</span>(<span class="op">{</span>
<span class="dt">color</span><span class="op">:</span> <span class="op">{</span>
<span class="dt">linearGradient</span><span class="op">:</span> <span class="op">{</span>
<span class="dt">x1</span><span class="op">:</span> <span class="dv">0</span><span class="op">,</span>
<span class="dt">x2</span><span class="op">:</span> <span class="dv">0</span><span class="op">,</span>
<span class="dt">y1</span><span class="op">:</span> <span class="dv">0</span><span class="op">,</span>
<span class="dt">y1</span><span class="op">:</span> <span class="dv">1</span>
<span class="op">},</span>
<span class="dt">stops</span><span class="op">:</span> [
[<span class="dv">0</span><span class="op">,</span> <span class="st">'#003399'</span>]<span class="op">,</span>
[<span class="dv">1</span><span class="op">,</span> <span class="st">'#3366AA'</span>]
]
<span class="op">},</span>
<span class="dt">chart</span><span class="op">:</span> <span class="op">{</span>
<span class="dt">renderTo</span><span class="op">:</span> <span class="st">'Tchart'</span><span class="op">,</span>
<span class="dt">type</span><span class="op">:</span> <span class="st">'spline'</span>
<span class="op">},</span>
<span class="dt">title</span><span class="op">:</span> <span class="op">{</span>
<span class="dt">text</span><span class="op">:</span> <span class="st">'本月温度情况'</span>
<span class="op">},</span>
<span class="dt">subtitle</span><span class="op">:</span> <span class="op">{</span>
<span class="dt">text</span><span class="op">:</span> <span class="st">''</span>
<span class="op">},</span>
<span class="dt">xAxis</span><span class="op">:</span> <span class="op">{</span>
<span class="dt">categories</span><span class="op">:</span> []<span class="op">,</span>
<span class="dt">title</span><span class="op">:</span> <span class="op">{</span>
<span class="dt">text</span><span class="op">:</span> <span class="st">''</span>
<span class="op">}</span>
<span class="op">},</span>
<span class="dt">yAxis</span><span class="op">:</span> <span class="op">{</span>
<span class="dt">title</span><span class="op">:</span> <span class="op">{</span>
<span class="dt">text</span><span class="op">:</span> <span class="st">'温度 (°C)'</span>
<span class="op">}</span>
<span class="op">},</span>
<span class="dt">tooltip</span><span class="op">:</span> <span class="op">{</span>
<span class="dt">backgroundColor</span><span class="op">:</span> <span class="st">'#FCFFC5'</span><span class="op">,</span>
<span class="dt">borderColor</span><span class="op">:</span> <span class="st">'black'</span><span class="op">,</span>
<span class="dt">borderRadius</span><span class="op">:</span> <span class="dv">10</span><span class="op">,</span>
<span class="dt">borderWidth</span><span class="op">:</span> <span class="dv">1</span><span class="op">,</span>
<span class="dt">enabled</span><span class="op">:</span> <span class="kw">true</span><span class="op">,</span>
<span class="dt">formatter</span><span class="op">:</span> <span class="kw">function</span>() <span class="op">{</span>
<span class="cf">return</span> <span class="st">'<b>'</span> <span class="op">+</span> <span class="kw">this</span>.<span class="va">series</span>.<span class="at">name</span> <span class="op">+</span> <span class="st">'</b><br/>'</span> <span class="op">+</span> <span class="kw">this</span>.<span class="at">x</span> <span class="op">+</span> <span class="st">': '</span> <span class="op">+</span> <span class="kw">this</span>.<span class="at">y</span> <span class="op">+</span> <span class="st">'°C'</span><span class="op">;</span>
<span class="op">}</span>
<span class="op">},</span>
<span class="dt">legend</span><span class="op">:</span> <span class="op">{</span>
<span class="dt">layout</span><span class="op">:</span> <span class="st">'vertical'</span><span class="op">,</span>
<span class="dt">align</span><span class="op">:</span> <span class="st">'right'</span><span class="op">,</span>
<span class="dt">verticalAlign</span><span class="op">:</span> <span class="st">'top'</span><span class="op">,</span>
<span class="dt">x</span><span class="op">:</span> <span class="op">-</span><span class="dv">10</span><span class="op">,</span>
<span class="dt">y</span><span class="op">:</span> <span class="dv">100</span><span class="op">,</span>
<span class="dt">borderWidth</span><span class="op">:</span> <span class="dv">0</span>
<span class="op">},</span>
<span class="dt">plotOptions</span><span class="op">:</span> <span class="op">{</span>
<span class="dt">line</span><span class="op">:</span> <span class="op">{</span>
<span class="dt">dataLabels</span><span class="op">:</span> <span class="op">{</span>
<span class="dt">enabled</span><span class="op">:</span> <span class="kw">true</span>
<span class="op">},</span>
<span class="dt">enableMouseTracking</span><span class="op">:</span> <span class="kw">false</span>
<span class="op">}</span>
<span class="op">},</span>
<span class="dt">series</span><span class="op">:</span> [<span class="op">{</span>
<span class="dt">name</span><span class="op">:</span> <span class="st">'本月'</span><span class="op">,</span>
<span class="dt">data</span><span class="op">:</span> zero
<span class="op">},</span> <span class="op">{</span>
<span class="dt">name</span><span class="op">:</span> <span class="st">'对比'</span><span class="op">,</span>
<span class="dt">data</span><span class="op">:</span> [<span class="fl">26.0</span>]
<span class="op">}</span>]
<span class="op">}</span>)<span class="op">;</span>
<span class="op">}</span>)<span class="op">;</span>
<span class="op">};</span>
<span class="kw">function</span> <span class="at">showTemper</span>() <span class="op">{</span>
<span class="kw">var</span> length <span class="op">=</span> dataLength[<span class="dv">0</span>]<span class="op">;</span>
<span class="va">$</span>.<span class="at">ajax</span>(<span class="op">{</span>
<span class="dt">url</span><span class="op">:</span> <span class="st">'/athome/'</span> <span class="op">+</span> length<span class="op">,</span>
<span class="dt">type</span><span class="op">:</span> <span class="st">'GET'</span><span class="op">,</span>
<span class="dt">dataType</span><span class="op">:</span> <span class="st">'json'</span><span class="op">,</span>
<span class="dt">async</span><span class="op">:</span> <span class="kw">true</span><span class="op">,</span>
<span class="dt">timeout</span><span class="op">:</span> <span class="dv">1000</span><span class="op">,</span>
<span class="dt">error</span><span class="op">:</span> <span class="kw">function</span>() <span class="op">{},</span>
<span class="dt">success</span><span class="op">:</span> <span class="kw">function</span>(sdata) <span class="op">{</span>
<span class="at">$</span>(<span class="st">'.temperStatus'</span>).<span class="at">empty</span>()<span class="op">;</span>
<span class="at">$</span>(<span class="st">'.temperStatus'</span>).<span class="at">append</span>(<span class="va">sdata</span>.<span class="at">temperature</span>)<span class="op">;</span>
<span class="op">}</span>
<span class="op">}</span>)<span class="op">;</span>
<span class="op">};</span>
<span class="at">$</span>(document).<span class="at">ready</span>(<span class="kw">function</span>() <span class="op">{</span>
<span class="at">setInterval</span>(<span class="st">"drawTemp();"</span><span class="op">,</span> <span class="dv">5000</span>)<span class="op">;</span>
<span class="at">setInterval</span>(<span class="st">"showTemper();"</span><span class="op">,</span> <span class="dv">800</span>)<span class="op">;</span>
<span class="at">drawTemp</span>()<span class="op">;</span>
<span class="at">showTemper</span>()<span class="op">;</span>
<span class="op">}</span>)<span class="op">;</span></code></pre></div>
<p>我们先把<code>HighChart</code>部分的代码删去,就变成下面的内容:</p>
<div class="sourceCode"><pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="kw">var</span> dataLength <span class="op">=</span> []<span class="op">;</span>
<span class="kw">function</span> <span class="at">drawTemp</span>() <span class="op">{</span>
<span class="kw">var</span> zero <span class="op">=</span> []<span class="op">;</span>
<span class="va">$</span>.<span class="at">getJSON</span>(<span class="st">'/athome/'</span><span class="op">,</span> <span class="kw">function</span>(json) <span class="op">{</span>
<span class="kw">var</span> items <span class="op">=</span> []<span class="op">;</span>
<span class="va">dataLength</span>.<span class="at">push</span>(<span class="va">json</span>.<span class="at">length</span>)<span class="op">;</span>
<span class="va">$</span>.<span class="at">each</span>(json<span class="op">,</span> <span class="kw">function</span>(key<span class="op">,</span> val) <span class="op">{</span>
<span class="va">zero</span>.<span class="at">push</span>(<span class="va">val</span>.<span class="at">temperature</span>)<span class="op">;</span>
<span class="op">}</span>)<span class="op">;</span>
<span class="op">}</span>)<span class="op">;</span>
<span class="op">};</span>
<span class="kw">function</span> <span class="at">showTemper</span>() <span class="op">{</span>
<span class="kw">var</span> length <span class="op">=</span> dataLength[<span class="dv">0</span>]<span class="op">;</span>
<span class="va">$</span>.<span class="at">ajax</span>(<span class="op">{</span>
<span class="dt">url</span><span class="op">:</span> <span class="st">'/athome/'</span> <span class="op">+</span> length<span class="op">,</span>
<span class="dt">type</span><span class="op">:</span> <span class="st">'GET'</span><span class="op">,</span>
<span class="dt">dataType</span><span class="op">:</span> <span class="st">'json'</span><span class="op">,</span>
<span class="dt">async</span><span class="op">:</span> <span class="kw">true</span><span class="op">,</span>
<span class="dt">timeout</span><span class="op">:</span> <span class="dv">1000</span><span class="op">,</span>
<span class="dt">error</span><span class="op">:</span> <span class="kw">function</span>() <span class="op">{},</span>
<span class="dt">success</span><span class="op">:</span> <span class="kw">function</span>(sdata) <span class="op">{</span>
<span class="at">$</span>(<span class="st">'.temperStatus'</span>).<span class="at">empty</span>()<span class="op">;</span>
<span class="at">$</span>(<span class="st">'.temperStatus'</span>).<span class="at">append</span>(<span class="va">sdata</span>.<span class="at">temperature</span>)<span class="op">;</span>
<span class="op">}</span>
<span class="op">}</span>)<span class="op">;</span>
<span class="op">};</span>
<span class="at">$</span>(document).<span class="at">ready</span>(<span class="kw">function</span>() <span class="op">{</span>
<span class="at">setInterval</span>(<span class="st">"drawTemp();"</span><span class="op">,</span> <span class="dv">5000</span>)<span class="op">;</span>
<span class="at">setInterval</span>(<span class="st">"showTemper();"</span><span class="op">,</span> <span class="dv">800</span>)<span class="op">;</span>
<span class="at">drawTemp</span>()<span class="op">;</span>
<span class="at">showTemper</span>()<span class="op">;</span>
<span class="op">}</span>)<span class="op">;</span></code></pre></div>
<p>代码看上去是由两部分组成的<code>drawTemperature</code>和<code>showTemperature</code></p>
<h3 id="实时数据"><span class="header-section-number">1.3.2</span> 实时数据</h3>
<p>剥离后的Ajax部分代码如下所示,主要用的是jQuery框架的getJSON来实现的</p>
<pre><code>var dataLength = [];
function drawTemp() {
var zero = [];
$.getJSON('/athome/', function(json) {
var items = [];
dataLength.push(json.length);
$.each(json, function(key, val) {
zero.push(val.temperature);
});
};</code></pre>
<p>实际上,我们做的只是从/athome/下面获取数据,再将数据堆到数组里面,再把这部分放到图形中。等等,什么是Ajax?</p>
<p>AJAX = Asynchronous JavaScript and XML(异步的 JavaScript 和 XML)。 AJAX 不是新的编程语言,而是一种使用现有标准的新方法。 AJAX 是与服务器交换数据并更新部分网页的艺术,在不重新加载整个页面的情况下。 JSON我们前面也已经了解过了,看看getJSON吧。</p>
<p>jQuery. getJSON</p>
<p>方法定义:jQuery.getJSON( url, data, callback )</p>
<p>通过get请求得到json数据</p>
<p>·url用于提供json数据的地址页 ·data(Optional)用于传送到服务器的键值对 ·callback(Optional)回调函数,json数据请求成功后的处理函数 我想你似乎应该懂得了一点,就是在不刷新网页的同时,用javascript获取数据放到图表上,就这么简单。</p>
</body>
</html>