2.5.frontend.md.html 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <meta http-equiv="Content-Style-Type" content="text/css" />
  6. <meta name="generator" content="pandoc" />
  7. <title></title>
  8. <style type="text/css">code{white-space: pre;}</style>
  9. <style type="text/css">
  10. div.sourceCode { overflow-x: auto; }
  11. table.sourceCode, tr.sourceCode, td.lineNumbers, td.sourceCode {
  12. margin: 0; padding: 0; vertical-align: baseline; border: none; }
  13. table.sourceCode { width: 100%; line-height: 100%; }
  14. td.lineNumbers { text-align: right; padding-right: 4px; padding-left: 4px; color: #aaaaaa; border-right: 1px solid #aaaaaa; }
  15. td.sourceCode { padding-left: 5px; }
  16. code > span.kw { color: #007020; font-weight: bold; } /* Keyword */
  17. code > span.dt { color: #902000; } /* DataType */
  18. code > span.dv { color: #40a070; } /* DecVal */
  19. code > span.bn { color: #40a070; } /* BaseN */
  20. code > span.fl { color: #40a070; } /* Float */
  21. code > span.ch { color: #4070a0; } /* Char */
  22. code > span.st { color: #4070a0; } /* String */
  23. code > span.co { color: #60a0b0; font-style: italic; } /* Comment */
  24. code > span.ot { color: #007020; } /* Other */
  25. code > span.al { color: #ff0000; font-weight: bold; } /* Alert */
  26. code > span.fu { color: #06287e; } /* Function */
  27. code > span.er { color: #ff0000; font-weight: bold; } /* Error */
  28. code > span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } /* Warning */
  29. code > span.cn { color: #880000; } /* Constant */
  30. code > span.sc { color: #4070a0; } /* SpecialChar */
  31. code > span.vs { color: #4070a0; } /* VerbatimString */
  32. code > span.ss { color: #bb6688; } /* SpecialString */
  33. code > span.im { } /* Import */
  34. code > span.va { color: #19177c; } /* Variable */
  35. code > span.cf { color: #007020; font-weight: bold; } /* ControlFlow */
  36. code > span.op { color: #666666; } /* Operator */
  37. code > span.bu { } /* BuiltIn */
  38. code > span.ex { } /* Extension */
  39. code > span.pp { color: #bc7a00; } /* Preprocessor */
  40. code > span.at { color: #7d9029; } /* Attribute */
  41. code > span.do { color: #ba2121; font-style: italic; } /* Documentation */
  42. code > span.an { color: #60a0b0; font-weight: bold; font-style: italic; } /* Annotation */
  43. code > span.cv { color: #60a0b0; font-weight: bold; font-style: italic; } /* CommentVar */
  44. code > span.in { color: #60a0b0; font-weight: bold; font-style: italic; } /* Information */
  45. </style>
  46. <link rel="stylesheet" href="css/vendor.css" type="text/css" />
  47. </head>
  48. <body>
  49. <html xmlns="http://www.w3.org/1999/xhtml" xmlns:wb="http://open.weibo.com/wb" >
  50. <head>
  51. <title>一步步搭建物联网系统(教你设计物联网系统)</title>
  52. <meta name="keywords" content="设计物联网系统,物联网系统设计">
  53. <meta name="description" content="一份关于如何设计物联网系统的文档">
  54. <link rel="stylesheet" href="../css/vendor.css" />
  55. </head>
  56. <body class="container inner wrap">
  57. <h1>一步步搭建物联网系统(教你设计物联网系统)</h1>
  58. </body>
  59. </html>
  60. <div id="TOC">
  61. <ul>
  62. <li><a href="#前端显示"><span class="toc-section-number">1</span> 前端显示</a><ul>
  63. <li><a href="#库与车轮子"><span class="toc-section-number">1.1</span> 库与车轮子</a></li>
  64. <li><a href="#库"><span class="toc-section-number">1.2</span> 库</a><ul>
  65. <li><a href="#jquery"><span class="toc-section-number">1.2.1</span> jQuery</a></li>
  66. <li><a href="#jquery-mobile"><span class="toc-section-number">1.2.2</span> jQuery Mobile</a></li>
  67. </ul></li>
  68. <li><a href="#网站前台显示"><span class="toc-section-number">1.3</span> 网站前台显示</a><ul>
  69. <li><a href="#highcharts"><span class="toc-section-number">1.3.1</span> Highcharts</a></li>
  70. <li><a href="#实时数据"><span class="toc-section-number">1.3.2</span> 实时数据</a></li>
  71. </ul></li>
  72. </ul></li>
  73. </ul>
  74. </div>
  75. <h1 id="前端显示"><span class="header-section-number">1</span> 前端显示</h1>
  76. <h2 id="库与车轮子"><span class="header-section-number">1.1</span> 库与车轮子</h2>
  77. <p>在多数的情况下我们都没有理由也没有必要去重新发明我们的车轮,在这时使用库会是一个比较好的做法。</p>
  78. <h2 id="库"><span class="header-section-number">1.2</span> 库</h2>
  79. <h3 id="jquery"><span class="header-section-number">1.2.1</span> jQuery</h3>
  80. <blockquote>
  81. <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>
  82. </blockquote>
  83. <p>在我们的代码里用到了jQuery,然而这是一种简单而且快速有速地方法。</p>
  84. <h3 id="jquery-mobile"><span class="header-section-number">1.2.2</span> jQuery Mobile</h3>
  85. <blockquote>
  86. <p>jQuery Mobile是jQuery 在手机上和平板设备上的版本。jQuery Mobile不仅会给主流移动平台带来jQuery核心库,而且会发布一个完整统一的jQuery移动UI框架。支持全球主流的移动平台。jQuery Mobile开发团队说:能开发这个项目,我们非常兴奋。移动Web太需要一个跨浏览器的框架,让开发人员开发出真正的移动Web网站。</p>
  87. </blockquote>
  88. <p>整个展示页面由三部分组成,即<code>header</code>,<code>content</code>,<code>footer</code>。而我们主要需要关心的是content,也就是真下的内容。</p>
  89. <p>我们只需要结合<code>HighChart</code>来设计我们的content就可以了,下面是一个简单地<code>HighChart</code>示例:</p>
  90. <pre><code>&lt;div id=&quot;Tchart&quot; style=&quot;min-width:800px;width:100%;height:300px&quot;&gt;&lt;/div&gt;
  91. &lt;/div&gt;</code></pre>
  92. <p>剩下的事就由<code>HighChart</code>来做。</p>
  93. <p>最后代码如下所示</p>
  94. <div class="sourceCode"><pre class="sourceCode html"><code class="sourceCode html"><span class="kw">&lt;body&gt;</span>
  95. <span class="kw">&lt;div</span><span class="ot"> data-role=</span><span class="st">&quot;page&quot;</span><span class="kw">&gt;</span>
  96. <span class="kw">&lt;div</span><span class="ot"> data-role=</span><span class="st">&quot;header&quot;</span><span class="ot"> class=</span><span class="st">&quot;ui-bar ui-bar-b&quot;</span><span class="ot"> class=</span><span class="st">&quot;jqm-header&quot;</span><span class="kw">&gt;</span>
  97. <span class="kw">&lt;h1&gt;</span>基础控制<span class="kw">&lt;/h1&gt;</span>
  98. <span class="kw">&lt;/div&gt;</span>
  99. <span class="kw">&lt;div</span><span class="ot"> data-role=</span><span class="st">&quot;collapsible&quot;</span><span class="ot"> data-collapsed=</span><span class="st">&quot;false&quot;</span><span class="kw">&gt;</span>
  100. <span class="kw">&lt;h3&gt;</span>查看温度情况<span class="kw">&lt;/h3&gt;</span>
  101. <span class="kw">&lt;div&gt;</span>
  102. <span class="kw">&lt;div</span><span class="ot"> id=</span><span class="st">&quot;Tchart&quot;</span><span class="ot"> style=</span><span class="st">&quot;min-width:800px;width:100%;height:300px&quot;</span><span class="kw">&gt;&lt;/div&gt;</span>
  103. <span class="kw">&lt;/div&gt;</span>
  104. <span class="kw">&lt;/div&gt;</span>
  105. <span class="kw">&lt;div</span><span class="ot"> data-role=</span><span class="st">&quot;footer&quot;</span><span class="ot"> class=</span><span class="st">&quot;ui-bar ui-bar-b&quot;</span><span class="ot"> data-position=</span><span class="st">&quot;fixed&quot;</span><span class="kw">&gt;</span>
  106. <span class="kw">&lt;h4&gt;&lt;a</span><span class="ot"> href=</span><span class="st">&quot;http://www.phodal.com&quot;</span><span class="kw">&gt;</span>Power by Phodal<span class="kw">&lt;/a&gt;&lt;/h4&gt;</span>
  107. <span class="kw">&lt;/div&gt;</span>
  108. <span class="kw">&lt;/body&gt;</span></code></pre></div>
  109. <p>可以看到上面的代码与一般的HTML不同的地方是<code>data-role</code>,<code>data-collapsed</code>,<code>data-position</code>,而这些是jQuery Mobile所拥有的data属性。</p>
  110. <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>
  111. <p>同上,我们也可以在网上找到其他相对就的属性。</p>
  112. <h2 id="网站前台显示"><span class="header-section-number">1.3</span> 网站前台显示</h2>
  113. <h3 id="highcharts"><span class="header-section-number">1.3.1</span> Highcharts</h3>
  114. <p>Highcharts有以下的特点</p>
  115. <ul>
  116. <li>兼容性:兼容当今所有的浏览器,包括 iPhone、IE 和火狐等等;</li>
  117. <li>对个人用户完全免费;</li>
  118. <li>纯JS,无BS;</li>
  119. <li>支持大部分的图表类型:直线图,曲线图、区域图、区域曲线图、柱状图、饼装图、散布图;</li>
  120. <li>跨语言:不管是 PHP、Asp.net 还是 Java 都可以使用,它只需要三个文件:一个是Highcharts 的核心文件 highcharts.js,还有 a canvas emulator for IE 和 Jquery类库或者 MooTools 类库;</li>
  121. <li>提示功能:鼠标移动到图表的某一点上有提示信息;</li>
  122. <li>放大功能:选中图表部分放大,近距离观察图表;</li>
  123. <li>易用性:无需要特殊的开发技能,只需要设置一下选项就可以制作适合自己的图表;</li>
  124. <li>时间轴:可以精确到毫秒;</li>
  125. </ul>
  126. <p>在这里只需将需要处理的数据存储到数组中,便可以将其渲染成为图形,下面的温度走势图便是基于Highcharts的结果:</p>
  127. <div class="figure">
  128. <img src="./images/temperture.png" alt="Temperture" />
  129. <p class="caption">Temperture</p>
  130. </div>
  131. <p>先看看最后代码如下所示:</p>
  132. <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>
  133. <span class="kw">function</span> <span class="at">drawTemp</span>() <span class="op">{</span>
  134. <span class="kw">var</span> zero <span class="op">=</span> []<span class="op">;</span>
  135. <span class="va">$</span>.<span class="at">getJSON</span>(<span class="st">&#39;/athome/&#39;</span><span class="op">,</span> <span class="kw">function</span>(json) <span class="op">{</span>
  136. <span class="kw">var</span> items <span class="op">=</span> []<span class="op">;</span>
  137. <span class="va">dataLength</span>.<span class="at">push</span>(<span class="va">json</span>.<span class="at">length</span>)<span class="op">;</span>
  138. <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>
  139. <span class="va">zero</span>.<span class="at">push</span>(<span class="va">val</span>.<span class="at">temperature</span>)<span class="op">;</span>
  140. <span class="op">}</span>)<span class="op">;</span>
  141. chart <span class="op">=</span> <span class="kw">new</span> <span class="va">Highcharts</span>.<span class="at">Chart</span>(<span class="op">{</span>
  142. <span class="dt">color</span><span class="op">:</span> <span class="op">{</span>
  143. <span class="dt">linearGradient</span><span class="op">:</span> <span class="op">{</span>
  144. <span class="dt">x1</span><span class="op">:</span> <span class="dv">0</span><span class="op">,</span>
  145. <span class="dt">x2</span><span class="op">:</span> <span class="dv">0</span><span class="op">,</span>
  146. <span class="dt">y1</span><span class="op">:</span> <span class="dv">0</span><span class="op">,</span>
  147. <span class="dt">y1</span><span class="op">:</span> <span class="dv">1</span>
  148. <span class="op">},</span>
  149. <span class="dt">stops</span><span class="op">:</span> [
  150. [<span class="dv">0</span><span class="op">,</span> <span class="st">&#39;#003399&#39;</span>]<span class="op">,</span>
  151. [<span class="dv">1</span><span class="op">,</span> <span class="st">&#39;#3366AA&#39;</span>]
  152. ]
  153. <span class="op">},</span>
  154. <span class="dt">chart</span><span class="op">:</span> <span class="op">{</span>
  155. <span class="dt">renderTo</span><span class="op">:</span> <span class="st">&#39;Tchart&#39;</span><span class="op">,</span>
  156. <span class="dt">type</span><span class="op">:</span> <span class="st">&#39;spline&#39;</span>
  157. <span class="op">},</span>
  158. <span class="dt">title</span><span class="op">:</span> <span class="op">{</span>
  159. <span class="dt">text</span><span class="op">:</span> <span class="st">&#39;本月温度情况&#39;</span>
  160. <span class="op">},</span>
  161. <span class="dt">subtitle</span><span class="op">:</span> <span class="op">{</span>
  162. <span class="dt">text</span><span class="op">:</span> <span class="st">&#39;&#39;</span>
  163. <span class="op">},</span>
  164. <span class="dt">xAxis</span><span class="op">:</span> <span class="op">{</span>
  165. <span class="dt">categories</span><span class="op">:</span> []<span class="op">,</span>
  166. <span class="dt">title</span><span class="op">:</span> <span class="op">{</span>
  167. <span class="dt">text</span><span class="op">:</span> <span class="st">&#39;&#39;</span>
  168. <span class="op">}</span>
  169. <span class="op">},</span>
  170. <span class="dt">yAxis</span><span class="op">:</span> <span class="op">{</span>
  171. <span class="dt">title</span><span class="op">:</span> <span class="op">{</span>
  172. <span class="dt">text</span><span class="op">:</span> <span class="st">&#39;温度 (°C)&#39;</span>
  173. <span class="op">}</span>
  174. <span class="op">},</span>
  175. <span class="dt">tooltip</span><span class="op">:</span> <span class="op">{</span>
  176. <span class="dt">backgroundColor</span><span class="op">:</span> <span class="st">&#39;#FCFFC5&#39;</span><span class="op">,</span>
  177. <span class="dt">borderColor</span><span class="op">:</span> <span class="st">&#39;black&#39;</span><span class="op">,</span>
  178. <span class="dt">borderRadius</span><span class="op">:</span> <span class="dv">10</span><span class="op">,</span>
  179. <span class="dt">borderWidth</span><span class="op">:</span> <span class="dv">1</span><span class="op">,</span>
  180. <span class="dt">enabled</span><span class="op">:</span> <span class="kw">true</span><span class="op">,</span>
  181. <span class="dt">formatter</span><span class="op">:</span> <span class="kw">function</span>() <span class="op">{</span>
  182. <span class="cf">return</span> <span class="st">&#39;&lt;b&gt;&#39;</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">&#39;&lt;/b&gt;&lt;br/&gt;&#39;</span> <span class="op">+</span> <span class="kw">this</span>.<span class="at">x</span> <span class="op">+</span> <span class="st">&#39;: &#39;</span> <span class="op">+</span> <span class="kw">this</span>.<span class="at">y</span> <span class="op">+</span> <span class="st">&#39;°C&#39;</span><span class="op">;</span>
  183. <span class="op">}</span>
  184. <span class="op">},</span>
  185. <span class="dt">legend</span><span class="op">:</span> <span class="op">{</span>
  186. <span class="dt">layout</span><span class="op">:</span> <span class="st">&#39;vertical&#39;</span><span class="op">,</span>
  187. <span class="dt">align</span><span class="op">:</span> <span class="st">&#39;right&#39;</span><span class="op">,</span>
  188. <span class="dt">verticalAlign</span><span class="op">:</span> <span class="st">&#39;top&#39;</span><span class="op">,</span>
  189. <span class="dt">x</span><span class="op">:</span> <span class="op">-</span><span class="dv">10</span><span class="op">,</span>
  190. <span class="dt">y</span><span class="op">:</span> <span class="dv">100</span><span class="op">,</span>
  191. <span class="dt">borderWidth</span><span class="op">:</span> <span class="dv">0</span>
  192. <span class="op">},</span>
  193. <span class="dt">plotOptions</span><span class="op">:</span> <span class="op">{</span>
  194. <span class="dt">line</span><span class="op">:</span> <span class="op">{</span>
  195. <span class="dt">dataLabels</span><span class="op">:</span> <span class="op">{</span>
  196. <span class="dt">enabled</span><span class="op">:</span> <span class="kw">true</span>
  197. <span class="op">},</span>
  198. <span class="dt">enableMouseTracking</span><span class="op">:</span> <span class="kw">false</span>
  199. <span class="op">}</span>
  200. <span class="op">},</span>
  201. <span class="dt">series</span><span class="op">:</span> [<span class="op">{</span>
  202. <span class="dt">name</span><span class="op">:</span> <span class="st">&#39;本月&#39;</span><span class="op">,</span>
  203. <span class="dt">data</span><span class="op">:</span> zero
  204. <span class="op">},</span> <span class="op">{</span>
  205. <span class="dt">name</span><span class="op">:</span> <span class="st">&#39;对比&#39;</span><span class="op">,</span>
  206. <span class="dt">data</span><span class="op">:</span> [<span class="fl">26.0</span>]
  207. <span class="op">}</span>]
  208. <span class="op">}</span>)<span class="op">;</span>
  209. <span class="op">}</span>)<span class="op">;</span>
  210. <span class="op">};</span>
  211. <span class="kw">function</span> <span class="at">showTemper</span>() <span class="op">{</span>
  212. <span class="kw">var</span> length <span class="op">=</span> dataLength[<span class="dv">0</span>]<span class="op">;</span>
  213. <span class="va">$</span>.<span class="at">ajax</span>(<span class="op">{</span>
  214. <span class="dt">url</span><span class="op">:</span> <span class="st">&#39;/athome/&#39;</span> <span class="op">+</span> length<span class="op">,</span>
  215. <span class="dt">type</span><span class="op">:</span> <span class="st">&#39;GET&#39;</span><span class="op">,</span>
  216. <span class="dt">dataType</span><span class="op">:</span> <span class="st">&#39;json&#39;</span><span class="op">,</span>
  217. <span class="dt">async</span><span class="op">:</span> <span class="kw">true</span><span class="op">,</span>
  218. <span class="dt">timeout</span><span class="op">:</span> <span class="dv">1000</span><span class="op">,</span>
  219. <span class="dt">error</span><span class="op">:</span> <span class="kw">function</span>() <span class="op">{},</span>
  220. <span class="dt">success</span><span class="op">:</span> <span class="kw">function</span>(sdata) <span class="op">{</span>
  221. <span class="at">$</span>(<span class="st">&#39;.temperStatus&#39;</span>).<span class="at">empty</span>()<span class="op">;</span>
  222. <span class="at">$</span>(<span class="st">&#39;.temperStatus&#39;</span>).<span class="at">append</span>(<span class="va">sdata</span>.<span class="at">temperature</span>)<span class="op">;</span>
  223. <span class="op">}</span>
  224. <span class="op">}</span>)<span class="op">;</span>
  225. <span class="op">};</span>
  226. <span class="at">$</span>(document).<span class="at">ready</span>(<span class="kw">function</span>() <span class="op">{</span>
  227. <span class="at">setInterval</span>(<span class="st">&quot;drawTemp();&quot;</span><span class="op">,</span> <span class="dv">5000</span>)<span class="op">;</span>
  228. <span class="at">setInterval</span>(<span class="st">&quot;showTemper();&quot;</span><span class="op">,</span> <span class="dv">800</span>)<span class="op">;</span>
  229. <span class="at">drawTemp</span>()<span class="op">;</span>
  230. <span class="at">showTemper</span>()<span class="op">;</span>
  231. <span class="op">}</span>)<span class="op">;</span></code></pre></div>
  232. <p>我们先把<code>HighChart</code>部分的代码删去,就变成下面的内容:</p>
  233. <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>
  234. <span class="kw">function</span> <span class="at">drawTemp</span>() <span class="op">{</span>
  235. <span class="kw">var</span> zero <span class="op">=</span> []<span class="op">;</span>
  236. <span class="va">$</span>.<span class="at">getJSON</span>(<span class="st">&#39;/athome/&#39;</span><span class="op">,</span> <span class="kw">function</span>(json) <span class="op">{</span>
  237. <span class="kw">var</span> items <span class="op">=</span> []<span class="op">;</span>
  238. <span class="va">dataLength</span>.<span class="at">push</span>(<span class="va">json</span>.<span class="at">length</span>)<span class="op">;</span>
  239. <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>
  240. <span class="va">zero</span>.<span class="at">push</span>(<span class="va">val</span>.<span class="at">temperature</span>)<span class="op">;</span>
  241. <span class="op">}</span>)<span class="op">;</span>
  242. <span class="op">}</span>)<span class="op">;</span>
  243. <span class="op">};</span>
  244. <span class="kw">function</span> <span class="at">showTemper</span>() <span class="op">{</span>
  245. <span class="kw">var</span> length <span class="op">=</span> dataLength[<span class="dv">0</span>]<span class="op">;</span>
  246. <span class="va">$</span>.<span class="at">ajax</span>(<span class="op">{</span>
  247. <span class="dt">url</span><span class="op">:</span> <span class="st">&#39;/athome/&#39;</span> <span class="op">+</span> length<span class="op">,</span>
  248. <span class="dt">type</span><span class="op">:</span> <span class="st">&#39;GET&#39;</span><span class="op">,</span>
  249. <span class="dt">dataType</span><span class="op">:</span> <span class="st">&#39;json&#39;</span><span class="op">,</span>
  250. <span class="dt">async</span><span class="op">:</span> <span class="kw">true</span><span class="op">,</span>
  251. <span class="dt">timeout</span><span class="op">:</span> <span class="dv">1000</span><span class="op">,</span>
  252. <span class="dt">error</span><span class="op">:</span> <span class="kw">function</span>() <span class="op">{},</span>
  253. <span class="dt">success</span><span class="op">:</span> <span class="kw">function</span>(sdata) <span class="op">{</span>
  254. <span class="at">$</span>(<span class="st">&#39;.temperStatus&#39;</span>).<span class="at">empty</span>()<span class="op">;</span>
  255. <span class="at">$</span>(<span class="st">&#39;.temperStatus&#39;</span>).<span class="at">append</span>(<span class="va">sdata</span>.<span class="at">temperature</span>)<span class="op">;</span>
  256. <span class="op">}</span>
  257. <span class="op">}</span>)<span class="op">;</span>
  258. <span class="op">};</span>
  259. <span class="at">$</span>(document).<span class="at">ready</span>(<span class="kw">function</span>() <span class="op">{</span>
  260. <span class="at">setInterval</span>(<span class="st">&quot;drawTemp();&quot;</span><span class="op">,</span> <span class="dv">5000</span>)<span class="op">;</span>
  261. <span class="at">setInterval</span>(<span class="st">&quot;showTemper();&quot;</span><span class="op">,</span> <span class="dv">800</span>)<span class="op">;</span>
  262. <span class="at">drawTemp</span>()<span class="op">;</span>
  263. <span class="at">showTemper</span>()<span class="op">;</span>
  264. <span class="op">}</span>)<span class="op">;</span></code></pre></div>
  265. <p>代码看上去是由两部分组成的<code>drawTemperature</code>和<code>showTemperature</code></p>
  266. <h3 id="实时数据"><span class="header-section-number">1.3.2</span> 实时数据</h3>
  267. <p>剥离后的Ajax部分代码如下所示,主要用的是jQuery框架的getJSON来实现的</p>
  268. <pre><code>var dataLength = [];
  269. function drawTemp() {
  270. var zero = [];
  271. $.getJSON(&#39;/athome/&#39;, function(json) {
  272. var items = [];
  273. dataLength.push(json.length);
  274. $.each(json, function(key, val) {
  275. zero.push(val.temperature);
  276. });
  277. };</code></pre>
  278. <p>实际上,我们做的只是从/athome/下面获取数据,再将数据堆到数组里面,再把这部分放到图形中。等等,什么是Ajax?</p>
  279. <p>AJAX = Asynchronous JavaScript and XML(异步的 JavaScript 和 XML)。 AJAX 不是新的编程语言,而是一种使用现有标准的新方法。 AJAX 是与服务器交换数据并更新部分网页的艺术,在不重新加载整个页面的情况下。 JSON我们前面也已经了解过了,看看getJSON吧。</p>
  280. <p>jQuery. getJSON</p>
  281. <p>方法定义:jQuery.getJSON( url, data, callback )</p>
  282. <p>通过get请求得到json数据</p>
  283. <p>·url用于提供json数据的地址页 ·data(Optional)用于传送到服务器的键值对 ·callback(Optional)回调函数,json数据请求成功后的处理函数 我想你似乎应该懂得了一点,就是在不刷新网页的同时,用javascript获取数据放到图表上,就这么简单。</p>
  284. </body>
  285. </html>