Welcome to mirror list, hosted at ThFree Co, Russian Federation.

HorizontalBar.php « StaticGraph « ImageGraph « plugins - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 2f6f4b8ca5106c0b308a1754ef6772d811c08be3 (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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
<?php
/**
 * Piwik - free/libre analytics platform
 *
 * @link http://piwik.org
 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 *
 */
namespace Piwik\Plugins\ImageGraph\StaticGraph;
use Piwik\Piwik;

/**
 *
 */
class HorizontalBar extends GridGraph
{
    const INTERLEAVE = 0.30;
    const PADDING_CHARS = ' ';
    const LEGEND_SQUARE_WIDTH = 11;
    const MIN_SPACE_BETWEEN_HORIZONTAL_VALUES = 5;
    const LOGO_MIN_RIGHT_MARGIN = 3;

    public function renderGraph()
    {
        $verticalLegend = false;

        // determine the maximum logo width & height
        list($maxLogoWidth, $maxLogoHeight) = self::getMaxLogoSize($this->abscissaLogos);

        foreach ($this->abscissaLogos as $logoPath) {
            list($logoWidth, $logoHeight) = self::getLogoSize($logoPath);
            $logoPathToHeight[$logoPath] = $logoHeight;
        }

        // truncate report
        $graphHeight = $this->getGraphBottom($horizontalGraph = true) - $this->getGridTopMargin($horizontalGraph = true, $verticalLegend);

        list($abscissaMaxWidth, $abscissaMaxHeight) = $this->getMaximumTextWidthHeight($this->abscissaSeries);
        list($ordinateMaxWidth, $ordinateMaxHeight) = $this->getMaximumTextWidthHeight($this->ordinateSeries);

        $numberOfSeries = count($this->ordinateSeries);
        $ordinateMaxHeight = $ordinateMaxHeight * $numberOfSeries;

        $textMaxHeight = $abscissaMaxHeight > $ordinateMaxHeight ? $abscissaMaxHeight : $ordinateMaxHeight;

        $minLineWidth = ($textMaxHeight > $maxLogoHeight ? $textMaxHeight : $maxLogoHeight) + (self::MIN_SPACE_BETWEEN_HORIZONTAL_VALUES * $numberOfSeries);
        $maxNumOfValues = floor($graphHeight / $minLineWidth);
        $abscissaSeriesCount = count($this->abscissaSeries);

        if ($maxNumOfValues < $abscissaSeriesCount - 1) {
            $sumOfOthers = array();
            $truncatedOrdinateSeries = array();
            $truncatedAbscissaLogos = array();
            $truncatedAbscissaSeries = array();
            foreach ($this->ordinateSeries as $column => $data) {
                $truncatedOrdinateSeries[$column] = array();
                $sumOfOthers[$column] = 0;
            }

            $i = 0;
            for (; $i < $maxNumOfValues; $i++) {
                foreach ($this->ordinateSeries as $column => $data) {
                    $truncatedOrdinateSeries[$column][] = $data[$i];
                }

                $truncatedAbscissaLogos[] = isset($this->abscissaLogos[$i]) ? $this->abscissaLogos[$i] : null;
                $truncatedAbscissaSeries[] = $this->abscissaSeries[$i];
            }

            for (; $i < $abscissaSeriesCount; $i++) {
                foreach ($this->ordinateSeries as $column => $data) {
                    $sumOfOthers[$column] += $data[$i];
                }
            }

            foreach ($this->ordinateSeries as $column => $data) {
                $truncatedOrdinateSeries[$column][] = $sumOfOthers[$column];
            }

            $truncatedAbscissaSeries[] = Piwik::translate('General_Others');
            $this->abscissaSeries = $truncatedAbscissaSeries;
            $this->ordinateSeries = $truncatedOrdinateSeries;
            $this->abscissaLogos = $truncatedAbscissaLogos;
        }

        // blank characters are used to pad labels so the logo can be displayed
        $paddingText = '';
        $paddingWidth = 0;
        if ($maxLogoWidth > 0) {
            while ($paddingWidth < $maxLogoWidth + self::LOGO_MIN_RIGHT_MARGIN) {
                $paddingText .= self::PADDING_CHARS;
                list($paddingWidth, $paddingHeight) = $this->getTextWidthHeight($paddingText);
            }
        }

        // determine the maximum label width according to the minimum comfortable graph size
        $gridRightMargin = $this->getGridRightMargin($horizontalGraph = true);
        $minGraphSize = ($this->width - $gridRightMargin) / 2;

        $metricLegendWidth = 0;
        foreach ($this->ordinateLabels as $column => $label) {
            list($textWidth, $textHeight) = $this->getTextWidthHeight($label);
            $metricLegendWidth += $textWidth;
        }

        $legendWidth = $metricLegendWidth + ((self::HORIZONTAL_LEGEND_LEFT_MARGIN + self::LEGEND_SQUARE_WIDTH) * $numberOfSeries);
        if ($this->showLegend) {
            if ($legendWidth > $minGraphSize) {
                $minGraphSize = $legendWidth;
            }
        }

        $gridLeftMarginWithoutLabels = $this->getGridLeftMargin($horizontalGraph = true, $withLabel = false);
        $labelWidthLimit =
            $this->width
            - $gridLeftMarginWithoutLabels
            - $gridRightMargin
            - $paddingWidth
            - $minGraphSize;

        // truncate labels if needed
        foreach ($this->abscissaSeries as &$label) {
            $label = $this->truncateLabel($label, $labelWidthLimit);
        }

        $gridLeftMarginBeforePadding = $this->getGridLeftMargin($horizontalGraph = true, $withLabel = true);

        // pad labels for logo space
        foreach ($this->abscissaSeries as &$label) {
            $label .= $paddingText;
        }

        $this->initGridChart(
            $displayVerticalGridLines = false,
            $bulletType = LEGEND_FAMILY_BOX,
            $horizontalGraph = true,
            $showTicks = false,
            $verticalLegend
        );

        $valueColor = $this->textColor;
        $this->pImage->drawBarChart(
            array(
                 'DisplayValues' => true,
                 'Interleave'    => self::INTERLEAVE,
                 'DisplayR'      => $valueColor['R'],
                 'DisplayG'      => $valueColor['G'],
                 'DisplayB'      => $valueColor['B'],
            )
        );

//		// display icons
        $graphData = $this->pData->getData();
        $numberOfRows = count($this->abscissaSeries);
        $logoInterleave = $this->getGraphHeight(true, $verticalLegend) / $numberOfRows;
        for ($i = 0; $i < $numberOfRows; $i++) {
            if (isset($this->abscissaLogos[$i])) {
                $logoPath = $this->abscissaLogos[$i];

                if (isset($logoPathToHeight[$logoPath])) {
                    $logoHeight = $logoPathToHeight[$logoPath];

                    $pathInfo = pathinfo($logoPath);
                    $logoExtension = strtoupper($pathInfo['extension']);
                    $drawingFunction = 'drawFrom' . $logoExtension;

                    $logoYPosition =
                        ($logoInterleave * $i)
                        + $this->getGridTopMargin(true, $verticalLegend)
                        + $graphData['Axis'][1]['Margin']
                        - $logoHeight / 2
                        + 1;

                    if (method_exists($this->pImage, $drawingFunction)) {
                        $this->pImage->$drawingFunction(
                            $gridLeftMarginBeforePadding,
                            $logoYPosition,
                            $logoPath
                        );
                    }
                }
            }
        }
    }
}