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 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
|
<?php
/**
* Matomo - free/libre analytics platform
*
* @link https://matomo.org
* @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
namespace Piwik\Plugins\CoreVisualizations\Visualizations;
use Piwik\API\Request;
use Piwik\Common;
use Piwik\DataTable;
use Piwik\Metrics;
use Piwik\Metrics\Formatter as MetricFormatter;
use Piwik\Period\Factory;
use Piwik\Plugin\Report;
use Piwik\Plugin\ReportsProvider;
use Piwik\Plugin\ViewDataTable;
use Piwik\Plugins\API\Filter\DataComparisonFilter;
use Piwik\SettingsPiwik;
use Piwik\View;
/**
* Reads the requested DataTable from the API and prepares data for the Sparklines view. It can display any amount
* of sparklines. Within a reporting page sparklines are shown in 2 columns, in a dashboard or when exported as a widget
* the sparklines are shown in one column.
*
* The sparklines view currently only supports requesting columns from the same API (the API method of the defining
* report) via {Sparklines\Config::addSparklineMetric($columns = array('nb_visits', 'nb_unique_visitors'))}.
*
* Example:
* $view->config->addSparklineMetric('nb_visits'); // if an array of metrics given, they will be displayed comma separated
* $view->config->addTranslation('nb_visits', 'Visits');
* Results in: [sparkline image] X visits
* Data is fetched from the configured $view->requestConfig->apiMethodToRequestDataTable.
*
* In case you want to add any custom sparklines from any other API method you can call
* {@link Sparklines\Config::addSparkline()}.
*
* Example:
* $sparklineUrlParams = array('columns' => array('nb_visits));
* $evolution = array('currentValue' => 5, 'pastValue' => 10, 'tooltip' => 'Foo bar');
* $view->config->addSparkline($sparklineUrlParams, $value = 5, $description = 'Visits', $evolution);
*
* @property Sparklines\Config $config
*/
class Sparklines extends ViewDataTable
{
public const ID = 'sparklines';
public static function getDefaultConfig()
{
return new Sparklines\Config();
}
public function supportsComparison()
{
return true;
}
/**
* @see ViewDataTable::main()
* @return mixed
*/
public function render()
{
$view = new View('@CoreVisualizations/_dataTableViz_sparklines.twig');
$columnsList = [];
if ($this->config->hasSparklineMetrics()) {
foreach ($this->config->getSparklineMetrics() as $cols) {
$columns = $cols['columns'];
if (!is_array($columns)) {
$columns = [$columns];
}
$columnsList = array_merge($columns, $columnsList);
}
}
$view->allMetricsDocumentation = array_merge(Metrics::getDefaultMetricsDocumentation(), $this->config->metrics_documentation);
$this->requestConfig->request_parameters_to_modify['columns'] = $columnsList;
$this->requestConfig->request_parameters_to_modify['format_metrics'] = '1';
/**
* This special request parameter is used to include trend indication columns for all evolution columns
* this is done to be able to determine safely in the view if an evolution is positive or negative, as this
* can't be done with formatted evolution values due to language specific signs being used.
*
* @see DataComparisonFilter::compareChangePercents
*/
$this->requestConfig->request_parameters_to_modify['include_trends'] = '1';
$request = $this->getRequestArray();
if (
$this->isComparing()
&& !empty($request['comparePeriods'])
&& count($request['comparePeriods']) == 1
) {
$this->requestConfig->request_parameters_to_modify['invert_compare_change_compute'] = 1;
}
if (!empty($this->requestConfig->apiMethodToRequestDataTable)) {
$this->fetchConfiguredSparklines();
}
$view->sparklines = $this->config->getSortedSparklines();
$view->isWidget = Common::getRequestVar('widget', 0, 'int');
$view->titleAttributes = $this->config->title_attributes;
$view->footerMessage = $this->config->show_footer_message;
$view->areSparklinesLinkable = $this->config->areSparklinesLinkable();
$view->isComparing = $this->isComparing();
$view->title = '';
if ($this->config->show_title) {
$view->title = $this->config->title;
}
return $view->render();
}
/**
* Load the datatable from the API using the pre-configured request object
*
* @param array $forcedParams
*
* @return DataTable
*/
protected function loadDataTableFromAPI(array $forcedParams = [])
{
if (!is_null($this->dataTable)) {
// data table is already there
// this happens when setDataTable has been used
return $this->dataTable;
}
if ($this->isComparing()) {
$forcedParams['compare'] = '1';
}
$this->dataTable = $this->request->loadDataTableFromAPI($forcedParams);
return $this->dataTable;
}
private function fetchConfiguredSparklines()
{
$data = $this->loadDataTableFromAPI(['format_metrics' => '0']);
$this->applyFilters($data);
if (!$this->config->hasSparklineMetrics()) {
foreach ($data->getColumns() as $column) {
$this->config->addSparklineMetric($column);
}
}
$report = ReportsProvider::factory($this->requestConfig->getApiModuleToRequest(), $this->requestConfig->getApiMethodToRequest());
$processedMetrics = Report::getProcessedMetricsForTable($data, $report);
$metricFormatter = new MetricFormatter();
$idSite = $this->getRequestArray()['idSite'] ?? false;
$firstRow = $data->getFirstRow();
if ($firstRow) {
$comparisons = $firstRow->getComparisons();
} else {
$comparisons = null;
}
$originalDate = Common::getRequestVar('date');
$originalPeriod = Common::getRequestVar('period');
$isComparing = $this->isComparing() && !empty($comparisons);
if ($isComparing) {
$comparisonRows = [];
foreach ($comparisons->getRows() as $comparisonRow) {
$segment = $comparisonRow->getMetadata('compareSegment');
if ($segment === false) {
$segment = Request::getRawSegmentFromRequest() ?: '';
}
$date = $comparisonRow->getMetadata('compareDate');
$period = $comparisonRow->getMetadata('comparePeriod');
$comparisonRows[$segment][$period][$date] = $comparisonRow;
}
}
foreach ($this->config->getSparklineMetrics() as $sparklineMetricIndex => $sparklineMetric) {
$column = $sparklineMetric['columns'];
$order = $sparklineMetric['order'];
$graphParams = $sparklineMetric['graphParams'];
if (!isset($order)) {
$order = 1000;
}
if ($column === 'label') {
continue;
}
if (empty($column)) {
$this->config->addPlaceholder($order);
continue;
}
if (!is_array($column)) {
$column = [$column];
}
$columnMetrics = [];
foreach ($column as $col) {
foreach ($processedMetrics as $metricObj) {
if ($metricObj->getName() === $col) {
$columnMetrics[$col] = $metricObj;
break;
}
}
}
$sparklineUrlParams = array_merge($this->config->custom_parameters, [
'columns' => $column,
'module' => $this->requestConfig->getApiModuleToRequest(),
'action' => $this->requestConfig->getApiMethodToRequest(),
]);
$periodObj = Factory::build($originalPeriod, $originalDate);
$comparePeriods = $data->getMetadata('comparePeriods');
$compareDates = $data->getMetadata('compareDates');
// the first entry includes the original period and we need to remove it
$compareDatesWithoutOriginalDate = $compareDates ? array_slice($compareDates, 1) : [];
$comparePeriodsWithoutOriginalPeriod = $comparePeriods ? array_slice($comparePeriods, 1) : [];
$periodSelector = new EvolutionPeriodSelector($this->config);
$comparisonPeriods = $periodSelector->getComparisonPeriodObjects($comparePeriodsWithoutOriginalPeriod, $compareDatesWithoutOriginalDate);
$sparklineUrlParams = $periodSelector->setDatePeriods($sparklineUrlParams, $periodObj, $comparisonPeriods, $isComparing);
if ($isComparing) {
$sparklineUrlParams['compareSegments'] = [];
$compareSegments = $data->getMetadata('compareSegments');
foreach ($compareSegments as $segmentIndex => $segment) {
$metrics = [];
$seriesIndices = [];
foreach ($comparePeriods as $periodIndex => $period) {
$date = $compareDates[$periodIndex];
$compareRow = $comparisonRows[$segment][$period][$date];
$segmentPretty = $compareRow->getMetadata('compareSegmentPretty');
$periodPretty = $compareRow->getMetadata('comparePeriodPretty');
$columnToUse = $this->removeUniqueVisitorsIfNotEnabledForPeriod($column, $period);
[$compareValues, $compareDescriptions, $evolutions] = $this->getValuesAndDescriptions($compareRow, $columnToUse, '_change', '_trend');
foreach ($compareValues as $i => $value) {
if (!isset($column[$i])) {
continue;
}
if (isset($columnMetrics[$column[$i]]) && $columnMetrics[$column[$i]]) {
$value = $columnMetrics[$columnToUse[$i]]->format($value, $metricFormatter);
} elseif (strpos($columnToUse[$i], 'revenue') !== false && $idSite > 0) {
$value = $metricFormatter->getPrettyMoney($value, $idSite);
}
$metricInfo = [
'value' => $value,
'description' => $compareDescriptions[$i],
'group' => $periodPretty,
];
if (isset($evolutions[$i])) {
$metricInfo['evolution'] = $evolutions[$i];
}
$metrics[] = $metricInfo;
}
$seriesIndices[] = DataComparisonFilter::getComparisonSeriesIndex($data, $periodIndex, $segmentIndex);
}
// only set the title (which is the segment) if comparing more than one segment
$title = count($compareSegments) > 1 ? $segmentPretty : null;
$params = array_merge($sparklineUrlParams, [
'segment' => $segment,
]);
$this->config->addSparkline($params, $metrics, $desc = null, null, ($order * 100) + $segmentIndex, $title, $sparklineMetricIndex, $seriesIndices, $graphParams);
}
} else {
[$values, $descriptions] = $this->getValuesAndDescriptions($firstRow, $column);
$metrics = [];
foreach ($values as $i => $value) {
if (!isset($column[$i])) {
continue;
}
if (isset($columnMetrics[$column[$i]]) && $columnMetrics[$column[$i]]) {
$value = $columnMetrics[$column[$i]]->format($value, $metricFormatter);
} elseif (strpos($column[$i], 'revenue') !== false && $idSite > 0) {
$value = $metricFormatter->getPrettyMoney($value, $idSite);
}
$newMetric = [
'value' => $value,
'description' => $descriptions[$i],
];
$metrics[] = $newMetric;
}
$evolution = null;
$computeEvolution = $this->config->compute_evolution;
if ($computeEvolution) {
$evolution = $computeEvolution(array_combine((is_array($column) ? $column : [$column]), $values), $processedMetrics);
$newMetric['evolution'] = $evolution;
}
$this->config->addSparkline($sparklineUrlParams, $metrics, $desc = null, $evolution, $order, $title = null, $group = $sparklineMetricIndex, $seriesIndices = null, $graphParams);
}
}
}
private function applyFilters(DataTable\DataTableInterface $table)
{
foreach ($this->config->getPriorityFilters() as $filter) {
$table->filter($filter[0], $filter[1]);
}
// queue other filters so they can be applied later if queued filters are disabled
foreach ($this->config->getPresentationFilters() as $filter) {
$table->queueFilter($filter[0], $filter[1]);
}
$table->applyQueuedFilters();
}
private function getValuesAndDescriptions($firstRow, $columns, $evolutionColumnNameSuffix = null, $trendColumnNameSuffix = null)
{
if (!is_array($columns)) {
$columns = [$columns];
}
$translations = $this->config->translations;
$values = [];
$descriptions = [];
$evolutions = [];
foreach ($columns as $col) {
$value = 0;
if ($firstRow) {
$value = $firstRow->getColumn($col);
}
if ($value === false) {
$value = 0;
}
if ($evolutionColumnNameSuffix !== null) {
$evolution = $firstRow->getColumn($col . $evolutionColumnNameSuffix);
$trend = $firstRow->getColumn($col . $trendColumnNameSuffix);
if ($evolution !== false) {
$evolutions[] = ['percent' => ltrim($evolution, '+'), 'trend' => $trend, 'tooltip' => ''];
}
}
$values[] = $value;
$descriptions[] = isset($translations[$col]) ? $translations[$col] : $col;
}
return [$values, $descriptions, $evolutions];
}
private function removeUniqueVisitorsIfNotEnabledForPeriod($columns, $period)
{
if (SettingsPiwik::isUniqueVisitorsEnabled($period)) {
return $columns;
}
if (!is_array($columns)) {
$columns = [$columns];
}
return array_diff($columns, ['nb_users', 'nb_uniq_visitors']);
}
}
|