插件71:建立Google圖表

<?php // Plug-in 71: Create Google Chart
/*
 * 插件說明:
 * 插件返回一個GD圖像,它代表由輸入參數生成的一個圖表。如果創建失敗,則返回FALSE。
 * 它需要以下參數,這些參數(除$width、 $height和$data外)的默認值都爲NULL或空字符串
 * $title 圖標標題
 * $tcolor 標題顏色
 * $tsize 標題大小
 * $type 圖表類型,取以下值之一
 *     -line 折線圖
 *     -vbar 柱形圖
 *     -hbar 條形圖
 *     -gometer google指數圖
 *     -pie 餅圖(默認)
 *     -pie3d 三維餅圖
 *     -venn 維恩圖
 *     -radar 雷達圖
 * $bwidth 柱形圖寬度(只用於柱形圖和條形圖)
 * $labels 標籤,用“|”符號分隔
 * $legends 圖例用“|”符號分隔
 * $colors 顏色,有逗號分隔
 * $bgfill 背景顏色(6爲十六進制數)
 * $border 邊框寬度(以像素爲單位)
 * $bcolor 邊框顏色(6爲十六進制數)
 * $width 圖表寬度(以像素爲單位)
 * $height 圖標高度(一像素爲單位)
 * $data 圖標數據,以逗號分隔
 */
// This is an executable example with additional code supplied
// To obtain just the plug-ins please click on the Download link

$title   = 'My Favorite Types of Cheese';
$tcolor  = 'FF0000';
$tsize   = '20';
$type    = 'pie3d';
$width   = '570';
$height  = '230';
$bwidth  = NULL;
$labels  = 'Stilton|Brie|Swiss|Cheddar|Edam|Colby|Gorgonzola';
$legends = $labels;
$colors  = 'BD0000,DE6B00,284B89,008951,9D9D9D,A5AB4B,8C70A4,' .
   'FFD200';
$bgfill  = 'EEEEFF';
$border  = '2';
$bcolor  = '444444';
$data    = '14.9,18.7,7.1,47.3,6.0,3.1,2.1';
$result  = PIPHP_CreateGoogleChart($title, $tcolor, $tsize,
   $type, $bwidth, $labels, $legends, $colors, $bgfill,
   $border, $bcolor, $width, $height, $data);

header('Content-type: image/png');
imagepng($result);

function PIPHP_CreateGoogleChart($title, $tcolor, $tsize,
   $type, $bwidth, $labels, $legends, $colors, $bgfill,
   $border, $bcolor, $width, $height, $data)
{
   // Plug-in 71: Create Google Chart
   //
   // This plug-in returns a GD image created using the Google
   // Charts API. It requires the following arguments where
   // those prefaced by (*) can be set to NULL or '' to use
   // default values:
   //
   //    $title:   (*)The title text
   //    $tcolor:  (*)The title color (6 hex digits)
   //    $tsize:   (*)The title font size
   //    $type:    (*)The chart type, out of: line, vbar, hbar,
   //                 gometer, pie, pie3d, venn and radar
   //    $bwidth:  (*)The width of bars in pixels, if bar chart
   //    $labels:  (*)Data labels, separated by | symbols
   //    $legends: (*)Data legends, separated by | symbols
   //    $colors:  (*)Data colors, separated by | symbols
   //    $bgfill:  (*)Background fill color (6 hex digits)
   //    $border:  (*)Border width in pixels
   //    $bcolor:  (*)Border color (6 hex digits)
   //    $width:   The chart width in pixels
   //    $height:  The chart height in pixels
   //    $data:    The data set, separated by commas

   $types = array('line'    => 'lc',
                  'vbar'    => 'bvg',
                  'hbar'    => 'bhg',
                  'gometer' => 'gom',
                  'pie'     => 'p',
                  'pie3d'   => 'p3',
                  'venn'    => 'v',
                  'radar'   => 'r');

   if (!isset($types[$type])) $type = 'pie';

   $tail  = "chtt=" . urlencode($title);
   $tail .= "&cht=$types[$type]";
   $tail .= "&chs=$width" . "x" . "$height";
   $tail .= "&chbh=$bwidth";
   $tail .= "&chxt=x,y";
   $tail .= "&chd=t:$data";

   if ($tcolor)
      if ($tsize) $tail .= "&chts=$tcolor,$tsize";
   if ($labels)   $tail .= "&chl=$labels";
   if ($legends)  $tail .= "&chdl=$legends";
   if ($colors)   $tail .= "&chco=$colors";
   if ($bgfill)   $tail .= "&chf=bg,s,$bgfill";

   $url   = "http://chart.apis.google.com/chart?$tail";

   // Uncomment the line below to return a URL to the chart image
   // return $url;
   //var_dump($url);
   $image = imagecreatefrompng($url);

   $w = imagesx($image);
   $h = imagesy($image);
   $image2 = imagecreatetruecolor($w + $border * 2,
      $h + $border * 2);
   $clr = imagecolorallocate($image,
      hexdec(substr($bcolor, 0, 2)),
      hexdec(substr($bcolor, 2, 2)),
      hexdec(substr($bcolor, 4, 2)));
   imagefilledrectangle($image2, 0, 0, $w + $border * 2,
      $h + $border * 2, $clr);
   imagecopy($image2, $image, $border, $border, 0, 0, $w, $h);
   imagedestroy($image);
   return $image2;
}

?>

發佈了160 篇原創文章 · 獲贊 12 · 訪問量 113萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章