用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 相关文章推荐
php部分常见问题总结
Mar 27 PHP
PHP 开发环境配置(Zend Studio)
Apr 28 PHP
php file_put_contents()功能函数(集成了fopen、fwrite、fclose)
May 24 PHP
php cookie名使用点号(句号)会被转换
Oct 23 PHP
php实现的debug log日志操作类实例
Jul 12 PHP
php源码 fsockopen获取网页内容实例详解
Sep 24 PHP
PHP链表操作简单示例
Oct 15 PHP
php json_encode与json_decode详解及实例
Dec 13 PHP
微信公众平台开发教程⑤ 微信扫码支付模式介绍
Apr 10 PHP
Laravel框架Auth用户认证操作实例分析
Sep 29 PHP
Thinkphp5.0 框架实现控制器向视图view赋值及视图view取值操作示例
Oct 12 PHP
PHP dirname简单使用代码实例
Nov 13 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
shopex中集成的站长统计功能的代码简单分析
2011/08/11 PHP
PHP实现无限极分类图文教程
2014/11/25 PHP
php采集内容中带有图片地址的远程图片并保存的方法
2015/01/03 PHP
PHP中imagick函数的中文解释
2015/01/21 PHP
php+mysqli实现批量执行插入、更新及删除数据的方法
2015/01/29 PHP
php写app接口并返回json数据的实例(分享)
2017/05/20 PHP
基于php中echo用逗号和用点号的区别详解
2018/01/23 PHP
对 lightbox JS 图片控件进行了一下改造, 使其他支持复杂的图片说明
2010/03/20 Javascript
javascript的原生方法获取数组中的最大(最小)值
2012/12/19 Javascript
根据IP的地址,区分不同的地区,查看不同的网站页面的js代码
2013/02/26 Javascript
BootStrap中Datetimepicker和uploadify插件应用实例小结
2016/05/26 Javascript
JavaScript实现通过select标签跳转网页的方法
2016/09/29 Javascript
jQuery extend()详解及简单实例
2017/05/06 jQuery
ajax+node+request爬取网络图片的实例(宅男福利)
2017/08/28 Javascript
Vue实现按钮级权限方案
2019/11/21 Javascript
[01:32:10]NAVI vs VG Supermajor 败者组 BO3 第一场 6.5
2018/06/06 DOTA
让 python 命令行也可以自动补全
2014/11/30 Python
python用模块zlib压缩与解压字符串和文件的方法
2016/12/16 Python
python 3利用Dlib 19.7实现摄像头人脸检测特征点标定
2018/02/26 Python
Python之读取TXT文件的方法小结
2018/04/27 Python
对python中list的拷贝与numpy的array的拷贝详解
2019/01/29 Python
Mac在python3环境下安装virtualwrapper遇到的问题及解决方法
2019/07/09 Python
python 直接赋值和copy的区别详解
2019/08/07 Python
Django 简单实现分页与搜索功能的示例代码
2019/11/07 Python
python 实现一个简单的线性回归案例
2020/12/17 Python
使用CSS3实现一个3D相册效果实例
2016/12/03 HTML / CSS
美国受信赖的教育产品供应商:Nest Learning
2018/06/14 全球购物
联想马亚西亚官方网站:Lenovo Malaysia
2018/09/19 全球购物
暑期社会实践学生的自我评价
2014/01/09 职场文书
给面试官的感谢信
2014/02/01 职场文书
军训自我鉴定100字
2014/02/13 职场文书
大学生万能检讨书范例
2014/10/04 职场文书
预备党员自我批评思想汇报
2014/10/10 职场文书
简爱读书笔记
2015/06/26 职场文书
投诉信格式范文
2015/07/02 职场文书
总经理聘用协议书
2015/09/21 职场文书