Skip to content

Commit

Permalink
initial commit graph classes
Browse files Browse the repository at this point in the history
  • Loading branch information
aurora committed May 6, 2012
1 parent 7d382ce commit 54c0db8
Show file tree
Hide file tree
Showing 4 changed files with 343 additions and 0 deletions.
63 changes: 63 additions & 0 deletions libs/chart/graph/bar.class.php
@@ -0,0 +1,63 @@
<?php

/*
* This file is part of asciidia
* Copyright (C) 2012 by Harald Lapp <harald@octris.org>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* 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, see <http://www.gnu.org/licenses/>.
*
* This script can be found at:
* https://github.com/aurora/asciidia
*/

namespace chart\graph {
/**
* Bar graph.
*
* @octdoc c:graph/bar
* @copyright copyright (c) 2012 by Harald Lapp
* @author Harald Lapp <harald@octris.org>
*/
class bar extends \chart\graph
/**/
{
/**
* Create bar graph.
*
* @octdoc m:bar/create
* @param context $context Drawing context.
* @param int $width Width of graph.
* @param int $height Height of graph.
* @param float $zero Zero line (X-Axis).
* @param float $x_mul Point multiplicator for X-Axis.
* @param float $y_mul Point multiplicator for Y-Axis.
*/
public function create(\context $context, $width, $height, $zero, $x_mul, $y_mul)
/**/
{
$values = $this->dataset->getValues();

$ctx = $context->addContext();

for ($i = 0, $cnt = count($values); $i < $cnt; ++$i) {
if ($values[$i] > 0) {
$ctx->addCommand(sprintf(
'rectangle %f,%f %f,%f',
$i * $x_mul, $zero, $i * $x_mul, $zero - $values[$i] * $y_mul
));
}
}
}
}
}
154 changes: 154 additions & 0 deletions libs/chart/graph/layered.class.php
@@ -0,0 +1,154 @@
<?php

/*
* This file is part of asciidia
* Copyright (C) 2012 by Harald Lapp <harald@octris.org>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* 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, see <http://www.gnu.org/licenses/>.
*
* This script can be found at:
* https://github.com/aurora/asciidia
*/

namespace chart\graph {
/**
* layered bar graph.
*
* @octdoc c:graph/layered
* @copyright copyright (c) 2012 by Harald Lapp
* @author Harald Lapp <harald@octris.org>
*/
class layered extends \chart\graph
/**/
{
/**
* Datasets.
*
* @octdoc p:graph/$datasets
* @var array
*/
protected $datasets;
/**/

/**
* Constructor.
*
* @octdoc m:graph/__construct
* @param array $datasets Datasets to use for graph
*/
public function __construct(array $datasets)
/**/
{
$this->datasets = $datasets;
}

/**
* Create bar graph.
*
* @octdoc m:layered/create
* @param context $context Drawing context.
* @param int $width Width of graph.
* @param int $height Height of graph.
* @param float $zero Zero line (X-Axis).
* @param float $x_mul Point multiplicator for X-Axis.
* @param float $y_mul Point multiplicator for Y-Axis.
*/
public function create(\context $context, $width, $height, $zero, $x_mul, $y_mul)
/**/
{
$values = array();

foreach ($this->datasets as $dataset) {
$tmp = $dataset->getValues();

for ($i = 0, $cnt = count($tmp); $i < $cnt; ++$i) {
if (!isset($values[$i])) {
$values[$i] = array();
}

$values[$i][] = $tmp[$i];
}
}

$ctx = $context->addContext();

for ($i = 0, $cnt = count($values); $i < $cnt; ++$i) {
$tmp = $values[$i];
arsort($tmp);

foreach ($tmp as $v) {
if ($v > 0) {
$ctx->addCommand(sprintf(
'rectangle %f,%f %f,%f',
$i * $x_mul, $zero, $i * $x_mul, $zero - $v * $y_mul
));
}
}
}
}

/** proxy for datasets **/

/**
* Get max value.
*
* @octdoc m:graph/getMax
* @return float Max value.
*/
public function getMax()
/**/
{
$tmp = array();
foreach ($this->datasets as $dataset) {
$tmp[] = $dataset->getMax();
}

return max($tmp);
}

/**
* Get min value.
*
* @octdoc m:graph/getMin
* @return float Min value.
*/
public function getMin()
/**/
{
$tmp = array();
foreach ($this->datasets as $dataset) {
$tmp[] = $dataset->getMin();
}

return min($tmp);
}

/**
* Get number of values.
*
* @octdoc m:graph/getCount
* @return int Number of values.
*/
public function getCount()
/**/
{
$tmp = array();
foreach ($this->datasets as $dataset) {
$tmp[] = $dataset->getCount();
}

return max($tmp);
}
}
}
65 changes: 65 additions & 0 deletions libs/chart/graph/line.class.php
@@ -0,0 +1,65 @@
<?php

