2014-10-17 104 views
1

美好的一天。幫助在JS Highcharts中以JSON格式發送數據。有兩個字段被選中:溫度和溼度。對他們應該基於時間表。有一個領域的一切工作,因爲只有兩個全部死亡。如何在HighCharts中返回json數據?

PHP:

<?php 
/* SQL */ 
    /* Connect */ 
     try 
     { 
      $connection = new PDO("mysql:host=localhost;dbname=my","root",""); 
     } 
     catch (PDOException $e) 
     { 
      echo 'Connection error: ' . $e->getMessage(); 
     } 
    /* /Connect */ 
    /* Query */ 
     $query = $connection->prepare("SELECT temperature, humidity FROM weather WHERE date >= CURDATE()"); 
     $query->execute(); 
     $result = $query->fetchAll(PDO::FETCH_ASSOC); 

     echo json_encode($result, JSON_NUMERIC_CHECK); 

    /* /Query */ 
/* /SQL */ 
?> 

JS:

$(function(){ 
var options = { 
    chart: { 
     renderTo: 'mychart', 
     type: 'spline' 
    }, 
    title: { 
     text: 'Temperature' 
    }, 
    xAxis: { 
     categories: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24] 
    }, 
    yAxis: { 
     title: { 
      text: 'Values' 
     } 
    }, 
    series: [{}] 
}; 

$.getJSON('../ajax/get_weather_day.php', function(data){ 
    options.series[0].name = "Temperature"; 
    options.series[0].data = data; 
    options.series[1].name = "Humidity"; 
    options.series[1].data = data; 
    var chart = new Highcharts.Chart(options); 
}); 
}); 

回答

2

你分配你的mysql表中的這兩個系列的這兩個領域的結果。

options.series[0].data = data; 
options.series[1].data = data; 

你應該拆分數據和分配權值向右系列(或者你可以做兩個Ajax調用兩全seperately - 檢查什麼,你的情況是更便宜)。

順便說一下,數據現在包含mysql結果行(值對),但highcharts將數值作爲數字序列。

在這裏看到:

http://api.highcharts.com/highcharts#series