Newer
Older
Benaka Moorthi
a validé
<?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_Plugins
* @package CoreVisualizations
Benaka Moorthi
a validé
*/
Benaka Moorthi
a validé
namespace Piwik\Plugins\CoreVisualizations\Visualizations\JqplotGraph;
Benaka Moorthi
a validé
use Piwik\Common;
use Piwik\DataTable;
Benaka Moorthi
a validé
use Piwik\Period\Range;
use Piwik\Plugin\Controller;
Benaka Moorthi
a validé
use Piwik\Plugins\CoreVisualizations\JqplotDataGenerator;
use Piwik\Plugins\CoreVisualizations\Visualizations\JqplotGraph;
use Piwik\Site;
use Piwik\ViewDataTable\Properties;
use Piwik\Visualization\Config;
use Piwik\Visualization\Request;
Benaka Moorthi
a validé
/**
Benaka Moorthi
a validé
* Visualization that renders HTML for a line graph using jqPlot.
Benaka Moorthi
a validé
*/
class Evolution extends JqplotGraph
{
const ID = 'graphEvolution';
const SERIES_COLOR_COUNT = 8;
Benaka Moorthi
a validé
/**
* Whether to show a line graph or a bar graph.
* Default value: true
Benaka Moorthi
a validé
*/
const SHOW_LINE_GRAPH = 'show_line_graph';
public static $clientSideProperties = array('show_line_graph');
Benaka Moorthi
a validé
public static $overridableProperties = array('show_line_graph');
public function init()
Benaka Moorthi
a validé
{
parent::init();
}
public function beforeLoadDataTable(Request $request, Config $properties)
{
parent::beforeLoadDataTable($request, $properties);
Benaka Moorthi
a validé
// period will be overridden when 'range' is requested in the UI
Benaka Moorthi
a validé
// but the graph will display for each day of the range.
// Default 'range' behavior is to return the 'sum' for the range
if (Common::getRequestVar('period', false) == 'range') {
$request->request_parameters_to_modify['period'] = 'day';
Benaka Moorthi
a validé
}
}
Benaka Moorthi
a validé
/**
* @param DataTable|DataTable\Map $dataTable
* @param \Piwik\Visualization\Config $properties
* @param \Piwik\Visualization\Request $request
*/
public function afterAllFilteresAreApplied($dataTable, Config $properties, Request $request)
{
parent::afterAllFilteresAreApplied($dataTable, $properties, $request);
Benaka Moorthi
a validé
$view = $this->viewDataTable;
Benaka Moorthi
a validé
if ($view->visualization_properties->x_axis_step_size === false) {
$view->visualization_properties->x_axis_step_size = $this->getDefaultXAxisStepSize($dataTable->getRowsCount());
Benaka Moorthi
a validé
}
}
Benaka Moorthi
a validé
public function configureVisualization(Config $properties)
{
$properties->datatable_js_type = 'JqplotEvolutionGraphDataTable';
Benaka Moorthi
a validé
}
public static function getDefaultPropertyValues()
Benaka Moorthi
a validé
{
$result = parent::getDefaultPropertyValues();
Benaka Moorthi
a validé
$result['show_all_views_icons'] = false;
$result['show_table'] = false;
$result['show_table'] = false;
$result['show_table_all_columns'] = false;
Benaka Moorthi
a validé
$result['hide_annotations_view'] = false;
Benaka Moorthi
a validé
$result['visualization_properties']['jqplot_graph']['x_axis_step_size'] = false;
Benaka Moorthi
a validé
$result['visualization_properties']['graphEvolution']['show_line_graph'] = true;
Benaka Moorthi
a validé
return $result;
}
protected function makeDataGenerator($properties)
{
return JqplotDataGenerator::factory('evolution', $properties);
}
/**
* Based on the period, date and evolution_{$period}_last_n query parameters,
* calculates the date range this evolution chart will display data for.
*/
Benaka Moorthi
a validé
{
Benaka Moorthi
a validé
$period = Common::getRequestVar('period');
$defaultLastN = self::getDefaultLastN($period);
$originalDate = Common::getRequestVar('date', 'last' . $defaultLastN, 'string');
if ($period != 'range') { // show evolution limit if the period is not a range
$view->show_limit_control = true;
Benaka Moorthi
a validé
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
// set the evolution_{$period}_last_n query param
if (Range::parseDateRange($originalDate)) { // if a multiple period
// overwrite last_n param using the date range
$oPeriod = new Range($period, $originalDate);
$lastN = count($oPeriod->getSubperiods());
} else { // if not a multiple period
list($newDate, $lastN) = self::getDateRangeAndLastN($period, $originalDate, $defaultLastN);
$view->request_parameters_to_modify['date'] = $newDate;
$view->custom_parameters['dateUsedInGraph'] = $newDate;
}
$lastNParamName = self::getLastNParamName($period);
$view->custom_parameters[$lastNParamName] = $lastN;
}
}
/**
* Returns the entire date range and lastN value for the current request, based on
* a period type and end date.
*
* @param string $period The period type, 'day', 'week', 'month' or 'year'
* @param string $endDate The end date.
* @param int|null $defaultLastN The default lastN to use. If null, the result of
* getDefaultLastN is used.
* @return array An array w/ two elements. The first is a whole date range and the second
* is the lastN number used, ie, array('2010-01-01,2012-01-02', 2).
*/
public static function getDateRangeAndLastN($period, $endDate, $defaultLastN = null)
{
if ($defaultLastN === null) {
$defaultLastN = self::getDefaultLastN($period);
}
$lastNParamName = self::getLastNParamName($period);
$lastN = Common::getRequestVar($lastNParamName, $defaultLastN, 'int');
$site = new Site(Common::getRequestVar('idSite'));
$dateRange = Controller::getDateRangeRelativeToEndDate($period, 'last' . $lastN, $endDate, $site);
return array($dateRange, $lastN);
}
/**
* Returns the default last N number of dates to display for a given period.
*
* @param string $period 'day', 'week', 'month' or 'year'
* @return int
*/
public static function getDefaultLastN($period)
{
switch ($period) {
case 'week':
return 26;
case 'month':
return 24;
case 'year':
return 5;
case 'day':
default:
return 30;
}
}
/**
* Returns the query parameter that stores the lastN number of periods to get for
* the evolution graph.
*
* @param string $period The period type, 'day', 'week', 'month' or 'year'.
* @return string
*/
public static function getLastNParamName($period)
{
return "evolution_{$period}_last_n";
}
Benaka Moorthi
a validé
public function getDefaultXAxisStepSize($countGraphElements)
Benaka Moorthi
a validé
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
{
// when the number of elements plotted can be small, make sure the X legend is useful
if ($countGraphElements <= 7) {
return 1;
}
$periodLabel = Common::getRequestVar('period');
switch ($periodLabel) {
case 'day':
case 'range':
$steps = 5;
break;
case 'week':
$steps = 4;
break;
case 'month':
$steps = 5;
break;
case 'year':
$steps = 5;
break;
default:
$steps = 5;
break;
}
$paddedCount = $countGraphElements + 2; // pad count so last label won't be cut off
return ceil($paddedCount / $steps);
}