open flash chart中文使用教程

open flash chart(OFC)是一个开源的flash图表软件,这是官方网址,他可以提供各种图表来展示你的数据。OFC数据获取方式是JSON。

最近公司的一个项目要用到这玩意,只能硬着头皮顶上去,花了好几天琢磨。最后总算是勉强完成任务,做的还是很丑陋。有待改进,如果哪位高人看了有什么建议,请下方留言。

以下是项目中用到的核心代码。有省略。

先看看<a href="http://www.mornone.cn/blog/demo/" target="_blank">demo</a>吧
<strong>index.php</strong>
< ?php
//header("Cache-Control: no-cache, must-revalidate");
header("Content-type: text/html; charset=utf-8");
?>
<script type="text/javascript" src="js/swfobject.js"></script>
< ?php
include_once 'ofc-library/open_flash_chart_object.php';
?>

<table width="90%" border="0" align="center" cellspacing="10" cellpadding="0">
  <tr>
    <td align="center">
<strong>品种调查结果</strong>
<script type="text/javascript">
swfobject.embedSWF("open-flash-chart.swf", "one", "450", "300","9.0.0", "expressInstall.swf",{"data-file":"ofc_data_pie.php?act=one","loading":"Loading Data,Please Waiting..."});

</script>
<div id="one"></div>
<br />
<br />
        </td>
    <td align="center">
<strong>品种调查结果</strong>    
<script type="text/javascript">
swfobject.embedSWF("open-flash-chart.swf", "two", "450", "300","9.0.0", "expressInstall.swf",{"data-file":"ofc_data_pie.php?act=two","loading":"Loading Data,Please Waiting..."});

</script>
<div id="two"></div>
<br />
<br />
        </td>
  </tr>
  <tr>
    <td align="center">
<script type="text/javascript">
swfobject.embedSWF("open-flash-chart.swf", "three", "450", "300","9.0.0", "expressInstall.swf",{"data-file":"ofc_data_pie.php?act=three","loading":"Loading Data,Please Waiting..."});

</script>
<div id="three"></div>
<br />
<br />
        </td>

    <td align="center">
<script type="text/javascript">
swfobject.embedSWF("open-flash-chart.swf", "four", "450", "300","9.0.0", "expressInstall.swf",{"data-file":"ofc_data_pie.php?act=four","loading":"Loading Data,Please Waiting..."});

</script>
<div id="four"></div>
<br />
<br />
        </td>
  </tr>
  <tr>
    <td align="center">
<script type="text/javascript">
swfobject.embedSWF("open-flash-chart.swf", "five", "450", "300","9.0.0", "expressInstall.swf",{"data-file":"ofc_data_pie.php?act=five","loading":"Loading Data,Please Waiting..."});
</script>
<div id="five"></div>
<br />
<br />
        </td>
    <td align="center" colspan="2">
<script type="text/javascript">
swfobject.embedSWF("open-flash-chart.swf", "six", "90%", "300","9.0.0", "expressInstall.swf",{"data-file":"ofc_data_pie.php?act=six","loading":"Loading Data,Please Waiting..."});

</script>
<div id="six"></div>   
<br />
<br />
        </td>
  </tr>
</table>
<strong>ofc_data_pie.php</strong>

< ?php
header("Cache-Control: no-cache, must-revalidate");
header("Content-type: text/html; charset=utf-8");
require_once 'config.php';
require_once 'ofc-library/open-flash-chart.php';

$mysql_connect=mysql_connect($DBHost,$DBUser,$DBPass);
mysql_select_db($DBTable);
//mysql_query("SET NAMES utf8");
$mysql="SELECT * FROM `questions` WHERE 1=1;";
$total_nums=mysql_fetch_row(mysql_query("SELECT COUNT(*) AS total_nums FROM `questions`"));
$total_nums=(int)$total_nums[0];//收到的问卷总数

$query=mysql_query($mysql);
if($query){
        $row=array();
        while($res=mysql_fetch_assoc($query)){
                $row[]=$res;
        }
}else{
        die('Query Faild');
}

