【问题标题】:php to highchart, how to send the right array?php到highchart,如何发送正确的数组?
【发布时间】:2018-04-13 08:00:54
【问题描述】:

我使用以下代码编写了此代码:

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,我一定会听

【问题讨论】:

  • 您的getPoints() 做什么/返回?因为我认为那是返回[{"time":"2018-04-13T07:11:45.208943754Z","value":48}]]
  • 是的。我只想保留最后一个值 48。
  • 你不能只访问 JSON 中的value 吗?我对 php 不是很好,但在我看来,应该有一种方法可以通过名称调用值。 ($points.value 可能吗?那种方式?)
  • 关闭@Codeer,其实是echo $test[0]-&gt;value;(JSON中有顶级数组)
  • 所以解决方案是$y = json_decode($points); $ret = array($x, $y[0]-&gt;value);

标签: php json highcharts influxdb


【解决方案1】:

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

但仍有 2 小时的延迟,但它现在显示在 highcharts 中。

<?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);

?>

【讨论】:

    猜你喜欢
    • 2016-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-17
    • 2014-01-08
    • 2019-10-24
    • 1970-01-01
    相关资源
    最近更新 更多