用Flash图形化数据(二)


Posted in PHP onOctober 09, 2006

让我们烤点甜饼(做饼图)
??成功地安装了PHP地Shockwave Flash支持后,就可以用PHP创建Shockwave文件了。学习的最好方法就是直接跳到程序去,所以下面就让我们看看程序。第一个文件包括怎样使用类的示例代码,同时也显示了如何将一个Flash文件嵌入到HTML文档中。

<?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 25 characters)
$graph->pie_title("City Results", 30);

// set graph legend
$graph->pie_legend($city_names);

// show graph
$graph->show();

// free resources
$graph->close();

?>  

<html>
<head>
<meta http=equiv="Expires" content="Fri, Jun 12 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">
</head>
<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>
</html>  

<?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 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<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(-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<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", 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,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 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

?>  

    注意,你可以将生成的SWF文件直接返回到浏览器中,而不必一定要像我一样把它写到一个文件中。这可能对测试来说是有用的,但你可能很少用到一个Flash文件,更多的时候你可能想把Flash文件嵌入到HTML文档中。如果你选择直接把Flash文件输出到浏览器中,你可以如下设置header content 类型:
   header("Content-type: application/x-shockwave-flash")
并把swf_openfile(filename",...)改成swf_openfile("php://stdout",...)

更多信息的链接:
    http://www.php.net/manual/ref.swf.php  关于swf_* PHP函数的说明
    http://reality.sgi.com/grafica/flash/  下载PHP的swf库
    http://openswf.org                     更多Flash工具和信息
    http://www.macromedia.com/software/flash/open/licensing/  
                                           关于Macromedia Flash SDK的更多信息 

PHP 相关文章推荐
cache_lite试用
Feb 14 PHP
php的日期处理函数及uchome的function_coomon中日期处理函数的研究
Jan 12 PHP
使用Smarty 获取当前日期时间和格式化日期时间的方法详解
Jun 18 PHP
PHP生成图片验证码、点击切换实例
Jun 25 PHP
php创建、获取cookie及基础要点分析
Jan 26 PHP
PHP中实现Bloom Filter算法
Mar 30 PHP
php使用指定编码导出mysql数据到csv文件的方法
Mar 31 PHP
关于Laravel Route重定向的一个注意点
Jan 16 PHP
YII2自动登录Cookie总是失效的解决方法
Jun 28 PHP
PHP5.0~5.6 各版本兼容性cURL文件上传功能实例分析
May 11 PHP
基于PHP的登录和注册的功能的实现
Aug 06 PHP
WordPress伪静态规则设置代码实例
Dec 10 PHP
用php来检测proxy
Oct 09 #PHP
如何将一个表单同时提交到两个地方处理
Oct 09 #PHP
PHP制作图型计数器的例子
Oct 09 #PHP
多php服务器实现多session并发运行
Oct 09 #PHP
提升PHP速度全攻略
Oct 09 #PHP
php4的彩蛋
Oct 09 #PHP
在PHP中使用灵巧的体系结构
Oct 09 #PHP
You might like
php防攻击代码升级版
2010/12/29 PHP
PHP中redis的用法深入解析
2014/02/20 PHP
JavaScript创建命名空间的5种写法
2014/06/24 PHP
laravel 4安装及入门图文教程
2014/10/29 PHP
在Ubuntu 18.04上安装PHP 7.3 7.2和7.0的方法
2019/04/09 PHP
解读IE和firefox下JScript和HREF的执行顺序
2008/01/12 Javascript
&amp;lt;script defer&amp;gt; defer 是什么意思
2009/05/10 Javascript
js中方法重载如何实现?以及函数的参数问题
2013/08/01 Javascript
仿百度联盟对联广告实现代码
2014/08/30 Javascript
ashx文件获取$.ajax()方法发送的数据
2016/05/26 Javascript
基于vue.js轮播组件vue-awesome-swiper实现轮播图
2017/03/17 Javascript
AngularJS实现自定义指令及指令配置项的方法
2017/11/20 Javascript
利用JavaScript的Map提升性能的方法详解
2019/08/14 Javascript
微信小程序实现上拉加载功能
2019/11/20 Javascript
vue学习笔记之过滤器的基本使用方法实例分析
2020/02/01 Javascript
通过实例解析vuejs如何实现调试代码
2020/07/16 Javascript
浅谈vant组件Picker 选择器选单选问题
2020/11/04 Javascript
在vue中使用inheritAttrs实现组件的扩展性介绍
2020/12/07 Vue.js
Vue实现简单购物车功能
2020/12/13 Vue.js
python利用beautifulSoup实现爬虫
2014/09/29 Python
Python面向对象编程中的类和对象学习教程
2015/03/30 Python
python使用range函数计算一组数和的方法
2015/05/07 Python
python dict.get()和dict['key']的区别详解
2016/06/30 Python
使用python装饰器计算函数运行时间的实例
2018/04/21 Python
python中实现字符串翻转的方法
2018/07/11 Python
Python字符串的修改方法实例
2019/12/19 Python
python import 上级目录的导入
2020/11/03 Python
New delete 与malloc free 的联系与区别
2013/02/04 面试题
自我评价是什么
2014/01/04 职场文书
小学毕业家长寄语
2014/01/19 职场文书
销售人员获奖感言
2014/02/05 职场文书
银行服务感言
2014/03/01 职场文书
雷人标语集锦
2014/06/19 职场文书
领导参观欢迎词
2015/01/26 职场文书
小学教师求职信范文
2015/03/20 职场文书
MySQL 逻辑备份 into outfile
2022/05/15 MySQL