plugin - chart with multiple series | ThingSpeak Plugins | Forum

Avatar

Please consider registering
Guest

Search

— Forum Scope —






— Match —





— Forum Options —





Minimum search word length is 3 characters - maximum search word length is 84 characters

Register Lost password?
sp_Feed sp_TopicIcon
plugin - chart with multiple series
Avatar
Naesstrom
New Member
Forum Posts: 1
sp_UserOfflineSmall Offline
1
February 24, 2016 - 6:35 am
sp_Permalink sp_Print

Heya

So I'm trying to get a chart working with 7 different series so that we can get a overview of the temp at work. I've managed to start getting the values in there (https://thingspeak.com/apps/plugins/49459) but I need to figure out how to get a median for the temperatures like on the individual charts (https://thingspeak.com/channels/84330)

According to what I read about getting the json data it should be enough to add the "median=60" as in the example below but for some reason it doesn't work, any ideas?
// get the data with a webservice call
$.getJSON('https://api.thingspeak.com/channels/' + channel_id + '/fields/' + field_number + '.json?median=60&offset=0&round=2&results=' + results + '&api_key=' + api_key, function(data) {

Avatar
Adarsh_Murthy

Forum Posts: 59
sp_UserOfflineSmall Offline
2
February 24, 2016 - 4:20 pm
sp_Permalink sp_Print

Hi,

You are correct about the median parameter, adding that should give you the median for each series. I just verified that adding the median parameter to the getJSON call as you have shown did work. Here is a copy of my Javascript section:

// variables for the first series
var series_1_channel_id = 84330;
var series_1_field_number = 3;
var series_1_read_api_key = '';
var series_1_results = 100;
var series_1_color = '#d62020';

// variables for the second series
var series_2_channel_id = 84330;
var series_2_field_number = 4;
var series_2_read_api_key = '';
var series_2_results = 100;
var series_2_color = '#00aaff';

// chart title
var chart_title = 'Data3&4';
// y axis title
var y_axis_title = 'Values';

// user's timezone offset
var my_offset = new Date().getTimezoneOffset();
// chart variable
var my_chart;

// when the document is ready
$(document).on('ready', function() {
// add a blank chart
addChart();
// add the first series
addSeries(series_1_channel_id, series_1_field_number, series_1_read_api_key, series_1_results, series_1_color);
// add the second series
addSeries(series_2_channel_id, series_2_field_number, series_2_read_api_key, series_2_results, series_2_color);
});

// add the base chart
function addChart() {
// variable for the local date in milliseconds
var localDate;

// specify the chart options
var chartOptions = {
chart: {
renderTo: 'chart-container',
defaultSeriesType: 'line',
backgroundColor: '#ffffff',
events: { }
},
title: { text: chart_title },
plotOptions: {
series: {
marker: { radius: 3 },
animation: true,
step: false,
borderWidth: 0,
turboThreshold: 0
}
},
tooltip: {
// reformat the tooltips so that local times are displayed
formatter: function() {
var d = new Date(this.x + (my_offset*60000));
var n = (this.point.name === undefined) ? '' : '
' + this.point.name;
return this.series.name + ':' + this.y + '' + n + '
' + d.toDateString() + '
' + d.toTimeString().replace(/\(.*\)/, "");
}
},
xAxis: {
type: 'datetime',
title: { text: 'Date' }
},
yAxis: { title: { text: y_axis_title } },
exporting: { enabled: false },
legend: { enabled: false },
credits: {
text: 'ThingSpeak.com',
href: 'https://thingspeak.com/',
style: { color: '#D62020' }
}
};

// draw the chart
my_chart = new Highcharts.Chart(chartOptions);
}

// add a series to the chart
function addSeries(channel_id, field_number, api_key, results, color) {
var field_name = 'field' + field_number;

// get the data with a webservice call
$.getJSON('https://api.thingspeak.com/channels/' + channel_id + '/fields/' + field_number + '.json?median=60&offset=0&round=2&results=' + results + '&api_key=' + api_key, function(data) {

// blank array for holding chart data
var chart_data = [];

// iterate through each feed
$.each(data.feeds, function() {
var point = new Highcharts.Point();
// set the proper values
var value = this[field_name];
point.x = getChartDate(this.created_at);
point.y = parseFloat(value);
// add location if possible
if (this.location) { point.name = this.location; }
// if a numerical value exists add it
if (!isNaN(parseInt(value))) { chart_data.push(point); }
});

// add the chart data
my_chart.addSeries({ data: chart_data, name: data.channel[field_name], color: color });
});
}

// 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)) - (my_offset * 60000);
}

Thanks,
Adarsh

Forum Timezone: America/New_York

Most Users Ever Online: 114

Currently Online:
19 Guest(s)

Currently Browsing this Page:
1 Guest(s)

Top Posters:

rw950431: 252

Vinod: 164

cstapels: 96

piajola: 75

vespapierre: 63

Adarsh_Murthy: 59

Newest Members:

winnieeq69

AutoCheernpen

Bostermet

engladNed

iivenul

olewaeayafaku

Forum Stats:

Groups: 4

Forums: 17

Topics: 1313

Posts: 4565

 

Member Stats:

Guest Posters: 1

Members: 5703

Moderators: 0

Admins: 2

Administrators: Hans, lee