trouble plotting multiple series of data on highstock

こ雲淡風輕ζ 提交于 2020-01-05 05:35:11

问题


Have a personal temperature monitor project that logs all temperatures in separate rooms at home into a mysql database and I came across highstock/charts the other day and have been playing with it, but I cant seem to get it to work with multiple series of data.

how the data is logged into the table;

datetime <-- (eg; 2013-07-18 15:52:26) time will be different for each sensor for each location

location <-- 6 of (lounge,kitchen,dinning,outside,master,spare)

temperature <-- (eg. 12.34)

What a record looks like in mysql; 2013-07-18 15:52:26 / master / 12.34

I have managed to get it all working for set of data, but I'm not sure what I need to do and how the best way is to format the json and mysql query so that highcharts can read the json/mysq data and plot ALL 6 locations temperature data on the one graph.

Complete code; The main highstock chart file. http://pastebin.com/XWkThfc8 and this is the file that generates the JSON from the mysql database. http://pastebin.com/RXBFr24P

This is what it currently looks like for one set of data using the above queries.... [1374593356000,17.31],[1374593427000,17.25],[1374593497000,17.31],[1374593567000,17.31],[1374593638000,17.31],[1374593708000,17.25],[1374593778000,17.25],[1374593849000,17.25],[1374593919000,17.25],[1374593989000,17.25],[1374594060000,17.25],[1374594130000,17.25]....etc

So my questions are;

What is the best way to change this so it plots all the data, at the moment it doesn't seem to work;

mysql_select_db("mqtt", $con);

$return_arr = array();

$fetch = mysql_query("SELECT timeof, message FROM temperatures WHERE DATE(timeof) BETWEEN DATE_SUB(CURDATE(), INTERVAL 7 DAY) AND CURDATE() AND locationmap = 'master'");
$i=0;
while ($row = mysql_fetch_array($fetch, MYSQL_ASSOC)) {
$rows[$i]=array(strtotime($row[timeof])*1000 ,(float)$row[message]);
$i++;
}

echo json_encode($rows);

I assume for multiple sets of data that it needs to be in this format.

[{name:'kitchen',dat‌​a:[[date,temp],[date,temp],[date,temp]]},{name:'lounge',dat‌​a:[[date,temp],[date,temp],[date,temp]]}]

Not sure how to change my query so that it pulls the data out for ALL locations and then encodes it correctly.

UPDATE-1

Latest code,

$return_arr = array();

$fetch = mysql_query("SELECT timeof, locationmap AS location, message AS temp FROM temperatures WHERE DATE(timeof) BETWEEN DATE_SUB(CURDATE(), INTERVAL 1 DAY) AND CURDATE() ORDER BY location, timeof ASC");
$i=0;
while ($row = mysql_fetch_array($fetch, MYSQL_ASSOC)) {
$rows[$i]=array(strtotime($row[timeof])*1000 ,(float)$row[temp],$row[location]);
$i++;
}

echo json_encode($rows);   

However it doesn't return the correct format as mentioned above.

It comes out like this. (Obviously there is 1000's of entires.)

[1375272426000,22.63,"cupboard"],[1375272496000,22.69,"cupboard"],[1375272566000,22.75,"cupboard"],[1375272637000,22.75,"cupboard"],[1375272707000,22.69,"cupboard"],[1375272777000,22.63,"cupboard"],[1375106429000,17.69,"kitchen"],[1375106500000,17.69,"kitchen"],[1375106570000,17.63,"kitchen"],[1375106640000,17.63,"kitchen"],[1375106711000,17.63,"kitchen"],[1375106781000,17.63,"kitchen"],[1375106851000,17.63,"kitchen"],[1375106921000,17.56,"kitchen"],[1375106992000,17.56,"kitchen"],[1375107062000,17.56,"kitchen"],[1375107132000,17.56,"kitchen"],[1375107203000,17.56,"kitchen"],[1375107273000,17.5,"kitchen"],[1375107343000,17.5,"kitchen"],[1375107413000,17.5,"kitchen"]

I think my query is correct now!?? just need some help with the json output!

UPDATE-2 Thx to the highcharts forums, this is the format I need the JSON in, just need some help with the php/mysql side...

[{
  name: 'kitchen',
  data: [
    [time, value],
    [time, value]
  ]
}, {
  name: 'attic',
  data: [
    [time, value],
    [time, value]
  ]
}]

UPDATE-3 Got it working with multiple series data, didn't relise i had to call my json php script multiple times, have put the two files on GIST for anyone having similar problems.

https://gist.github.com/matbor/8854385 https://gist.github.com/matbor/8853902


回答1:


I think the best way is to follow the "compare demo" and make your php code generate only one array, depending on an url parameter which specifies the room :

json.php?room=attic
json.php?room=cupboard

An you only output the data of the room

Then, it's highstocks that calls the separate php files with :

names = ['attic', 'cupboard', '...'],

$.each(names, function(i, name) {

    $.getJSON('jsonp.php?room='+ name.toLowerCase() +'-c.json&callback=?',  function(data) {
...

But to be honest I'm still looking for a way to avoid multiple http calls/sql queries :-) especially for data like [date1,value1,value1,value3],[date2,value1,value1,value3]...




回答2:


Yes it should be in format which you include, morever date should be timestamp (time in milisceonds) and temp needs number value.




回答3:


I have been using this project for about 6 months now and thought I would share a little tip that others might find handy.
Everything was running really well, I have 5 raspberry pi's logging temperature data into a MYSQL database on a CentOS server, but all of a sudden it broke. The page would not display anything at all, but in the Firefox developer tools console under the Network tab I could see that 3 of the 5 database queries were resulting in a 500 internal server error.
This seemed strange as I had not modified anything, and some troubleshooting eventually led me to the httpd error_log. There was a php error indicating the maximum memory allocation had been exhausted.

PHP Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 757 bytes) in /var/www/html/temperatures/json_hs_temps.php

I added the following line of code to the json_hs_temps.php file to increase the memory from the default of 128Mb to 1Gb.

ini_set('memory_limit', '1024M');

This immediately fixed the problem and the graphs load fine again.

Furthermore, the httpd error_log was also blowing out to large sizes, (gigabytes) due to the following timezone error:

PHP Warning: strtotime(): It is not safe to rely on the system's timezone settings. You are required to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected the timezone 'UTC' for now, but please set date.timezone to select your timezone. in /var/www/html/temperatures/json_hs_temps.php on line 27, referer: http://192.168.178.49/temperatures/index_hs_temps_multi.php

The solution to this was to add the following line of code to the json_hs_temps.php file to set a time zone.

date_default_timezone_set("Australia/Hobart");

I assume this could happen to others using this code so I hope it helps somebody else out.



来源:https://stackoverflow.com/questions/17716547/trouble-plotting-multiple-series-of-data-on-highstock

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!