added csv export

improved jpgraph plotting
This commit is contained in:
Steffen Vogel 2010-07-20 11:28:22 +02:00
parent e6daf91cd9
commit 925faf8bc7
4 changed files with 97 additions and 24 deletions

View file

@ -22,31 +22,56 @@
namespace Volkszaehler\View\Csv;
class Csv extends \Volkszaehler\View\View {
protected $csv = array();
protected $header = array();
protected $footer = array();
/*
* constructor
*/
public function __construct(Http\Request $request, Http\Response $response) {
public function __construct(\Volkszaehler\View\Http\Request $request, \Volkszaehler\View\Http\Response $response) {
parent::__construct($request, $response);
$this->csv['source'] = 'volkszaehler.org';
$this->csv['version'] = \Volkszaehler\VERSION;
$this->header[] = 'source: volkszaehler.org';
$this->header[] = 'version: ' . \Volkszaehler\VERSION;
$this->response->setHeader('Content-type', 'text/csv');
$this->response->setHeader('Content-Disposition', 'attachment; filename="data.csv"');
}
public function render() {
parent::render();
// TODO implement
foreach ($this->header as $line) {
echo $line . PHP_EOL;
}
echo PHP_EOL;
foreach ($this->csv as $array) {
echo implode(";", $array) . PHP_EOL;
}
echo PHP_EOL;
foreach ($this->footer as $line) {
echo $line . PHP_EOL;
}
}
public function addDebug() {
// TODO implement debug output for csv view
$config = \Volkszaehler\Util\Registry::get('config');
$this->footer[] = 'time: ' . $this->getTime();
$this->footer[] = 'database: ' . $config['db']['driver'];
foreach (\Volkszaehler\Util\Debug::getSQLLogger()->queries as $query) {
$this->footer[] = 'query: ' . $query['sql'];
}
}
public function addException(\Exception $exception) {
// TODO implement exception output for csv view
echo $exception;
}
}

View file

@ -0,0 +1,28 @@
<?php
/*
* Copyright (c) 2010 by Justin Otherguy <justin@justinotherguy.org>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License (either version 2 or
* version 3) as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
* For more information on the GPL, please go to:
* http://www.gnu.org/copyleft/gpl.html
*/
namespace Volkszaehler\View\Csv;
class Data extends Csv {
public function add($obj, $data) {
$this->csv = array_merge($this->csv, $data);
}
}

View file

@ -25,12 +25,17 @@ require_once \Volkszaehler\BACKEND_DIR . '/lib/vendor/JpGraph/jpgraph.php';
require_once \Volkszaehler\BACKEND_DIR . '/lib/vendor/JpGraph/jpgraph_scatter.php';
require_once \Volkszaehler\BACKEND_DIR . '/lib/vendor/JpGraph/jpgraph_date.php';
/*
* JpGraph plotting
*
* @todo add caching
* @todo unifiy axes of same unit
*/
class JpGraph extends View {
protected $width = 800;
protected $height = 600;
protected $plotCount = 0;
protected $channels = array();
protected static $colors = array('chartreuse', 'chocolate1', 'cyan', 'blue', 'lightcyan4', 'gold');
@ -44,23 +49,24 @@ class JpGraph extends View {
// Specify what scale we want to use,
$this->graph->SetScale('datlin');
$this->graph->legend->setPos(0.15,0.025, 'left', 'top');
$this->graph->SetMarginColor('white');
$this->graph->SetMargin(90,10,18,90);
$this->graph->SetMargin(90,65,10,90);
$this->graph->SetYDeltaDist(65);
$this->graph->yaxis->SetTitlemargin(36);
$this->graph->SetTickDensity(TICKD_DENSE, TICKD_SPARSE);
$this->graph->xaxis->SetFont(FF_ARIAL);
$this->graph->yaxis->SetFont(FF_ARIAL);
$this->graph->xaxis->SetLabelAngle(45);
$this->graph->xaxis->SetLabelAngle(60);
$this->graph->xaxis->SetLabelFormatCallback(function($label) { return date('j.n.y G:i', $label); });
//$this->graph->img->SetAntiAliasing();
}
/*
* @todo add title unit etc..
*/
public function add(\Volkszaehler\Model\Channel $obj, $data = NULL) {
$count = count($this->channels);
$xData = $yData = array();
foreach ($data as $reading) {
$xData[] = $reading['timestamp']/1000;
@ -70,18 +76,32 @@ class JpGraph extends View {
// Create the linear plot
$plot = new \ScatterPlot($yData, $xData);
$plot->mark->SetColor(self::$colors[$this->plotCount]);
$plot->mark->SetFillColor(self::$colors[$this->plotCount]);
$plot->setLegend($obj->getName() . ': ' . $obj->getDescription() . ' [' . $obj->getUuid() . ']');
$plot->mark->SetColor(self::$colors[$count]);
$plot->mark->SetFillColor(self::$colors[$count]);
$plot->mark->SetType(MARK_DIAMOND);
$plot->mark->SetWidth(1);
$plot->SetLinkPoints(true, self::$colors[$this->plotCount]);
$plot->SetLinkPoints(true, self::$colors[$count]);
$this->plotCount++;
if ($count == 0) {
$yaxis = $this->graph->yaxis;
$this->graph->Add($plot);
}
else {
$this->graph->SetYScale($count-1,'lin');
$yaxis = $this->graph->ynaxis[$count-1];
$this->graph->SetMargin(60,($count) * 65,10,90);
$this->graph->AddY($count-1, $plot);
}
// Add the plot to the graph
$this->graph->Add($plot);
$yaxis->title->Set($obj->getUnit());
$yaxis->title->SetFont(FF_ARIAL);
$yaxis->SetColor(self::$colors[$count]);
$yaxis->SetTitleMargin('50');
$this->channels[] = $obj;
}
public function addException(\Exception $e) { echo $e; }

View file

@ -19,17 +19,17 @@
* http://www.gnu.org/copyleft/gpl.html
*/
namespace Volkszaehler\View;
namespace Volkszaehler\View\Json;
use Volkszaehler\Util;
class Json extends View {
class Json extends \Volkszaehler\View\View {
protected $json = array();
/*
* constructor
*/
public function __construct(Http\Request $request, Http\Response $response) {
public function __construct(\Volkszaehler\View\Http\Request $request, \Volkszaehler\View\Http\Response $response) {
parent::__construct($request, $response);
$this->json['source'] = 'volkszaehler.org';