summaryrefslogtreecommitdiffstats
path: root/library/Icinga/Chart/Graph/BarGraph.php
blob: adef4289445f1e5a9cc280617167b9151fd40fc8 (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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
<?php
/* Icinga Web 2 | (c) 2013 Icinga Development Team | GPLv2+ */

namespace Icinga\Chart\Graph;

use DOMElement;
use Icinga\Chart\Primitive\Animation;
use Icinga\Chart\Primitive\Drawable;
use Icinga\Chart\Primitive\Rect;
use Icinga\Chart\Primitive\Styleable;
use Icinga\Chart\Render\RenderContext;

/**
 * Bar graph implementation
 */
class BarGraph extends Styleable implements Drawable
{
    /**
     * The dataset order
     *
     * @var int
     */
    private $order = 0;

    /**
     * The width of the bars.
     *
     * @var int
     */
    private $barWidth = 3;

    /**
     * The dataset to use for this bar graph
     *
     * @var array
     */
    private $dataSet;

    /**
     * The tooltips
     *
     * @var
     */
    private $tooltips;

    /**
     * All graphs
     *
     * @var
     */
    private $graphs;

    /**
     * Create a new BarGraph with the given dataset
     *
     * @param array $dataSet    An array of data points
     * @param int   $order      The graph number displayed by this BarGraph
     * @param array $tooltips   The tooltips to display for each value
     */
    public function __construct(
        array $dataSet,
        array &$graphs,
        $order,
        array $tooltips = null
    ) {
        $this->order = $order;
        $this->dataSet = $dataSet;

        $this->tooltips = $tooltips;
        foreach ($this->tooltips as $value) {
            $ts[] = $value;
        }
        $this->tooltips = $ts;

        $this->graphs = $graphs;
    }

    /**
     * Apply configuration styles from the $cfg
     *
     * @param array $cfg        The configuration as given in the drawBars call
     */
    public function setStyleFromConfig(array $cfg)
    {
        foreach ($cfg as $elem => $value) {
            if ($elem === 'color') {
                $this->setFill($value);
            } elseif ($elem === 'width') {
                $this->setStrokeWidth($value);
            }
        }
    }

    /**
     * Draw a single rectangle
     *
     * @param array     $point          The
     * @param string    $fill           The fill color to use
     * @param           $strokeWidth
     * @param null      $index
     *
     * @return Rect
     */
    private function drawSingleBar($point, $fill, $strokeWidth, $index = null)
    {
        $rect = new Rect($point[0] - ($this->barWidth / 2), $point[1], $this->barWidth, 100 - $point[1]);
        $rect->setFill($fill);
        $rect->setStrokeWidth($strokeWidth);
        $rect->setStrokeColor('black');
        if (isset($index)) {
            $rect->setAttribute('data-icinga-graph-index', $index);
        }
        $rect->setAttribute('data-icinga-graph-type', 'bar');
        $rect->setAdditionalStyle('clip-path: url(#clip);');
        return $rect;
    }

    /**
     * Render this BarChart
     *
     * @param   RenderContext   $ctx    The rendering context to use for drawing
     *
     * @return  DOMElement      $dom    Element
     */
    public function toSvg(RenderContext $ctx)
    {
        $doc = $ctx->getDocument();
        $group = $doc->createElement('g');
        $idx = 0;

        if (count($this->dataSet) > 15) {
            $this->barWidth = 2;
        }
        if (count($this->dataSet) > 25) {
            $this->barWidth = 1;
        }

        foreach ($this->dataSet as $x => $point) {
            // add white background bar, to prevent other bars from altering transparency effects
            $bar = $this->drawSingleBar($point, 'white', $this->strokeWidth, $idx++)->toSvg($ctx);
            $group->appendChild($bar);

            // draw actual bar
            $bar = $this->drawSingleBar($point, $this->fill, $this->strokeWidth)->toSvg($ctx);
            if (isset($this->tooltips[$x])) {
                $data = array(
                    'label' => isset($this->graphs[$this->order]['label']) ?
                            strtolower($this->graphs[$this->order]['label']) : '',
                    'color' => isset($this->graphs[$this->order]['color']) ?
                            strtolower($this->graphs[$this->order]['color']) : '#fff'
                );
                $format = isset($this->graphs[$this->order]['tooltip'])
                    ? $this->graphs[$this->order]['tooltip'] : null;
                $title = $ctx->getDocument()->createElement('title');
                $title->textContent = $this->tooltips[$x]->renderNoHtml($this->order, $data, $format);
                $bar->appendChild($title);
            }
            $group->appendChild($bar);
        }
        return $group;
    }
}