Skip to content

Commit

Permalink
charts.php
Browse files Browse the repository at this point in the history
  • Loading branch information
povilasb committed Nov 29, 2011
1 parent 7c17e41 commit 4844e6c
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 12 deletions.
Binary file added Design/Fonts/arial.ttf
Binary file not shown.
1 change: 1 addition & 0 deletions Includes.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
require_once("XlsParsers/ParamosPriemonesParseris.php");
require_once("XlsParsers/ParamosKiekiaiParseris.php");
require_once("Utilities/Importer.php");
require_once("Utilities/ImageText.php");

require_once('phpgraph/phpgraphlib.php');
require_once('phpgraph/phpgraphlib_pie.php');
Expand Down
11 changes: 11 additions & 0 deletions Test/ImageText_Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<head>
<meta charset="utf-8"/>
</head>

<?php
session_start();
require_once("../Includes.php");

//ImageText::createTextImage(550, 200, "Not very simple text Not very simple text Not very simple text Not very simple text");
print("čęąėįšų");
?>
58 changes: 46 additions & 12 deletions Utilities/Charts.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
define("CHART_HEIGHT", 200);

if (!isset($_GET["chart"])){
ErrorMessages::setError(10, "null", "Charts.php");
ImageText::createTextImage(CHART_WIDTH, CHART_HEIGHT, "Bloga nuoroda");
exit(0);
}

