使用 PHP/MySQL 的 highcharts 的 Json 结构

Posted

技术标签:

【中文标题】使用 PHP/MySQL 的 highcharts 的 Json 结构【英文标题】:Json structure for highcharts with PHP/MySQL 【发布时间】:2018-01-03 03:31:00 【问题描述】:

我想检索用于运行 Highcharts 图形 (demonstration site here) 的相同 JSON 结构:

?'([
[Date.UTC(2013,5,2),0.7695],
[Date.UTC(2013,5,3),0.7648],
[Date.UTC(2013,5,4),0.7645],
[Date.UTC(2013,5,5),0.7638],
[Date.UTC(2013,5,6),0.7549],
[Date.UTC(2013,5,7),0.7562],
[Date.UTC(2013,5,9),0.7574],
[Date.UTC(2013,5,10),0.7543],
[Date.UTC(2013,5,11),0.7510],
[Date.UTC(2013,5,12),0.7498],
[Date.UTC(2013,5,13),0.7477],
[Date.UTC(2013,5,14),0.7492],
[Date.UTC(2013,5,16),0.7487],
[Date.UTC(2013,5,17),0.7480],
[Date.UTC(2013,5,18),0.7466],
[Date.UTC(2013,5,19),0.7521],
[Date.UTC(2013,5,20),0.7564],
[Date.UTC(2013,5,21),0.7621],
[Date.UTC(2013,5,23),0.7630],
[Date.UTC(2013,5,24),0.7623],
[Date.UTC(2013,5,25),0.7644],
[Date.UTC(2013,5,26),0.7685],
[Date.UTC(2013,5,27),0.7671],
[Date.UTC(2013,5,28),0.7687],

我在 data.php 文件中的数据库中检索我的 SQL 数据:

$sth = mysqli_query($link, "SELECT date_insert, valeur FROM mesure_bac where type_capteur='TEMPERATURE'and id_bac='".$id_bac."'");

$rows = array();

while($r = mysqli_fetch_assoc($sth)) 

  $rows['data'][] = $r;




$result = array();
array_push($result,$rows);


print json_encode($result, JSON_NUMERIC_CHECK);

只是,执行结果 JSON 给了我以下结构:

["data":["date_insert":"2017-07-06 15:05:17","valeur":25,"date_insert":"2017-07-06 15:07:16","valeur":25,"date_insert":"2017-07-06 15:16:50","valeur":25,"date_insert":"2017-07-06 15:27:00","valeur":25,"date_insert":"2017-07-06 15:37:09","valeur":24,"date_insert":"2017-07-06 15:47:18","valeur":25,"date_insert":"2017-07-06 15:57:27","valeur":25,"date_insert":"2017-07-06 16:07:37","valeur":25,"date_insert":"2017-07-06 16:17:46","valeur":25,"date_insert":"2017-07-06 16:27:55","valeur":25,"date_insert":"2017-07-06 16:38:04","valeur":25,"date_insert":"2017-07-06 16:48:14","valeur":25,"date_insert":"2017-07-06 16:58:23","valeur":25,"date_insert":"2017-07-06 17:08:32","valeur":25,"date_insert":"2017-07-06 17:18:41","valeur":25,"date_insert":"2017-07-06 17:28:51","valeur":25,"date_insert":"2017-07-06 17:39:00","valeur":25,"date_insert":"2017-07-06 17:49:09","valeur":25,"date_insert":"2017-07-06 17:59:18","valeur":25,"date_insert":"2017-07-06 18:09:28","valeur":26,"date_insert":"2017-07-06 18:19:37","valeur":26,"date_insert":"2017-07-06 18:29:46","valeur":25,"date_insert":"2017-07-06 18:39:55","valeur":26,"date_insert":"2017-07-06 18:50:05","valeur":26,"date_insert":"2017-07-06 19:00:14","valeur":26,"date_insert":"2017-07-06 19:10:23","valeur":25,"date_insert":"2017-07-06 19:20:32","valeur":25,"date_insert":"2017-07-06 19:30:42","valeur":25,

执行与演示相同的 JSON 数据结构的 php 转换是什么?

谢谢。

【问题讨论】:

【参考方案1】:

您必须自己创建结构。这应该可以完成工作

while($r = mysqli_fetch_assoc($sth)) 
    $rows['data'][] = array(
        "Date.UTC(" . str_replace('-', ',', $r['date_insert']) . ")",
        $r['valeur']
    );

【讨论】:

你知道我上次回答的问题吗?【参考方案2】:

您也可以在 javascript 端执行简单的parsing。看看下面的例子。

示例:http://jsfiddle.net/o226yvvv/

【讨论】:

以上是关于使用 PHP/MySQL 的 highcharts 的 Json 结构的主要内容,如果未能解决你的问题,请参考以下文章

Highcharts 定制 PHP/MySQL

PHP mySQL Highcharts Json 数组

来自 php mysql 的 Highcharts 钻取 json

Highcharts+PHP+Mysql生成饼状统计图

Highcharts 丢失值区域图;Highcharts 反转x轴与y轴;Highcharts 曲线区域图;Highcharts 区间区域图;Highcharts 使用区间和线的区域图

highcharts的简单使用