用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 相关文章推荐
推荐文章系统(一)
Oct 09 PHP
为查询结果建立向后/向前按钮
Oct 09 PHP
如何利用php array_multisort函数 对数据库结果进行复杂排序
Jun 08 PHP
奉献出一个封装的curl函数 便于调用(抓数据专用)
Jul 22 PHP
解析php开发中的中文编码问题
Aug 08 PHP
destoon各类调用汇总
Jun 20 PHP
用php来限制每个ip每天浏览页面数量的实现思路
Feb 24 PHP
关于WordPress的SEO优化相关的一些PHP页面脚本技巧
Dec 10 PHP
thinkPHP中分页用法实例分析
Dec 26 PHP
使用WAMP搭建PHP本地开发环境
May 10 PHP
深入理解 PHP7 中全新的 zval 容器和引用计数机制
Oct 15 PHP
解决在laravel中auth建立时候遇到的问题
Oct 15 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/10/09 PHP
深入PHP5中的魔术方法详解
2013/06/17 PHP
Kindeditor编辑器添加图片上传水印功能(php代码)
2017/08/03 PHP
php实现数组重复数字统计实例
2018/09/30 PHP
jquery插件validate验证的小例子
2013/05/08 Javascript
jQuery实现仿美橙互联两级导航菜单效果完整实例
2015/09/17 Javascript
require、backbone等重构手机图片查看器
2016/11/17 Javascript
工厂模式在JS中的实践
2017/01/18 Javascript
用js制作淘宝放大镜效果
2020/10/28 Javascript
面试常见的js算法题
2017/03/23 Javascript
PHP实现记录代码运行时间封装类实例教程
2017/05/08 Javascript
详解微信小程序与内嵌网页交互实现支付功能
2018/10/22 Javascript
js实现时间日期校验
2020/05/26 Javascript
Python生成pdf文件的方法
2014/08/04 Python
python实现将文本转换成语音的方法
2015/05/28 Python
python安装Scrapy图文教程
2017/08/14 Python
Python实现学生成绩管理系统
2020/04/05 Python
PyQt5 QTable插入图片并动态更新的实例
2019/06/18 Python
Python Pandas实现数据分组求平均值并填充nan的示例
2019/07/04 Python
用python给csv里的数据排序的具体代码
2020/07/17 Python
python实现学生管理系统开发
2020/07/24 Python
python实现简单的井字棋游戏(gui界面)
2021/01/22 Python
html5的localstorage详解
2017/05/09 HTML / CSS
HTML5中5个简单实用的API
2014/04/28 HTML / CSS
DHC美国官网:日本通信销售第一的化妆品品牌
2017/11/12 全球购物
车间工艺员岗位职责
2013/12/09 职场文书
员工拾金不昧表扬信
2014/01/09 职场文书
企业年度评优方案
2014/06/02 职场文书
党的群众路线教育实践活动自我剖析材料
2014/10/08 职场文书
中学生的1000字检讨书
2014/10/11 职场文书
出差报告怎么写
2014/11/06 职场文书
2014年售后服务工作总结
2014/11/18 职场文书
2015年宣传部部长竞选演讲稿
2014/11/28 职场文书
早上好问候语大全
2015/11/10 职场文书
小学体育队列队形教学反思
2016/02/16 职场文书
HR必备:销售经理聘用合同范本
2019/08/21 职场文书