/*
* This file is part of asciidia
* Copyright (C) 2012 by Harald Lapp <harald@octris.org>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* 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, see <http://www.gnu.org/licenses/>.
*
* This script can be found at:
* https://github.com/aurora/asciidia
*/

namespace chart\graph {
/**
* Line graph.
*
* @octdoc c:graph/line
* @copyright copyright (c) 2012 by Harald Lapp
* @author Harald Lapp <harald@octris.org>
*/
class line extends \chart\graph
/**/
{
/**
* Create bar graph.
*
* @octdoc m:line/create
* @param context $context Drawing context.
* @param int $width Width of graph.
* @param int $height Height of graph.
* @param float $zero Zero line (X-Axis).
* @param float $x_mul Point multiplicator for X-Axis.
* @param float $y_mul Point multiplicator for Y-Axis.
*/
public function create(\context $context, $width, $height, $zero, $x_mul, $y_mul)
/**/
{
$points = array();
$values = $this->dataset->getValues();

$ctx = $context->addContext();
$ctx->addCommand(vsprintf('stroke rgb(%d,%d,%d) stroke-width 3', array(0,0,0)));

for ($i = 1, $cnt = count($values); $i < $cnt; ++$i) {
$ctx->drawLine(
($i - 1) * $x_mul,
$zero - $values[$i - 1] * $y_mul,
$i * $x_mul,
$zero - $values[$i] * $y_mul
);
}
}
}
}
61 changes: 61 additions & 0 deletions libs/chart/graph/spline.class.php
@@ -0,0 +1,61 @@
<?php

/*
* This file is part of asciidia
* Copyright (C) 2012 by Harald Lapp <harald@octris.org>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* 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, see <http://www.gnu.org/licenses/>.
*
* This script can be found at:
* https://github.com/aurora/asciidia
*/

namespace chart\graph {
/**
* Spline graph.
*
* @octdoc c:graph/spline
* @copyright copyright (c) 2012 by Harald Lapp
* @author Harald Lapp <harald@octris.org>
*/
class spline extends \chart\graph
/**/
{
/**
* Create bar graph.
*
* @octdoc m:spline/create
* @param context $context Drawing context.
* @param int $width Width of graph.
* @param int $height Height of graph.
* @param float $zero Zero line (X-Axis).
* @param float $x_mul Point multiplicator for X-Axis.
* @param float $y_mul Point multiplicator for Y-Axis.
*/
public function create(\context $context, $width, $height, $zero, $x_mul, $y_mul)
/**/
{
$points = array();
$values = $this->dataset->getValues();

for ($i = 0, $cnt = count($values); $i < $cnt; ++$i) {
$points[] = array($i * $x_mul, $zero - $values[$i] * $y_mul);
}

$ctx = $context->addContext();
$ctx->addCommand(vsprintf('stroke rgb(%d,%d,%d) stroke-width 3', array(0,0,0)));
$ctx->drawSpline($points);
}
}
}

0 comments on commit 54c0db8

Please sign in to comment.