From 0cca2826bde7dfb9bb01df593317b51868e2c418 Mon Sep 17 00:00:00 2001 From: Steffen Vogel Date: Sat, 31 Jul 2010 15:43:49 +0200 Subject: [PATCH] added some data and input validation --- backend/lib/View/JSON.php | 4 +--- backend/lib/View/JpGraph.php | 45 ++++++++++++++++++++---------------- backend/lib/View/View.php | 2 ++ 3 files changed, 28 insertions(+), 23 deletions(-) diff --git a/backend/lib/View/JSON.php b/backend/lib/View/JSON.php index 5924d7b..0ba27df 100644 --- a/backend/lib/View/JSON.php +++ b/backend/lib/View/JSON.php @@ -34,8 +34,6 @@ use Volkszaehler\Model; * @author Steffen Vogel */ class JSON extends View { - const PRECISSION = 5; - protected $json = array(); protected $padding = FALSE; @@ -131,7 +129,7 @@ class JSON extends View { foreach ($data as $reading) { $jsonData[] = array( (int) $reading[0], - (float) round($reading[1], JSON::PRECISSION), + (float) round($reading[1], View::PRECISSION), (int) $reading[2] ); } diff --git a/backend/lib/View/JpGraph.php b/backend/lib/View/JpGraph.php index ffc88fc..99a228f 100644 --- a/backend/lib/View/JpGraph.php +++ b/backend/lib/View/JpGraph.php @@ -94,34 +94,39 @@ class JpGraph extends View { * @param $data */ public function addChannel(Model\Channel $channel, array $data = NULL){ - $count = count($this->channels); - $xData = $yData = array(); + if (isset($data) && count($data) > 0) { + $count = count($this->channels); + $xData = $yData = array(); - foreach ($data as $reading) { - $xData[] = $reading[0] / 1000; - $yData[] = $reading[1]; - } + foreach ($data as $reading) { + $xData[] = $reading[0] / 1000; + $yData[] = $reading[1]; + } - // Create the scatter plot - $plot = new \ScatterPlot($yData, $xData); + // Create the scatter plot + $plot = new \ScatterPlot($yData, $xData); - $plot->setLegend($channel->getName() . ': ' . $channel->getDescription() . ' [' . $channel->getUnit() . ']'); - $plot->SetLinkPoints(TRUE, self::$colors[$count]); + $plot->setLegend($channel->getName() . ': ' . $channel->getDescription() . ' [' . $channel->getUnit() . ']'); + $plot->SetLinkPoints(TRUE, self::$colors[$count]); - $plot->mark->SetColor(self::$colors[$count]); - $plot->mark->SetFillColor(self::$colors[$count]); - $plot->mark->SetType(MARK_DIAMOND); - $plot->mark->SetWidth(1); + $plot->mark->SetColor(self::$colors[$count]); + $plot->mark->SetFillColor(self::$colors[$count]); + $plot->mark->SetType(MARK_DIAMOND); + $plot->mark->SetWidth(1); - $axis = $this->getAxisIndex($channel); - if ($axis >= 0) { - $this->graph->AddY($axis, $plot); + $axis = $this->getAxisIndex($channel); + if ($axis >= 0) { + $this->graph->AddY($axis, $plot); + } + else { + $this->graph->Add($plot); + } + + $this->channels[] = $channel; } else { - $this->graph->Add($plot); + throw new \Exception('can\'t plot channels without data!'); } - - $this->channels[] = $channel; } /** diff --git a/backend/lib/View/View.php b/backend/lib/View/View.php index 6eeb7ac..383a2e6 100644 --- a/backend/lib/View/View.php +++ b/backend/lib/View/View.php @@ -47,6 +47,8 @@ interface ViewInterface { * */ abstract class View implements ViewInterface { + const PRECISSION = 5; + public $request; protected $response;