openflashchart 2.0 简单案例php版
程序员文章站
2022-07-07 08:18:28
1.openflashchart是一种比较实用的图标呈现插件,而且是开源的,网址 2.flashchart类 复制代码 代码如下: flashchart class cod...
1.openflashchart是一种比较实用的图标呈现插件,而且是开源的,网址
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;
}
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;
}