Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // user's timezone offset
- var myOffset = new Date().getTimezoneOffset();
- // converts date format from JSON
- function getChartDate(d) {
- // get the data using javascript's date object (year, month, day, hour, minute, second)
- // months in javascript start at 0, so remember to subtract 1 when specifying the month
- // offset in minutes is converted to milliseconds and subtracted so that chart's x-axis is correct
- 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);
- }
- $(document).on('page:load ready', function() {
- // blank array for holding chart data
- var chartData = [];
- // variable for the local date in milliseconds
- var localDate;
- // variable for the last date added to the chart
- var last_date;
- // get the data with a webservice call
- $.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) {
- // if no access
- 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.'); }
- // iterate through each feed
- $.each(data.feeds, function() {
- var p = new Highcharts.Point();
- // set the proper values
- var v = this.field1;
- p.x = getChartDate(this.created_at);
- p.y = parseFloat(v);
- // add location if possible
- if (this.location) { p.name = this.location; }
- // if a numerical value exists add it
- if (!isNaN(parseInt(v))) { chartData.push(p); }
- });
- // specify the chart options
- var chartOptions = {
- chart: {
- renderTo: 'container',
- defaultSeriesType: 'line',
- backgroundColor:'rgba(0, 0, 0, 0.4)',
- events: {
- load: function() {
- //if dynamic and no "timeslice" options are set
- // GAK 02/16/2013 Let's try to add the last "average" slice if params[:average]
- var url = 'http://api.thingspeak.com/channels/35510/feed/last.json?callback=?&offset=0&location=true&results=30' ;
- if ("".length > 0) {
- url = 'http://api.thingspeak.com/channels/35510/feed/last_average.json?callback=?&offset=0&location=true&average=&results=30' ;
- } else if ("".length > 0) {
- url = 'http://api.thingspeak.com/channels/35510/feed/last_median.json?callback=?&offset=0&location=true&median=&results=30' ;
- } else if ("".length > 0) {
- url = 'http://api.thingspeak.com/channels/35510/feed/last_sum.json?callback=?&offset=0&location=true&sum=&results=30' ;
- }
- if ('true' === 'true' && (''.length < 1)) {
- // push data every 15 seconds
- setInterval(function() {
- // get the data with a webservice call if we're just getting the last channel
- $.getJSON(url, function(data) {
- // if data exists
- if (data && data.field1) {
- var p = new Highcharts.Point();
- // set the proper values
- var v = data.field1;
- p.x = getChartDate(data.created_at);
- p.y = parseFloat(v);
- // add location if possible
- if (data.location) { p.name = data.location; }
- // get the last date if possible
- if (dynamicChart.series[0].data.length > 0) {
- last_date = dynamicChart.series[0].data[dynamicChart.series[0].data.length-1].x;
- }
- var shift = false ; //default for shift
- //if push is requested in parameters
- // then if results is and data.length is < results, shift = false
- var results = 60;
- if ( results && dynamicChart.series[0].data.length+1 >= results ) {
- shift = true ;
- }
- // if a numerical value exists and it is a new date, add it
- if (!isNaN(parseInt(v)) && (p.x != last_date)) {
- dynamicChart.series[0].addPoint(p, true, shift);
- }
- else {
- dynamicChart.series[0].data[dynamicChart.series[0].data.length-1].update(p);
- }
- }
- });
- }, 15000);
- }
- }
- }
- },
- title: {
- style: {
- color: '#CC0000',
- fontWeight: 'bold'
- }
- },
- plotOptions: {
- line: {
- color: '#E6E600'
- },
- bar: {
- color: '#d62020'
- },
- column: {
- color: '#d62020'
- },
- spline: {
- color: '#d62020'
- },
- series: {
- marker: {
- radius: 4
- },
- animation: true,
- step: false,
- borderWidth: 0,
- turboThreshold: 0
- }
- },
- tooltip: {
- borderWidth: 2,
- borderRadius: 20,
- // reformat the tooltips so that local times are displayed
- formatter: function() {
- var d = new Date(this.x + (myOffset*60000));
- var n = (this.point.name === undefined) ? '' : '<br/>' + this.point.name;
- return this.series.name + ':<b>' + this.y + '</b>' + n + '<br/>' + d.toDateString() + '<br/>' + d.toTimeString().replace(/\(.*\)/, "");
- }
- },
- xAxis: {
- type: 'datetime',
- title: {
- text: 'test'
- }
- },
- yAxis: {
- title: {
- style: {
- letterSpacing: 1,
- color: 'red',
- fontSize: '16px'
- }
- },
- min: 15 ,
- max: 30
- },
- exporting: {
- enabled: false
- },
- legend: {
- enabled: false
- },
- series: [
- { name: 'Temperatura : ' }
- ],
- credits: {
- text: '',
- href: 'https://thingspeak.com/',
- style: { color: '#D62020' }
- }
- };
- // add the data to the chart
- chartOptions.series[0].data = chartData;
- // set chart labels here so that decoding occurs properly
- chartOptions.title.text = decodeURIComponent('');
- chartOptions.xAxis.title.text = '';
- chartOptions.yAxis.title.text = 'Temperatura °C';
- // draw the chart
- var dynamicChart = new Highcharts.Chart(chartOptions);
- // end getJSON success
- })
- // chained to getjson, on error
- .error(function() {
- $('#container').html('Invalid Channel.');
- });
- }); // end document.ready
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement