用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 相关文章推荐
第1次亲密接触PHP5(1)
Oct 09 PHP
php4的session功能评述(一)
Oct 09 PHP
PHP多个版本的分析解释
Jul 21 PHP
PHP判断远程url是否有效的几种方法小结
Oct 08 PHP
PHP 正则表达式之正则处理函数小结(preg_match,preg_match_all,preg_replace,preg_split)
Oct 05 PHP
ThinkPHP3.1新特性之命名范围的使用
Jun 19 PHP
PHP提示Cannot modify header information - headers already sent by解决方法
Sep 22 PHP
php格式化电话号码的方法
Apr 24 PHP
ThinkPHP V2.2说明文档没有说明的那些事实例小结
Jul 01 PHP
php处理单文件、多文件上传代码分享
Aug 24 PHP
ThinkPHP5.0 图片上传生成缩略图实例代码说明
Jun 20 PHP
禁止直接访问php文件代码分享
May 05 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如何得到当前页和上一页的地址?
2006/11/27 PHP
PHP MSSQL 分页实例
2016/04/13 PHP
PHP常用函数之根据生日计算年龄功能示例
2019/10/21 PHP
PHP读取Excel内的图片(phpspreadsheet和PHPExcel扩展库)
2019/11/19 PHP
(JS实现)MapBar中坐标的加密和解密的脚本
2007/05/16 Javascript
jQuery中replaceAll()方法用法实例
2015/01/16 Javascript
JavaScript实现LI列表数据绑定的方法
2015/08/04 Javascript
Javascript设计模式理论与编程实战之简单工厂模式
2015/11/03 Javascript
jQuery简单实现提交数据出现loading进度条的方法
2016/03/29 Javascript
javaScript事件学习小结(四)event的公共成员(属性和方法)
2016/06/09 Javascript
JS跨域交互(jQuery+php)之jsonp使用心得
2016/07/01 Javascript
JavaScript中捕获与冒泡详解及实例
2017/02/03 Javascript
基于JavaScript实现的顺序查找算法示例
2017/04/14 Javascript
JavaScript该如何学习 怎样轻松学习JavaScript
2017/06/12 Javascript
jQuery自动或手动图片切换效果
2017/10/11 jQuery
JavaScript enum枚举类型定义及使用方法
2020/05/15 Javascript
vue实现图片上传功能
2020/05/28 Javascript
详解JS深拷贝与浅拷贝
2020/08/04 Javascript
[00:49]完美世界DOTA2联赛10月28日开团时刻:随便打
2020/10/29 DOTA
剖析Python的Tornado框架中session支持的实现代码
2015/08/21 Python
python 多维切片之冒号和三个点的用法介绍
2018/04/19 Python
python sys.argv[]用法实例详解
2018/05/25 Python
Python面向对象类的继承实例详解
2018/06/27 Python
Python 3.x 判断 dict 是否包含某键值的实例讲解
2018/07/06 Python
Python使用LDAP做用户认证的方法
2019/06/20 Python
Python猴子补丁知识点总结
2020/01/05 Python
Python xmltodict模块安装及代码实例
2020/10/05 Python
pandas抽取行列数据的几种方法
2020/12/13 Python
Ootori在线按摩椅店:一家专业的按摩椅制造商
2019/04/10 全球购物
就业协议书样本
2014/08/20 职场文书
企业法人授权委托书
2014/09/25 职场文书
学校捐款活动总结
2015/05/09 职场文书
大学毕业典礼致辞
2015/07/29 职场文书
2016年春节慰问信息大全
2015/11/30 职场文书
会计手工模拟做账心得体会
2016/01/22 职场文书
Win10 最新稳定版本 21H2开始推送
2022/04/19 数码科技