通过GoogChart.class.php类 ,实现饼形图显示统计;
显示效果如图:
首先,创建数据库,代码如下:
--
-- 表的结构 `ed_engines`
--
CREATE TABLE IF NOT EXISTS `ed_engines` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`type` char(10) DEFAULT NULL,
`name` char(10) DEFAULT NULL,
`counts` int(10) DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=10 ;
--
-- 转存表中的数据 `ed_engines`
--
INSERT INTO `ed_engines` (`id`, `type`, `name`, `counts`) VALUES
(1, 'baidu', '百度', 10),
(2, 'soso', '搜搜', 0),
(3, 'sogou', '搜狗', 0),
(4, 'so', '360搜索', 0),
(5, 'google', '谷歌', 0),
(6, 'bing', '必应', 0),
(7, 'yahoo', '雅虎', 0),
(8, 'youdao', '有道', 0),
(9, 'direct', '直接访问', 0);
在网站主入口文件插入以下代码:class BaseAction extends Action {<br>
public function _initialize() {<br>
<br>
//访客来源判断<br>
$refer = $_SERVER["HTTP_REFERER"];//获取上一页面地址<br>
if($refer){<br>
$refer_string = parse_url($refer, PHP_URL_HOST);//<br>
$arr=array("baidu","soso","sogou","so","google","bing","yahoo","youdao");//常用搜索引擎,注意"soso","sogou","so"的顺序<br>
foreach($arr as $re){<br>
if(substr_count($refer_string,$re)>=1){<br>
$map['type'] = $re;<br>
break;<br>
}<br>
}<br>
M("engines")->where($map)->setInc('counts',1); <br>
}else{<br>
$map['type'] = 'direct';<br>
M("engines")->where($map)->setInc('counts',1);<br>
}<br>
}
在后台管理页面中插入以下代码,显示饼形图统计效果:public function main()<br>
{<br>
/*引入GoogChart类*/<br>
import("ORG.Util.GoogChart");<br>
$chart = new GoogChart();<br>
$engine=M("engines")->field('type,name,counts')->select();<br>
<br>
//设置$data数组数据;<br>
foreach($engine as $k=>$value){ <br>
$data[$value[name].",".$value[counts]."次访问"]=$value[counts];<br>
}<br>
<br>
//设置颜色值 <br>
$color = array(<br>
'#0181ec'<br>
);<br>
$chart->setChartAttrs( array(<br>
'type' => 'pie',<br>
'title' => '搜索引擎来源统计',<br>
'data' => $data,<br>
'size' => array( 550, 300 ),<br>
'color' => $color<br>
));<br>
$this->assign('chart',$chart); <br>
//设置$chart,通过页面模版中调用{$chart},显示统计图;<br>
<br>
}<br>
/**来源统计结束**/
附件,附上GoogChart.class.php类和example.php
googchart_0_1.zip ( 3.12 KB 下载:138 次 )
AD:真正免费,域名+虚机+企业邮箱=0元