私は初心者の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);
?>
私はあなたのコードで少し混乱しています。毎秒プロットに新しいデータポイントを追加しています。これは円グラフで行うのは奇妙なことでしょうか?毎秒円グラフを再描画することを意味しますか(再び奇妙に思われます)?それとも、一度そのチャートを描きたいのですか? – Mark
こんにちはマーク。一度それを描くだけです。 – gregm
ここでは、縦棒グラフの例が役立ちます。 http://sgeek.org/create-chart-using-mysql-highcharts/ –