2012-11-25 39 views
0

我正在嘗試將由php文件創建的json數據解析到另一個腳本並將其值顯示給一個高圖。嘗試將JSON數據傳遞給Highcharts而沒有成功

我data.php它創建JSON數據是這樣的:

<?php 
header("Content-type: application/json"); 

$dbhost = "localhost"; 
$dbuser = "db"; 
$dbpass = "xxxxx"; 
$dbname = "db"; 
$db = mysql_connect($dbhost, $dbuser, $dbpass); 
mysql_select_db($dbname,$db); 
$query = "SELECT * FROM monitor_total"; 

$result = mysql_query($query,$db); 

while($row = mysql_fetch_array($result)) { 
    $date = $row["date"]; 
    $count = $row["count"]; 
    $array[] = array("date" =>$date,"count"=>$count); 
} 
echo json_encode($array); 
?> 

的data.php輸出爲:

[{"date":"2012-11-23","count":"582311"},{"date":"2012-11-24","count":"582322"},{"date":"2012-11-22","count":"582121"},{"date":"2012-11-21","count":"581321"},{"date":"2012-11-19","count":"572821"},{"date":"2012-11-20","count":"581321"},{"date":"2012-11-18","count":"582421"},{"date":"2012-11-17","count":"579321"},{"date":"2012-11-16","count":"581321"},{"date":"2012-11-25","count":"558178"}] 

<script>

var monitor_graph; // globally available 
    monitor_graph = new Highcharts.Chart({ 
    chart: { 
     renderTo: 'graph', 
     type: 'spline', 
     events: { 
      load: requestData 
     } 
    }, 
    title: { 
     text: 'Registered and total players by date' 
    }, 
    xAxis: { 
     categories: [] 
    }, 
    yAxis: { 
     title: { 
      text: 'Players' 
     } 
    }, 
    series: [{ 
     name: 'Total Players', 
     data: [] 
    }] 
    }); 
function requestData() { 
$.ajax({ 
     url: 'data.php', 
    success: function(data) { 
     $.each(data, function(i,item){ 

       monitor_graph.series[0].setData(??????????); 

     }); 
    setTimeout(requestData, 1000); 
    }, 
}); 
} 

如何創建一個名爲總球員的系列賽,其中x軸的'item.date'和y軸的'item.count'?

請幫幫我!

UPDATE: 我加入這裏面的每個:

monitor_graph.xAxis[0].setCategories([item.date]); 
monitor_graph.series[0].setData([parseFloat(item.count)]); 

,我現在拿到1分正是因爲我需要它,但有以下錯誤:

Unexpected value NaN parsing y attribute. 
+0

相關@ http://stackoverflow.com/questions/12485560/highcharts-returning-error-14日期需求是**劃時代時間**爲'int'(不'string')的值也需要是一個數字('float'或'int')在你的情況下,一切都是字符串 –

+0

看@我的更新請 – Alexandros

+0

我沒有得到錯誤14! – Alexandros

回答

2

你需要修改你的成功回調,這樣的事情

var categories=[]; 
var seriesData=[]; 
$.each(data,function(){ 
    categories.push(this.date); 
    seriesData.push(parseInt(this.count)); 
}); 
chart.xAxis[0].setCategories(categories); 
chart.series[0].setData(seriesData); 

Series.setData()方法預計INT /浮動,而不是一個字符串數組,並應與整個數據集,而不是每個點被調用。你在$.each裏面這樣做應該在這個循環之外完成。同去同類別

Demo @ jsFiddle

+0

多謝隊友..我嘗試了我在這裏找到的任何示例中的每一個命令,但都在循環內部:/它現在運行良好,但我仍然得到這個:意外的值NaN解析y屬性。 – Alexandros

+1

@Alex是的,我在控制檯也看到了,但不要擔心它只是一個警告,不是一個錯誤,所以不應該破壞任何東西 –

+0

和1最後一個問題:我怎麼能扭轉圖中點的順序。現在它首先給我最新的一個! – Alexandros

1

您收到的數據是以具有對象的數組的形式。你需要遍歷數組,你已經在你的$ .each函數中做了這個。這會讓你以i變量的形式訪問每個對象。這個變量可以通過點符號訪問你需要的屬性。在你的情況下,這將是i.date和i.count。

從我可以告訴,setData方法接受您可以設置的值的數組,所以替換你的????與以下內容:

monitor_graph.series[0].setData([i.date, i.count]); 

希望有所幫助。

+0

我在螢火蟲中得到這個錯誤:TypeError:a是undechart在highchart.js行183 – Alexandros