#native_company# #native_desc#
#native_cta#

Graphing With Flash (SWF) Page 4

By Bryan Mattern
on January 10, 2001

<?php

class flash_pie {

// class variables

// setup some global colors

var $r_arr = array(0.1,  101010.3882352940.40.3882352940.929411765);

var 
$g_arr = array(1,    001100.8,         0.40.8,         0.439215686);

var 
$b_arr = array(0.25010111,           0.41,           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_file100045030111);

    

    
// set up viewport for flash movie

    
swf_ortho2 (-400300 , -90250); 

    // 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<count($values); $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(-20000);

        } 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(0001);

    
swf_shapearc(00100$begin$end);

    
swf_shapecurveto(0000);

    
swf_endshape($objID);

    swf_pushmatrix();

    
swf_placeobject($objID1);

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

        if (
$i == 0) {

            
swf_translate(120750);

        } else {

            
swf_translate(0200);

        }

        
swf_placeobject($i+10001);

        swf_translate(050);

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

        
swf_translate(-$width00);

    }

    
swf_translate($width30*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,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$y0);

    
swf_placeobject($id1);

}

function pie_title($text$size) {

    
// simple function to draw title and set lineup

    // $text should not exceed about 25 characters

    
$this->pie_text(99$text$size0150);

    
swf_translate(0, -3000);

}       

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 also feed the resulting SWF file out to the browser instead
of writing it out to a file as I present in the tutorial. While this may be useful for testing purposes,
you will rarely use just a flash file, most often, you will want to embed the flash file in an HTML
document. If you choose to output the Flash file directly to the browser, you can do so by setting
the header content type with:
header(“Content-type: application/x-shockwave-flash”)
and changing swf_openfile(“filename”, …) to swf_openfile(“php://stdout”, …)

Links to More Information

You can read up on the swf_* PHP functions at:
http://www.php.net/manual/ref.swf.php
You can download the swf library used by PHP at:
http://reality.sgi.com/grafica/flash/
You can find more Flash tools and information at:
http://openswf.org
You can find out more about the Macromedia Flash SDK at:
http://www.macromedia.com/software/flash/open/licensing/

1
|
2
|
3
|
4