PageRenderTime 44ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/plugins/CoreVisualizations/Visualizations/Sparkline.php

https://github.com/CodeYellowBV/piwik
PHP | 128 lines | 77 code | 24 blank | 27 comment | 14 complexity | 87ec3e738f458d08473c3bb2bfbe9d51 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;
  10. use Exception;
  11. use Piwik\Common;
  12. use Piwik\DataTable;
  13. use Piwik\Plugin\ViewDataTable;
  14. /**
  15. * Reads the requested DataTable from the API and prepare data for the Sparkline view.
  16. *
  17. */
  18. class Sparkline extends ViewDataTable
  19. {
  20. const ID = 'sparkline';
  21. /**
  22. * @see ViewDataTable::main()
  23. * @return mixed
  24. */
  25. protected function buildView()
  26. {
  27. // If period=range, we force the sparkline to draw daily data points
  28. $period = Common::getRequestVar('period');
  29. if ($period == 'range') {
  30. $_GET['period'] = 'day';
  31. }
  32. $this->loadDataTableFromAPI();
  33. // then revert the hack for potentially subsequent getRequestVar
  34. $_GET['period'] = $period;
  35. $values = $this->getValuesFromDataTable($this->dataTable);
  36. if (empty($values)) {
  37. $values = array_fill(0, 30, 0);
  38. }
  39. $graph = new \Piwik\Visualization\Sparkline();
  40. $graph->setValues($values);
  41. $height = Common::getRequestVar('height', 0, 'int');
  42. if (!empty($height)) {
  43. $graph->setHeight($height);
  44. }
  45. $width = Common::getRequestVar('width', 0, 'int');
  46. if (!empty($width)) {
  47. $graph->setWidth($width);
  48. }
  49. $graph->main();
  50. return $graph;
  51. }
  52. /**
  53. * @param DataTable\Map $dataTableMap
  54. * @param string $columnToPlot
  55. *
  56. * @return array
  57. * @throws \Exception
  58. */
  59. protected function getValuesFromDataTableMap($dataTableMap, $columnToPlot)
  60. {
  61. $dataTableMap->applyQueuedFilters();
  62. $values = array();
  63. foreach ($dataTableMap->getDataTables() as $table) {
  64. if ($table->getRowsCount() > 1) {
  65. throw new Exception("Expecting only one row per DataTable");
  66. }
  67. $value = 0;
  68. $onlyRow = $table->getFirstRow();
  69. if (false !== $onlyRow) {
  70. if (!empty($columnToPlot)) {
  71. $value = $onlyRow->getColumn($columnToPlot);
  72. } // if not specified, we load by default the first column found
  73. // eg. case of getLastDistinctCountriesGraph
  74. else {
  75. $columns = $onlyRow->getColumns();
  76. $value = current($columns);
  77. }
  78. }
  79. $values[] = $value;
  80. }
  81. return $values;
  82. }
  83. protected function getValuesFromDataTable($dataTable)
  84. {
  85. $columns = $this->config->columns_to_display;
  86. $columnToPlot = false;
  87. if (!empty($columns)) {
  88. $columnToPlot = reset($columns);
  89. if ($columnToPlot == 'label') {
  90. $columnToPlot = next($columns);
  91. }
  92. }
  93. // a Set is returned when using the normal code path to request data from Archives, in all core plugins
  94. // however plugins can also return simple datatable, hence why the sparkline can accept both data types
  95. if ($this->dataTable instanceof DataTable\Map) {
  96. $values = $this->getValuesFromDataTableMap($dataTable, $columnToPlot);
  97. } elseif ($this->dataTable instanceof DataTable) {
  98. $values = $this->dataTable->getColumn($columnToPlot);
  99. } else {
  100. $values = false;
  101. }
  102. return $values;
  103. }
  104. }