2017-06-19 135 views
3

我正在嘗試顯示動態高圖表,我們可以按年,月和日來更改圖表。Highcharts顯示數據但無法顯示圖表

我已經成功,我可以根據每次更改X軸,通過Json數組的數據也成功傳遞,但圖表不出來。

的Javascript:

$.ajax({ 
     url: 'content/getChart.php', 
     type: "POST", 
     async: true, 
     dataType: 'json', 
     data: { 
      type: type, 
      part: part, 
      year: year_val, 
      month: month_val, 
      day: day_val, 
      days_in_month: days_in_month 
     }, 
     success: function (data){ 
      var data_array = []; 
      $.each(data, function(i, val) { 
       //data_array.push({x: val.time * 1000, y: val.value}); //first result 
       data_array.push(val.time * 1000, val.value); 
      }); 
      purchase_chart.series[0].setData(data_array); 
      console.log(data_array); 
     } 
    }); 

PHP:

$purchase_array = array(); 
$array_test = array(); 
$resQry = ""; 
for($i = $start; $i <= $end; $i++){ 
    $resQry = ""; 
    $resQry .= "SELECT COUNT(*) AS `total_sum`"; 
    $resQry .= "FROM `purchase`"; 
    if($type == "daily" && $year != 0 && $month != 0 && $day != 0){ 
     $resQry .= "WHERE YEAR(`purchase_date`) = '$year' AND MONTH(`purchase_date`) = '$month' AND DAY(`purchase_date`) = '$i' "; 
     $timemk = mktime(0, 0, 0, $month, $i, $year); 
    } 
    else if($type == "monthly" && $year != 0 && $month != 0){ 
     $resQry .= "WHERE YEAR(`purchase_date`) = '$year' AND MONTH(`purchase_date`) = '$i' "; 
     $timemk = mktime(0, 0, 0, $i, 0, $year); 
    } 
    else if($type == "yearly" && $year != 0){ 
     $resQry .= "WHERE YEAR(`purchase_date`) = '$i' "; 
     $timemk = mktime(0, 0, 0, 0, 0, $i); 
    } 
    $resQry .= "ORDER BY `purchase_date`"; 
    $result = $conn->query($resQry); 
    $row = $result->fetch_array(); 
    $purchase_array[$timemk]['time'][] = $timemk; 
    $purchase_array[$timemk]['value'][] = (int)$row['total_sum']; 
} 
$conn->close(); 
echo(json_encode($purchase_array)); 

我已經嘗試過很多辦法可用,但只能使我只要這一點。

輸出: The one that i've circled has value of 12, if i hover on the X axis i can see the value shown 12 with it's correct date

JSON輸出:

[{"x": 1385766000000,"y": [0]}, 
{"x": 1417302000000,"y": [0]}, 
{"x": 1448838000000,"y": [0]}, 
{"x": 1480460400000,"y": [12]}, 
{"x": 1511996400000,"y": [0]}, 
{"x": 1543532400000,"y": [0]}, 
{"x": 1575068400000,"y": [0}] 

結果還是一樣的舊代碼。但是,如果我手動放置json數組,它會給我更高的錯誤#15。

以及如果我把陣列這樣的(手動),圖表顯示

[[1385766000000,0], 
[1417302000000,0], 
[1448838000000,0], 
[1480460400000,12], 
[1511996400000,0], 
[1543532400000,0], 
[1575068400000,0]] 
+0

什麼是被帶到了錯誤?你有沒有試圖通過DOM檢查它(它可能是一個Z索引問題)? – FieryCat

+0

我會建議打開控制檯。在大多數情況下,Highchart會通過問題描述鏈接將錯誤推入其中,並解決問題。 – FieryCat

回答

1

嘗試改變.setData(data_array);到下一行:

purchase_chart.series[0].setData([data_array], true); 

它會自動觸發圖表的重繪行動。

而且,接下來的選項可能會有所幫助:

// Front-End: change datetime to milliseconds 
data_array.push([val.time * 1000, val.value]); 

// Back-End [1]: submit time; remove convertation to string 
$timemk = mktime(...); 

// Back-End [2]: order dates; Highchart will stack when data is not ordered 
$resQry .= "ORDER BY `purchase_date`"; 

// Back-End [3]: remove array bind for values 
$purchase_array[$timemk] = array(
    'time' => $timemk, 
    'value' => (int) $row['total_sum'] 
); 
+0

我試過這個,仍然不能正常工作 – ghost123

+0

@ ghost123,你能分享你的服務器響應嗎? – FieryCat

+0

我已經根據您的更新更新了我的帖子 – ghost123