Advertisement
Guest User

222222

a guest
Feb 6th, 2016
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.94 KB | None | 0 0
  1.  
  2. // user's timezone offset
  3. var myOffset = new Date().getTimezoneOffset();
  4.  
  5. // converts date format from JSON
  6. function getChartDate(d) {
  7. // get the data using javascript's date object (year, month, day, hour, minute, second)
  8. // months in javascript start at 0, so remember to subtract 1 when specifying the month
  9. // offset in minutes is converted to milliseconds and subtracted so that chart's x-axis is correct
  10. return Date.UTC(d.substring(0,4), d.substring(5,7)-1, d.substring(8,10), d.substring(11,13), d.substring(14,16), d.substring(17,19)) - (myOffset * 60000);
  11. }
  12.  
  13. $(document).on('page:load ready', function() {
  14. // blank array for holding chart data
  15. var chartData = [];
  16. // variable for the local date in milliseconds
  17. var localDate;
  18. // variable for the last date added to the chart
  19. var last_date;
  20.  
  21. // get the data with a webservice call
  22. $.getJSON('http://api.thingspeak.com/channels/35510/field/1.json?callback=?&offset=0&start=2015-05-12%0000:00:10&results=8000&average=1440', function(data) {
  23.  
  24. // if no access
  25. if (data == '-1') { $('#container').append('This channel is not public. To embed charts, the channel must be public or a read key must be specified.'); }
  26.  
  27. // iterate through each feed
  28. $.each(data.feeds, function() {
  29. var p = new Highcharts.Point();
  30. // set the proper values
  31. var v = this.field1;
  32. p.x = getChartDate(this.created_at);
  33. p.y = parseFloat(v);
  34. // add location if possible
  35. if (this.location) { p.name = this.location; }
  36. // if a numerical value exists add it
  37. if (!isNaN(parseInt(v))) { chartData.push(p); }
  38. });
  39.  
  40. // specify the chart options
  41. var chartOptions = {
  42. chart: {
  43. renderTo: 'container',
  44. defaultSeriesType: 'line',
  45. backgroundColor:'rgba(0, 0, 0, 0.4)',
  46. events: {
  47.  
  48. load: function() {
  49. //if dynamic and no "timeslice" options are set
  50. // GAK 02/16/2013 Let's try to add the last "average" slice if params[:average]
  51.  
  52. var url = 'http://api.thingspeak.com/channels/35510/feed/last.json?callback=?&offset=0&location=true&results=30' ;
  53. if ("".length > 0) {
  54. url = 'http://api.thingspeak.com/channels/35510/feed/last_average.json?callback=?&offset=0&location=true&average=&results=30' ;
  55. } else if ("".length > 0) {
  56. url = 'http://api.thingspeak.com/channels/35510/feed/last_median.json?callback=?&offset=0&location=true&median=&results=30' ;
  57. } else if ("".length > 0) {
  58. url = 'http://api.thingspeak.com/channels/35510/feed/last_sum.json?callback=?&offset=0&location=true&sum=&results=30' ;
  59. }
  60.  
  61. if ('true' === 'true' && (''.length < 1)) {
  62. // push data every 15 seconds
  63. setInterval(function() {
  64. // get the data with a webservice call if we're just getting the last channel
  65. $.getJSON(url, function(data) {
  66. // if data exists
  67. if (data && data.field1) {
  68.  
  69. var p = new Highcharts.Point();
  70. // set the proper values
  71. var v = data.field1;
  72.  
  73. p.x = getChartDate(data.created_at);
  74. p.y = parseFloat(v);
  75. // add location if possible
  76. if (data.location) { p.name = data.location; }
  77. // get the last date if possible
  78. if (dynamicChart.series[0].data.length > 0) {
  79. last_date = dynamicChart.series[0].data[dynamicChart.series[0].data.length-1].x;
  80. }
  81. var shift = false ; //default for shift
  82.  
  83. //if push is requested in parameters
  84. // then if results is and data.length is < results, shift = false
  85. var results = 60;
  86.  
  87. if ( results && dynamicChart.series[0].data.length+1 >= results ) {
  88. shift = true ;
  89. }
  90. // if a numerical value exists and it is a new date, add it
  91. if (!isNaN(parseInt(v)) && (p.x != last_date)) {
  92. dynamicChart.series[0].addPoint(p, true, shift);
  93. }
  94. else {
  95. dynamicChart.series[0].data[dynamicChart.series[0].data.length-1].update(p);
  96. }
  97. }
  98. });
  99.  
  100. }, 15000);
  101. }
  102. }
  103. }
  104. },
  105. title: {
  106. style: {
  107. color: '#CC0000',
  108. fontWeight: 'bold'
  109. }
  110. },
  111. plotOptions: {
  112. line: {
  113. color: '#E6E600'
  114. },
  115. bar: {
  116. color: '#d62020'
  117. },
  118. column: {
  119. color: '#d62020'
  120. },
  121. spline: {
  122. color: '#d62020'
  123. },
  124. series: {
  125. marker: {
  126. radius: 4
  127. },
  128. animation: true,
  129. step: false,
  130. borderWidth: 0,
  131. turboThreshold: 0
  132. }
  133. },
  134. tooltip: {
  135. borderWidth: 2,
  136. borderRadius: 20,
  137. // reformat the tooltips so that local times are displayed
  138. formatter: function() {
  139. var d = new Date(this.x + (myOffset*60000));
  140. var n = (this.point.name === undefined) ? '' : '<br/>' + this.point.name;
  141. return this.series.name + ':<b>' + this.y + '</b>' + n + '<br/>' + d.toDateString() + '<br/>' + d.toTimeString().replace(/\(.*\)/, "");
  142. }
  143. },
  144. xAxis: {
  145. type: 'datetime',
  146. title: {
  147. text: 'test'
  148. }
  149. },
  150. yAxis: {
  151.  
  152. title: {
  153. style: {
  154. letterSpacing: 1,
  155. color: 'red',
  156. fontSize: '16px'
  157.  
  158. }
  159. },
  160. min: 15 ,
  161. max: 30
  162. },
  163. exporting: {
  164. enabled: false
  165.  
  166. },
  167. legend: {
  168. enabled: false
  169. },
  170. series: [
  171. { name: 'Temperatura : ' }
  172. ],
  173. credits: {
  174. text: '',
  175. href: 'https://thingspeak.com/',
  176. style: { color: '#D62020' }
  177. }
  178. };
  179.  
  180. // add the data to the chart
  181. chartOptions.series[0].data = chartData;
  182.  
  183. // set chart labels here so that decoding occurs properly
  184. chartOptions.title.text = decodeURIComponent('');
  185. chartOptions.xAxis.title.text = '';
  186. chartOptions.yAxis.title.text = 'Temperatura °C';
  187.  
  188. // draw the chart
  189. var dynamicChart = new Highcharts.Chart(chartOptions);
  190.  
  191. // end getJSON success
  192. })
  193. // chained to getjson, on error
  194. .error(function() {
  195. $('#container').html('Invalid Channel.');
  196. });
  197.  
  198. }); // end document.ready
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement