Highcharts使用json添加点到线图 [英] Highcharts add point to line chart with json
问题描述
我需要从我从mysql数据库获得的数据添加点。在这个时候,我通过json获取数据,但我不知道为什么在输出数组中,数据有如下双引号():
[{name:'Chip 3',data:[[moment('2015-05-14 13:26:21','HH:mm:ss')。valueOf(),29] ,[moment('2015-05-14 13
:26:51','HH:mm:ss')。valueOf(),29],[moment('2015-05-14 13:27: 21','HH:mm:ss')。valueOf(),29],[moment('2015-05-14
13:27:51','HH:mm:ss')。valueOf ),29],[moment('2015-05-14 13:28:21','HH:mm:ss')。valueOf(),29],[moment('2015-05-14
'),'HH:mm:ss')。valueOf b $ b 因此,Highcharts无法访问数据并在图表上显示数据。现在我需要从数组中删除双引号或者做其他事情来使Highcharts可以对数据进行regconize。
这是我用于获取data.php文件的代码更新系列数据。
<?php
header(Content-type:text / json );
include_once'include / connection.php';
$ db = new DB_Class();
$ query =从datatable中选择不同的idchip;
$ result = mysql_query($ query);
$ rows = array();
$ count = 0;
$ getall = array();
while($ row = mysql_fetch_array($ result)){
$ table = array();
$ query2 =select datetime,temperature from datatable where idchip =。$ row ['idchip']。'group by datetime';
$ dataresult = mysql_query($ query2);
while($ datarow = mysql_fetch_array($ dataresult))
{
$ data ='';
$ datatimes。= $ datarow ['0']。',';
$ b $ data。=[moment('。$ datarow ['0']。'','HH:mm:ss')。valueOf(),。(整数)$数据行[ '1'] ],。
$ stringdata。= $ data;
}
$ newstring = $ stringdata;
$ stringdata ='';
$ stringtime ='';
$ namedata ={name:'Chip。$ row [idchip]。',data:[$ newstring]};
$ getall [] = $ namedata;
}
echo json_encode($ getall);
?>
这是我用来获取ajax返回数据的代码。
function getData(){
jQuery.ajax({$ b $ url:'data.php',
类型:'GET' ,
dataType:'json',
mimeType:'multipart / form-data',
contentType:false,
cache:false,
processData:false,
成功:function(data,jqXHR){
if(data ==null){
} else {
$ .getJSON(data.php ,函数(json){
图表= new Highcharts.Chart({
图表:{
renderTo:'container',
defaultSeriesType:'spline',
事件:{
//载入:请求数据
}
},
系列:json
});
}) ;
}
},
错误:function(textStatus){
console.log(error。达姆。 );
//console.log(error);
}
});
}
解决方案我认为是这样的
$ query =select datatable distinct idchip;
$ result = mysql_query($ query);
$ rows = array();
$ count = 0;
$ getall = array();
while($ row = mysql_fetch_array($ result)){
$ table = array();
$ stringdata = array();
$ query2 =select datetime,temperature from datatable where idchip =。$ row ['idchip']。'group by datetime';
$ dataresult = mysql_query($ query2);
$ stringdata = array();
while($ datarow = mysql_fetch_array($ dataresult))
{
$ stringdata [] =moment('。$ datarow '','HH:mm:ss')。valueOf(),。(integer)$ datarow ['1'];
}
$ namedata ['名称' ] =Chip。$ row [idchip];
$ namedata ['data'] = $ stringdata;
$ getall [] = $ namedata;
}
echo json_encode($ getall);
I need to add point from data which I get from mysql database. At this time, i get data by json but i don't know why in the output array, data have double quote(" ") like this:
["{name:'Chip 3',data:[[moment('2015-05-14 13:26:21','HH:mm:ss').valueOf(),29],[moment('2015-05-14 13
:26:51','HH:mm:ss').valueOf(),29],[moment('2015-05-14 13:27:21','HH:mm:ss').valueOf(),29],[moment('2015-05-14
13:27:51','HH:mm:ss').valueOf(),29],[moment('2015-05-14 13:28:21','HH:mm:ss').valueOf(),29],[moment('2015-05-14
14:42:54','HH:mm:ss').valueOf(),32],]}"]
So Highcharts can not access to the data and show data on chart. Now I need to remove double quote from array or do something else to make Highcharts can regconize data.
This is my code in data.php file which i use to get and update series data.
<?php
header("Content-type: text/json");
include_once 'include/connection.php';
$db = new DB_Class();
$query = "select distinct idchip from datatable ";
$result = mysql_query( $query );
$rows = array();
$count = 0;
$getall = array();
while( $row = mysql_fetch_array( $result ) ) {
$table = array();
$query2 = "select datetime,temperature from datatable where idchip=".$row['idchip'].' group by datetime ';
$dataresult = mysql_query($query2);
while($datarow = mysql_fetch_array($dataresult))
{
$data = '';
$datatimes .= $datarow['0'].',';
$data .= "[moment('".$datarow['0']."','HH:mm:ss').valueOf(),".(integer)$datarow['1']."],";
$stringdata .= $data;
}
$newstring = $stringdata ;
$stringdata = '';
$stringtime = '';
$namedata = "{name:'Chip ".$row["idchip"]."',data:[$newstring]}";
$getall[] = $namedata;
}
echo json_encode($getall);
?>
This is code which i use to get ajax return data.
function getData() {
jQuery.ajax({
url: 'data.php',
type: 'GET',
dataType: 'json',
mimeType: 'multipart/form-data',
contentType: false,
cache: false,
processData: false,
success: function( data, jqXHR ) {
if( data == "null" ) {
} else {
$.getJSON("data.php", function(json) {
chart = new Highcharts.Chart({
chart: {
renderTo: 'container',
defaultSeriesType: 'spline',
events: {
// load: requestData
}
},
series: json
});
});
}
},
error: function( textStatus ) {
console.log(" error. damm. ");
//console.log(error);
}
});
}
解决方案 I think something like this
$query = "select distinct idchip from datatable ";
$result = mysql_query( $query );
$rows = array();
$count = 0;
$getall = array();
while( $row = mysql_fetch_array( $result ) ) {
$table = array();
$stringdata = array();
$query2 = "select datetime,temperature from datatable where idchip=".$row['idchip'].' group by datetime ';
$dataresult = mysql_query($query2);
$stringdata = array();
while($datarow = mysql_fetch_array($dataresult))
{
$stringdata[] = "moment('".$datarow['0']."','HH:mm:ss').valueOf(),".(integer)$datarow['1'];
}
$namedata['name'] = "Chip ".$row["idchip"];
$namedata['data'] = $stringdata;
$getall[] = $namedata;
}
echo json_encode($getall);
这篇关于Highcharts使用json添加点到线图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!