摘要:php畫圓餅圖(二)
GoogleChart.php
<?
class GoogleChart{
var $width,$height;
var $chart_type;
var $api_url;
var $parameters;
function GoogleChart($w,$h,$type,$url="http://chart.apis.google.com/chart"){
$this->parameters = array();
$this->width = $w;
$this->height = $h;
$this->chart_type = $type;
$this->api_url = $url;
$this->parameters['chs'] = $w."x".$h;
$this->parameters['cht'] = $type;
}
function draw(){
$url = $this->api_url;
$params = array();
foreach($this->parameters as $key => $value){
$params[] = "$key=$value";
}
$url .= "?".implode("&",$params);
// header("Location:http://tw.yahoo.com.tw/");
echo "<script>";
echo " location.href='$url'; ";
echo "</script>";
// header("Location: $url");
echo $url;
}
} //class GoogleChart
class GooglePieChart extends GoogleChart{
function GooglePieChart($w,$h,$type="p"){
parent::GoogleChart($w,$h,$type);
}
function setData($data,$labels=null,$legends=null){
$this->parameters['chd'] = "t:".implode(",",$data);
if($labels) $this->setLabels($labels);
if($legends) $this->setLegends($legends);
}
function setLabels($labels){
$this->parameters['chl'] = implode("|",$labels);
}
function setLegends($legends){
$this->parameters['chdl'] = implode("|",$legends);
}
} //class GooglePieChart extends: GoogleChart
class GooglePieChart3D extends GooglePieChart{
function GooglePieChart3D($w,$h){
parent::GooglePieChart($w,$h,"p3");
}
}
test.php
?>
<?php
include("GoogleChart.php");
$chart = new GooglePieChart(400,200);
$data = array(3,4,5,6,7);
$labels = array("台北","基隆","台中","台南","高雄");
$legends = array("台北","基隆","台中","台南","高雄");
$chart->setData($data,$labels,$legends);
$chart->draw();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>無標題文件</title>
</head>
<body>
</body>
</html>