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
|
<?php
require_once "ViewDataTable/GenerateGraphData.php";
/**
* Piwik_ViewDataTable_GenerateGraphData for the Evolution graph (eg. Last 30 days visits) using Piwik_Visualization_ChartEvolution
*
* @package Piwik_ViewDataTable
*
*/
class Piwik_ViewDataTable_GenerateGraphData_ChartEvolution extends Piwik_ViewDataTable_GenerateGraphData
{
protected function getViewDataTableId()
{
return 'generateDataChartEvolution';
}
function __construct()
{
require_once "Visualization/ChartEvolution.php";
$this->view = new Piwik_Visualization_ChartEvolution;
}
var $lineLabels = array();
var $data = array();
private function generateLine( $dataArray, $columns, $schema = "##label## ##column##" )
{
$data = array();
foreach($dataArray as $keyName => $table)
{
$table->applyQueuedFilters();
// initialize data (default values for all lines is 0)
$dataRow = array();
$rows = $table->getRows();
foreach($rows as $row)
{
$rowLabel = $schema;
if( strpos($rowLabel, "##label##") !== false )
{
$rowLabel = str_replace("##label##", $row->getColumn('label'), $rowLabel);
}
foreach($columns as $col)
{
$label = $rowLabel;
if( strpos($label, "##column##") !== false )
{
$label = str_replace("##column##", $col, $label);
}
if( !isset($this->lineLabels[$label]) )
{
$this->lineLabels[$label] = count($this->lineLabels);
}
$lineNb = $this->lineLabels[$label];
$value = $row->getColumn($col);
$dataRow['value'.$lineNb] = $value;
}
}
$data[] = $dataRow;
}
return $data;
}
private function generateLabels( $dataArray )
{
$data = array();
foreach($dataArray as $keyName => $table)
{
$table->applyQueuedFilters();
$data[] = array('label' => $keyName);
}
return $data;
}
private function addArray( &$data, $newData )
{
for($i = 0; $i < count($newData); $i++)
{
foreach($newData[$i] as $key => $value)
{
$data[$i][$key] = $value;
}
}
}
private function fillValues( &$data )
{
$nbLines = count($this->lineLabels);
for($i = 0; $i < count($data); $i++)
{
for($j = 0; $j < $nbLines; $j++)
{
if( !isset($data[$i]['value'.$j]) )
{
$data[$i]['value'.$j] = 0;
}
}
}
}
/*
* generates data for evolution graph from a numeric DataTable (DataTable that has only 'label' and 'value' columns)
*/
protected function generateDataFromNumericDataTable($dataArray, $siteLabel = "")
{
$columnsToDisplay = Piwik_Common::getRequestVar('columns', array(), 'array');
// for numeric we want to have only one column name
if( count($columnsToDisplay) != 1 )
{
$columnsToDisplay = array( 'nb_uniq_visitors' );
}
$label = $siteLabel . array_shift($columnsToDisplay);
$this->addArray($this->data, $this->generateLabels($dataArray));
$this->addArray($this->data, $this->generateLine($dataArray,array('value'),$label));
$this->fillValues($this->data);
}
/*
* generates data for evolution graph from a DataTable that has named columns (i.e. 'nb_hits', 'nb_uniq_visitors')
*/
protected function generateDataFromRegularDataTable($dataArray, $siteLabel = "")
{
// get list of columns to display i.e. array('nb_hits','nb_uniq_visitors')
$columnsToDisplay = Piwik_Common::getRequestVar('columns', array(), 'array');
// default column
if( count($columnsToDisplay) == 0 )
{
$columnsToDisplay = array( 'nb_uniq_visitors' );
}
$this->addArray($this->data, $this->generateLabels($dataArray));
$this->addArray($this->data, $this->generateLine($dataArray, $columnsToDisplay, $siteLabel."##label## ##column##"));
$this->fillValues($this->data);
}
protected function handleSiteGenerateDataFromDataTable($dataArray, $siteLabel = "")
{
// detect if we got numeric Datatable or regular DataTable
foreach($dataArray as $table)
{
$row = $table->getFirstRow();
if( $row != null )
{
$columns = $row->getColumns();
// if we got 2 columns - 'label' and 'value' this is numeric DataTable
if( count($columns) == 2 && isset($columns['label']) && isset($columns['value']) )
{
$this->generateDataFromNumericDataTable($dataArray, $siteLabel);
}
else
{
$this->generateDataFromRegularDataTable($dataArray, $siteLabel);
}
break;
}
}
}
public function generateDataFromDataTable()
{
$data = array();
if( $this->dataTable->getRowsCount() )
{
$row = null;
// find first table with rows
foreach($this->dataTable->getArray() as $idsite => $table)
{
// detect if we got data from more than one site
if( $table instanceof Piwik_DataTable_Array)
{
// multiple sites
$site = new Piwik_Site($idsite);
$this->handleSiteGenerateDataFromDataTable($table->getArray(), $site->getName()." ");
}
else if( $table instanceof Piwik_DataTable_Simple && $this->dataTable->getKeyName() == 'idSite')
{
// multiple sites (when numeric DataTable)
$site = new Piwik_Site($idsite);
$this->handleSiteGenerateDataFromDataTable($table->getFirstRow()->getColumn('value')->getArray(), $site->getName()." ");
}
else
{
// single site
$this->handleSiteGenerateDataFromDataTable($this->dataTable->getArray());
break;
}
}
}
array_unshift($this->data, array_keys($this->lineLabels));
return $this->data;
}
}
|