summaryrefslogtreecommitdiffstats
path: root/library/Icinga/Chart/Graph/StackedGraph.php
blob: 49801a9a8d9946d2c6bb90da971ddd60152daca8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
<?php
/* Icinga Web 2 | (c) 2013 Icinga Development Team | GPLv2+ */

namespace Icinga\Chart\Graph;

use DOMElement;
use Icinga\Chart\Primitive\Drawable;
use Icinga\Chart\Render\RenderContext;

/**
 * Graph implementation that stacks several graphs and displays them in a cumulative way
 */
class StackedGraph implements Drawable
{
    /**
     * All graphs displayed in this stackedgraph
     *
     * @var array
     */
    private $stack = array();

    /**
     * An associative array containing x points as the key and an array of y values as the value
     *
     * @var array
     */
    private $points = array();

    /**
     * Add a graph to this stack and aggregate the values on the fly
     *
     * This modifies the dataset as a side effect
     *
     * @param array $subGraph
     */
    public function addGraph(array &$subGraph)
    {
        foreach ($subGraph['data'] as &$point) {
            $x = $point[0];
            if (!isset($this->points[$x])) {
                $this->points[$x] = 0;
            }
            // store old y-value for displaying the actual (non-aggregated)
            // value in the tooltip
            $point[2] = $point[1];

            $this->points[$x] += $point[1];
            $point[1] = $this->points[$x];
        }
    }

    /**
     * Add a graph to the stack
     *
     * @param $graph
     */
    public function addToStack($graph)
    {
        $this->stack[] = $graph;
    }

    /**
     * Empty the stack
     *
     * @return bool
     */
    public function stackEmpty()
    {
        return empty($this->stack);
    }

    /**
     * Render this stack in the correct order
     *
     * @param   RenderContext $ctx  The context to use for rendering
     *
     * @return  DOMElement          The SVG representation of this graph
     */
    public function toSvg(RenderContext $ctx)
    {
        $group = $ctx->getDocument()->createElement('g');
        $renderOrder = array_reverse($this->stack);
        foreach ($renderOrder as $stackElem) {
            $group->appendChild($stackElem->toSvg($ctx));
        }
        return $group;
    }
}