Hi Sara and Rui
I’m working my through ESP Build Web server, and in the 3.3 Charts chapter I noticed the X axis is date and time but in UTC timezone. So I searched the Highcharts website and found this global modifier worked well to give me the correct timezone on my charts:
Highcharts.setOptions({ time: { timezoneOffset: -570 //my timezone is +09:30, however -570 is used } });
Just thought you’d like to know in case you wanted to include in next update of the eBook.
I’m enjoying the book, thanks for your efforts.
Sean
Hi Sean.
Thanks for sharing that.
I’ll definitely include that information in the next eBook update.
Regards,
Sara
Hi Sara,
In Build_Web_Server 3.3 i find the proposed correction for a time zone not update, i tried it but it did not worked.
i joined the code for the scrpt-sensors.js file i used. I checked and my computer time is correct, and the browser i used is Google Chrome.
Any idea ?
Regards,
Roger
Start your code here
// Get current sensor readings when the page loads
window.addEventListener(‘load’, getReadings);
// Create Temperature Chart
Highcharts.setOptions({
time: {
timezoneOffset: -240 //Add your time zone offset here in minutes
}
});
var chartT = new Highcharts.Chart({
chart:{
renderTo:’chart-temperature’
},
//time: {
//timezone: ‘America/New_York’
//},
series: [
{
name: ‘BMP280’
}
],
title: {
text: undefined
},
plotOptions: {
line: {
animation: false,
dataLabels: {
enabled:true
}
},
xAxis: {
type: ‘datetime’,
dateTimeLabelFormats: { second: ‘%H:%M:%S’ }
},
yAxis: {
title: {
text:’Temperature Celsius Degrees’
}
},
credits: {
enabled:false
}
}});
// Create Pressure Chart
var chartP = new Highcharts.Chart({
chart:{
renderTo:’chart-pressure’
},
//time: {
// timezone: ‘America/New_York’
//},
series: [{
name: ‘BMP280’
}],
title: {
text: undefined
},
plotOptions: {
line: {
animation: false,
dataLabels: {
enabled:true
}
},
series: {
color:’#50b8b4′
}
},
xAxis: {
type: ‘datetime’,
dateTimeLabelFormats: { second: ‘%H:%M:%S’ }
},
yAxis: {
title: {
text:’Pressure hPa ‘
}
},
credits: {
enabled:false
}
});
//Plot temperature in the temperature chart
function plotTemperature(value) {
var x = (new Date()).getTime()
var y = Number(value);
if(chartT.series[0].data.length > 40) {
chartT.series[0].addPoint([x, y], true, true, true);
} else {
chartT.series[0].addPoint([x, y], true, false, true);
}
}
//Plot pressure in the pressure chart
function plotPressure(value) {
var x = (new Date()).getTime()
var y = Number(value);
if(chartP.series[0].data.length > 40) {
chartP.series[0].addPoint([x, y], true, true, true);
} else {
chartP.series[0].addPoint([x, y], true, false, true);
}
}
// Function to get current readings on the webpage when it loads for the first time
function getReadings(){
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
var myObj = JSON.parse(this.responseText);
console.log(myObj);
var temp = myObj.temperature;
var press = myObj.pressure;
plotTemperature(temp);
plotPressure(press);
}
};
xhr.open(“GET”, “/readings”, true);
xhr.send();
}
if (!!window.EventSource) {
var source = new EventSource(‘/events’);
source.addEventListener(‘open’, function(e) {
console.log(“Events Connected”);
}, false);
source.addEventListener(‘error’, function(e) {
if (e.target.readyState != EventSource.OPEN) {
console.log(“Events Disconnected”);
}
}, false);
source.addEventListener(‘message’, function(e) {
console.log(“message”, e.data);
}, false);
source.addEventListener(‘new_readings’, function(e) {
console.log(“new_readings”, e.data);
var myObj = JSON.parse(e.data);
console.log(myObj);
plotTemperature(myObj.temperature);
plotPressure(myObj.pressure);
}, false);
}
Hi.
Yes.
I recently found out that by default, the charts will show the time in UTC. If you want it to display in your timezone, you must set the useUTC parameter (which is a time parameter) as false:
time:{
useUTC: false
},
So, add that when creating the chart as follows:
var chart = new Highcharts.Chart({
time:{
useUTC: false
},
(...)
To learn more about this property, check this link on the documentation:
I hope this helps.
Regards,
Sara
Hi Sara,
I adjusted my Highcharts for time zone successfully but daylight saving offset is not accounted for. I’m UTC +10hrs but currently +11hrs with daylight saving active. I can’t see anything on the Highcharts webpage referencing a daylight saving offset so unless I’ve missed it I guess I’d need another way to do that if I wanted to, perhaps using ntp somehow? Any solutions/advice?
Hi Sara,
I didn’t appreciate that your previous answer addressed daylight saving time as well as timezone. I just tried it and it worked so apologies for unnecessary question.