图表应用

function db_create_login_line_xml($startDate, $endDate, $path, $beginRow = "", $endRow = "") {
global $languagesArray;
$xml = new DOMDocument('1.0','utf-8');
$xml->formatOutput = true;
$chart = $xml->createElement('graph');
$caption=$xml->createAttribute('caption');
$Vcaption=$xml->createTextNode('各服务器在线率');
$caption->appendChild($Vcaption);
$subCaption=$xml->createAttribute('subCaption');
$VsubCaption=$xml->createTextNode($startDate." -> ".$endDate);
$subCaption->appendChild($VsubCaption);
$xAxisName=$xml->createAttribute('xAxisName');
$VxAxisName=$xml->createTextNode('服务器名');
$xAxisName->appendChild($VxAxisName);
$yAxisName=$xml->createAttribute('yAxisName');
$VyAxisName=$xml->createTextNode('在线百分比');
$yAxisName->appendChild($VyAxisName);
$useRoundEdges=$xml->createAttribute('useRoundEdges');
$VuseRoundEdges=$xml->createTextNode('1');
$useRoundEdges->appendChild($VuseRoundEdges);
$bgColor=$xml->createAttribute('bgColor');
$VbgColor=$xml->createTextNode('FFFFFF,FFFFFF');
$bgColor->appendChild($VbgColor);
$showBorder=$xml->createAttribute('showBorder');
$VshowBorder=$xml->createTextNode('1');
$showBorder->appendChild($VshowBorder);
$formatNumberScale=$xml->createAttribute('formatNumberScale');
$VformatNumberScale=$xml->createTextNode('0');
$formatNumberScale->appendChild($VformatNumberScale);
$exportEnabled=$xml->createAttribute('exportEnabled');
$VexportEnabled=$xml->createTextNode('1');
$exportEnabled->appendChild($VexportEnabled);
$exportAction=$xml->createAttribute('exportAction');
$VexportAction=$xml->createTextNode('Download');
$exportAction->appendChild($VexportAction);
$exportHandler=$xml->createAttribute('exportHandler');
$VexportHandler=$xml->createTextNode('Includes/ExportHandler/FCExporter.php');
$exportHandler->appendChild($VexportHandler);

//图表内样式
$baseFont=$xml->createAttribute('baseFont');
$VbaseFont=$xml->createTextNode('');
$baseFont->appendChild($VbaseFont);
$baseFontSize=$xml->createAttribute('baseFontSize');
$VbaseFontSize=$xml->createTextNode('10');
$baseFontSize->appendChild($VbaseFontSize);
$baseFontColor=$xml->createAttribute('baseFontColor');
$VbaseFontColor=$xml->createTextNode('');
$baseFontColor->appendChild($VbaseFontColor);
//数字样式
$decimals=$xml->createAttribute('decimals');
$Vdecimals=$xml->createTextNode('2');
$decimals->appendChild($Vdecimals);
//百分号
$numberSuffix=$xml->createAttribute('numberSuffix');
$VnumberSuffix=$xml->createTextNode('%25');
$numberSuffix->appendChild($VnumberSuffix);
//图表外样式
$outCnvBaseFont=$xml->createAttribute('outCnvBaseFont');
$VoutCnvBaseFont=$xml->createTextNode('');
$outCnvBaseFont->appendChild($VoutCnvBaseFont);
$outCnvBaseFontSize=$xml->createAttribute('outCnvBaseFontSize');
$VoutCnvBaseFontSize=$xml->createTextNode('12');
$outCnvBaseFontSize->appendChild($VoutCnvBaseFontSize);
$outCnvBaseFontColor=$xml->createAttribute('outCnvBaseFontColor');
$VoutCnvBaseFontColor=$xml->createTextNode('');
$outCnvBaseFontColor->appendChild($VoutCnvBaseFontColor);

//生成图片
$exportDialogMessage=$xml->createAttribute('exportDialogMessage');
$VexportDialogMessage=$xml->createTextNode('图片君很快就出来了。。。。');
$exportDialogMessage->appendChild($VexportDialogMessage);
//右键菜单
$exportFormats=$xml->createAttribute('exportFormats');
$VexportFormats=$xml->createTextNode('JPG=保存为 JPG 图片|PDF=保存为 PDF 文件|PNG=保存为 PNG 图片');
$exportFormats->appendChild($VexportFormats);

$chart->appendChild($exportDialogMessage);
$chart->appendChild($exportFormats);

$chart->appendChild($decimals);
$chart->appendChild($numberSuffix);

$chart->appendChild($baseFont);
$chart->appendChild($baseFontSize);
$chart->appendChild($baseFontColor);
$chart->appendChild($outCnvBaseFont);
$chart->appendChild($outCnvBaseFontSize);
$chart->appendChild($outCnvBaseFontColor);
$chart->appendChild($caption);
$chart->appendChild($subCaption);
$chart->appendChild($xAxisName);
$chart->appendChild($yAxisName);
$chart->appendChild($useRoundEdges);
$chart->appendChild($bgColor);
$chart->appendChild($showBorder);
$chart->appendChild($formatNumberScale);
$chart->appendChild($exportEnabled);
$chart->appendChild($exportAction);
$chart->appendChild($exportHandler);


//获取每个服务器上的可用用户数
global $bidb;
$s = "SELECT sername,working FROM `serverauth` order by sername asc";
$q = $bidb->query($s);
if (DB::iserror($q)) {die($q->getMessage());}
while ($row = $q->fetchRow(DB_FETCHMODE_ASSOC)){
$ser[$row['sername']]=$row['working'];
}

global $loginlogdb;
$startDateArray = explode('-',$startDate);
$endDateArray = explode('-',$endDate);
//默认今天输入的时间从0点开始 到24点结束;
$startDate =$startDate;
$endDate =$endDate;

$startYear = $startDateArray[0];
$startMonth = $startDateArray[1];
$startDay = $startDateArray[2];
$endYear = $endDateArray[0];
$endMonth = $endDateArray[1];
$endDay = $endDateArray[2];

$attachDBs = array();
$showtablesql = 'show tables;'; //显示所有表

$rs = $loginlogdb->query($showtablesql);
//var_dump($rs);
if (DB::iserror($rs)) {die($rs->getMessage());}

while ($row = $rs->fetchRow(DB_FETCHMODE_ASSOC)) {
$table = $row['Tables_in_loginlog'];
if ((strcasecmp($table, "${startYear}-${startMonth}") < 0 || strcasecmp($table, "${endYear}-${endMonth}") > 0)) {
continue; //如果匹配不到表则跳出循环
}
$attachDBs[] = $table;
}


if (!empty($attachDBs)){
//子项
$selectSQL = "SELECT `uname`,`servname`";
$fromSQL = " FROM ";
$whereSQL = " WHERE 1 ";

$whereSQL1 = " AND `logintime`>='${startDate}'";
$whereSQL2 = " AND `logintime`<='${endDate}'";

$unionAllSQL = NULL;
$rowCountSQL = NULL;

$i = 0;
foreach ($attachDBs as $tableName){
$i++;
$unionAllSQL .= "UNION ALL {$selectSQL} {$fromSQL} `{$tableName}`{$whereSQL}";
if ($i == 1) {
$unionAllSQL .= $whereSQL1;
}
}

$unionAllSQL = substr($unionAllSQL,9);
//var_dump($unionAllSQL);
$unionAllSQL .= $whereSQL2;

$sql="SELECT pro.servname,pro.uname,COUNT(distinct `uname`) AS number FROM ($unionAllSQL) AS pro GROUP BY `servname` ORDER BY `number` DESC limit ".$beginRow.",".$endRow.";";


$q = $loginlogdb->query($sql);
//echo $sql;exit;
if (DB::iserror($q)) {die($q->getMessage());}

$bool = TRUE;//只执行一次 设置y轴最大值
while($row = $q->fetchRow(DB_FETCHMODE_ASSOC)){

extract($row);
foreach($ser as $key=>$val){
if($servname == $key){
$num = $number/$val;
$num = sprintf("%.2f", $num);
$set = $xml->createElement('set');
$label = $xml->createAttribute('label');

$Vlabel = $xml->createTextNode( $servname );
$label->appendChild($Vlabel);
$value = $xml->createAttribute('value');

if ($bool) {
$yAxisMaxValue = $xml->createAttribute('yAxisMaxValue');
$VyAxisMaxValue = $xml->createTextNode($num+0.1);

$yAxisMaxValue->appendChild($VyAxisMaxValue);
$chart->appendChild($yAxisMaxValue);
$bool = FALSE;
}

$Vvalue = $xml->createTextNode($num);
$value->appendChild($Vvalue);
$set->appendChild($label);
$set->appendChild($value);
$chart->appendChild($set);
}
}


}
}
$xml->appendChild($chart);
$xml->save($path);
}

posted @ 2015-08-13 13:12  梁小粽  阅读(168)  评论(0编辑  收藏  举报