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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
|
<?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
*/
namespace Piwik;
use Exception;
use Piwik\Common;
use Piwik\Metrics;
use Piwik\DataTable;
use Piwik\Visualization;
/**
* Generates JSON data used to configure and populate JQPlot graphs.
*
* Supports pie graphs, bar graphs and time serieses (aka, evolution graphs).
*/
class JqplotDataGenerator
{
/**
* View properties. @see Piwik_ViewDataTable for more info.
*
* @var array
*/
protected $properties;
/**
* This object does most of the work in generating the JQPlot JSON data.
*
* @var Visualization\
*/
protected $visualization;
/**
* Creates a new JqplotDataGenerator instance for a graph type and view properties.
*
* @param string $type 'pie', 'bar', or 'evolution'
* @param array $properties The view properties.
* @throws \Exception
* @return \Piwik\JqplotDataGenerator
*/
public static function factory($type, $properties)
{
switch ($type) {
case 'evolution':
return new JqplotDataGenerator\Evolution($properties);
case 'pie':
$visualization = new Visualization\Chart\Pie();
return new JqplotDataGenerator($visualization, $properties);
case 'bar':
$visualization = new Visualization\Chart\VerticalBar();
return new JqplotDataGenerator($visualization, $properties);
default:
throw new Exception("Unknown JqplotDataGenerator type '$type'.");
}
}
/**
* Constructor.
*
* @param Visualization\ $visualization
* @param array $properties
*/
public function __construct($visualization, $properties)
{
$this->visualization = $visualization;
$this->properties = $properties;
}
/**
* Generates JSON graph data and returns it.
*
* @param DataTable|DataTable\Map $dataTable
* @return string
*/
public function generate($dataTable)
{
if (!empty($this->properties['graph_limit'])) {
$offsetStartSummary = $this->properties['graph_limit'] - 1;
$sortColumn = !empty($this->properties['filter_sort_column'])
? $this->properties['filter_sort_column']
: Metrics::INDEX_NB_VISITS;
$dataTable->filter(
'AddSummaryRow', array($offsetStartSummary, Piwik_Translate('General_Others'), $sortColumn));
}
if ($dataTable->getRowsCount() > 0) {
// if addTotalRow was called in GenerateGraphHTML, add a row containing totals of
// different metrics
if (!empty($this->properties['add_total_row'])) {
$dataTable->queueFilter('AddSummaryRow', array(0, Piwik_Translate('General_Total'), null, false));
}
$this->initChartObjectData($dataTable);
}
$this->visualization->customizeChartProperties();
return $this->visualization->render();
}
/**
* @param DataTable|DataTable\Map $dataTable
*/
protected function initChartObjectData($dataTable)
{
$dataTable->applyQueuedFilters();
// We apply a filter to the DataTable, decoding the label column (useful for keywords for example)
$dataTable->filter('ColumnCallbackReplace', array('label', 'urldecode'));
$xLabels = $dataTable->getColumn('label');
$columnNames = $this->properties['columns_to_display'];
if (($labelColumnIndex = array_search('label', $columnNames)) !== false) {
unset($columnNames[$labelColumnIndex]);
}
$columnNameToTranslation = $columnNameToValue = array();
foreach ($columnNames as $columnName) {
$columnNameToTranslation[$columnName] = @$this->properties['translations'][$columnName];
$columnNameToValue[$columnName] = $dataTable->getColumn($columnName);
}
$visualization = $this->visualization;
$visualization->setAxisXLabels($xLabels);
$visualization->setAxisYValues($columnNameToValue);
$visualization->setAxisYLabels($columnNameToTranslation);
$visualization->setAxisYUnit($this->properties['y_axis_unit']);
$visualization->setDisplayPercentageInTooltip($this->properties['display_percentage_in_tooltip']);
// show_all_ticks is not real query param, it is set by GenerateGraphHTML.
if ($this->properties['show_all_ticks']) {
$visualization->showAllTicks();
}
$units = $this->getUnitsForColumnsToDisplay();
$visualization->setAxisYUnits($units);
$this->addSeriesPickerToView();
}
protected function getUnitsForColumnsToDisplay()
{
// derive units from column names
$units = $this->deriveUnitsFromRequestedColumnNames();
if (!empty($this->properties['y_axis_unit'])) {
// force unit to the value set via $this->setAxisYUnit()
foreach ($units as &$unit) {
$unit = $this->properties['y_axis_unit'];
}
}
// the bar charts contain the labels a first series
// this series has to be removed from the units
if ($this->visualization instanceof Visualization\Chart\VerticalBar) {
array_shift($units);
}
return $units;
}
private function deriveUnitsFromRequestedColumnNames()
{
$idSite = Common::getRequestVar('idSite', null, 'int');
$units = array();
foreach ($this->properties['columns_to_display'] as $columnName) {
$derivedUnit = Metrics::getUnit($columnName, $idSite);
$units[$columnName] = empty($derivedUnit) ? false : $derivedUnit;
}
return $units;
}
/**
* Used in initChartObjectData to add the series picker config to the view object
*/
protected function addSeriesPickerToView()
{
if (count($this->properties['selectable_columns'])
&& Common::getRequestVar('showSeriesPicker', $this->properties['show_series_picker']) == 1
) {
$selectableColumns = array();
foreach ($this->properties['selectable_columns'] as $column) {
$selectableColumns[] = array(
'column' => $column,
'translation' => @$this->properties['translations'][$column],
'displayed' => in_array($column, $this->properties['columns_to_display'])
);
}
$this->visualization->setSelectableColumns(
$selectableColumns, $this->properties['allow_multi_select_series_picker']);
}
}
}
|