PageRenderTime 40ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/plugins/CoreVisualizations/Visualizations/JqplotGraph/Evolution.php

https://github.com/CodeYellowBV/piwik
PHP | 200 lines | 118 code | 34 blank | 48 comment | 11 complexity | 0cb8db0af48f46242882f46dfb9a42d7 MD5 | raw file
Possible License(s): LGPL-3.0, JSON, MIT, GPL-3.0, LGPL-2.1, GPL-2.0, AGPL-1.0, BSD-2-Clause, BSD-3-Clause
  1. <?php
  2. /**
  3. * Piwik - free/libre analytics platform
  4. *
  5. * @link http://piwik.org
  6. * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
  7. *
  8. */
  9. namespace Piwik\Plugins\CoreVisualizations\Visualizations\JqplotGraph;
  10. use Piwik\Common;
  11. use Piwik\DataTable;
  12. use Piwik\Period\Range;
  13. use Piwik\Plugins\CoreVisualizations\JqplotDataGenerator;
  14. use Piwik\Plugins\CoreVisualizations\Visualizations\JqplotGraph;
  15. use Piwik\Site;
  16. /**
  17. * Visualization that renders HTML for a line graph using jqPlot.
  18. *
  19. * @property Evolution\Config $config
  20. */
  21. class Evolution extends JqplotGraph
  22. {
  23. const ID = 'graphEvolution';
  24. const SERIES_COLOR_COUNT = 8;
  25. public static function getDefaultConfig()
  26. {
  27. return new Evolution\Config();
  28. }
  29. public function beforeRender()
  30. {
  31. parent::beforeRender();
  32. $this->config->datatable_js_type = 'JqplotEvolutionGraphDataTable';
  33. }
  34. public function beforeLoadDataTable()
  35. {
  36. $this->calculateEvolutionDateRange();
  37. parent::beforeLoadDataTable();
  38. // period will be overridden when 'range' is requested in the UI
  39. // but the graph will display for each day of the range.
  40. // Default 'range' behavior is to return the 'sum' for the range
  41. if (Common::getRequestVar('period', false) == 'range') {
  42. $this->requestConfig->request_parameters_to_modify['period'] = 'day';
  43. }
  44. $this->config->custom_parameters['columns'] = $this->config->columns_to_display;
  45. }
  46. public function afterAllFiltersAreApplied()
  47. {
  48. parent::afterAllFiltersAreApplied();
  49. if (false === $this->config->x_axis_step_size) {
  50. $rowCount = $this->dataTable->getRowsCount();
  51. $this->config->x_axis_step_size = $this->getDefaultXAxisStepSize($rowCount);
  52. }
  53. }
  54. protected function makeDataGenerator($properties)
  55. {
  56. return JqplotDataGenerator::factory('evolution', $properties);
  57. }
  58. /**
  59. * Based on the period, date and evolution_{$period}_last_n query parameters,
  60. * calculates the date range this evolution chart will display data for.
  61. */
  62. private function calculateEvolutionDateRange()
  63. {
  64. $period = Common::getRequestVar('period');
  65. $defaultLastN = self::getDefaultLastN($period);
  66. $originalDate = Common::getRequestVar('date', 'last' . $defaultLastN, 'string');
  67. if ('range' != $period) { // show evolution limit if the period is not a range
  68. $this->config->show_limit_control = true;
  69. // set the evolution_{$period}_last_n query param
  70. if (Range::parseDateRange($originalDate)) {
  71. // if a multiple period
  72. // overwrite last_n param using the date range
  73. $oPeriod = new Range($period, $originalDate);
  74. $lastN = count($oPeriod->getSubperiods());
  75. } else {
  76. // if not a multiple period
  77. list($newDate, $lastN) = self::getDateRangeAndLastN($period, $originalDate, $defaultLastN);
  78. $this->requestConfig->request_parameters_to_modify['date'] = $newDate;
  79. $this->config->custom_parameters['dateUsedInGraph'] = $newDate;
  80. }
  81. $lastNParamName = self::getLastNParamName($period);
  82. $this->config->custom_parameters[$lastNParamName] = $lastN;
  83. }
  84. }
  85. /**
  86. * Returns the entire date range and lastN value for the current request, based on
  87. * a period type and end date.
  88. *
  89. * @param string $period The period type, 'day', 'week', 'month' or 'year'
  90. * @param string $endDate The end date.
  91. * @param int|null $defaultLastN The default lastN to use. If null, the result of
  92. * getDefaultLastN is used.
  93. * @return array An array w/ two elements. The first is a whole date range and the second
  94. * is the lastN number used, ie, array('2010-01-01,2012-01-02', 2).
  95. */
  96. public static function getDateRangeAndLastN($period, $endDate, $defaultLastN = null)
  97. {
  98. if ($defaultLastN === null) {
  99. $defaultLastN = self::getDefaultLastN($period);
  100. }
  101. $lastNParamName = self::getLastNParamName($period);
  102. $lastN = Common::getRequestVar($lastNParamName, $defaultLastN, 'int');
  103. $site = new Site(Common::getRequestVar('idSite'));
  104. $dateRange = Range::getRelativeToEndDate($period, 'last' . $lastN, $endDate, $site);
  105. return array($dateRange, $lastN);
  106. }
  107. /**
  108. * Returns the default last N number of dates to display for a given period.
  109. *
  110. * @param string $period 'day', 'week', 'month' or 'year'
  111. * @return int
  112. */
  113. public static function getDefaultLastN($period)
  114. {
  115. switch ($period) {
  116. case 'week':
  117. return 26;
  118. case 'month':
  119. return 24;
  120. case 'year':
  121. return 5;
  122. case 'day':
  123. default:
  124. return 30;
  125. }
  126. }
  127. /**
  128. * Returns the query parameter that stores the lastN number of periods to get for
  129. * the evolution graph.
  130. *
  131. * @param string $period The period type, 'day', 'week', 'month' or 'year'.
  132. * @return string
  133. */
  134. public static function getLastNParamName($period)
  135. {
  136. return "evolution_{$period}_last_n";
  137. }
  138. public function getDefaultXAxisStepSize($countGraphElements)
  139. {
  140. // when the number of elements plotted can be small, make sure the X legend is useful
  141. if ($countGraphElements <= 7) {
  142. return 1;
  143. }
  144. $periodLabel = Common::getRequestVar('period');
  145. switch ($periodLabel) {
  146. case 'day':
  147. case 'range':
  148. $steps = 5;
  149. break;
  150. case 'week':
  151. $steps = 4;
  152. break;
  153. case 'month':
  154. $steps = 5;
  155. break;
  156. case 'year':
  157. $steps = 5;
  158. break;
  159. default:
  160. $steps = 5;
  161. break;
  162. }
  163. $paddedCount = $countGraphElements + 2; // pad count so last label won't be cut off
  164. return ceil($paddedCount / $steps);
  165. }
  166. }