2012-12-01 64 views
2

我是Highcharts的新手,並試圖學習如何在PHP和MySQL中使用它。我想從數組中的一些示例數據中顯示一個簡單的柱形圖。但是,我遇到了一些問題實現這一點。這些列未顯示在圖表中,我正在獲取圖例中空白的額外標籤。如果有人能夠幫助我清理這些代碼,使其工作並提供示例代碼和評論以幫助我們更好地理解它,那將會有很大的幫助。我相信其他幾個人也在尋找一個類似的例子。我瀏覽過這個網站,並通過HighCharts論壇和示例,無法找到我正在尋找的內容,這可以更好地幫助我學習如何使用PHP和MySQL創建動態列類型圖表。如果你可以提前幫助很多謝謝。HIghCharts PHP MySQL

這是我的代碼,我知道它有幾個問題,我試圖保持這個很簡單的例子,很容易讓新手和學生理解。非常感謝您的幫助:

我的樣本數據包含該人的姓氏和銷售額。它是在一個TSV格式和陣列看起來像這樣:李四3567右5476 - 約翰遜4351布朗2945

data.php

<?php 
    $con = mysql_connect("localhost","root",""); 
    if (!$con) { 
     die('Could not connect: ' . mysql_error()); 
    } 
    mysql_select_db("test", $con); 
    $result = mysql_query("SELECT * FROM user"); 
    while($row = mysql_fetch_array($result)) { 
     echo $row['lastname'] . "\t" . $row['sales']. "\n"; 
    } 
    mysql_close($con); 
    ?> 

的index.php

<!DOCTYPE html><html><head> 
</head><body> 
<div id="container" style="height: 400px; width: 900px"></div> 

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script> 
<script src="http://www.highcharts.com/js/highcharts.src.js"></script> 
<script> 
jQuery(function($) { 
    var options = { 
     chart: {renderTo: 'container', defaultSeriesType:'column', height: 400}, 
     title: {text: 'Sales Performance'}, 
     xAxis: {title: {text: 'Sales Peoples'}, type: 'linear'}, 
     yAxis: {title: {text: 'Dollars'}, plotLines: [{ value: 0, width: 1, color: '#808080'}]}, 
     tooltip: { 
      formatter: function() { 
       return '<b>'+ this.series.name +'</b><br/>'+'('+this.x +' : '+ this.y +')'; 
      } 
     }, 
     legend: {layout: 'vertical', align: 'left', verticalAlign: 'top', x: 40, y: 100, borderWidth: 0, width: 300 }, 
     series: [] 
    }; 

    jQuery.get('data.php', null, function(tsv) { 
     var data = {}; 
     tsv = tsv.split(/\n/g); // split into rows 
     for (var row=0, rows=tsv.length; row<rows; row++) { 
      var line = tsv[row].split(/\t/g), // split into columns 
       series_name = line[0], 
       x = Number(line[1]), 
       y = Number(line[2]); 
      if (!data[series_name]) data[series_name] = []; 
      data[series_name].push([x,y]); 
      //alert("The data is: "+series_name) 
     } 
     for (var series_name in data) { 
      options.series.push({ 
       name: series_name, 
       data: data[series_name] 
      }); 
     } 
     new Highcharts.Chart(options); 
    }); 

}); 
</script> 
</body></html> 
+0

教程可以幫你http://sgeek.org/create-chart-using-mysql-highcharts/ –

回答

1

數據[SERIES_NAME ] .push([X,Y]);

data[series_name].push(x); 

你沒有yline[2]

+0

嗨Kazatca,感謝您的回覆。這有助於清理一些,但仍然無法使用。我嘗試了你的建議,並且我也修改了formater的這一行。 'return''+ this.series.name +'
'+'('+ this.x +')';' –

+0

在實現了Kazatca的建議之後,我在series_name和x上添加了一個提示。從'tsv = tsv.split'後面我可以看到,在名稱中有一個空白值,併爲x的最後一個值觸發一個NaN。從我讀到的那將導致圖表不顯示在現在的問題HighCharts。如果有人可以幫我用一些代碼來檢查空白值,並刪除它們,如果它們或NaN存在,可能會清理它並使其顯示圖表。我仍然是一個學習新手,我不知道如何以編程方式做到這一點。非常感謝提前! –

+0

tsv.split(/ \ n /)總是返回[「some data」,「some data」,「」],因爲tsv以「\ n」結尾。在服務器端試試implode(「\ n」,$ collect)。 – kazatca