Let's bake some cookies (make a pie chart)
The successful installation of PHP Shockwave Flash support, you can use PHP to create shockwave files. The best way to learn is to jump directly to the program, so let's look at the procedure below. The first file includes how to use the sample code for the class and also shows how to embed a Flash file in an HTML document.
<?php
Include class needed for flash graph
Include ("class.pie.flash.php");
mysql_connect ("localhost", "root", "");
$query = "Select DISTINCT city_name, COUNT (city_id)
From city
GROUP by City_name; ";
Setup some global colors
var $r _arr = Array (0.1, 1, 0, 1, 0, 1, 0.388235294, 0.4, 0.388235294, 0.929411765);
var $g _arr = Array (1, 0, 0, 1, 1, 0, 0.8, 0.4, 0.8, 0.439215686);
var $b _arr = Array (0.25, 0, 1, 0, 1, 1, 1, 0.4, 1, 0.043137255);
var $percents;
function Flash_pie ($values, $this _file) {//begin constructor
To the write out code directly to browser, the set content header and use "Php://stdout"
Swf_openfile ("Php://stdout", 700, 250, 30, 1, 1, 1);
Header ("Content-type:application/x-shockwave-flash");
Set up viewport for Flash movie
Swf_ortho2 (-400, 300,-90, 250);
Choose the font we'll use for pie graph
Swf_definefont ("Mod");
Get sum of array for percents/slices
while (the list ($key, $val) = each ($values)) {
$sum = $sum + $val;
}
for ($i =0; $i <count ($values); $i + +) {
Calculate how big they need to be and then
Draw all about our slices
if ($i = = 0) {
Setup parameters for the slice
$begin = 0;
$val = $values [$i]/$sum;
$end = $val *360;
Swf_translate (-200, 0, 0);
} else {
Setup parameters for every other slice
$begin = $end;
$val = $values [$i]/$sum;
$end = $end + $val *360;
}
function call to add slice
$objID = 1+ $i *10;
$this->show_slice ($i, $objID, $begin, $end);
Put together percent array to all labels
$this->percents[$i] = round ($values [$i]/$sum *100);
}
}//end Flash_pie
function Show_slice ($i, $objID, $begin, $end) {
Draws a slice and places it in our frame
Swf_addcolor ($this->r_arr[$i], $this->g_arr[$i], $this->b_arr[$i], 1);
function Pie_legend ($labels) {
Draws the legend and labels and places it in our frame
for ($i =0; $i <count ($labels); $i + +) {
Swf_addcolor ($this->r_arr[$i], $this->g_arr[$i], $this->b_arr[$i], 1);
function Pie_text ($id, $text, $size, $x, $y) {
Simple function to draw text ($text) in ($x, $y) with font size ($size)
Set color of text to black
Swf_addcolor (0,0,0,0);
Set font size and slant
Swf_fontsize ($size);
Swf_fontslant (0);
Define, position and place text in frame
Swf_definetext ($id, "$text", 1);
Swf_translate ($x, $y, 0);
Swf_placeobject ($id, 1);
}
function Pie_title ($text, $size) {
Simple function to draw title and set lineup
$text should not exceed about characters
$this->pie_text ($text, $size, 0, 150);
Swf_translate (0,-300, 0);
}
Function Show () {
Show the Frame
Swf_showframe ();
}
function Close () {
Flush our buffer and return movie
$data = Swf_closefile (1);
}
}//End Class Flash_pie
?>
Note that you can return the generated SWF file directly to the browser without having to write it to a file like I did. This may be useful for testing, but you may rarely use a flash file, and more often you might want to embed the Flash file in an HTML document. If you choose to export the Flash file directly to the browser, you can set the header content type as follows:
Header ("Content-type:application/x-shockwave-flash")
and put swf_openfile (filename ",...) Change to Swf_openfile ("Php://stdout",...)
Links to more information:
http://www.php.net/manual/ref.swf.php about the swf_* php function
http://reality.sgi.com/grafica/flash/download PHP's SWF library
http://openswf.org more flash tools and information
http://www.macromedia.com/software/flash/open/licensing/
More about the Macromedia Flash SDK
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.