Graphical data with Flash (ii) _php Foundation

Source: Internet
Author: User
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; ";

$result = Mysql_db_query ("Hermes", $query);

while ($row = Mysql_fetch_array ($result)) {
$city _counts[] = $row [COUNT (city_id)];
$city _names[] = $row ["City_name"];
}

Mysql_free_result ($result);

Instantiate new Object
$graph = new Flash_pie ($city _counts, "city.swf");

Set graph title (should not exceed about characters)
$graph->pie_title ("City Results", 30);

Set graph legend
$graph->pie_legend ($city _names);

Show graph
$graph->show ();

Free resources
$graph->close ();

?>

<meta http=equiv= "Expires" content= "Fri, June 1981 08:20:00 GMT" >
<meta http=equiv= "Pragma" content= "No-cache" >
<meta http=equiv= "Cache-control" content= "No-cache" >
<meta http=equiv= "Content-type" content= "text/html; Charset=iso-8859-1 ">
<body bgcolor=white>
<div align=center>
<embed src= "city.swf" Quality=high loop=false pluginspage= "http://www.macromedia.com/"
Shockwave/download/index.cgi? P1_prod_version=shockwaveflash "
Type= "Application/x-shockwave-flash" width=600 height=300></embed>
</div>
</body>



<?php

Class Flash_pie {

class variables

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");

Swf_openfile ($this _file, 1000, 450, 30, 1, 1, 1);

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);

Swf_startshape ($objID);
Swf_shapefillsolid (0, 0, 0, 1);
Swf_shapearc (0, 0, $begin, $end);
Swf_shapecurveto (0, 0, 0, 0);
Swf_endshape ($objID);

Swf_pushmatrix ();
Swf_placeobject ($objID, 1);
Swf_popmatrix ();
Swf_showframe ();
}


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);

Swf_definerect ($i +1000, 1, 0, 20, 20, 0);
if ($i = = 0) {
Swf_translate (120, 75, 0);
} else {
Swf_translate (0, 20, 0);
}
Swf_placeobject ($i +1000, 1);

Swf_translate (0, 5, 0);
Unset ($label);
$label = $labels [$i];
$label. = "(";
$label. = $this->percents[$i];
$label. = "percent)";
if ($i ==0) {
$width = (Swf_textwidth ($label)/4) +30;
} else {
$width = Round (Swf_textwidth ($label)/2) +30;
}
$this->pie_text ($i -1000, "$label", $width, 0);
Swf_translate (-$width, 0, 0);
}
Swf_translate ($width, 30*count ($labels), 0);
}


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

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.