foreach($row as $val){
        $id                             =       $val['id'];//调查者ID
        $jibendiaocha   =       $val['basic_infor'];//基本调查
        $nandudiaocha   =       $val['important_infor'];//难度调查
        $ip                             =       $val['ip'];//调查者IP

        ////下面开始对基本调查数据和难度调查数据进行分割
        //print_r($jibendiaocha);
        $basic_infor    =       explode(',',$jibendiaocha);
        $important_infor=       explode(',',$nandudiaocha);
        //print_r($basic_infor);
        $changbai_gong  +=      (int)$basic_infor[0];//
        $dabai_gong             +=      (int)$basic_infor[1];//
        $duluoke_gong   +=      (int)$basic_infor[2];//
        $pitelan_gong   +=      (int)$basic_infor[3];//
       
        $changbai_mu    +=      (int)$basic_infor[4];//
        $dabai_mu               +=      (int)$basic_infor[5];//
        $duluoke_mu             +=      (int)$basic_infor[6];//
        $pitelan_mu             +=      (int)$basic_infor[7];//
        $eryuan_mu              +=      (int)$basic_infor[8];//
       
        $zongcunlan             =       (int)$basic_infor[9];//
                switch($zongcunlan){
                        case $zongcunlan<=500:
                        $zongcunlan_1 +=1;
                        break;
                       
                        case $zongcunlan>500 && $zongcunlan< =1000:
                        $zongcunlan_2 +=1;
                        break;
                                               
                        case $zongcunlan>1000 && $zongcunlan< =3000:
                        $zongcunlan_3 +=1;
                        break;

                        case $zongcunlan>3000 && $zongcunlan< =5000:
                        $zongcunlan_4 +=1;
                        break;

                        case $zongcunlan>=5000:
                        $zongcunlan_5 +=1;
                        break;
                }              
        $zongcunlan_total+=$zongcunlan; //
       
        $max_muzhu              =       (int)$basic_infor[10];//
                switch($max_muzhu){
                        case $max_muzhu< =50:
                        $max_muzhu_1 +=1;
                        break;
                       
                        case $max_muzhu>50 && $max_muzhu< =100:
                        $max_muzhu_2 +=1;
                        break;
                                               
                        case $max_muzhu>100 && $max_muzhu< =200:
                        $max_muzhu_3 +=1;
                        break;

                        case $max_muzhu>200 && $max_muzhu< =300:
                        $max_muzhu_4 +=1;
                        break;

                        case $max_muzhu>300 && $max_muzhu< =400:
                        $max_muzhu_5 +=1;
                        break;

                        case $max_muzhu>400 && $max_muzhu< =500:
                        $max_muzhu_6 +=1;
                        break;

                        case $max_muzhu>500 && $max_muzhu< =600:
                        $max_muzhu_7 +=1;
                        break;

                        case $max_muzhu>=600:
                        $max_muzhu_8 +=1;
                        break;
                }              
        $max_muzhu_total+=$max_muzhu;   //
               
        $year_chulan    =       (int)$basic_infor[11];//
                switch($year_chulan){
                        case $year_chulan< =500:
                        $year_chulan_1 +=1;
                        break;
                       
                        case $year_chulan>500 && $year_chulan< =1000:
                        $year_chulan_2 +=1;
                        break;
                                               
                        case $year_chulan>1000 && $year_chulan< =3000:
                        $year_chulan_3 +=1;
                        break;

                        case $year_chulan>3000 && $year_chulan< =5000:
                        $year_chulan_4 +=1;
                        break;

                        case $year_chulan>=5000:
                        $year_chulan_5 +=1;
                        break;
                }              
        $year_chulan_total+=$year_chulan;       //


        $difficulty_1   +=      (int)$important_infor[0];//
        $difficulty_2   +=      (int)$important_infor[1];//
        $difficulty_3   +=      (int)$important_infor[2];//
        $difficulty_4   +=      (int)$important_infor[3];//
        $difficulty_5   +=      (int)$important_infor[4];//
               
        //echo $changbai_gong."<br />".$dabai_gong."<br />".$duluoke_gong."<br />".$pitelan_gong;
        //上面开始对基本调查数据和难度调查数据进行分割

}



$act=$_GET['act'];
//echo $act;

//////////////////////////////////////////////////////

if($act=='one'){
$title = new title( '' );
$title->set_style("font-size:14px; font-weight:bold;");

$pie = new pie();
//$pie->alpha(0.5);
$pie->start_angle( 50 );//起始旋转角度
$pie->add_animation( new pie_fade() );
//$pie->set_tooltip( '#val# of #total#<br />#percent# of 100%' );
$pie->tooltip( '占总数的#percent#' );       //设置鼠标移至色块上的tip
$pie->colours( array('#0000FF','#FF368D','#FF00FF','#FF0000') );//设置色块的颜色
$pie->label_colour('0x000000');//设置色块外部说明的颜色
//$pie->set_label( '1212','#000000','font-size:18px;');
$pie->radius(50);
//echo $changbai_gong."<br />".$dabai_gong."<br />".$duluoke_gong."<br />".$pitelan_gong;
$pie->values( array(
        new pie_value($changbai_gong, "选项1"),
        new pie_value($dabai_gong, "选项2"),
        new pie_value($duluoke_gong, "选项3"),
        new pie_value($pitelan_gong, "选项4")
        )
);//设置色块的内容

$chart = new open_flash_chart();
$chart->set_title( $title );
$chart->add_element( $pie );
$chart->set_bg_colour( '#FEFFE8' );//flash背景颜色
$chart->x_axis = null;
}

