Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <script type='text/javascript' src='https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js'></script>
- <script type='text/javascript' src='https://www.google.com/jsapi'></script>
- <script type='text/javascript'>
- // set your channel id here
- var channel_id = 21261;
- // set your channel's read api key here if necessary
- var api_key = 'API_HERE';
- // minimum value for the gauge
- var min_gauge_value = -10;
- // maximum value for the gauge
- var max_gauge_value = 40;
- // red range starting point for the gauge
- var red_from = 30;
- // red range ending point for the gauge
- var red_to = 40;
- // blue range starting point for the gauge
- var blue_from = -10;
- // blue range ending point for the gauge
- var blue_to = 0;
- // major ticks
- var major_ticks = ['-10','0','10','20','30','40'];
- // name of the gauge
- var gauge_name = '°C';
- // global variables
- var chart, charts, data;
- // load the google gauge visualization
- google.load('visualization', '1', {packages:['gauge']});
- google.setOnLoadCallback(initChart);
- // display the data
- function displayData(point) {
- data.setValue(0, 0, gauge_name);
- data.setValue(0, 1, point);
- chart.draw(data, options);
- }
- // load the data
- function loadData() {
- // variable for the data point
- var p;
- // get the data from thingspeak
- $.getJSON('https://api.thingspeak.com/channels/' + channel_id + '/feed/last.json?api_key=' + api_key, function(data) {
- // get the data point
- p = data.field2;
- // if there is a data point display it
- if (p) {
- p = Math.round(p * 10)/10;
- displayData(p);
- }
- });
- }
- // initialize the chart
- function initChart() {
- data = new google.visualization.DataTable();
- data.addColumn('string', 'Label');
- data.addColumn('number', 'Value');
- data.addRows(1);
- chart = new google.visualization.Gauge(document.getElementById('gauge_div'));
- 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};
- loadData();
- // load new data every 15 seconds
- setInterval('loadData()', 15000);
- }
- </script>
Add Comment
Please, Sign In to add comment