Advertisement
GiorgioAresu

Temperature comparison plugin for ThingSpeak

Feb 8th, 2015
495
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.10 KB | None | 0 0
  1. <script type="text/javascript">
  2. // variables for the first series
  3. var series_1_channel_id = Channel1id;
  4. var series_1_field_number = 1;
  5. var series_1_read_api_key = 'Channel1API';
  6. var series_1_days = 1;
  7. var series_1_color = '#00aaff';
  8.  
  9. // variables for the second series
  10. var series_2_channel_id = Channel2id;
  11. var series_2_field_number = 2;
  12. var series_2_read_api_key = 'Channel2API';
  13. var series_2_days = 1;
  14. var series_2_color = '#d62020';
  15.  
  16. // chart title
  17. var chart_title = 'Last 24h';
  18. // y axis title
  19. var y_axis_title = 'Temperature (°C)';
  20.  
  21. // user's timezone offset
  22. var my_offset = new Date().getTimezoneOffset();
  23. // chart variable
  24. var my_chart;
  25.  
  26. // when the document is ready
  27. $(document).on('ready', function() {
  28. // add a blank chart
  29. addChart();
  30. // add the first series
  31. addSeries(series_1_channel_id, series_1_field_number, series_1_read_api_key, series_1_days, series_1_color);
  32. // add the second series
  33. addSeries(series_2_channel_id, series_2_field_number, series_2_read_api_key, series_2_days, series_2_color);
  34. });
  35.  
  36. // add the base chart
  37. function addChart() {
  38. // variable for the local date in milliseconds
  39. var localDate;
  40.  
  41. // specify the chart options
  42. var chartOptions = {
  43. chart: {
  44. renderTo: 'chart-container',
  45. defaultSeriesType: 'line',
  46. backgroundColor: '#ffffff',
  47. events: { }
  48. },
  49. title: { text: chart_title },
  50. plotOptions: {
  51. series: {
  52. marker: { radius: 3 },
  53. animation: true,
  54. step: false,
  55. borderWidth: 0,
  56. turboThreshold: 0
  57. }
  58. },
  59. tooltip: {
  60. // reformat the tooltips so that local times are displayed
  61. formatter: function() {
  62. var d = new Date(this.x + (my_offset*60000));
  63. var n = (this.point.name === undefined) ? '' : '<br>' + this.point.name;
  64. return this.series.name + ':<b>' + this.y + '</b>' + n + '<br>' + d.toDateString() + '<br>' + d.toTimeString().replace(/\(.*\)/, "");
  65. }
  66. },
  67. xAxis: {
  68. type: 'datetime',
  69. title: { text: 'Date' }
  70. },
  71. yAxis: { title: { text: y_axis_title } },
  72. exporting: { enabled: false },
  73. legend: { enabled: true },
  74. credits: {
  75. text: 'ThingSpeak.com',
  76. href: 'https://thingspeak.com/',
  77. style: { color: '#D62020' }
  78. }
  79. };
  80.  
  81. // draw the chart
  82. my_chart = new Highcharts.Chart(chartOptions);
  83. }
  84.  
  85. // add a series to the chart
  86. function addSeries(channel_id, field_number, api_key, days, color) {
  87. var field_name = 'field' + field_number;
  88.  
  89. // get the data with a webservice call
  90. $.getJSON('https://api.thingspeak.com/channels/' + channel_id + '/fields/' + field_number + '.json?offset=0&round=2&days=' + days + '&api_key=' + api_key, function(data) {
  91.  
  92. // blank array for holding chart data
  93. var chart_data = [];
  94.  
  95. // iterate through each feed
  96. $.each(data.feeds, function() {
  97. var point = new Highcharts.Point();
  98. // set the proper values
  99. var value = this[field_name];
  100. point.x = getChartDate(this.created_at);
  101. point.y = parseFloat(value);
  102. // add location if possible
  103. if (this.location) { point.name = this.location; }
  104. // if a numerical value exists add it
  105. if (!isNaN(parseInt(value))) { chart_data.push(point); }
  106. });
  107.  
  108. // add the chart data
  109. my_chart.addSeries({ data: chart_data, name: data.channel[field_name], color: color });
  110. });
  111. }
  112.  
  113. // converts date format from JSON
  114. function getChartDate(d) {
  115. // get the data using javascript's date object (year, month, day, hour, minute, second)
  116. // months in javascript start at 0, so remember to subtract 1 when specifying the month
  117. // offset in minutes is converted to milliseconds and subtracted so that chart's x-axis is correct
  118. 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)) - (my_offset * 60000);
  119. }
  120.  
  121. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement