GiorgioAresu

Google Gauge plugin for ThingSpeak

Feb 8th, 2015
349
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.29 KB | None | 0 0
  1. <script type='text/javascript' src='https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js'></script>
  2. <script type='text/javascript' src='https://www.google.com/jsapi'></script>
  3. <script type='text/javascript'>
  4.  
  5. // set your channel id here
  6. var channel_id = 21261;
  7. // set your channel's read api key here if necessary
  8. var api_key = 'API_HERE';
  9. // minimum value for the gauge
  10. var min_gauge_value = -10;
  11. // maximum value for the gauge
  12. var max_gauge_value = 40;
  13. // red range starting point for the gauge
  14. var red_from = 30;
  15. // red range ending point for the gauge
  16. var red_to = 40;
  17. // blue range starting point for the gauge
  18. var blue_from = -10;
  19. // blue range ending point for the gauge
  20. var blue_to = 0;
  21. // major ticks
  22. var major_ticks = ['-10','0','10','20','30','40'];
  23. // name of the gauge
  24. var gauge_name = '°C';
  25.  
  26. // global variables
  27. var chart, charts, data;
  28.  
  29. // load the google gauge visualization
  30. google.load('visualization', '1', {packages:['gauge']});
  31. google.setOnLoadCallback(initChart);
  32.  
  33. // display the data
  34. function displayData(point) {
  35. data.setValue(0, 0, gauge_name);
  36. data.setValue(0, 1, point);
  37. chart.draw(data, options);
  38. }
  39.  
  40. // load the data
  41. function loadData() {
  42. // variable for the data point
  43. var p;
  44.  
  45. // get the data from thingspeak
  46. $.getJSON('https://api.thingspeak.com/channels/' + channel_id + '/feed/last.json?api_key=' + api_key, function(data) {
  47.  
  48. // get the data point
  49. p = data.field2;
  50.  
  51. // if there is a data point display it
  52. if (p) {
  53. p = Math.round(p * 10)/10;
  54. displayData(p);
  55. }
  56. });
  57. }
  58.  
  59. // initialize the chart
  60. function initChart() {
  61.  
  62. data = new google.visualization.DataTable();
  63. data.addColumn('string', 'Label');
  64. data.addColumn('number', 'Value');
  65. data.addRows(1);
  66.  
  67. chart = new google.visualization.Gauge(document.getElementById('gauge_div'));
  68. options = {width: 200, height: 200, min: min_gauge_value, max: max_gauge_value, redFrom: red_from, redTo: red_to, yellowFrom: blue_from, yellowTo: blue_to, yellowColor: "#7f7fff", minorTicks: 5, majorTicks: major_ticks};
  69.  
  70. loadData();
  71.  
  72. // load new data every 15 seconds
  73. setInterval('loadData()', 15000);
  74. }
  75.  
  76. </script>
Add Comment
Please, Sign In to add comment