Expand All @@ -18,20 +18,26 @@
switch ($chart){
case "padaliniu_paraiskos":
if (!isset($_GET["menuo"])){
ErrorMessages::setError(10, "null", "Charts.php");
ImageText::createTextImage(CHART_WIDTH, CHART_HEIGHT, "Bloga nuoroda");
exit(0);
}

$data = $_GET["menuo"] . "-01";
if (ObjectValidation::validateDate($data) == false){
ErrorMessages::setError(12, "null", "Charts.php");
ImageText::createTextImage(CHART_WIDTH, CHART_HEIGHT, "Blogas datos formatas");
exit(0);
}

$graph->setTitle($data);

$dbQuery = "SELECT * FROM PadaliniuParaiskuKiekis WHERE Nuo = '" . $data . "'";
$dbResult = db::select($dbQuery);

if (empty($dbResult["data"])){
ImageText::createTextImage(CHART_WIDTH, CHART_HEIGHT, "Neturima duomenų apie pasirinktą mėnesį");
exit(0);
}

$chartData = array();
foreach ($dbResult["data"] as $i){
$chartData[$i["Kodas"]] = $i["Paraiskos"];
Expand All @@ -41,20 +47,26 @@

case "is_paraiskos":
if (!isset($_GET["menuo"])){
ErrorMessages::setError(10, "null", "Charts.php");
ImageText::createTextImage(CHART_WIDTH, CHART_HEIGHT, "Bloga nuoroda");
exit(0);
}

$data = $_GET["menuo"] . "-01";
if (ObjectValidation::validateDate($data) == false){
ErrorMessages::setError(12, "null", "Charts.php");
ImageText::createTextImage(CHART_WIDTH, CHART_HEIGHT, "Blogas datos formatas");
exit(0);
}

$graph->setTitle($data);

$dbQuery = "SELECT * FROM IsParaiskuKiekis WHERE Nuo = '" . $data . "'";
$dbResult = db::select($dbQuery);

if (empty($dbResult["data"])){
ImageText::createTextImage(CHART_WIDTH, CHART_HEIGHT, "Neturima duomenų apie pasirinktą mėnesį");
exit(0);
}

$chartData = array();
foreach ($dbResult["data"] as $i){
$chartData[$i["Kodas"]] = $i["Paraiskos"];
Expand All @@ -64,19 +76,40 @@

case "padaliniu_valandos":
if ((!isset($_GET["menuoNuo"])) || (!isset($_GET["menuoIki"]))){
ErrorMessages::setError(10, "null", "Charts.php");
ImageText::createTextImage(CHART_WIDTH, CHART_HEIGHT, "Bloga nuoroda");
exit(0);
}

$dataNuo = $_GET["menuoNuo"] . "-01";
$dataIki = $_GET["menuoIki"] . "-01";
$dataIki = date('Y-m-d',strtotime("$dataIki + 1 months"));
if ((ObjectValidation::validateDate($dataNuo) == false) || (ObjectValidation::validateDate($dataIki) == false)){
ErrorMessages::setError(12, "null", "Charts.php");
ImageText::createTextImage(CHART_WIDTH, CHART_HEIGHT, "Blogas datos formatas");
exit(0);
}

$graph->setTitle($dataNuo . " - " . date('Y-m-d',strtotime("$dataIki - 1 day")));
$graph->setTitle($dataNuo . " - " . date('Y-m-d',strtotime("$dataIki - 1 day + 1 months")));

$dbQuery = "SELECT MIN(Nuo) AS `min` FROM PadaliniuValanduKiekis";
$dbResult = db::select($dbQuery);
$minData = $dbResult["data"][0]["min"];
$dbQuery = "SELECT MAX(Nuo) AS `max` FROM PadaliniuValanduKiekis";
$dbResult = db::select($dbQuery);
$maxData = $dbResult["data"][0]["max"];

if ($dataNuo < $minData)
$dataNuo = $minData;
if ($dataIki > $maxData)
$dataIki = $maxData;
$dataIki = date('Y-m-d',strtotime("$dataIki + 1 months"));

if ($dataNuo > $dataIki){
ImageText::createTextImage(CHART_WIDTH, CHART_HEIGHT, "Blogas laiko intervalas");
exit(0);
}
if (($dataIki < $minData) || ($dataNuo > $maxData)){
ImageText::createTextImage(CHART_WIDTH, CHART_HEIGHT, "Neturima duomenų apie pasirinktą laiko intervalą");
exit(0);
}

$chartData = array();
$data = $dataNuo;
Expand All @@ -96,15 +129,15 @@

case "is_valandos":
if ((!isset($_GET["menuoNuo"])) || (!isset($_GET["menuoIki"]))){
ErrorMessages::setError(10, "null", "Charts.php");
ImageText::createTextImage(CHART_WIDTH, CHART_HEIGHT, "Bloga nuoroda");
exit(0);
}

$dataNuo = $_GET["menuoNuo"] . "-01";
$dataIki = $_GET["menuoIki"] . "-01";
$dataIki = date('Y-m-d',strtotime("$dataIki + 1 months"));
if ((ObjectValidation::validateDate($dataNuo) == false) || (ObjectValidation::validateDate($dataIki) == false)){
ErrorMessages::setError(12, "null", "Charts.php");
ImageText::createTextImage(CHART_WIDTH, CHART_HEIGHT, "Blogas datos formatas");
exit(0);
}

Expand All @@ -127,13 +160,14 @@
break;

default:
ErrorMessages::setError(11, "null", "Charts.php");
ImageText::createTextImage(CHART_WIDTH, CHART_HEIGHT, "Bloga diagramos rūšis");
exit(0);
break;
}

$graph->addData($chartData);
$graph->setGradient('red', 'maroon');
$graph->setDataValues(true);
$graph->setupXAxis(20);
$graph->createGraph();
?>
28 changes: 28 additions & 0 deletions Utilities/ImageText.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

class ImageText
{
static public function createTextImage($width, $height, $text){
$img = @imagecreatetruecolor($width, $height);

$bgColor = imagecolorallocate($img, 255, 255, 255);
$textColor = imagecolorallocate($img, 0, 0, 0);
$fontSize = 11;
$fontName = "arial.ttf";

imagefilledrectangle($img, 0, 0, $width, $height, $bgColor);

$fontSizeBounds = imagettfbbox($fontSize, 0, $fontName, $text);
$lineSize = $fontSizeBounds[4];
$lineHeight = abs($fontSizeBounds[5]);
$x = (int)(($width - $lineSize) / 2);
$y = (int)(($height - $lineHeight) / 2);
imagettftext($img, $fontSize, 0, $x, $y, $textColor, $fontName, $text);

header('Content-Type: image/png');
imagepng($img);
imagedestroy($img);
}
}

?>
Binary file added Utilities/arial.ttf
Binary file not shown.

0 comments on commit 4844e6c

Please sign in to comment.