-
Notifications
You must be signed in to change notification settings - Fork 17
/
TreemapDataGenerator.php
334 lines (287 loc) · 10.1 KB
/
TreemapDataGenerator.php
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
<?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\TreemapVisualization;
use Piwik\Archive\DataTableFactory;
use Piwik\Common;
use Piwik\DataTable;
use Piwik\DataTable\Filter\CalculateEvolutionFilter;
use Piwik\DataTable\Map;
use Piwik\Piwik;
/**
* A utility class that generates JSON data meant to be used with the JavaScript
* Infovis Toolkit's treemap visualization.
*/
class TreemapDataGenerator
{
public const DEFAULT_MAX_ELEMENTS = 10;
public const MIN_NODE_AREA = 400; // 20px * 20px
/**
* The list of row metadata that should appear in treemap JSON data, if in the row.
*
* @var array
*/
private static $rowMetadataToCopy = array('logo', 'url');
/**
* The name of the root node.
*
* @var string
*/
private $rootName = '';
/**
* The offset of the first row in the DataTable. When exploring aggregate rows (ie, the
* 'Others' row), the DataTable used won't have the initial rows, so the row offsets
* aren't the same as the row IDs. In order to make sure each node has a unique ID,
* we need to to know the actual row offset of each row.
*
* @var int
*/
private $firstRowOffset = 0;
/**
* The name of the metric to generate a treemap for.
*
* @var string
*/
private $metricToGraph;
/**
* The internationalized label of the metric to graph. Used in the tooltip of each node.
*
* @var string
*/
private $metricTranslation;
/**
* The available screen width for the treemap visualization.
*
* @var int
*/
private $availableWidth;
/**
* The available screen height for the treemap visualization.
*
* @var int
*/
private $availableHeight;
/**
* Whether to include evolution values in the output JSON.
*
* @var bool
*/
private $showEvolutionValues = false;
/**
* Holds the date of the past period. Implementation detail.
*
* @var string
*/
private $pastDataDate = null;
/**
* Callback used to format row labels before they are used in treemap nodes.
*
* @var callback
*/
private $labelFormatter = null;
/**
* Constructor.
*
* @param string $metricToGraph @see self::$metricToGraph
* @param string $metricTranslation
*/
public function __construct($metricToGraph, $metricTranslation)
{
$this->metricToGraph = $metricToGraph;
$this->metricTranslation = $metricTranslation;
}
/**
* Sets the name of the root node.
*
* @param string $name
*/
public function setRootNodeName($name)
{
$this->rootName = $name;
}
/**
* Sets the offset of the first row in the converted DataTable.
*
* @param int $offset
*/
public function setInitialRowOffset($offset)
{
$this->firstRowOffset = (int)$offset;
}
/**
* Configures the generator to calculate the evolution of column values and include
* this data in the outputted tree structure.
*/
public function showEvolutionValues()
{
$this->showEvolutionValues = true;
}
/**
* Sets the callback used to format row labels before they are used in treemap nodes.
*
* @param callback $formatter
*/
public function setLabelFormatter($formatter)
{
$this->labelFormatter = $formatter;
}
/**
* Sets the available screen dimensions for this visualization.
*
* @param int $availableWidth The available screen width for the display.
* @param int $availableHeight The available screen height for the display.
*/
public function setAvailableDimensions($availableWidth, $availableHeight)
{
$this->availableWidth = $availableWidth;
$this->availableHeight = $availableHeight;
}
/**
* Generates an array that can be encoded as JSON and used w/ the JavaScript Infovis Toolkit.
*
* @param \Piwik\DataTable $dataTable
* @return array
*/
public function generate($dataTable)
{
// sanity check: if the dataTable is not a Map, we don't have the data to calculate evolution
// values, so make sure we don't try
if (!($dataTable instanceof Map)) {
$this->showEvolutionValues = false;
}
// if showEvolutionValues is true, $dataTable must be a DataTable\Map w/ two child tables
$pastData = false;
if ($this->showEvolutionValues) {
$pastData = $dataTable->getFirstRow();
$dataTable = $dataTable->getLastRow();
$this->pastDataDate = $pastData->getMetadata(DataTableFactory::TABLE_METADATA_PERIOD_INDEX)->getLocalizedShortString();
}
$root = $this->makeNode('treemap-root', $this->rootName);
$tableId = Common::getRequestVar('idSubtable', '');
$this->addDataTableToNode($root, $dataTable, $pastData, $tableId, $this->firstRowOffset);
return $root;
}
/**
* Computes the maximum number of elements allowed in the report to display, based on the
* available screen width/height, and truncates the report data so the number of rows
* will not exceed the max.
*
* @param DataTable $dataTable The report data. Must be sorted by the metric to graph.
* @param int $availableWidth Available width in pixels.
* @param int $availableHeight Available height in pixels.
*/
public function truncateBasedOnAvailableSpace($dataTable)
{
$truncateAfter = self::DEFAULT_MAX_ELEMENTS - 1;
if (
is_numeric($this->availableWidth)
&& is_numeric($this->availableHeight)
) {
$totalArea = $this->availableWidth * $this->availableHeight;
$dataTable->filter('ReplaceColumnNames');
$metricValues = $dataTable->getColumn($this->metricToGraph);
$metricSum = array_sum($metricValues);
if ($metricSum != 0) {
// find the row index in $dataTable for which all rows after it will have treemap
// nodes that are too small. this is the row from which we truncate.
// Note: $dataTable is sorted at this point, so $metricValues is too
$result = 0;
foreach ($metricValues as $value) {
$nodeArea = ($totalArea * $value) / $metricSum;
if ($nodeArea < self::MIN_NODE_AREA) {
break;
} else {
++$result;
}
}
$truncateAfter = $result;
}
}
$dataTable->filter('Truncate', array($truncateAfter));
}
private function addDataTableToNode(&$node, $dataTable, $pastData = false, $tableId = '', $offset = 0)
{
foreach ($dataTable->getRows() as $rowId => $row) {
$pastRow = $pastData ? $pastData->getRowFromLabel($row->getColumn('label')) : false;
$childNode = $this->makeNodeFromRow($tableId, $rowId, $row, $pastRow);
if (empty($childNode)) {
continue;
}
if ($rowId == DataTable::ID_SUMMARY_ROW) {
$childNode['data']['aggregate_offset'] = $offset + $dataTable->getRowsCount() - 1;
} elseif ($row->getIdSubDataTable() !== null) {
$childNode['data']['idSubtable'] = $row->getIdSubDataTable();
}
$node['children'][] = $childNode;
}
}
private function makeNodeFromRow($tableId, $rowId, $row, $pastRow)
{
$label = $row->getColumn('label');
if ($this->labelFormatter) {
$formatter = $this->labelFormatter;
$label = $formatter($row, $label);
}
$columnValue = $row->getColumn($this->metricToGraph) ?: 0;
if ($columnValue == 0) { // avoid issues in JIT w/ 0 $area values
return false;
}
$data = array();
$data['$area'] = $columnValue;
// add metadata
if ($rowId !== DataTable::ID_SUMMARY_ROW) {
foreach (self::$rowMetadataToCopy as $metadataName) {
$metadataValue = $row->getMetadata($metadataName);
if ($metadataValue !== false) {
$data['metadata'][$metadataName] = $metadataValue;
}
}
}
// add evolution
if (
$rowId !== DataTable::ID_SUMMARY_ROW
&& $this->showEvolutionValues
) {
if ($pastRow === false) {
$data['evolution'] = 100;
} else {
$pastValue = $pastRow->getColumn($this->metricToGraph) ?: 0;
$data['evolution'] = CalculateEvolutionFilter::calculate(
$columnValue,
$pastValue,
$quotientPrecision = 0,
$appendPercentSign = false
);
}
}
// add node tooltip
$data['metadata']['tooltip'] = "\n" . $columnValue . ' ' . $this->metricTranslation;
if (isset($data['evolution'])) {
$plusOrMinus = $data['evolution'] >= 0 ? '+' : '-';
$evolutionChange = $plusOrMinus . abs($data['evolution']) . '%';
$data['metadata']['tooltip'] = Piwik::translate('General_XComparedToY', array(
$data['metadata']['tooltip'] . "\n" . $evolutionChange,
$this->pastDataDate
));
}
return $this->makeNode($this->getNodeId($tableId, $rowId), $label, $data);
}
private function getNodeId($tableId, $rowId)
{
if ($rowId == DataTable::ID_SUMMARY_ROW) {
$rowId = $this->firstRowOffset . '_' . $rowId;
} else {
$rowId = $this->firstRowOffset += $rowId;
}
return $tableId . '_' . $rowId;
}
private function makeNode($id, $title, $data = array())
{
return array('id' => $id, 'name' => $title, 'data' => $data, 'children' => array());
}
}