//////////////////////////////////////////////////////

if($act=='two'){

$title = new title( '' );
$title->set_style("font-size:14px; font-weight:bold;");

$pie = new pie();
//$pie->alpha(0.5);
$pie->start_angle( 180 );
$pie->add_animation( new pie_fade() );
//$pie->set_tooltip( '#val# of #total#<br />#percent# of 100%' );
$pie->tooltip( '占总数的#percent#' );       //设置鼠标移至色块上的tip
$pie->colours( array('#0000FF','#FF368D','#FF00FF','#FF0000','#660000') );//设置色块的颜色
$pie->label_colour('0x000000');//设置色块外部说明的颜色


//$pie->set_label( '1212','#000000','font-size:18px;');
//$pie->radius(50);
//$pie_v=new pie_value();
//$pie_v->set_label("$changbai_mu","#FF0000","font-size:14px");

$pie->values( array(
        new pie_value($changbai_mu, "选项1"),
        new pie_value($dabai_mu, "选项2"),
        new pie_value($duluoke_mu, "选项3"),
        new pie_value($pitelan_mu, "选项4"),
        new pie_value($eryuan_mu, "选项5")
//$pie_v
        )
);//设置色块的内容




$chart = new open_flash_chart();
$chart->set_title( $title );
$chart->add_element( $pie );
$chart->set_bg_colour( '#FEFFE8' );

$chart->x_axis = null;
}

//////////////////////////////////////////////////////

if($act=='three'){
$title = new title( '当前总量调查结果' );
$title->set_style("font-size:14px; font-weight:bold;");

$bar = new bar_glass();

$bar_val = new bar_value(500,-20);

$bar->set_colour( '#0000FF', '#0000FF' );
//$bar->key( "有",'');

$bar->set_values( array($zongcunlan_1,$zongcunlan_2,$zongcunlan_3,$zongcunlan_4,$zongcunlan_5) );
$bar->set_tooltip('#key# #val# 人');



$y = new y_axis();
$y->set_range( 0, 50, 5);


$tmpx = array();  //X轴数据  

$x_zhou=array(
'≤500','500-1000','1000-3000','3000-5000','≥5000'
);

foreach($x_zhou as $val){
        $tmpx[] = $val;
}

$x = new x_axis();  
$x->set_range( 0, 5);//x轴最大最小值
$x->stroke(2);//x轴细线宽度
$x->tick_height(5);//刻度线的高度
$x->set_steps(1);

$x->set_labels_from_array( $tmpx );  

$x_zhou_label = new x_legend();
$x_zhou_label->x_legend('总 '.$zongcunlan_total.' ,共收到调查 '. $total_nums .' 份');
$x_zhou_label->set_style("font-size:12px; font-weight:bold;");


$chart = new open_flash_chart();
$chart->set_title( $title );
$chart->add_element( $bar );
$chart->set_bg_colour( '#FEFFE8' );
$chart->set_x_axis( $x );
$chart->set_y_axis( $y );
$chart->set_x_legend( $x_zhou_label );

}


//////////////////////////////////////////////////////

if($act=='four'){
$title = new title( '最大量调查结果' );
$title->set_style("font-size:14px; font-weight:bold;");

$bar = new bar_glass();

$bar_val = new bar_value(500,-20);

$bar->set_colour( '#0000FF', '#0000FF' );
//$bar->key( "有",'');

$bar->set_values( array($max_muzhu_1,$max_muzhu_2,$max_muzhu_3,$max_muzhu_4,$max_muzhu_5,$max_muzhu_6,$max_muzhu_7,$max_muzhu_8) );
$bar->set_tooltip('#key# #val# 人');



$y = new y_axis();
$y->set_range( 0, 50, 5);


$tmpx = array();  //X轴数据  

$x_zhou=array(
'≤50','50-100','100-200','200-300','300-400','400-500','500-600','≥600'
);

foreach($x_zhou as $val){
        $tmpx[] = $val;
}

$x = new x_axis();  
$x->set_range( 0, 8);//x轴最大最小值
$x->stroke(2);//x轴细线宽度
$x->tick_height(5);//刻度线的高度
$x->set_steps(1);

$x->set_labels_from_array( $tmpx );  

$x_zhou_label = new x_legend();
$x_zhou_label->x_legend('最大总量 '.$max_muzhu_total.' ,共收到调查 '. $total_nums .' 份');
$x_zhou_label->set_style("font-size:12px; font-weight:bold;");


$chart = new open_flash_chart();
$chart->set_title( $title );
$chart->add_element( $bar );
$chart->set_bg_colour( '#FEFFE8' );
$chart->set_x_axis( $x );
$chart->set_y_axis( $y );
$chart->set_x_legend( $x_zhou_label );

}


