图标呈现插件FlashChart应用实例,供大家学习参考。
1.openflashchart是一种比较实用的图标呈现插件,而且是开源的,网址http://teethgrinder.co.uk/open-flash-chart/
2.FlashChart类
<?php
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)
}
}
这是类的内容:
{
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,调用代码
<div ><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;
}
mktime函数 时间戳!
$now = mktime(0,0,0,date("m"),date("d"),date("Y"));
echo "now is ".date("Y/m/d", $now);
显示结果:
now is 2012/05/30
显然这不是我想要的结果。
于是,按照旧有的思维,想当然的改造成下面这个形式:
$now = mktime(date("h"),date("M"),date("s"),date("m"),date("d"),date("Y"));
echo "now is ".date("Y/M/d h:i:s", $now);
注意红色的部分,通常如果月份用m,那么分钟就应该是M。或者前者用M,后者用m。
显示结果:
Warning: mktime() expects parameter 2 to be long, string given in D:\usr\webroot\testPHP\index.php on line 46
now is 1970/01/01 08:Jan:00
正确答案是这样的:
$now = mktime(date("h"),date("i"),date("s"),date("m"),date("d"),date("Y"));
echo "now is ".date("Y/m/d h:i:s", $now);
哈哈~是“i”而不是什么m或者M,这里给出大家这个示例只是想让PHP的初学少走一些弯路。
至于M是什么意思,聪明的,你肯定明白的。
显示结果:
now is 2012/05/30 04:54:25
今天、今月、上月、今年的起点与终点及时间戳的代码。
<?php
/**
php时间戳综合应用的例子
date: 2013/2/19
link: http://www.
*/
$t = time();
$t1 = mktime(0,0,0,date(“m”,$t),date(“d”,$t),date(“Y”,$t));
$t2 = mktime(0,0,0,date(“m”,$t),1,date(“Y”,$t));
$t3 = mktime(0,0,0,date(“m”,$t)-1,1,date(“Y”,$t));
$t4 = mktime(0,0,0,1,1,date(“Y”,$t));
$e1 = mktime(23,59,59,date(“m”,$t),date(“d”,$t),date(“Y”,$t));
$e2 = mktime(23,59,59,date(“m”,$t),date(“t”),date(“Y”,$t));
$e3 = mktime(23,59,59,date(“m”,$t)-1,date(“t”,$t3),date(“Y”,$t));
$e4 = mktime(23,59,59,12,31,date(“Y”,$t));
//测试
echo date(“当前 Y-m-d H:i:s”,$t).” $t<br>”;
echo date(“今天起点 Y-m-d H:i:s”,$t1).” $t1<br>”;
echo date(“今月起点 Y-m-d H:i:s”,$t2).” $t2<br>”;
echo date(“上月起点 Y-m-d H:i:s”,$t3).” $t3<br>”;
echo date(“今年起点 Y-m-d H:i:s”,$t4).” $t4<br>”;
//测试
echo date(“今天终点 Y-m-d H:i:s”,$e1).” $e1<br>”;
echo date(“今月终点 Y-m-d H:i:s”,$e2).” $e2<br>”;
echo date(“上月终点 Y-m-d H:i:s”,$e3).” $e3<br>”;
echo date(“今年终点 Y-m-d H:i:s”,$e4).” $e4<br>”;
?>
输出结果:
当前 2011-05-24 15:42:55 1306222975
今天起点 2011-05-24 00:00:00 1306166400
今月起点 2011-05-01 00:00:00 1304179200
上月起点 2011-04-01 00:00:00 1301587200
今年起点 2011-01-01 00:00:00 1293811200
今天终点 2011-05-24 23:59:59 1306252799
今月终点 2011-05-31 23:59:59 1306857599
上月终点 2011-04-30 23:59:59 1304179199
今年终点 2011-12-31 23:59:59 1325347199