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

Sparkline.php « ViewDataTable « core - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: ae85101a7812b81016cea1c7d859ecf1e82c6e71 (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
<?php
/**
 * Piwik - Open source web analytics
 *
 * @link http://piwik.org
 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 *
 * @category Piwik
 * @package Piwik
 */
use Piwik\Common;

/**
 * Reads the requested DataTable from the API and prepare data for the Sparkline view.
 *
 * @package Piwik
 * @subpackage Piwik_ViewDataTable
 */
class Piwik_ViewDataTable_Sparkline extends Piwik_ViewDataTable
{
    protected function getViewDataTableId()
    {
        return 'sparkline';
    }

    /**
     * @see Piwik_ViewDataTable::main()
     * @return mixed
     */
    public function main()
    {
        if ($this->mainAlreadyExecuted) {
            return;
        }
        $this->mainAlreadyExecuted = true;

        // If period=range, we force the sparkline to draw daily data points
        $period = Common::getRequestVar('period');
        if ($period == 'range') {
            $_GET['period'] = 'day';
        }
        $this->loadDataTableFromAPI();
        // then revert the hack for potentially subsequent getRequestVar
        $_GET['period'] = $period;

        $values = $this->getValuesFromDataTable($this->dataTable);
        $this->isDataAvailable = true;
        if (empty($values)) {
            $values = array_fill(0, 30, 0);
            $this->isDataAvailable = false;
        }

        $graph = new Piwik_Visualization_Sparkline();
        $graph->setValues($values);

        $height = Common::getRequestVar('height', 0, 'int');
        if (!empty($height)) {
            $graph->setHeight($height);
        }

        $width = Common::getRequestVar('width', 0, 'int');
        if (!empty($width)) {
            $graph->setWidth($width);
        }

        $graph->main();

        $this->view = $graph;
    }

    protected function getValuesFromDataTableArray($dataTableArray, $columnToPlot)
    {
        $dataTableArray->applyQueuedFilters();
        $values = array();
        foreach ($dataTableArray->getArray() as $table) {
            if ($table->getRowsCount() > 1) {
                throw new Exception("Expecting only one row per DataTable");
            }
            $value = 0;
            $onlyRow = $table->getFirstRow();
            if ($onlyRow !== false) {
                if (!empty($columnToPlot)) {
                    $value = $onlyRow->getColumn($columnToPlot);
                } // if not specified, we load by default the first column found
                // eg. case of getLastDistinctCountriesGraph
                else {
                    $columns = $onlyRow->getColumns();
                    $value = current($columns);
                }
            }
            $values[] = $value;
        }
        return $values;
    }

    protected function getValuesFromDataTable($dataTable)
    {
        $columns = $this->getColumnsToDisplay();
        $columnToPlot = false;
        if (!empty($columns)) {
            $columnToPlot = $columns[0];
        }
        $values = false;
        // a Piwik_DataTable_Array is returned when using the normal code path to request data from Archives, in all core plugins
        // however plugins can also return simple datatable, hence why the sparkline can accept both data types
        if ($this->dataTable instanceof Piwik_DataTable_Array) {
            $values = $this->getValuesFromDataTableArray($dataTable, $columnToPlot);
        } elseif ($this->dataTable instanceof Piwik_DataTable) {
            $values = $this->dataTable->getColumn($columnToPlot);
        }
        return $values;
    }
}