/** * Creates a new horizontal bar chart. * * @param integer width of the image * @param integer height of the image */ public function HorizontalBarChart($width = 600, $height = 250) { parent::BarChart($width, $height); $this->emptyToFullRatio = 1 / 5; //$this->plot->setGraphPadding(new Padding(5, 30, 30, 50)); $this->plot->setGraphPadding(new Padding(-20, 5, 30, 60)); }
/** * Creates a new vertical bar chart * * @access public * @param integer width of the image * @param integer height of the image */ function VerticalChart($width = 600, $height = 250) { parent::BarChart($width, $height); $this->setLabelMarginLeft(50); $this->setLabelMarginRight(30); $this->setLabelMarginTop(40); $this->setLabelMarginBottom(50); }
/** * Creates a new horizontal bar chart * * @access public * @param integer width of the image * @param integer height of the image */ function HorizontalChart($width = 600, $height = 250, $title = '') { parent::BarChart($width, $height); $this->setLabelMarginLeft(150); $this->setLabelMarginRight(30); if ($title == 'no_title') { $this->setLabelMarginTop(10); } else { $this->setLabelMarginTop(40); } $this->setLabelMarginBottom(30); }
/** * Creates a new horizontal bar chart. * * @param integer width of the image * @param integer height of the image */ public function revenueBarChart($width = 600, $height = 250) { parent::BarChart($width, $height); $this->emptyToFullRatio = 1 / 5; $this->plot->setGraphPadding(new Padding(5, 30, 30, 50)); }
/** * Render the chart image * * @access public * @param string name of the file to render the image to (optional) */ function render($imgType, $fileName = null) { $this->computeBound(); $this->computeLabelMargin(); $this->createImage(); $this->printLogo(); $this->printTitle(); $this->printAxis(); $this->printLine(); parent::render($imgType, $filename); }
function depth($value = NULL) { return parent::depth($value === NULL ? NULL : 0); }
<?php require '../../lib/BarChart.php'; ?> <h2>No autoscaling</h2> <?php $values1 = array(-10, 0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110); $values2 = array(10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130); $chart = new BarChart('bvg', 500, 200); $chart->setDataFormat(Chart::TEXT); $chart->setAutoscale(Chart::AUTOSCALE_OFF); $data1 = new ChartData($values1); $data1->setAutoscale(false); $chart->addData($data1); $data2 = new ChartData($values2); $data2->setColor('336699'); $data2->setAutoscale(false); $chart->addData($data2); $y_axis = new ChartAxis('y'); $chart->addAxis($y_axis); echo $chart->toHtml(); var_dump($chart->getQuery()); ?> <h2>Chart-wide manual scaling (text encoding)</h2> <?php $chart->setScale(-10, 130); echo $chart->toHtml(); var_dump($chart->getQuery()); ?>
$data[$i] = 0; $labels[$i] = date("M", mktime(0, 0, 0, $i)); $colors[$i] = array('r' => 0xcc, 'g' => 0x33, 'b' => 0x33); } // retrieve total hits $query = sprintf(' SELECT MONTH(ACCESS_TIME) AS ATIME, COUNT(IP_ADDRESS) AS TOTAL FROM %sSITE_ACCESS WHERE DATE(ACCESS_TIME) BETWEEN "%d-01-01" AND "%d-12-31" GROUP BY ATIME ORDER BY ATIME ASC', DB_TBL_PREFIX, $year, $year); $result = mysql_query($query, $GLOBALS['DB']); while ($row = mysql_fetch_assoc($result)) { $data[$row['ATIME']] = $row['TOTAL']; } mysql_free_result($result); } else { die; } } // present bar chart $chart = new BarChart(500, 200); $chart->graphData($data, $colors, $labels); $chart->flushImage();
<?php /** * Created by PhpStorm. * User: martinmatak * Date: 06/03/16 * Time: 18:25 */ namespace graph_library; require_once '../Autoloader.php'; header('Content-Type:image/png'); $platno = new Canvas(); $podaci = new DataCollection(); $barchart = new BarChart('Cijena dionica', 295, 500); $legend = new Legend(); $legend->addItem(new LegendItem('Relativni odnos dionica zadnjih 5 godina')); $podaci->addItems(array(new DataCollectionItem([2011, 10]), new DataCollectionItem([2012, 15]), new DataCollectionItem([2013, 8]), new DataCollectionItem([2014, 19]), new DataCollectionItem([2015, 22]))); $barchart->setLegend($legend, 10, 25); $barchart->colorData(16, 80, 57, $barchart->addData($podaci)); $platno->addChart($barchart, 30, 305); $lineChart = new LineChart('Cijena dionica', 300, 300); $lineChart->setLegend($legend, 10, 30); $lineChart->colorData(16, 80, 57, $lineChart->addData($podaci)); $platno->addChart($lineChart, 2, 0); // pie chart $pieChart = new PieChart('naslov pie charta', 300, 300); $legend = new Legend(); $legend->addItem(new LegendItem('1. item legende')); $legend->addItem(new LegendItem('2. item legende')); $podatak = new DataCollection();
<?php require '../../lib/BarChart.php'; ?> <h2>Chart autoscaling by values (text encoding)</h2> <?php $values1 = array(-10, 0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110); $values2 = array(10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130); $chart = new BarChart('bvg', 500, 200); $chart->setDataFormat(Chart::TEXT); $chart->setAutoscale(Chart::AUTOSCALE_VALUES); $data = new ChartData($values1); $chart->addData($data); $data = new ChartData($values2); $data->setColor('336699'); $chart->addData($data); $y_axis = new ChartAxis('y'); $chart->addAxis($y_axis); echo $chart->toHtml(); var_dump($chart->getQuery()); ?> <h2>Chart autoscaling by values (simple encoding)</h2> <?php $chart->setDataFormat(Chart::SIMPLE_ENCODING); echo $chart->toHtml(); var_dump($chart->getQuery());
function stats() { $this->validateRights([USER_ADMIN]); if (isset($_REQUEST['top_articles'])) { if ($t = $this->db->fetch('SELECT views FROM articles ORDER BY views DESC LIMIT 5')) { $v = [0]; foreach ($t as $a) { $v[] = $a['views']; } unset($v[0]); $this->data['image'] = new GDImage(NULL, new Size(800, 500)); $g = new BarChart(new Boundary(0, 0, $this->data['image']->size()->width, $this->data['image']->size()->height)); $g->depth(15); $g->background(new Color(255, 144, 130, 127)); $g->values(['bgcolors' => [new Color(255, 102, 91), new Color(50, 255, 0), new Color(63, 168, 255), new Color(255, 220, 22), new Color(255, 112, 238)], 'labels' => TRUE, 'thickness' => 3]); $g->data($v); $g->title(['text' => 'Топ 5 публикаций', 'color' => new Color(255, 255, 255), 'font' => new Font(PATH_ROOT . '/css/fonts/OpenSans-Bold.ttf', 14), 'margin' => ['top' => 0, 'bottom' => 15]]); $g->axis(['font_x' => new Font(PATH_ROOT . '/css/fonts/OpenSans-Regular.ttf', 8), 'font_y' => new Font(PATH_ROOT . '/css/fonts/OpenSans-Regular.ttf', 8), 'bgcolor' => new Color(255, 255, 255, 100), 'fgcolor' => new Color(255, 255, 255), 'y_lines' => TRUE]); $g->max(50); $g->min(0); $g->draw($this->data['image']->getHandle()); } $this->data['subaction'] = 'image'; } else { if (isset($_REQUEST['storage_usage'])) { $this->validateArgs($_REQUEST, [['start_date', 'string'], ['end_date', 'string'], ['resolution', 'numeric']]); $s = $_REQUEST['start_date']; $e = $_REQUEST['end_date']; $r = $_REQUEST['resolution']; $v = []; if ($t = $this->db->call('show_storage_usage', [$s, $e, $r])) { $b = $t[0]['balance']; for ($i = 1; $i < count($t); $i++) { if ($r == 1) { $v[sprintf('%02d.%02d.%04d', $t[$i]['day'], $t[$i]['month'], $t[$i]['year'])] = round(($b += $t[$i]['balance']) / 1024 / 1024, 2); } else { if ($r == 2) { $v[sprintf('%02d.%04d', $t[$i]['month'], $t[$i]['year'])] = round(($b += $t[$i]['balance']) / 1024 / 1024, 2); } } } $this->data['image'] = new GDImage(NULL, new Size(800, 500)); $g = new LineChart(new Boundary(0, 0, $this->data['image']->size()->width, $this->data['image']->size()->height)); $g->depth(15); $g->background(new Color(255, 144, 130, 127)); $g->values(['bgcolors' => [new Color(0, 255, 0)], 'fgcolors' => [new Color(255, 255, 255)], 'font' => new Font(PATH_ROOT . '/css/fonts/OpenSans-Regular.ttf', 10), 'labels' => TRUE, 'thickness' => 2]); $g->data($v); $g->title(['text' => 'Использование хранилища', 'color' => new Color(255, 255, 255), 'font' => new Font(PATH_ROOT . '/css/fonts/OpenSans-Bold.ttf', 14), 'margin' => ['top' => 0, 'bottom' => 15]]); $g->axis(['font_x' => new Font(PATH_ROOT . '/css/fonts/OpenSans-Regular.ttf', 8, 90), 'font_y' => new Font(PATH_ROOT . '/css/fonts/OpenSans-Regular.ttf', 8), 'bgcolor' => new Color(255, 255, 255, 100), 'fgcolor' => new Color(255, 255, 255), 'y_lines' => TRUE]); $g->draw($this->data['image']->getHandle()); } $this->data['subaction'] = 'image'; } else { if ($t = $this->db->fetch('SELECT COUNT(*) AS total_users FROM users')) { $this->data['stats']['total_users'] = $t[0]['total_users']; } if ($t = $this->db->fetch('SELECT COUNT(*) AS total_admins FROM users WHERE is_admin=1')) { $this->data['stats']['total_admins'] = $t[0]['total_admins']; } if ($t = $this->db->fetch('SELECT id, login, DATE_FORMAT(reg_date, \'%e.%m.%Y %H:%i\') AS reg_date FROM users WHERE reg_date=(SELECT MAX(reg_date) FROM users)')) { $this->data['stats']['last_user'] = $t[0]; } if ($t = $this->db->fetch('SELECT SUM(file_size) AS total_size FROM storage')) { $this->data['stats']['storage_usage'] = round($t[0]['total_size'] / 1024 / 1024, 1) . ($this->app->config['storage']['max_size'] != -1 ? ' из ' . $this->app->config['storage']['max_size'] : '') . ' МБ'; } if ($t = $this->db->fetch('SELECT COUNT(*) AS total_sections FROM sections')) { $this->data['stats']['total_sections'] = $t[0]['total_sections']; } if ($t = $this->db->fetch('SELECT COUNT(*) AS total_articles FROM articles')) { $this->data['stats']['total_articles'] = $t[0]['total_articles']; } if ($t = $this->db->fetch('SELECT COUNT(*) AS total_unverified_articles FROM articles WHERE verifier_id IS NULL')) { $this->data['stats']['total_unverified_articles'] = $t[0]['total_unverified_articles']; } if ($t = $this->db->fetch('SELECT COUNT(*) AS total_comments FROM comments')) { $this->data['stats']['total_comments'] = $t[0]['total_comments']; } if ($t = $this->db->fetch('SELECT articles.id AS id, articles.title AS title, data_folder, users.id AS user_id, login FROM articles INNER JOIN sections ON articles.section_id=sections.id INNER JOIN users ON author_id=users.id ORDER BY views DESC LIMIT 5')) { foreach ($t as &$a) { $a['href'] = $this->app->config['path']['section'] . $a['data_folder'] . '/' . $a['id'] . '/'; } unset($a); $this->data['stats']['top_articles'] = $t; } } } }
<?php /** * Created by PhpStorm. * User: martinmatak * Date: 06/03/16 * Time: 17:50 */ namespace graph_library; require_once '../Autoloader.php'; header('Content-Type:image/png'); $platno = new Canvas(); $podaci = new DataCollection(); $line_chart = new BarChart('Cijena dionica', 300, 300); $legend = new Legend(); $legend->addItem(new LegendItem('Relativni odnos dionica zadnjih 5 godina')); $podaci->addItems(array(new DataCollectionItem([10, '2011']), new DataCollectionItem([15, '2012']), new DataCollectionItem([8, '2013']), new DataCollectionItem([19, '2014']), new DataCollectionItem([22, '2015']))); $line_chart->setLegend($legend, 10, 25); $id_podataka = $line_chart->addData($podaci); $line_chart->colorData(16, 80, 57, $id_podataka); $platno->addChart($line_chart, 0, 0); $slika = $platno->render(); imagepng($slika); $platno->save('bar.png');
if (isset($system_id)) { $system_id = qpg($system_id); $sql .= "AND request.system_id={$system_id} "; } if (isset($request_type)) { $request_type = qpg($request_type); $sql .= "AND request.request_type={$request_type} "; } if (isset($requested_by)) { $requested_by = intval($requested_by); $sql .= "AND requester_id = {$requested_by} "; } $sql .= "GROUP BY status_lookup.lookup_desc "; $sql .= "ORDER BY 2 DESC;"; // echo "$sql"; $pie = new BarChart($sql); /* include("page-header.php"); */ $pie->Render(); /* include("page-footer.php"); */ error_reporting(7); if ($debuglevel > 0) { $total_query_time = sprintf("%3.06lf", $total_query_time); error_log("{$sysabbr} total_query_ TQ: {$total_query_time} URI: {$REQUEST_URI}", 0); $total_time = sprintf("%3.06lf", duration($begin_processing, microtime())); error_log("{$sysabbr} process_time TT: {$total_time} Agent: {$HTTP_USER_AGENT} Referrer: {$HTTP_REFERER} ", 0); error_log("=============================================== Endof {$PHP_SELF}"); }
/** * Creates a new vertical bar chart * * @param integer width of the image * @param integer height of the image */ public function VerticalBarChart($width = 600, $height = 250, $low = 0) { parent::BarChart($width, $height, $low); $this->emptyToFullRatio = 1 / 5; $this->plot->setGraphPadding(new Padding(5, 30, 50, 50)); }
/** * Detailed year report. * * @return void * @access public */ public function year() { // which year? if (isset($this->get["year"])) { $year = $this->get["year"]; } else { $year = strftime("%Y", time()); } // list of overview boxes to render (in order). $boxes = array("general_stats", "top_src", "top_dst", "top_clid"); // set list of boxes to render $this->template->boxes = $boxes; $this->template->boxlist = "'" . implode("','", $boxes) . "'"; // set month and year information in template $this->template->year = $year; // array of month names for chart X axis $monthnames = array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"); // create calls chart $chart_calls = new BarChart("Calls per month ({$year})"); $chart_calls->dimensions = "700x350"; $chart_calls->margins = array(35, 35, 35, 35); $chart_calls->barwidth = 45; $chart_calls->palette = CHART_PALETTE; $chart_calls->x_labels = $monthnames; // create minutes chart $chart_mins = new BarChart("Minutes per month ({$year})"); $chart_mins->dimensions = "700x350"; $chart_mins->margins = array(35, 35, 35, 35); $chart_mins->barwidth = 45; $chart_mins->palette = CHART_PALETTE; $chart_mins->x_labels = $monthnames; // calculate daily statistics foreach (range(1, 12) as $month) { $month = sprintf("%02d", $month); $stat = $this->db->GetRow("\n\t\t\t\tSELECT\tCOUNT(*) AS calls,\n\t\t\t\t\tSUM(duration) AS seconds\n\t\t\t\tFROM " . DB_TABLE . "\n\t\t\t\tWHERE \tcalldate >= '{$year}-{$month}-01 00:00:00'\n\t\t\t\t\tAND calldate < DATE_ADD('{$year}-{$month}-01 00:00:00', INTERVAL 1 MONTH);\n\t\t\t"); $chart_calls->values[] = $stat["calls"]; $chart_mins->values[] = round($stat["seconds"] / 60); } // create time of day breakdown chart $chart_todb = new BarChart("Time of day breakdown (number of calls) ({$year})"); $chart_todb->dimensions = "700x350"; $chart_todb->margins = array(35, 35, 35, 35); $chart_todb->barwidth = 16; $chart_todb->palette = CHART_PALETTE; $chart_todb->x_labels = range(0, 23); // calculate time of day breakdown $hours = array(); foreach (range(0, 23) as $hour) { $stat = $this->db->GetOne(sprintf("\n\t\t\t\tSELECT COUNT(*) AS calls\n\t\t\t\tFROM " . DB_TABLE . "\n\t\t\t\tWHERE\tcalldate LIKE '{$year}%%'\n\t\t\t\t\tAND calldate LIKE '%% %02d:%%'\n\t\t\t", $hour)); $chart_todb->values[] = $stat; } // assign chart URLs to template $this->template->chart_calls = $chart_calls->saveFile(CHART_CACHE); $this->template->chart_mins = $chart_mins->saveFile(CHART_CACHE); $this->template->chart_todb = $chart_todb->saveFile(CHART_CACHE); // render page $this->template->show("year"); }
/** * Creates a new line chart. * Line charts allow for XYDataSet and XYSeriesDataSet in order to plot several lines. * * @param integer width of the image * @param integer height of the image */ public function LineChart($width = 600, $height = 250) { parent::BarChart($width, $height); $this->plot->setGraphPadding(new Padding(5, 30, 50, 50)); }
/** * Creates a new vertical bar chart * * @param integer width of the image * @param integer height of the image */ public function VerticalBarChart($width = 600, $height = 250) { parent::BarChart($width, $height); $this->emptyToFullRatio = 1 / 4; $this->plot->setGraphPadding(new Padding(-10, 5, 70, 45)); }
/** * Load overview box content. * * @return void * @access public */ public function box() { // prepare box data switch ($this->get["box"]) { case "most_recent": // most recent calls $recent = $this->db->GetAssoc("\n\t\t\t\t\tSELECT uniqueid,calldate,src,dst,SEC_TO_TIME(duration) AS formatted_duration\n\t\t\t\t\tFROM " . DB_TABLE . "\n\t\t\t\t\tORDER BY calldate DESC LIMIT 12;\n\t\t\t\t"); $this->template->cdrs = $recent; break; case "summary_day": // summary for the current day $barchart = new BarChart(date("l jS F Y") . " (minutes)"); $barchart->dimensions = "300x265"; $barchart->x_labels = range(0, 23); $barchart->margins = array(25, 5, 5, 5); $barchart->barwidth = 6; $barchart->palette = CHART_PALETTE; foreach ($barchart->x_labels as $hour) { $from = sprintf(date("Y-m-d") . " %02d:00:00", $hour); $to = sprintf(date("Y-m-d") . " %02d:59:59", $hour); $sql = "SELECT COALESCE(SUM(duration),0) FROM " . DB_TABLE . " WHERE calldate >= '{$from}' AND calldate <= '{$to}'"; $minutes = round($this->db->getOne($sql) / 60); $barchart->values[] = $minutes; } $this->template->chart = $barchart->saveFile(CHART_CACHE); break; case "summary_week": // summary for the current week if (date("l") == "Monday") { $mm = strtotime("today"); } else { $mm = strtotime("last Monday"); } $barchart = new BarChart("W/B " . date("l jS F Y", $mm) . " (minutes)"); $barchart->dimensions = "300x265"; $barchart->x_labels = array("Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"); $barchart->margins = array(25, 5, 5, 5); $barchart->barwidth = 25; $barchart->palette = CHART_PALETTE; for ($i = 0; $i <= 6; $i++) { $ts = $mm + 86400 * $i; $from = sprintf(date("Y-m-d", $ts) . " 00:00:00"); $to = sprintf(date("Y-m-d", $ts) . " 23:59:59"); $sql = "SELECT COALESCE(SUM(duration),0) FROM " . DB_TABLE . " WHERE calldate >= '{$from}' AND calldate <= '{$to}'"; $minutes = round($this->db->getOne($sql) / 60); $barchart->values[] = $minutes; } $this->template->chart = $barchart->saveFile(CHART_CACHE); break; case "top_src": // most popular sources $popular = $this->db->GetAssoc("\n\t\t\t\t\tSELECT src, COUNT(*) AS count\n\t\t\t\t\tFROM " . DB_TABLE . "\n\t\t\t\t\tWHERE src IS NOT NULL AND src != ''\n\t\t\t\t\tGROUP BY src\n\t\t\t\t\tORDER BY count DESC LIMIT 12;\n\t\t\t\t"); $this->template->numbers = $popular; break; case "top_dst": // most popular destinations $popular = $this->db->GetAssoc("\n\t\t\t\t\tSELECT dst, COUNT(*) AS count\n\t\t\t\t\tFROM " . DB_TABLE . "\n\t\t\t\t\tWHERE dst IS NOT NULL AND dst != ''\n\t\t\t\t\tGROUP BY dst\n\t\t\t\t\tORDER BY count DESC LIMIT 12;\n\t\t\t\t"); $this->template->numbers = $popular; break; case "top_clid": // most popular caller IDs $popular = $this->db->GetAssoc("\n\t\t\t\t\tSELECT clid, COUNT(*) AS count\n\t\t\t\t\tFROM " . DB_TABLE . "\n\t\t\t\t\tWHERE clid IS NOT NULL AND clid != ''\n\t\t\t\t\tGROUP BY clid\n\t\t\t\t\tORDER BY count DESC LIMIT 12;\n\t\t\t\t"); $this->template->numbers = $popular; break; case "longest_calls": // longest calls $recent = $this->db->GetAssoc("\n\t\t\t\t\tSELECT uniqueid,calldate,src,dst,SEC_TO_TIME(duration) AS formatted_duration\n\t\t\t\t\tFROM " . DB_TABLE . "\n\t\t\t\t\tORDER BY duration DESC LIMIT 12;\n\t\t\t\t"); $this->template->cdrs = $recent; break; } // render box template $this->template->show("box_" . $this->get["box"]); }