//////////////////////////////////////////////////////

if($act=='five'){
$title = new title( '年量调查结果' );
$title->set_style("font-size:14px; font-weight:bold;");

$bar = new bar_glass();

$bar_val = new bar_value(500,-20);

$bar->set_colour( '#0000FF', '#0000FF' );
//$bar->key( "有",'');

$bar->set_values( array($year_chulan_1,$year_chulan_2,$year_chulan_3,$year_chulan_4,$year_chulan_5) );
$bar->set_tooltip('#key# #val# 人');



$y = new y_axis();
$y->set_range( 0, 50, 5);


$tmpx = array();  //X轴数据  

$x_zhou=array(
'≤500','500-1000','1000-3000','3000-5000','≥5000'
);

foreach($x_zhou as $val){
        $tmpx[] = $val;
}

$x = new x_axis();  
$x->set_range( 0, 5);//x轴最大最小值
$x->stroke(2);//x轴细线宽度
$x->tick_height(5);//刻度线的高度
$x->set_steps(1);

$x->set_labels_from_array( $tmpx );  

$x_zhou_label = new x_legend();
$x_zhou_label->x_legend('年量总量 '.$year_chulan_total.' ,共收到调查 '. $total_nums .' 份');
$x_zhou_label->set_style("font-size:12px; font-weight:bold;");


$chart = new open_flash_chart();
$chart->set_title( $title );
$chart->add_element( $bar );

$tooltip = new tooltip();
//$tooltip->set_hover();
$tooltip->set_stroke( 1 );
$tooltip->set_colour( "#000000" );
$tooltip->set_background_colour( "#ffffff" );
$chart->set_tooltip( $tooltip );

$chart->set_bg_colour( '#FEFFE8' );
$chart->set_x_axis( $x );
$chart->set_y_axis( $y );
$chart->set_x_legend( $x_zhou_label );

}


//////////////////////////////////////////////////////

if($act=='six'){
$title = new title( '当前困难排序调查结果' );
$title->set_style("font-size:14px; font-weight:bold;");



$tooltip = new tooltip();
$tooltip->set_hover();
$tooltip->set_stroke( 1 );
$tooltip->set_colour( "#000000" );
$tooltip->set_background_colour( "#ffffff" );

$hbar = new hbar( '#0000FF' );
$hbar->set_tooltip( '#val#分' );
$hbar->set_values( array($difficulty_1,$difficulty_2,$difficulty_3,$difficulty_4,$difficulty_5) );

$x = new x_axis();
$x->set_offset( false );
$x->set_range( 0, 350);
$x->steps(20);
//$x_labels = array('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
$x->set_labels_from_array( $x_labels );
$x_zhou_label = new x_legend();
$x_zhou_label->x_legend('共收到调查 '. $total_nums .' 份');
$x_zhou_label->set_style("font-size:12px; font-weight:bold;");


$y=new y_axis();
//$y_labels = new y_axis_labels();
////$y_labels->set_vertical();          //设置x轴标签为垂直
////$y_labels->set_colour( '#CF4D5F' ); //设置x轴标签颜色
//$y_labels->set_size( 14 );           //设置x轴标签字体大小
//$y_labels->set_labels($ydata);      //设置x轴标签数据
//$y->set_labels( $y_labels );

$y = new y_axis();
$y->set_offset( true );
$y->set_labels( array(
"繁殖问题严重",
"资金周转困难",
"担心2010年行情不好",
"疾病影响太大",
"价格太高",
 ) );
//$y->set_size( 16 );           //设置y轴标签字体大小



$chart = new open_flash_chart();
$chart->set_title( $title );
$chart->add_element( $hbar );

$chart->set_x_axis( $x );
$chart->add_y_axis( $y );
$chart->set_tooltip( $tooltip );
$chart->set_bg_colour( '#FEFFE8' );
$chart->set_x_legend( $x_zhou_label );
}


echo $chart->toPrettyString();

?>

以下是在学习过程中得到一些启示的网站。
http://www.yibin001.com/archives/6082
http://teethgrinder.co.uk/open-flash-chart-2/pie-chart.php
http://blog.sina.com.cn/s/blog_4e32d6820100do8g.html
http://hi.baidu.com/any110/blog/item/0115aff873467206d9f9fd01.html


本文固定链接:成事博客open flash chart中文使用教程
喜欢 (0)分享 (0)
发表我的评论
取消评论

表情     6 + 9 = ? (必填)

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址