2011-11-28 22 views
0

私は初心者のPHP/js/mysqlプログラマです。Highcharts AJAX/JSON経由の円グラフ:スライスエラーとJson形式が正しくない?

jqueryを使用してハイチャートで円グラフを作成しようとしています。ここで、データはphpのecho json_encode(mysqlの選択クエリを含む)のajaxを介して動的に検出されます。

2つの問題:どこでもフレア:

1)円グラフは、これらの末尾の "0%スライス" を持っています。これらがどこから来ているのか、それが何を意味するのか、どのように修正するのか分かりません。

2)Jsonは私には初めてです。 jsonデータフィードが通過しているように見えますが(Firebugはそれを見ています)、フォーマットは次のようになります。私はそれを名前とパーセント数だけに煮詰めようとしています。この[[Pages]、45.0]のように、どうしたらよいかわかりません。これはjson/phpで行われるのですか、それともSQLクエリ自体で行うべきですか?

[{"contenttype":"BLOGPOST","count(*)":"2076"},{"contenttype":"COMMENT","count(*)":"2054"},{"contenttype":"MAIL","count(*)":"29448"},{"contenttype":"PAGE","count(*)":"33819"}] 

感謝すべてのヘルプは

highchartsのJSファイルはここにある:

//Define the chart variable globally, 
var chart; 

//Request data from the server, add it to the graph and set a timeout to request again 

function requestData() { 
$.ajax({ 
    url: 'hc1.php', 
    success: function(point) { 
     var series = chart.series[0], 
      shift = series.data.length > 20; // shift if the series is longer than 20 

     // add the point 
     chart.series[0].addPoint(point, true, shift); 

     // call it again after one second 
     setTimeout(requestData, 1000);  
    }, 
    cache: false 
}); 
} 

$(document).ready(function(){ 

//Create the test chart 
chart = new Highcharts.Chart({ 
    chart: { 
      renderTo: 'mycontainer2', 
      plotBackgroundColor: null, 
      plotBorderWidth: null, 
      plotShadow: false, 
      events: {load: requestData} 
    }, 
    title: {text: 'Content Types in Wiki'}, 

tooltip: {formatter: function() {return '<b>'+ this.point.name +'</b>: '+ this.percentage +' %';} 
    }, 

plotOptions: { 
    pie: { 
     allowPointSelect: true, 
     cursor: 'pointer', 
     dataLabels: { 
      enabled: true, 
      //color: Highcharts.theme.textColor || '#000000', 
      //connectorColor: Highcharts.theme.textColor || '#000000', 
      formatter: function() { 
       return '<b>'+ this.point.name +'</b>: '+ this.percentage +' %'; 
      } 
     } 
    } 
    }, 



     series: [{ 
     type: 'pie', 
     name: 'Content', 
     data: [] 
    }] 
});   

PHPファイルはここにある:

<?php 
// Set the JSON header 
header("Content-type: text/json"); 

// Connect to db 
include('dbconnect.php'); 

// Count version 1 of content types of interest 
$query = ("select contenttype, count(*) 
    from CONTENT 
    where version='1' and contenttype='page' or contenttype='comment' or contenttype='blogpost' or contenttype='mail' or contenttype='drafts' 
    group by CONTENT.contenttype;"); 

// execute query 
$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error()); 

// create a php array and echo it as json 
//$row = mysql_fetch_assoc($result); 
//echo json_encode($row); 


$results = array(); while ($row = mysql_fetch_assoc($result)) { $results[] = $row; } 
echo json_encode($results); 
?> 
+0

私はあなたのコードで少し混乱しています。毎秒プロットに新しいデータポイントを追加しています。これは円グラフで行うのは奇妙なことでしょうか?毎秒円グラフを再描画することを意味しますか(再び奇妙に思われます)?それとも、一度そのチャートを描きたいのですか? – Mark

+0

こんにちはマーク。一度それを描くだけです。 – gregm

+0

ここでは、縦棒グラフの例が役立ちます。 http://sgeek.org/create-chart-using-mysql-highcharts/ –

答えて

0

まず問題、どのようにあなたはあなたのデータを入手しますか?ハイチャートが受け入れるフォーマット(配列の配列[[名前、パーセント]、[ネクストネーム、パーセント]など)に変換しますか?私はあなたのPHPでこれを処理します:

<snip> 

// execute query 
$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error()); 

$total = 0; 
$results = array(); 

while ($row = mysql_fetch_assoc($result)) { 
    $results[$row["contenttype"] = $row["count()"]; 
    $total += $row["count()"]; 
} 

$forHigh = array(); 
foreach ($results as $k => $v) { 
    $forHigh[]=array($k,($v/$total) * 100); // get percent and push onto array 
} 

echo json_encode($forHigh); // this should be an array of array 

は今私達のJSON返される構造がHighChartsの準備ができていることを、私たちは、プロットを作成するために、私たちのJSON呼び出しの後に一度プロットの作成を呼び出す必要があります。私は$ .ajax呼び出しの成功コールバックでそれを行います。

$.ajax({ 
    url: 'hc1.php', 
    success: function(jsonData) { 
     chart = new Highcharts.Chart({ 
     <snip> 
       series: [{ 
        type: 'pie', 
        name: 'Content', 
        data: jsonData 
       }] 
     <snip> 
    }, 
    cache: false 
}); 
+0

ありがとうマーク。もう1つの質問。 "php -f hc1.php"を使ってPHPスクリプトをテストすると、空のセット[]が返されます。 json文字列は生成されません。何か不足していますか?ありがとう! – gregm

+0

うーん、私はそこにエラーがあるかもしれない、私は定期的にPHPコードを書いていない(と私は本当に私が書いたものをテストすることができませんでした)。私が入力したときに$ row ["count()"]が気になってしまいました(オリジナルのJSONをキー名として残すことに驚きました)。私はあなたのSQLクエリを "select contenttype、count(*)num"に変更し、 "count()"をnumに置き換えます。それが動作しない場合は、ステップスルーしてデバッグする必要があります。 – Mark

関連する問題