= 1024 && ($i < $max)) { $value /= 1024; $i++; } if ($title == "suffix") { $tmp = $i; $res = $suffix[$tmp]; } elseif ($title == "count") { $res = $i; } else { $res = round($value, $decimals); } return $res; } function formatScale($value, $max, $decimal) { $res = array(); foreach($value as $item) { $tmp = humanReadable($item, false, $max, $decimal); array_push($res, $tmp); } return $res; } if ($imgtype == "historical") { $i = "0"; foreach (dbFetchRows("SELECT * FROM `bill_history` WHERE `bill_id` = ? ORDER BY `bill_datefrom` LIMIT 12", array($bill_id)) as $data) { $datefrom = strftime("%e %b %Y", strtotime($data['bill_datefrom'])); $dateto = strftime("%e %b %Y", strtotime($data['bill_dateto'])); $datelabel = $datefrom."\n".$dateto; $traf['in'] = round(substr(formatStorage($data['traf_in'] * 1024 * 1024), 0, -2)); $traf['out'] = round(substr(formatStorage($data['traf_out'] * 1024 * 1024), 0, -2)); $traf['total'] = round(substr(formatStorage($data['traf_total'] * 1024 * 1024), 0, -2)); if ($data['bill_type'] == "Quota") { $traf['allowed']= round(substr(formatStorage($data['bill_allowed'] * 1024 * 1024), 0, -2)); $traf['overuse']= round(substr(formatStorage($data['bill_overuse'] * 1024 * 1024), 0, -2)); } else { $traf['allowed']= "0"; $traf['overuse']= "0"; } array_push($ticklabels, $datelabel); array_push($in_data, $traf['in']); array_push($out_data, $traf['out']); array_push($tot_data, $traf['total']); array_push($allow_data, $traf['allowed']); array_push($overuse_data, $traf['overuse']); $i++; //print_r($data); } if ($i < 12) { $y = 12 - $i; for ($x=0;$x<$y;$x++) { $allowed = (($x == "0") ? $traf['allowed'] : "0" ); array_push($in_data, "0"); array_push($out_data, "0"); array_push($tot_data, "0"); array_push($allow_data, $allowed); array_push($overuse_data, "0"); array_push($ticklabels, ""); } } $graph_name = "Historical bandwidth over the last 12 billing periods"; } else { $data = array(); $max = 9999999999999999999999999999999999999999999; $min_data = array("in"=>$max, "out"=>$max, "tot"=>$max); $average = 0; if ($imgtype == "day") { foreach(dbFetch("SELECT DISTINCT UNIX_TIMESTAMP(timestamp) as timestamp, SUM(delta) as traf_total, SUM(in_delta) as traf_in, SUM(out_delta) as traf_out FROM bill_data WHERE `bill_id` = ? AND `timestamp` >= FROM_UNIXTIME(?) AND `timestamp` <= FROM_UNIXTIME(?) GROUP BY DATE(timestamp) ORDER BY timestamp ASC", array($bill_id, $start, $end)) as $data) { $traf['in'] = (isset($data['traf_in']) ? $data['traf_in'] : 0); $traf['out'] = (isset($data['traf_out']) ? $data['traf_out'] : 0); $traf['total'] = (isset($data['traf_total']) ? $data['traf_total'] : 0); $datelabel = strftime("%e\n%b", $data['timestamp']); array_push($ticklabels, $datelabel); array_push($in_data, $traf['in']); array_push($out_data, $traf['out']); array_push($tot_data, $traf['total']); // $min_data['in'] = (($traf['in'] < $min_data['in']) ? $traf['in'] : $min_data['in']); // $min_data['out']= (($traf['out'] < $min_data['out']) ? $traf['out'] : $min_data['out']); // $min_data['tot']= (($traf['total'] < $min_data['tot']) ? $traf['total'] : $min_data['tot']); $average += $data['traf_total']; } $min_data['in'] = min($in_data); $min_data['out'] = min($out_data); $min_data['tot'] = min($tot_data); $ave_count = count($tot_data); if ($imgbill != false) { $days = strftime("%e", date($end - $start)) - $ave_count - 1; for ($x=0;$x<$days;$x++) { array_push($ticklabels, ""); array_push($in_data, 0); array_push($out_data, 0); array_push($tot_data, 0); } } } elseif ($imgtype == "hour") { foreach(dbFetch("SELECT DISTINCT UNIX_TIMESTAMP(timestamp) as timestamp, SUM(delta) as traf_total, SUM(in_delta) as traf_in, SUM(out_delta) as traf_out FROM bill_data WHERE `bill_id` = ? AND `timestamp` >= FROM_UNIXTIME(?) AND `timestamp` <= FROM_UNIXTIME(?) GROUP BY HOUR(timestamp) ORDER BY timestamp ASC", array($bill_id, $start, $end)) as $data) { $traf['in'] = (isset($data['traf_in']) ? $data['traf_in'] : 0); $traf['out'] = (isset($data['traf_out']) ? $data['traf_out'] : 0); $traf['total'] = (isset($data['traf_total']) ? $data['traf_total'] : 0); $datelabel = strftime("%H:%M", $data['timestamp']); array_push($ticklabels, $datelabel); array_push($in_data, $traf['in']); array_push($out_data, $traf['out']); array_push($tot_data, $traf['total']); // $min_data['in'] = (($traf['in'] < $min_data['in']) ? $traf['in'] : $min_data['in']); // $min_data['out']= (($traf['out'] < $min_data['out']) ? $traf['out'] : $min_data['out']); // $min_data['tot']= (($traf['tot'] < $min_data['tot']) ? $traf['tot'] : $min_data['tot']); $average += $data['traf_total']; } $min_data['in'] = min($in_data); $min_data['out'] = min($out_data); $min_data['tot'] = min($tot_data); $ave_count = count($tot_data); } $decimal = 0; $max = min($min_data); $yaxistitle = humanReadable($max, "suffix"); $max = humanReadable($max, "count"); $in_data = formatScale($in_data, $max, $decimal); $out_data = formatScale($out_data, $max, $decimal); $tot_data = formatScale($tot_data, $max, $decimal); $average = humanReadable(($average / $ave_count), false, $max, $decimal); for ($x=0;$x<=count($tot_data);$x++) { array_push($ave_data, $average); } $graph_name = date('M j g:ia', $start)." - ".date('M j g:ia', $end); } // Create the graph. These two calls are always required $graph = new Graph($xsize, $ysize, $graph_name); $graph->img->SetImgFormat("png"); $graph->SetScale("textlin"); $graph->title->Set("$graph_name"); $graph->title->SetFont(FF_FONT2, FS_BOLD, 10); $graph->SetMarginColor("white"); $graph->SetFrame(false); $graph->SetMargin("75", "30", "30", "65"); $graph->legend->SetFont(FF_FONT1, FS_NORMAL); $graph->legend->SetLayout(LEGEND_HOR); $graph->legend->Pos("0.52", "0.91", "center"); $graph->xaxis->SetFont(FF_FONT1, FS_BOLD); $graph->xaxis->SetPos('min'); $graph->xaxis->SetTitleMargin(30); $graph->xaxis->SetTickLabels($ticklabels); $graph->xgrid->Show(true,true); $graph->xgrid->SetColor('#e0e0e0','#efefef'); $graph->yaxis->SetFont(FF_FONT1); $graph->yaxis->SetTitleMargin(50); $graph->yaxis->title->SetFont(FF_FONT1, FS_NORMAL, 10); $graph->yaxis->title->Set($yaxistitle); $graph->ygrid->SetFill(true,'#EFEFEF@0.5','#FFFFFF@0.5'); // Create the bar plots $barplot_tot = new BarPlot($tot_data); $barplot_tot->SetLegend("Traffic total"); $barplot_tot->SetColor("#d5d5d5"); $barplot_tot->SetFillColor("#d5d5d5@0.5"); $barplot_in = new BarPlot($in_data); $barplot_in->SetLegend("Traffic In"); $barplot_in->SetColor('darkgreen'); $barplot_in->SetFillColor('lightgreen@0.4'); $barplot_in->SetWeight(1); $barplot_out = new BarPlot($out_data); $barplot_out->SetLegend("Traffic Out"); $barplot_out->SetColor('darkblue'); $barplot_out->SetFillColor('lightblue@0.4'); $barplot_out->SetWeight(1); if ($imgtype == "historical") { $barplot_over = new BarPlot($overuse_data); $barplot_over->SetLegend("Traffic Overusage"); $barplot_over->SetColor('darkred'); $barplot_over->SetFillColor('lightred@0.4'); $barplot_over->SetWeight(1); $lineplot_allow = new LinePlot($allow_data); $lineplot_allow->SetLegend("Traffic Allowed"); $lineplot_allow->SetColor('black'); $lineplot_allow->SetWeight(1); $gbplot = new GroupBarPlot(array($barplot_in, $barplot_out, $barplot_tot, $barplot_over)); } else { $lineplot_allow = new LinePlot($ave_data); $lineplot_allow->SetLegend("Average per day"); $lineplot_allow->SetColor('black'); $lineplot_allow->SetWeight(1); $gbplot = new GroupBarPlot(array($barplot_in, $barplot_out, $barplot_tot)); } $graph->Add($gbplot); $graph->Add($lineplot_allow); // Display the graph $graph->Stroke(); ?>