Untitled


SUBMITTED BY: Guest

DATE: June 14, 2013, 12:09 p.m.

FORMAT: Text only

SIZE: 11.0 kB

HITS: 5587

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  5. <title></title>
  6. <link rel="stylesheet" type="text/css" href="../jquery.jqplot.min.css" />
  7. <!--
  8. <link rel="stylesheet" type="text/css" href="examples.css" />
  9. -->
  10. <!-- BEGIN: load jquery -->
  11. <script language="javascript" type="text/javascript" src="../jquery.min.js"></script>
  12. <!-- END: load jquery -->
  13. <!-- BEGIN: load jqplot -->
  14. <script language="javascript" type="text/javascript" src="../jquery.jqplot.min.js"></script>
  15. <script language="javascript" type="text/javascript" src="../plugins/jqplot.dateAxisRenderer.min.js"></script>
  16. <script language="javascript" type="text/javascript" src="../plugins/jqplot.categoryAxisRenderer.min.js"></script>
  17. <script language="javascript" type="text/javascript" src="../plugins/jqplot.canvasOverlay.min.js"></script>
  18. <!-- END: load jqplot -->
  19. <script language="javascript" type="text/javascript">
  20. var data = [[0, 1332.63],
  21. [1, 1332.87],
  22. [2, 1332.41],
  23. [3, 1325.83],
  24. [4, 1328.26],
  25. [5, 1319.44],
  26. [6, 1310.19],
  27. [7, 1313.8],
  28. [8, 1309.66],
  29. [9, 1297.54],
  30. [10, 1293.77],
  31. [11, 1298.38],
  32. [12, 1279.21]];
  33. ///////
  34. // Put plot options in seperate object as a convienence.
  35. // These will be reused when calling $.jqplot()
  36. ///////
  37. var plotOptions = {
  38. gridPadding: {top: 1},
  39. grid: {shadow:false, borderWidth:1.0},
  40. seriesDefaults: {
  41. yaxis: 'y2axis'
  42. },
  43. axes: {
  44. xaxis: {
  45. //renderer:$.jqplot.DateAxisRenderer,
  46. //renderer:$.jqplot.LinearAxisRenderer
  47. //tickOptions:{formatString:'%b %d'},
  48. min:0,
  49. max:1000
  50. },
  51. y2axis: {
  52. tickOptions:{formatString:'%.4f'}
  53. }
  54. },
  55. series: [{
  56. showMarker: false
  57. }],
  58. // noDataIndicator option. If no data is passed in on plot creation,
  59. // A div will be placed roughly in the center of the plot. Whatever
  60. // text or html is given in the "indicator" option will be rendered
  61. // inside the div.
  62. noDataIndicator: {
  63. show: true,
  64. // Here, an animated gif image is rendered with some loading text.
  65. indicator: '<img src="ajax-loader.gif" /><br />Loading Data...',
  66. // IMPORTANT: Have axes options set since y2axis is off by default
  67. // and the yaxis is on be default. This is necessary due to the way
  68. // plots are constructed from data and we don't have any data
  69. // when using the "noDataIndicator".
  70. axes: {
  71. xaxis: {
  72. min: 0,
  73. max: 5,
  74. tickInterval: 1,
  75. showTicks: false
  76. },
  77. yaxis: {
  78. show: false
  79. },
  80. y2axis: {
  81. show: true,
  82. min: 0,
  83. max: 8,
  84. tickInterval: 2,
  85. showTicks: false
  86. }
  87. }
  88. },
  89. // Canvas overlay provides display of arbitrary lines on the plot and
  90. // provides a convienent api to manipulate those lines after creation.
  91. canvasOverlay: {
  92. show: true,
  93. // An array of objects to draw over plot. Here two horizontal lines.
  94. // Options to control linne width, color, position and shadow are set.
  95. // Can also provide a "name" option to refer to the line by name.
  96. // IMPORTANT: set the proper yaxis to "bind" the line to.
  97. objects: [
  98. {dashedHorizontalLine: {
  99. name: 'current',
  100. y: 6,
  101. lineWidth: 1.5,
  102. color: 'rgb(60, 60, 60)',
  103. yaxis: 'y2axis',
  104. shadow: false,
  105. dashPattern: [12, 12]
  106. }}
  107. ]
  108. }
  109. };
  110. ////////
  111. // Build the initial plot with no data.
  112. // It will show the animated gif indicator since we have
  113. // the "noDataIndicator" option set on the plot.
  114. ///////
  115. $(document).ready(function(){
  116. plot1 = $.jqplot('chart1',[],plotOptions);
  117. });
  118. ///////
  119. // Functions to handle recreation of plot when data is available
  120. // and to simulate new data coming into the plot and plot updates.
  121. ///////
  122. ///////
  123. // Callback executed when "start" button on web page is pressed.
  124. // Simulates recreation of plot when actual data is available. Sequence is:
  125. //
  126. // 1) empty plot container.
  127. // 2) Recreate plot with call to $.jqplot()
  128. // 3) Update position of current price line based on data.
  129. // 4) Create the "pricePointer", a div indicating the current price.
  130. // 5) Call updatePricePointer to set current price text and position div.
  131. // 6) Mock up streaming with a setInterval call.
  132. ///////
  133. function startit(initialData) {
  134. // 1) Empty container.
  135. $('#chart1').empty();
  136. // 2) Recreate plot.
  137. // Note, only call the $.jqplot() method when entire plot needs recreated.
  138. plot1 = $.jqplot('chart1', [initialData], plotOptions);
  139. // get handle on last data point and current price line.
  140. var dp = initialData[initialData.length-1];
  141. var co = plot1.plugins.canvasOverlay;
  142. var current = co.get('current');
  143. // 3) Update the limit and stop lines based on the latest data.
  144. current.options.y = dp[1];
  145. co.draw(plot1);
  146. // 4) Create the 'pricePointer', element and append to the plot.
  147. $('<div id="pricePointer" style="position:absolute;"></div>').appendTo(plot1.target);
  148. // 5) updatePricePointer method sets text and position to value passed in.
  149. updatePricePointer(dp[1]);
  150. // 6) Stuff to mock up streaming.
  151. counter = 0;
  152. Interval = setInterval(runUpdate, 500);
  153. d = dp = co = limit = stop = null;
  154. }
  155. ///////
  156. // Function to generate an updated data value
  157. // for the last data point in the series.
  158. // This does not change the timestamp, just the data value.
  159. ///////
  160. function getNewDataPoint() {
  161. // Need to generate some mock data. Will use the last data from
  162. // the plot as a base set. Adjust the close value by a random amount,
  163. // a slight adjustment to current price
  164. var val = Math.random()*20 * (Math.random() - 0.1);
  165. var d = plot1.series[0].data;
  166. var dp = d[d.length-1];
  167. // get the current values.
  168. var ts = dp[0]+1;//increment x tick by 1!
  169. var curclose = dp[1];
  170. // Set the new close value
  171. var newval = curclose + val;
  172. val = d = dp = curclose = null;
  173. // Now return a new data set, note haven't changed or used the datetime value.
  174. return ([ts, newval]);
  175. }
  176. ////////
  177. // function to simulate application loop, where app does something
  178. // every time it gets a new data point.
  179. ////////
  180. function runUpdate() {
  181. // Simulate 1000 iterations of streaming for testing purposes.
  182. if (counter < 1000) {
  183. var newdata = getNewDataPoint();
  184. // updatePlot method each time have an updated data point.
  185. updatePlot(newdata);
  186. // update pointer div with latest data val.
  187. updatePricePointer(newdata[1]);
  188. // Stuff for mock streaming
  189. counter++;
  190. newdata = null;
  191. }
  192. // Remove the setInterval after 1000 iterations.
  193. else {
  194. clearInterval(Interval);
  195. }
  196. }
  197. ////////
  198. // Function updating plot when last data point is updated. If totally new
  199. // data is fed in, can recreate plot with call to $.jqplot();
  200. ////////
  201. function updatePlot(newdata) {
  202. // get references for convienence.
  203. var d = plot1.series[0].data;
  204. var dp = d[d.length-1];
  205. // Update the data on the series in the plot.
  206. // Becuase of some inconsistent reference handling in some browsers,
  207. // it is safest to set individual data elements by value.
  208. dp[0] = newdata[0]; // the datetme
  209. dp[1] = newdata[1]; // Close
  210. // Get handle on the canvas overlary for the current price line.
  211. var co = plot1.plugins.canvasOverlay;
  212. var current = co.get('current');
  213. // Update the y value of the current price line.
  214. // This does not redraw the lines, just updates the values.
  215. current.options.y = dp[1];
  216. // Check to see if we need to rescale the plot,
  217. // if the data is now above/below the axes.
  218. if (dp[1] > plot1.axes.y2axis.max) {
  219. plot1.replot({resetAxes:true});
  220. // Need to re-create the 'pricePointer' element as it will be destoyed
  221. // when the plot is recreated.
  222. $('<div id="pricePointer" style="position:absolute;"></div>').appendTo(plot1.target);
  223. // Set the "pricePointer" to the current price.
  224. updatePricePointer(dp[1]);
  225. }
  226. else if (dp[1] < plot1.axes.y2axis.min) {
  227. plot1.replot({resetAxes:true});
  228. // Need to re-create the 'pricePointer' element as it will be destoyed
  229. // when the plot is recreated.
  230. $('<div id="pricePointer" style="position:absolute;"></div>').appendTo(plot1.target);
  231. // Set the "pricePointer" to the current price.
  232. updatePricePointer(dp[1]);
  233. }
  234. // If no axes rescaling needed, just redraw the series on the canvas
  235. // and redraw the current price line at new positions.
  236. else {
  237. plot1.drawSeries({}, 0);
  238. co.draw(plot1);
  239. }
  240. d = dp = newdata = limit = stop = co = null;
  241. }
  242. ////////
  243. // function to update the text and reposition the price pointer
  244. ////////
  245. function updatePricePointer(val) {
  246. // get handle on the price pointer element.
  247. var div = $('#pricePointer');
  248. // Get a handle on the plot axes and one of the y2axis ticks.
  249. var axis = plot1.axes.y2axis;
  250. var tick = axis._ticks[0];
  251. // use the tick's formatter to format the value string.
  252. var str = tick.formatter(tick.formatString, val);
  253. // set the text of the pointer.
  254. div.html('&laquo;&nbsp;'+str);
  255. // Create css positioning strings for the pointer.
  256. var left = plot1._gridPadding.left + plot1.grid._width + 3 + 'px';
  257. // use the axis positioning functions to set the right y position.
  258. var top = axis.u2p(val) - div.outerHeight()/2 + 'px';
  259. // set the div in the right spot
  260. div.css({left:left, top:top});
  261. div = axis = tick = str = left = top = null;
  262. }
  263. function stopit() {
  264. clearInterval(Interval);
  265. }
  266. </script>
  267. <style type="text/css">
  268. #pricePointer {
  269. background-color: rgba(40,40,40,0.7);
  270. color: rgb(240,240,240);
  271. padding: 2px 2px 2px 0px;
  272. }
  273. </style>
  274. </head>
  275. <body>
  276. <div id="chart1" style="margin:20px;height:400px; width:800px;"></div>
  277. <div id="pricePointer" style="display:none;"></div>
  278. <button onclick="startit(data)">Start</button>
  279. <button onclick="stopit()">Stop</button>
  280. </div>
  281. </body>
  282. </html>

comments powered by Disqus