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

StackedBarEvolution.php « JqplotDataGenerator « PagePerformance « plugins - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 9c005dc620ef9e8ff366ccf1b24c0df00e365b05 (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
<?php
/**
 * Matomo - free/libre analytics platform
 *
 * @link https://matomo.org
 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 *
 */
namespace Piwik\Plugins\PagePerformance\JqplotDataGenerator;

use Piwik\DataTable;
use Piwik\Period;
use Piwik\Plugins\CoreVisualizations\JqplotDataGenerator;

/**
 * Generates JQPlot JSON data/config for evolution graphs.
 */
class StackedBarEvolution extends JqplotDataGenerator\Evolution
{
    public function generate($dataTable)
    {
        $visualization = new Chart();

        if ($dataTable->getRowsCount() > 0) {
            $dataTable->applyQueuedFilters();
            $this->initChartObjectData($dataTable, $visualization);
        }

        return $visualization->render();
    }

    /**
     * @param DataTable|DataTable\Map $dataTable
     * @param Chart $visualization
     */
    protected function initChartObjectData($dataTable, $visualization)
    {
        // if the loaded datatable is a simple DataTable, it is most likely a plugin plotting some custom data
        // we don't expect plugin developers to return a well defined Set

        if ($dataTable instanceof DataTable) {
            parent::initChartObjectData($dataTable, $visualization);
            return;
        }

        $dataTables = $dataTable->getDataTables();

        // determine x labels based on both the displayed date range and the compared periods
        /** @var Period[][] $xLabels */
        $xLabels = [
            [], // placeholder for first series
        ];

        $this->addSelectedSeriesXLabels($xLabels, $dataTables);

        $columnsToDisplay = array_values($this->properties['columns_to_display']);

        // collect series data to show. each row-to-display/column-to-display permutation creates a series.
        $allSeriesData = array();
        foreach ($columnsToDisplay as $column) {
            $allSeriesData[] = $this->getSeriesData($column, $dataTable);
        }

        $visualization->dataTable = $dataTable;
        $visualization->properties = $this->properties;

        $seriesLabels = [];
        foreach ($columnsToDisplay as $columnName) {
            $seriesLabels[] = [
                'internalLabel' => $columnName,
                'label' => @$this->properties['translations'][$columnName] ?: $columnName
            ];
        }

        $visualization->setAxisYValues($allSeriesData, $seriesLabels);
        $visualization->setAxisYUnits($this->getUnitsForColumnsToDisplay());

        $xLabelStrs = [];
        $xAxisTicks = [];
        foreach ($xLabels as $index => $seriesXLabels) {
            $xLabelStrs[$index] = array_map(function (Period $p) { return $p->getLocalizedLongString(); }, $seriesXLabels);
            $xAxisTicks[$index] = array_map(function (Period $p) { return $p->getLocalizedShortString(); }, $seriesXLabels);
        }

        $visualization->setAxisXLabelsMultiple($xLabelStrs, [], $xAxisTicks);
    }

    private function getSeriesData($column, DataTable\Map $dataTable)
    {
        $seriesData = array();
        foreach ($dataTable->getDataTables() as $childTable) {
            $row = $childTable->getFirstRow();

            // get series data point. defaults to 0 if no row or no column value.
            if ($row === false) {
                $seriesData[] = 0;
            } else {
                $seriesData[] = $row->getColumn($column) ? : 0;
            }
        }

        return $seriesData;
    }
}