php到highchart,如何发送正确的数组?

时间:2018-04-13 08:00:55

标签: php json highcharts influxdb

我用以下代码制作了这段代码:

https://github.com/influxdata/influxdb-php

https://www.highcharts.com/docs/working-with-data/live-data

<?php 
include('/opt/lampp/htdocs/example/vendor/autoload.php');

$host = '127.0.0.1';
$port = 8086;
$dbname = 'aTimeSeries';

/*
$client = new \InfluxDB\Client($host, $port);

$database = $client->selectDB('aTimeSeries');
*/

//the exact same thing
$database = \InfluxDB\Client::fromDSN(sprintf('influxdb://user:pass@%s:%s/%s', $host, $port, $dbname));

//query of the last value
$result = $database->query('select * from valeurs group by * order by desc limit 1');

//recup le point
$points = $result->getPoints();

// Set the JSON header
header("Content-type: application/json");

// The x value is the current JavaScript time, which is the Unix time multiplied by 1000.
$x = time() * 1000;
// The y value is a random number
//$y = rand(0,100);
$y = $points;

// Create a PHP array and echo it as JSON
$ret = array($x, $y);
echo json_encode($ret);

?>

输出

[1523603506000,[{"time":"2018-04-13T07:11:45.208943754Z","value":48}]]

但我想这样:

[1523603506000,48]

或者这个:

[2018-04-13T07:11:45.208943754Z,48]

如果我只尝试输出$points,我只得到数组的最后一部分,但它不是我想要的。

PS:如果你有一个更好的解决方案来做同样的事情,也许使用nodeJS,我一定会听#

1 个答案:

答案 0 :(得分:0)

感谢您的帮助, 我有另一个问题,但我解决了。

但仍然有2个小时的延迟,但它现在显示在高图中。

<?php 
include('/opt/lampp/htdocs/example/vendor/autoload.php');

$host = '127.0.0.1';
$port = 8086;
$dbname = 'aTimeSeries';

//directly get the database object
$database = \InfluxDB\Client::fromDSN(sprintf('influxdb://user:pass@%s:%s/%s', $host, $port, $dbname));

//query of the last value
$result = $database->query('select * from valeurs group by * order by desc limit 1');

//get the point
$points = $result->getPoints();

//make the array right
$points = json_encode($points);
$points = json_decode($points);

//take only the seconds of the time
$points[0]->time = substr($points[0]->time, 0,20);

// Set the JSON header
header("Content-type: text/json");

// The x value is the current JavaScript time, which is the Unix time multiplied by 1000 and take the time
$x = strtotime($points[0]->time) * 1000;

// The y value take the value which is a random value
$y = $points[0]->value;

// Create a PHP array and echo it as JSON
$ret = array($x, $y);
echo json_encode($ret);

?>