openflashchart 2.0 簡單案例php版

來源:互聯網
上載者:User

1.openflashchart是一種比較實用的表徵圖呈現外掛程式,而且是開源的,網址http://teethgrinder.co.uk/open-flash-chart/

2.FlashChart類 複製代碼 代碼如下:FlashChart Class Code

class FlashChart
{
private $id;
private $height;
private $width;
private $path;
function __construct($path="",$width=300,$height=500,$id="myChart")
{
global $flash_chart;
$this->id=$id;
$this->height=$height;
$this->width=$width;
$this->path=$path;
if(!$flash_chart)
{
echo '<script type="text/javascript" src="'.$path.'js/json/json2.js"></script>';
echo '<script type="text/javascript" src="'.$path.'js/swfobject.js"></script>';
echo '<script type="text/javascript" src="'.$path.'js/jquery-1.4.4.min.js"></script>';
$flash_chart=true;
}
}

function __destruct()
{
unset($this->id,$this->height,$this->width,$this->path);
}
function setID($id)
{
$this->id=$id;
}
function setChart($file,$info)
{
$tp=new TemplateData($file);
echo '<script type="text/javascript">';
echo "data_{$this->id}=".$tp->changeInfo($info).';';
echo "function ofc_get_dataOf{$this->id}(){return JSON.stringify(data_{$this->id});}";
echo "swfobject.embedSWF('".$this->path."/open-flash-chart.swf', '$this->id', '$this->width','$this->height','9.0.0','expressInstall.swf',{'get-data':'ofc_get_dataOf{$this->id}'} )";
echo '</script>';
}
}

3,TemplateData類

把一個簡單的表徵圖的配置從已經寫好的txt文本裡取出來載入所用的類 :例如 複製代碼 代碼如下:{
"title":
{
"text":"(title)",
"style":"{color:#FF0000;font-size:24px;}"
},
"y_legend":{
"text": "iWebShop",
"style": "{color: #736AFF;font-size:16px;}"
},

"elements":[
{
"type": "line",
"colour": "#736AFF",
"text": "註冊使用者量(人)",
"width": 1,
"dot-style": {
"type":"solid-dot", "colour":"#a44a80", "dot-size": 3,
"tip":"#val#人<br>#x_label#" },
"on-show": {"type": "shrink-in", "cascade":1, "delay":0.5},
"values" : [(numbers)]
}
],

"x_axis":{
"labels": {
"labels":[(dates)]
}
},

"y_axis":{
"steps": (steps),
"max": (max)
}
}

這是類的內容: 複製代碼 代碼如下:

class TemplateData
{
public $substitution;
private $templateFile;
function __construct($filename)
{
$this->templateFile=@file_get_contents($filename) or die("not find templateFile");
}
function __destruct() {
unset ($this->templateFile,$this->substitution);
}
function setTemplateFile($tfile)
{
$this->templateFile=$tfile;
}
function getTemplateFile()
{
return $this->templateFile;
}
function replaceReal($matches)
{
extract($this->substitution, EXTR_OVERWRITE);
return isset($$matches[1])?$$matches[1]:$matches[1];
}
function changeInfo($subs)
{
$this->substitution=$subs;
return preg_replace_callback("(\((\w+)\))",array(&$this, 'replaceReal'),$this->getTemplateFile());
}

}

4,調用的代碼 複製代碼 代碼如下:

<!--//這裡的myChat是顯示flash的地方,所以不可為空,改動的話要在聲明FlashChart類的時候定義,詳細見FlashChart類-->
<div class='content_box'><div id="myChart"></div></div>

<?php
include("flashchart.php");
include("templatedata.php");
$fc=new FlashChart('chart/',"100%",320);
$infos=array(
'numbers'=>"30000,10000,5000,6000000,700",
'dates'=>"\"字串1\",\"字串2\",\"字串3\",\"字串4\",\"字串5\"",
'steps'=>600000,
'max'=>6000000
);
$info=array("title"=>'使用者註冊統計','numbers'=>$infos['numbers'],'dates'=>$infos['dates'],'steps'=>$infos['steps'],'max'=>$infos['max']);
$fc->setChart("chart/templatechart/user-add.txt",$info);

5,還有一個處理資料的函數,把查詢出來的資料集轉換成ofc用的資料

複製代碼 代碼如下:

/**
* @brief ofc資料處理
* @params 資料庫查詢出關於x,y軸的資料的資料集
* @note 後台
*/
/*
public function init_count($rs)
{

$numbers ='';
$dates = '';
$max = 0;
foreach($rs as $row)
{

$numbers .= $row['num'].',';//y軸資料
$dates .='"'.$row['month'].'",';//x軸資料
if($max<$row['num']) $max = $row['num'];
}
$steps=ceil($max/10);
$result= array(
'steps' => $steps,
'numbers' => strlen($numbers)>1 ? substr($numbers,0,-1):null,
'dates' => strlen($dates)>1 ? substr($dates,0,-1) : null,
'max' => $max+$steps
);
return $result;
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.