用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+MySQL5.0中文乱码解决方法
Nov 20 PHP
php 生成饼图 三维饼图
Sep 28 PHP
PHP中计算字符串相似度的函数代码
Dec 29 PHP
实用的简单PHP分页集合包括使用方法
Oct 21 PHP
php实现设计模式中的单例模式详解
Oct 11 PHP
Zend Framework教程之Zend_Config_Xml用法分析
Mar 23 PHP
thinkphp3.x中session方法的用法分析
May 20 PHP
在PHP语言中使用JSON和将json还原成数组的方法
Jul 19 PHP
php实现登录tplink WR882N获取IP和重启的方法
Jul 20 PHP
PHP-FPM运行状态的实时查看及监控详解
Nov 18 PHP
老生常谈PHP数组函数array_merge(必看篇)
May 25 PHP
php接口实现拖拽排序功能
Apr 23 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
图象函数中的中文显示
2006/10/09 PHP
php插入mysql数据返回id的方法
2018/05/31 PHP
自适应高度框架 ----属个人收藏内容
2007/01/22 Javascript
用javascript做拖动布局的思路
2008/05/31 Javascript
Javascript获取窗口(容器)的大小及位置参数列举及简要说明
2012/12/09 Javascript
详解JavaScript的AngularJS框架中的表达式与指令
2016/03/05 Javascript
JavaScript实现页面跳转的方式汇总
2016/05/16 Javascript
jQuery实现输入框邮箱内容自动补全与上下翻动显示效果【附demo源码下载】
2016/09/20 Javascript
简单实现JS计算器功能
2016/12/21 Javascript
ionic2自定义cordova插件开发以及使用(Android)
2017/06/19 Javascript
angular中的cookie读写方法
2017/08/02 Javascript
详解Vue中一种简易路由传参办法
2017/09/15 Javascript
AngularJs用户登录问题处理(交互及验证、阻止FQ处理)
2017/10/26 Javascript
element-ui中的select下拉列表设置默认值方法
2018/08/24 Javascript
Vue 中对图片地址进行拼接的方法
2018/09/03 Javascript
vue微信分享插件使用方法详解
2020/02/18 Javascript
js+audio实现音乐播放器
2020/09/13 Javascript
javascript实现搜索筛选功能实例代码
2020/11/12 Javascript
使用js获取身份证年龄的示例代码
2020/12/11 Javascript
python3编写C/S网络程序实例教程
2014/08/25 Python
Python实现模拟登录及表单提交的方法
2015/07/25 Python
Eclipse中Python开发环境搭建简单教程
2016/03/23 Python
分享一个简单的python读写文件脚本
2017/11/25 Python
python异常处理try except过程解析
2020/02/03 Python
基于Keras 循环训练模型跑数据时内存泄漏的解决方式
2020/06/11 Python
西班牙国家航空官方网站:Iberia
2017/11/16 全球购物
英国最大的汽车配件在线商店:Euro Car Parts
2019/09/30 全球购物
作为网站管理者应当如何防范XSS
2014/08/16 面试题
设计模式的基本要素是什么
2014/04/21 面试题
计算机操作自荐信
2013/12/07 职场文书
干部四风问题整改措施思想汇报
2014/10/13 职场文书
2015年保安个人工作总结
2015/04/02 职场文书
基层党支部承诺书
2015/04/30 职场文书
《分数乘法》教学反思
2016/02/24 职场文书
2016年小学党支部创先争优活动总结
2016/04/05 职场文书
Python数据可视化之用Matplotlib绘制常用图形
2021/06/03 Python