Tutorial PHP : Cara Menampilkan Grafik Pie dengan PHP dan MySQL
Halo sahabat Belajar Pintar PHP sekalian, sebelumnya saya mengucapkan terimakasih atas kesetiannya terhadap blog saya ini yaitu belajar pintar php. Kali ini saya akan share sedikit ilmu tentang cara membuat grafik pada aplikasi php. Oke langsung saja ke TKP .. !!!!
Ini tips kepada anda bagaimana menampilkan Grafik dalam bentuk Pie menggunakan PHP dan MySQL. Silahkan anda membuat database nya menggunakan tabel dibawah, atau anda dapat memodifikasi sesuai kebutuhan.
CREATE TABLE `tabelpengunjung` (
`nomor` int(5) NOT NULL auto_increment,
`nama` varchar(50) NOT NULL,
`tanggal` date NOT NULL,
PRIMARY KEY (`nomor`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=9 ;
INSERT INTO `tabelpengunjung` (`nomor`, `nama`, `tanggal`) VALUES
(1, ‘Ivan’, ’2010-05-04′),
(2, ‘Nita’, ’2010-05-11′),
(3, ‘Lily’, ’2010-05-18′),
(4, ‘Viekka’, ’2010-06-15′),
(5, ‘Dinda’, ’2010-07-13′),
(6, ‘Putrie’, ’2010-07-06′),
(7, ‘Joko’, ’2010-08-11′),
(8, ‘Jaka’, ’2010-08-17′);
`nomor` int(5) NOT NULL auto_increment,
`nama` varchar(50) NOT NULL,
`tanggal` date NOT NULL,
PRIMARY KEY (`nomor`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=9 ;
INSERT INTO `tabelpengunjung` (`nomor`, `nama`, `tanggal`) VALUES
(1, ‘Ivan’, ’2010-05-04′),
(2, ‘Nita’, ’2010-05-11′),
(3, ‘Lily’, ’2010-05-18′),
(4, ‘Viekka’, ’2010-06-15′),
(5, ‘Dinda’, ’2010-07-13′),
(6, ‘Putrie’, ’2010-07-06′),
(7, ‘Joko’, ’2010-08-11′),
(8, ‘Jaka’, ’2010-08-17′);
Selanjutnya buat halaman php dengan nama "pie.php" , lalu copy-pastekan script dibawah ini kedalamnya.
<?php
mysql_connect("localhost","root","");
mysql_select_db("test");
$data = "";
$label = "";
$query = mysql_query(“select count(*) as jumlah, monthname(tanggal) as bulan
from tabelpengunjung group by monthname(tanggal)”);
while($q=mysql_fetch_array($query)){
$data = $data.$q['jumlah'].”*”;
$label = $label.$q['bulan'].”*”;
}
$data = substr($data,0,-1);
$label = substr($label,0,-1);
$show_label = true;
$show_percent = true;
$show_text = true;
$show_parts = false;
$label_form = ‘square’;
$width = 199;
$background_color = ‘FFFFFF’;
$text_color = ’000000′;
$colors = array(’003366′, ‘CCD6E0′, ’7F99B2′,’F7EFC6′, ‘C6BE8C’, ‘CC6600′,’990000′,’520000′,’BFBFC1′,’808080′);
$shadow_height = 16;
$shadow_dark = true;
$height = $width/2;
$data = explode(‘*’,$data);
if ($label != ”) $label = explode(‘*’,$label);
for ($i = 0; $i < count($label); $i++)
{
if ($data[$i]/array_sum($data) < 0.1) $number[$i] = ‘ ‘.number_format(($data[$i]/array_sum($data))*100,1,’,',’.').’%';
else $number[$i] = number_format(($data[$i]/array_sum($data))*100,1,’,',’.').’%';
if (strlen($label[$i]) > $text_length) $text_length = strlen($label[$i]);
}
if (is_array($label))
{
$antal_label = count($label);
$xtra = (5+15*$antal_label)-($height+ceil($shadow_height));
if ($xtra > 0) $xtra_height = (5+15*$antal_label)-($height+ceil($shadow_height));
$xtra_width = 5;
if ($show_label) $xtra_width += 20;
if ($show_percent) $xtra_width += 45;
if ($show_text) $xtra_width += $text_length*8;
if ($show_parts) $xtra_width += 35;
}
$img = ImageCreateTrueColor($width+$xtra_width, $height+ceil($shadow_height)+$xtra_height);\
ImageFill($img, 0, 0, colorHex($img, $background_color));
foreach ($colors as $colorkode)
{
$fill_color[] = colorHex($img, $colorkode);
$shadow_color[] = colorHexshadow($img, $colorkode, $shadow_dark);
}
$label_place = 5;
if (is_array($label))
{
for ($i = 0; $i < count($label); $i++)
{
if ($label_form == ’round’ && $show_label)
{
imagefilledellipse($img,$width+11,$label_place+5,10,10,colorHex($img, $colors[$i % count($colors)]));
imageellipse($img,$width+11,$label_place+5,10,10,colorHex($img, $text_color));
}
else if ($label_form == ‘square’ && $show_label)
{
imagefilledrectangle($img,$width+6,$label_place,$width+16,$label_place+10,colorHex($img, $colors[$i % count($colors)]));
imagerectangle($img,$width+6,$label_place,$width+16,$label_place+10,colorHex($img, $text_color));
}
if ($show_percent) $label_output = $number[$i].’ ‘;
if ($show_text) $label_output = $label_output.$label[$i].’ ‘;
if ($show_parts) $label_output = $label_output.$data[$i];
imagestring($img,’2′,$width+20,$label_place,$label_output,colorHex($img, $text_color));
$label_output = ”;
$label_place = $label_place + 15;
}
}
$centerX = round($width/2);
$centerY = round($height/2);
$diameterX = $width-4;
$diameterY = $height-4;
$data_sum = array_sum($data);
$start = 270;
for ($i = 0; $i < count($data); $i++)
{
$value += $data[$i];
$end = ceil(($value/$data_sum)*360) + 270;
$slice[] = array($start, $end, $shadow_color[$value_counter % count($shadow_color)], $fill_color[$value_counter % count($fill_color)]);
$start = $end;
$value_counter++;
}
for ($i=$centerY+$shadow_height; $i>$centerY; $i–)
{
for ($j = 0; $j < count($slice); $j++)
{
if ($slice[$j][0] != $slice[$j][1]) ImageFilledArc($img, $centerX, $i, $diameterX, $diameterY, $slice[$j][0], $slice[$j][1], $slice[$j][2], IMG_ARC_PIE);
}
}
for ($j = 0; $j < count($slice); $j++)
{
if ($slice[$j][0] != $slice[$j][1]) ImageFilledArc($img, $centerX, $centerY, $diameterX, $diameterY, $slice[$j][0], $slice[$j][1], $slice[$j][3], IMG_ARC_PIE);
}
OutputImage($img);
ImageDestroy($img);
function colorHex($img, $HexColorString)
{
$R = hexdec(substr($HexColorString, 0, 2));
$G = hexdec(substr($HexColorString, 2, 2));
$B = hexdec(substr($HexColorString, 4, 2));
return ImageColorAllocate($img, $R, $G, $B);
}
function colorHexshadow($img, $HexColorString, $mork)
{
$R = hexdec(substr($HexColorString, 0, 2));
$G = hexdec(substr($HexColorString, 2, 2));
$B = hexdec(substr($HexColorString, 4, 2));
if ($mork)
{
($R > 99) ? $R -= 100 : $R = 0;
($G > 99) ? $G -= 100 : $G = 0;
($B > 99) ? $B -= 100 : $B = 0;
}
else
{
($R < 220) ? $R += 35 : $R = 255;
($G < 220) ? $G += 35 : $G = 255;
($B < 220) ? $B += 35 : $B = 255;
}
return ImageColorAllocate($img, $R, $G, $B);
}
function OutputImage($img)
{
header(‘Content-type: image/jpg’);
ImageJPEG($img,NULL,100);
}
?>
mysql_connect("localhost","root","");
mysql_select_db("test");
$data = "";
$label = "";
$query = mysql_query(“select count(*) as jumlah, monthname(tanggal) as bulan
from tabelpengunjung group by monthname(tanggal)”);
while($q=mysql_fetch_array($query)){
$data = $data.$q['jumlah'].”*”;
$label = $label.$q['bulan'].”*”;
}
$data = substr($data,0,-1);
$label = substr($label,0,-1);
$show_label = true;
$show_percent = true;
$show_text = true;
$show_parts = false;
$label_form = ‘square’;
$width = 199;
$background_color = ‘FFFFFF’;
$text_color = ’000000′;
$colors = array(’003366′, ‘CCD6E0′, ’7F99B2′,’F7EFC6′, ‘C6BE8C’, ‘CC6600′,’990000′,’520000′,’BFBFC1′,’808080′);
$shadow_height = 16;
$shadow_dark = true;
$height = $width/2;
$data = explode(‘*’,$data);
if ($label != ”) $label = explode(‘*’,$label);
for ($i = 0; $i < count($label); $i++)
{
if ($data[$i]/array_sum($data) < 0.1) $number[$i] = ‘ ‘.number_format(($data[$i]/array_sum($data))*100,1,’,',’.').’%';
else $number[$i] = number_format(($data[$i]/array_sum($data))*100,1,’,',’.').’%';
if (strlen($label[$i]) > $text_length) $text_length = strlen($label[$i]);
}
if (is_array($label))
{
$antal_label = count($label);
$xtra = (5+15*$antal_label)-($height+ceil($shadow_height));
if ($xtra > 0) $xtra_height = (5+15*$antal_label)-($height+ceil($shadow_height));
$xtra_width = 5;
if ($show_label) $xtra_width += 20;
if ($show_percent) $xtra_width += 45;
if ($show_text) $xtra_width += $text_length*8;
if ($show_parts) $xtra_width += 35;
}
$img = ImageCreateTrueColor($width+$xtra_width, $height+ceil($shadow_height)+$xtra_height);\
ImageFill($img, 0, 0, colorHex($img, $background_color));
foreach ($colors as $colorkode)
{
$fill_color[] = colorHex($img, $colorkode);
$shadow_color[] = colorHexshadow($img, $colorkode, $shadow_dark);
}
$label_place = 5;
if (is_array($label))
{
for ($i = 0; $i < count($label); $i++)
{
if ($label_form == ’round’ && $show_label)
{
imagefilledellipse($img,$width+11,$label_place+5,10,10,colorHex($img, $colors[$i % count($colors)]));
imageellipse($img,$width+11,$label_place+5,10,10,colorHex($img, $text_color));
}
else if ($label_form == ‘square’ && $show_label)
{
imagefilledrectangle($img,$width+6,$label_place,$width+16,$label_place+10,colorHex($img, $colors[$i % count($colors)]));
imagerectangle($img,$width+6,$label_place,$width+16,$label_place+10,colorHex($img, $text_color));
}
if ($show_percent) $label_output = $number[$i].’ ‘;
if ($show_text) $label_output = $label_output.$label[$i].’ ‘;
if ($show_parts) $label_output = $label_output.$data[$i];
imagestring($img,’2′,$width+20,$label_place,$label_output,colorHex($img, $text_color));
$label_output = ”;
$label_place = $label_place + 15;
}
}
$centerX = round($width/2);
$centerY = round($height/2);
$diameterX = $width-4;
$diameterY = $height-4;
$data_sum = array_sum($data);
$start = 270;
for ($i = 0; $i < count($data); $i++)
{
$value += $data[$i];
$end = ceil(($value/$data_sum)*360) + 270;
$slice[] = array($start, $end, $shadow_color[$value_counter % count($shadow_color)], $fill_color[$value_counter % count($fill_color)]);
$start = $end;
$value_counter++;
}
for ($i=$centerY+$shadow_height; $i>$centerY; $i–)
{
for ($j = 0; $j < count($slice); $j++)
{
if ($slice[$j][0] != $slice[$j][1]) ImageFilledArc($img, $centerX, $i, $diameterX, $diameterY, $slice[$j][0], $slice[$j][1], $slice[$j][2], IMG_ARC_PIE);
}
}
for ($j = 0; $j < count($slice); $j++)
{
if ($slice[$j][0] != $slice[$j][1]) ImageFilledArc($img, $centerX, $centerY, $diameterX, $diameterY, $slice[$j][0], $slice[$j][1], $slice[$j][3], IMG_ARC_PIE);
}
OutputImage($img);
ImageDestroy($img);
function colorHex($img, $HexColorString)
{
$R = hexdec(substr($HexColorString, 0, 2));
$G = hexdec(substr($HexColorString, 2, 2));
$B = hexdec(substr($HexColorString, 4, 2));
return ImageColorAllocate($img, $R, $G, $B);
}
function colorHexshadow($img, $HexColorString, $mork)
{
$R = hexdec(substr($HexColorString, 0, 2));
$G = hexdec(substr($HexColorString, 2, 2));
$B = hexdec(substr($HexColorString, 4, 2));
if ($mork)
{
($R > 99) ? $R -= 100 : $R = 0;
($G > 99) ? $G -= 100 : $G = 0;
($B > 99) ? $B -= 100 : $B = 0;
}
else
{
($R < 220) ? $R += 35 : $R = 255;
($G < 220) ? $G += 35 : $G = 255;
($B < 220) ? $B += 35 : $B = 255;
}
return ImageColorAllocate($img, $R, $G, $B);
}
function OutputImage($img)
{
header(‘Content-type: image/jpg’);
ImageJPEG($img,NULL,100);
}
?>
Oke, scipt diatas digunakan utuk membuat grafik pie.
kemudian kita akan membuat halaman yang berfungsi untuk menampilkan grafik pie tersebut didalam file lain dengan cara, buat sebuah halaman html dengan nama "tampilkangrafik.html". lalu copy-pastekan script dibawah ini kedalamnya.
<img src=pie.php>
Jika sudah, sekarang silahkan jalankan script tersebut di webserver kesayangan anda.
(yang dijalankan adalah halaman "tampilkangrafik.html".
Selamat Mencoba.
No comments:
Post a Comment