上图为pchart 绘出,本人之所以写这个文章,是希望能有更多的user来使用pchart,这样pchart也会出更多的代码!
绘图的数据时直接从公司的wiki网站的mysql上抓的(因为网站也是我自己架设的,所以可以直接抓到相关的数据)!
wiki的mysql中数据比较的乱,有很多的html标签,如果只是使用一次是没有什么问题的,但是如果经常使用这些数据绘制不同的图标,就会自己都觉得很烦!
本人做法:现将wiki的mysql 数据处理并导入到新的MySQL中,然后在利用新的mysql和pchart来画图!
代码如下,有问题的可以留言!
处理wiki 并导入新的mysql代码:
<?php $did = 804; //sa grid $wyear = 2014; $connect=new mysqli("localhost","pchart","????","pchartdb"); if(mysqli_connect_errno()){ echo "error:".mysqli_connect_error(); $connect=null; exit; } $mysqli=new mysqli("localhost","wiki","????","wiki"); if(mysqli_connect_errno()){ echo "error:".mysqli_connect_error(); $mysqli=null; exit; } $thisyear=date('Y'); $thismonth=date("m"); $thismonth=$thismonth; $sql_report = "select title,content from wiki_doc where did='$did'"; $result=$mysqli->query($sql_report); $sql_result = $result->num_rows; #print_r($sql_result); for($i=0;$i<=$sql_result;$i++){ $rows=$result->fetch_assoc(); #$content = strip_tags($rows['content']); #$content = trim($content); #str_replace(' ','=',$content); // must use this way ,cat not use strip_tags $content = $rows['content']; $pattern = "/<[^<]*>/"; $replacement="="; $content1 = preg_replace($pattern,$replacement,$content); $pattern1 = "/=+/"; $replacement1 = ":"; $content2 = preg_replace($pattern1,$replacement1,$content1); $content2 = preg_replace('/ /','',$content2); //=================================================================== $grid_table = split(':',$content2); $i = 6; while($grid_table[$i] > 0){ echo "$grid_table[$i]\n"; $check_result = 0; $grid_job = $grid_table[$i]; $month = ($i - 3)/3; $select_check = "select month from pchart_sa_grid where month='$month' and year='$wyear'"; $check_result=$connect->query($select_check); $check_result_value = $check_result->num_rows; echo "month:$month wyear:$wyear check_result_value:$check_result_value\n"; echo "check_result:$check_result\n"; if (!$check_result_value){ echo "Insert\n"; $insert = "insert into pchart_sa_grid(year,month,gridjob) values('$wyear','$month','$grid_job')"; $insert_result=$connect->query($insert); echo "insert_result:$insert_result\n"; if($insert_result == 0){ echo "Error: Insert action failed ,critical<br>"; } else{ echo "Info:pchartdb table insert successfully<br>"; } } else{ echo "update\n"; $update = "update pchart_sa_grid set month='$month',gridjob='$grid_job' where month='$month' and year='$wyear'"; $update_result=$connect->query($update); if($update_result == 0){ echo "Error: Update action failed ,critical<br>"; } else{ echo "Info:pchartdb table update successfully<br>"; } } $i = $i+3; #$check_result_value = $check_result->num_rows; #echo "check_result_value:$check_result_value <br>"; } #print_r($grid_table); }
pchart 画图代码:
<?php system("php /wiki/php/update_sa_grid.php > /dev/null"); $grid = array(); $connect=new mysqli("localhost","pchart","12345678","pchartdb"); if(mysqli_connect_errno()){ echo "error:".mysqli_connect_error(); $connect=null; exit; } $sql = "select * from pchart_sa_grid"; $result = $connect->query($sql); $sql_result = $result->num_rows; for($i=0;$i< $sql_result;$i++){ $rows=$result->fetch_assoc(); $gridnumber = $rows['gridjob']; $grid_year = $rows['year']; $grid_month = $rows['month']; $grid_time = mktime(0,0,0,$grid_month,1,$grid_year); #$grid_time = $grid_year."/".$grid_month; $grid["$grid_time"] = $gridnumber; //print_r($rows); } //print_r($grid); // dogrid 有两个功能 // 1. 将grid 数组进行排序,按照key的时间先后排序 // 2. 取出最新的12个月的数据 dogrid($grid); // print_r($grid); function dogrid(&$grid){ $key_arr = array(); $key_arr_new = array(); $gridtime = array(); $gridnumber = array(); $pic = array(); if(!is_array($grid)){ echo "Error001 grid is not a arrary\n"; exit; } //数据的key 提取出来,是时间 foreach ($grid as $key => $value){ array_push($key_arr,$key); } // 将提取出来的时间进行排序,目的是取出最新的12个月的数据 arsort($key_arr); reset($key_arr); $arr_number = count($key_arr); if($arr_number > 12){ for($i=0;$i<12;$i++){ $key_new = array_shift($key_arr); array_push($key_arr_new,$key_new); } } else{ for($i=0;$i < $arr_number;$i++){ $key_new = array_shift($key_arr); array_push($key_arr_new,$key_new); } } //将提取出来的12个月数据,重新反向排序,目的是输出的时候能够,先输出最旧的月份 sort($key_arr_new); reset($key_arr_new); $arr_number_new = count($key_arr_new); // 将月份 x坐标,和 grid job y坐标分别赋值给画图的数组 for($i=0;$i<$arr_number_new;$i++){ array_push($gridtime,date('Y/m',$key_arr_new[$i])); array_push($gridnumber,$grid["$key_arr_new[$i]"]); } // 因为返回值,只能是一个,所以定义一个数据将其返回! 因为用的指针引用,所以最终的grid 数组里面的值是会变得 $grid = array(); $grid['x'] = $gridtime; $grid['y'] = $gridnumber; return $grid; } $gridnumber = $grid['y']; $gridtime = $grid['x']; // Standard inclusions include("pChart/pData.class"); include("pChart/pChart.class"); // Dataset definition $DataSet = new pData; $DataSet->AddPoint($gridnumber,"Serie1"); // $DataSet->AddPoint(array(23,432,43,153,234),"Serie2"); $DataSet->AddPoint($gridtime,"Serie3"); $DataSet->AddSerie("Serie1"); //$DataSet->AddSerie("Serie2"); $DataSet->SetAbsciseLabelSerie("Serie3"); $DataSet->SetSerieName("Grid Job Number","Serie1"); //$DataSet->SetSerieName("Outgoing","Serie2"); $DataSet->SetYAxisName("Grid Job Number"); $DataSet->SetYAxisFormat("metric"); // $DataSet->SetXAxisFormat("date"); // Initialise the graph $Test = new pChart(700,230); $Test->setFontProperties("Fonts/tahoma.ttf",8); $Test->setGraphArea(85,30,650,200); $Test->drawFilledRoundedRectangle(7,7,693,223,5,240,240,240); $Test->drawRoundedRectangle(5,5,695,225,5,230,230,230); $Test->drawGraphArea(255,255,255,TRUE); $Test->drawScale($DataSet->GetData(),$DataSet->GetDataDescription(),SCALE_NORMAL,150,150,150,TRUE,0,2); $Test->drawGrid(4,TRUE,230,230,230,50); // Draw the 0 line $Test->setFontProperties("Fonts/tahoma.ttf",6); $Test->drawTreshold(0,143,55,72,TRUE,TRUE); // Draw the line graph $Test->drawLineGraph($DataSet->GetData(),$DataSet->GetDataDescription()); $Test->drawPlotGraph($DataSet->GetData(),$DataSet->GetDataDescription(),3,2,255,255,255); // Finish the graph $Test->setFontProperties("Fonts/tahoma.ttf",8); $Test->drawLegend(90,35,$DataSet->GetDataDescription(),255,255,255); $Test->setFontProperties("Fonts/tahoma.ttf",10); $Test->drawTitle(60,22,"Grid Job Number",50,50,50,585); $Test->Render("grid.png"); ?>