Using Flash graphical data (2) Let's bake cookies (for pie charts)
After the Shockwave Flash support in PHP is successfully installed, you can use PHP to create the Shockwave file. The best way to learn is to directly jump to the program, so let's look at the program below. The first file includes sample code for how to use the class, and shows how to embed a Flash file into an HTML document.
// 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 (shocould not exceed about 25 characters)
$ Graph-> pie_title ("City Results", 30 );
// Set graph legend
$ Graph-> pie_legend ($ city_names );
// Show graph
$ Graph-> show ();
// Free resources
$ Graph-> close ();
?>
Type = "application/x-shockwave-flash" width = 600 height = 300>
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 write out code directly to browser, 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 will use for pie graph
Swf_definefont (10, "Mod ");
// Get sum of array for percents/slices
While (list ($ key, $ val) = each ($ values )){
$ Sum = $ sum + $ val;
}
For ($ I = 0; $ I // Calculate how big they need to be and then
// Draw all of our slices
If ($ I = 0 ){
// Setup parameters for first 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 for 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,100, $ 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 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", 15, $ 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) at ($ x, $ y) with font size ($ size)
// Set color of text to black
Swf_addcolor (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 shocould not exceed about 25 characters
$ This-> pie_text (99, $ 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: you can directly return the generated SWF file to the browser without having to write it into a file like me. This may be useful for testing, but you may rarely use a Flash file. in more cases, you may want to embed the Flash file into HTML documents. If you choose to directly output the Flash file to the browser, you can set the header content type as follows:
Header ("Content-type: application/x-shockwave-flash ")
Change swf_openfile (filename ",...) to swf_openfile (" php: // stdout ",...)
Link for more information:
Http://www.php.net/manual/ref.swf.php description of swf _ * PHP functions
Http://reality.sgi.com/grafica/flash/ Download PHP swf Library
Http://openswf.org more Flash tools and information
Http://www.macromedia.com/software/flash/open/licensing/
More information about Macromedia Flash SDK