效果
view javascript代码
<script type= "text/javascript" src= "__PUBLIC__/static/swfobject.js"></script>
<script type= "text/javascript">
/*var flashvars = {"data-file":"__APP__/Count/ajaxData/?date=7&begintime=&endtime=&fid=&clientid=&clientgame=&__hash__=26a721da5822ea11b7d2b88fffda1952_91d1f04a4c5b515fb45685df309c3d17"}; //这里是数据源
var params = {menu: "false",scale: "noScale",wmode:"transparent"};
swfobject.embedSWF("__PUBLIC__/static/open-flash-chart.swf", "chart", "100%", "380px", "9.0.0","__PUBLIC__/static/js/expressInstall.swf", flashvars,params);*/
function reLoadLine()
{
var form_id = $( "form[name='search']");
var aj_url = "__APP__/Count/ajaxData/?"+ form_id.serialize();
var params = {menu: "false",scale: "noScale",wmode: "transparent"};
aj_url = encodeURIComponent(aj_url);
var flashvars ={ "data-file": aj_url}; //这里是数据源
swfobject.embedSWF( "__PUBLIC__/static/open-flash-chart.swf?r="+Math.random() * 100000, 'chart', "98%", "380px", "9.0.0", "__PUBLIC__/static/js/expressInstall.swf",flashvars,params);
}
$('#ajaxButton').click( function(){
var dt=$( "input:radio[name='date']:checked").val();
if(dt==1){
//时间
var begintime = $('#count_begintime').val();
var endtime = $('#count_endtime').val();
if( begintime=='' || endtime == ''){
//$("div>.ckbox").removeClass("unchecked").removeClass("checked").addClass("unchecked");
alert( "发布时段不能为空,请先选择查看时段!");
return false;
} else{
//验证发布日期是否正确
var startDate = Date.parse(begintime.replace(/-/g, "/"));
var endDate = Date.parse(endtime.replace(/-/g, "/"))
var cha = startDate - endDate;
var cha2 = (endDate - startDate)/(1000*60*60*24);
if (cha >= 0) {
alert( "开始时间不能大于等于结束时间,请重新选择查看时段!");
return false;
}
if(cha2 > 30){
alert( "两个时间差不能大于30天,请重新选择查看时段!");
return false;
}
}
}
reLoadLine();
});
$(document).ready( function(){
$( "input[name='date']").click( function(){
var dt=$( "input:radio[name='date']:checked").val();
if(dt==1){
$( "#count_begintime,#count_endtime").addClass('required');
$( ".unit").show();
} else{
$( "#count_begintime,#count_endtime").removeClass('required');
$( ".unit").hide();
}
});
setTimeout(reLoadLine,100);
});
</script>
view 加载 swf代码 这个不能少
< div id ="chart" > </ div >
控制器代码
<?php
// 本类由系统自动生成,仅供测试用途
class CountAction extends Action {
function _initialize() {
//导入ofc库
import ( '@.ORG.Tool.Ofclibrary.Openflashchart');
}
public function index(){
import( "@.Action.SortlistAction");
$list = new SortlistAction(); // 实例化SortlistAction
$ClientData = $list->getClientData();
$ this->assign( 'client', $ClientData );
$ this->display();
}
//根据条件生成图表数据
public function ajaxData(){
$date = !empty($_GET['date'])?$ this->_get('date'):30;
switch($date)
{
case 7:
$ this->endDate = date( "Y-m-d", strtotime('-1 day'));
$ this->startDate = date( "Y-m-d", strtotime('-7 day'));
break;
case 30:
$ this->endDate = date( "Y-m-d", strtotime('-1 day'));
$ this->startDate = date( "Y-m-d", strtotime('-30 day'));
break;
case 1:
$ this->endDate = date( "Y-m-d",strtotime($_GET['count_endtime']));
$ this->startDate = date( "Y-m-d",strtotime($_GET['count_begintime']));
break;
}
$data = $ this->aGetFadeData($ this->startDate, $ this->endDate, $_GET['gametype'], $_GET['clientid'], $_GET['clientgame']);
//获取line和max
$max = 0;
$lines = array();
$lines= array(
'values' => $data,
'color' => "#336699",
);
$max = $max = max($data);
$lables = array_keys($data);
echo $ this->lines($lines, $lables, $max);
exit;
}
/**
* 创造直线图
* @param arrLinesParams = array (
* 'linename' = array('values', 'colour'),
* ....
* ) : 此为一个图表中所要展示的几条线,其中键linename为此线的名称,values:为线上各关键节点组成的数组
* colour:线条颜色,没有默认值请填写如:#008B8B
* @param $arrXLabels : X轴的坐标,按照所选日期取值
* @param $intYmax : 几组直线节点数值中的最大值,确定Y轴所用
* @return JSON str
*/
public function lines( $arrLinesParams, $arrXLabels, $intYmax)
{
$line = new line();
$data = array();
// return $arrLinesParams['values'];
foreach ($arrLinesParams['values'] as $k=>$v){
if($v== null)
$v=0;
$data[] = intval($v);
}
//return $data;
$line-> set_values($data);
$line-> set_colour($arrLinesParams['color']);
$line-> set_width(2);
$line->set_key('图标统计');
$dot = new solid_dot();
$dot-> size(3)
-> halo_size(1)
-> colour($arrLinesParams['colour'])
-> tooltip('#val#');
$line-> set_default_dot_style($dot);
$ this->arrLine[] = $line;
//return $arrXLabels;
//x为时间
foreach($arrXLabels as &$item)
{
$item = substr($item,0);
}
// return $this->arrLine;
$jsonData = $ this->getJsonData($line, $arrXLabels, $intYmax);
return $jsonData;
}
/**
* 利用open_flash_chart创造多条直线
* @return jsondata
*/
protected function getJsonData($arrLine, $arrXLabels, $intYmax){
$chart = new open_flash_chart();
$title = $ this->set_Title();
$chart->set_title( $title );
$t = $ this->set_Tooltip();
$chart-> set_tooltip($t);
$chart-> set_bg_colour('#FFFFFF');
$x_Axis = $ this->setX_Axis($arrXLabels);
$y_Axis = $ this->setY_Axis($intYmax);
$chart->set_x_axis($x_Axis);
$chart->set_y_axis($y_Axis);
$chart->add_element($arrLine);
$jsonData = $chart->toPrettyString();
return $jsonData;
}
/**
* 设置标题
*/
protected function set_Title(){
$title = new title( "图标任务数"); //标题
$title->set_style( "{font-size: 20px; color: #000000; text-align: center;font-weight:bold;}" ); //标题样式
return $title;
}
/**
* 设置x轴
*/
protected function setX_Axis($arrLabels) {
$x_labels = $ this->setX_labels($arrLabels);
/*$count = count($arrLabels);
if($count > 10)
{
$x_labels->visible_steps(intval($count / 7));
}*/
$x_labels->visible_steps(1);
$x = new x_axis();
$x->colour('#C0C0C0')
->grid_colour('#E0E0E0')
->set_labels($x_labels);
return $x;
}
/**
* 设置Y轴
*/
protected function setY_Axis($intYmax) {
$y_labels = new x_axis_labels();
$y_labels ->set_colour('#696969');
if($intYmax < 10) $intYmax=10;
//$intYStep = $intYmax / 10;
$mod = 0;
if($intYmax%5){
$mod = $intYmax%5;
}
$intYmax = $intYmax + 5 - $mod;
$intYStep = 5;
$y = new y_axis();
$y->set_colour('#C0C0C0');
$y->set_grid_colour('#E0E0E0');
$y->set_range(0, $intYmax, $intYStep);
$y->set_labels($y_labels);
return $y;
}
/**
* 设置x轴的坐标刻度
*/
protected function setX_labels($arrLabels = array()) {
$x = new x_axis_labels();
$x->set_colour('#696969');
$x->set_vertical(330);
$x->set_steps(1);
$x->set_labels($arrLabels);
return $x;
}
/**
* 设置x轴刻印文字
*/
protected function setX_Legend() {
$x = new x_legend('');
$x->set_style( '{font-size: 20px; color: #778877}' );
return $x;
}
/**
* 设置y轴刻印文字
*/
protected function setY_Legend() {
$y = new y_legend('');
$y->set_style( '{font-size: 20px; color: #778877}' );
return $y;
}
/**
* 设置tooltip,值框属性
* @return Object t
*/
protected function set_Tooltip() {
$t = new tooltip();
$t->set_shadow( false );
$t->set_stroke( 1 );
$t->set_colour( '#6E604F' );
$t->set_background_colour( '#BDB396' );
$t->set_title_style( "{font-size: 14px; color: #CC2A43;}" );
$t->set_body_style( "{font-size: 10px; font-weight: bold; color: #9932CC;}" );
return $t;
}
/**
* 根据分类查询务id列表
*/
public function getLid($startDate, $endDate,$cat=''){
$Sortinfo = M( "sortinfo");
//根据任务查lid
$where = "endtime <= '" . $startDate . "' and '" . $endDate . "' <= endtime";
if(!empty($cat)){
$where = " gametype = '".$cat. "'";
$arr_lid = $Sortinfo->field('lid')->where($where)->select();
} else{
$arr_lid = $Sortinfo->field('lid')->select();
}
$data = array();
foreach ($arr_lid as $val){
$data[] = $val['lid'];
}
$data = array_unique($data); //去重复的id
$lidlist = implode(',', $data);
return $lidlist;
}
/**
* 根据条件统计任务数
*/
public function getCount($startDate, $endDate,$date, $cat='', $clientid='',$clientgame=''){
$lidlist = $ this->getLid($startDate, $endDate,$cat);
$Sortlist = D ( "Sortlist" );
$time = $date . ' 23:59:59';
//$where = " ( level = 4 and endtime <= '" . $date . "' and endtime <= '" . $endDate . "') or ( level = 7 and endtime <= '" . $date . "' and endtime <= '" . $endDate . "')";
$where = " (( level = 4 and begintime <= '" . $time . "' and '" . $date . "' <= endtime) or ( level = 7 and begintime <= '" . $time . "' and '" . $date . "' <= stoptime))";
if(!empty($clientid)){
$where .= " and clientid = " . $clientid;
}
if(!empty($clientgame)){
$where .= " and clientgame = '" . $clientgame . "' ";
}
$where .= " and id in (" . $lidlist . ")";
$count = $Sortlist->where($where)->count('id');
//echo $Sortlist->getLastSql();exit;
return $count;
}
/**
* 获取数据
*/
public function aGetFadeData($startDate, $endDate, $cat, $clientid,$clientgame)
{
$ret = array();
$dates = $ this->aGetCdateKeys($startDate, $endDate);
/*$Count = M("Gis_count");
$where = " dtime >= '" . $startDate . "' and dtime <= '".$endDate."'";
if(!empty($cat)){
$where .= ' and gametype = "' . $cat . '" ';
}
if(!empty($clientid)){
$where .= ' and clientid= '.$clientid;
}
if(!empty($clientgame)){
$where .= ' and clientgame = "' . $cat . '" ';
}
$list = $Count->field('dtime,count')->where($where)->select();
$date = array();
foreach ($list as $k=>$val){
$date[$val['dtime']] = $val['count'];
}*/
$i =1 ;
foreach ($dates as $val){
$ret[$val]= $ this->getCount($startDate, $endDate,$val, $cat, $clientid,$clientgame);
$i++;
if( $i > 30) break;
}
return $ret;
}
/**
* 获取from到to之间的所有时间
*/
private function aGetCdateKeys($from, $to)
{
$ret = array();
$endTime = strtotime($to);
$startTime = strtotime($from);
$time = $startTime;
while($time <= $endTime)
{
//入ret
$ret[] = date( "Y-m-d", $time);
//递增
$time = strtotime( "+1 day", $time);
}
return $ret;
}
}
本文出自 “烛光照亮天堂-IT技术之路” 博客,转载请与作者联系!