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

Chart.php « Visualization « core - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 3c6374e8525a69857b3bc7d39dd07b00782e6963 (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
186
187
<?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\Core\Piwik;
use Piwik\Core\Common;

/**
 * Generates the data in the Open Flash Chart format, from the given data.
 *
 * @package Piwik
 * @subpackage Piwik_Visualization
 */
abstract class Piwik_Visualization_Chart implements Piwik_View_Interface
{

    // the data kept here conforms to the jqplot data layout
    // @see http://www.jqplot.com/docs/files/jqPlotOptions-txt.html
    protected $series = array();
    protected $data = array();
    protected $axes = array();
    protected $tooltip = array();
    protected $seriesColors = array('#000000');
    protected $seriesPicker = array();

    // other attributes (not directly used for jqplot)
    protected $maxValue;
    protected $yUnit = '';
    protected $displayPercentageInTooltip = true;
    protected $xSteps = 2;

    /**
     * Whether to show every x-axis tick or only every other one.
     */
    protected $showAllTicks = false;

    public function setAxisXLabels(&$xLabels)
    {
        $this->axes['xaxis']['ticks'] = & $xLabels;
    }

    public function setAxisXOnClick(&$onClick)
    {
        $this->axes['xaxis']['onclick'] = & $onClick;
    }

    public function setAxisYValues(&$values)
    {
        foreach ($values as $label => &$data) {
            $this->series[] = array(
                // unsanitize here is safe since data gets outputted as JSON, not HTML
                // NOTE: this is a quick fix for a double-encode issue. if this file is refactored,
                // this fix can probably be removed (or at least made more understandable).
                'label'         => Common::unsanitizeInputValue($label),
                'internalLabel' => $label
            );

            array_walk($data, create_function('&$v', '$v = (float)$v;'));
            $this->data[] = & $data;
        }
    }

    protected function addTooltipToValue($seriesIndex, $valueIndex, $tooltipTitle, $tooltipText)
    {
        $this->tooltip[$seriesIndex][$valueIndex] = array($tooltipTitle, $tooltipText);
    }

    public function setAxisYUnit($yUnit)
    {
        $yUnits = array();
        for ($i = 0; $i < count($this->data); $i++) {
            $yUnits[] = $yUnit;
        }
        $this->setAxisYUnits($yUnits);
    }

    public function setAxisYUnits($yUnits)
    {
        // generate an axis config for each unit
        $axesIds = array();
        // associate each series with the appropriate axis
        $seriesAxes = array();
        // units for tooltips
        $seriesUnits = array();
        foreach ($yUnits as $unit) {
            // handle axes ids: first y[]axis, then y[2]axis, y[3]axis...
            $nextAxisId = empty($axesIds) ? '' : count($axesIds) + 1;

            $unit = $unit ? $unit : '';
            if (!isset($axesIds[$unit])) {
                $axesIds[$unit] = array('id' => $nextAxisId, 'unit' => $unit);
                $seriesAxes[] = 'y' . $nextAxisId . 'axis';
            } else {
                // reuse existing axis
                $seriesAxes[] = 'y' . $axesIds[$unit]['id'] . 'axis';
            }
            $seriesUnits[] = $unit;
        }

        // generate jqplot axes config
        foreach ($axesIds as $axis) {
            $axisKey = 'y' . $axis['id'] . 'axis';
            $this->axes[$axisKey]['tickOptions']['formatString'] = '%s' . $axis['unit'];
        }

        $this->tooltip['yUnits'] = $seriesUnits;

        // add axis config to series
        foreach ($seriesAxes as $i => $axisName) {
            $this->series[$i]['yaxis'] = $axisName;
        }
    }

    public function setAxisYLabels($labels)
    {
        foreach ($this->series as &$series) {
            $label = $series['internalLabel'];
            if (isset($labels[$label])) {
                $series['label'] = $labels[$label];
            }
        }
    }

    public function setSelectableColumns($selectableColumns, $multiSelect = true)
    {
        $this->seriesPicker['selectableColumns'] = $selectableColumns;
        $this->seriesPicker['multiSelect'] = $multiSelect;
    }

    public function setDisplayPercentageInTooltip($display)
    {
        $this->displayPercentageInTooltip = $display;
    }

    public function setXSteps($steps)
    {
        $this->xSteps = $steps;
    }

    /**
     * Show every x-axis tick instead of just every other one.
     */
    public function showAllTicks()
    {
        $this->showAllTicks = true;
    }

    public function render()
    {
        Piwik::overrideCacheControlHeaders();

        // See http://www.jqplot.com/docs/files/jqPlotOptions-txt.html
        $data = array(
            'params'       => array(
                'axes'         => &$this->axes,
                'series'       => &$this->series,
                'seriesColors' => &$this->seriesColors
            ),
            'data'         => &$this->data,
            'tooltip'      => &$this->tooltip,
            'seriesPicker' => &$this->seriesPicker
        );

        Piwik_PostEvent('Visualization_Chart.render', array(&$data));
        return Common::json_encode($data);
    }

    public function customizeChartProperties()
    {
        // x axis labels with steps
        if (isset($this->axes['xaxis']['ticks'])) {
            foreach ($this->axes['xaxis']['ticks'] as $i => &$xLabel) {
                $this->axes['xaxis']['labels'][$i] = $xLabel;
                if (!$this->showAllTicks && ($i % $this->xSteps) != 0) {
                    $xLabel = ' ';
                }
            }
        }
    }

}