diff options
Diffstat (limited to 'plugins/CoreVisualizations/Visualizations')
8 files changed, 404 insertions, 61 deletions
diff --git a/plugins/CoreVisualizations/Visualizations/Graph.php b/plugins/CoreVisualizations/Visualizations/Graph.php index f7767d9527..bc37ea8747 100644 --- a/plugins/CoreVisualizations/Visualizations/Graph.php +++ b/plugins/CoreVisualizations/Visualizations/Graph.php @@ -63,6 +63,14 @@ abstract class Graph extends Visualization $this->requestConfig->request_parameters_to_modify['format_metrics'] = 1; + // if addTotalRow was called in GenerateGraphHTML, add a row containing totals of + // different metrics + if ($this->config->add_total_row) { + $this->requestConfig->request_parameters_to_modify['totals'] = 1; + $this->requestConfig->request_parameters_to_modify['keep_totals_row'] = 1; + $this->requestConfig->request_parameters_to_modify['keep_totals_row_label'] = Piwik::translate('General_Total'); + } + $this->metricsFormatter = new Numeric(); } diff --git a/plugins/CoreVisualizations/Visualizations/HtmlTable.php b/plugins/CoreVisualizations/Visualizations/HtmlTable.php index 4bf717fd1b..a07cc6af2b 100644 --- a/plugins/CoreVisualizations/Visualizations/HtmlTable.php +++ b/plugins/CoreVisualizations/Visualizations/HtmlTable.php @@ -42,6 +42,15 @@ class HtmlTable extends Visualization public function beforeLoadDataTable() { $this->checkRequestIsNotForMultiplePeriods(); + + if ($this->isComparing()) { + // forward the comparisonIdSubtables var if present so it will be used when next/prev links are clicked + $comparisonIdSubtables = Common::getRequestVar('comparisonIdSubtables', false, 'string'); + if (!empty($comparisonIdSubtables)) { + $comparisonIdSubtables = Common::unsanitizeInputValue($comparisonIdSubtables); + $this->config->custom_parameters['comparisonIdSubtables'] = $comparisonIdSubtables; + } + } } public function beforeRender() @@ -96,6 +105,12 @@ class HtmlTable extends Visualization $this->config->columns_to_display = $this->dataTable->getColumns(); } + if ($this->isComparing() + && !empty($this->dataTable) + ) { + $this->assignTemplateVar('comparisonTotals', $this->dataTable->getMetadata('comparisonTotals')); + } + // Note: This needs to be done right before rendering, as otherwise some plugins might change the columns to display again if ($this->isFlattened()) { $dimensions = $this->dataTable->getMetadata('dimensions'); @@ -144,6 +159,11 @@ class HtmlTable extends Visualization }); } } + + $this->assignTemplateVar('segmentTitlePretty', $this->dataTable->getMetadata('segmentPretty')); + + $period = $this->dataTable->getMetadata('period'); + $this->assignTemplateVar('periodTitlePretty', $period ? $period->getLocalizedShortString() : ''); } public function beforeGenericFiltersAreAppliedToLoadedDataTable() @@ -212,6 +232,11 @@ class HtmlTable extends Visualization return null; } + public function supportsComparison() + { + return true; + } + protected function isFlattened() { return $this->requestConfig->flat || Common::getRequestVar('flat', ''); diff --git a/plugins/CoreVisualizations/Visualizations/JqplotGraph/Bar.php b/plugins/CoreVisualizations/Visualizations/JqplotGraph/Bar.php index 579016f122..0da05ea68e 100644 --- a/plugins/CoreVisualizations/Visualizations/JqplotGraph/Bar.php +++ b/plugins/CoreVisualizations/Visualizations/JqplotGraph/Bar.php @@ -50,6 +50,11 @@ class Bar extends JqplotGraph protected function makeDataGenerator($properties) { - return JqplotDataGenerator::factory('bar', $properties); + return JqplotDataGenerator::factory('bar', $properties, $this); + } + + public function supportsComparison() + { + return true; } } diff --git a/plugins/CoreVisualizations/Visualizations/JqplotGraph/Evolution.php b/plugins/CoreVisualizations/Visualizations/JqplotGraph/Evolution.php index 5de7fc5114..337bb233b2 100644 --- a/plugins/CoreVisualizations/Visualizations/JqplotGraph/Evolution.php +++ b/plugins/CoreVisualizations/Visualizations/JqplotGraph/Evolution.php @@ -9,8 +9,11 @@ namespace Piwik\Plugins\CoreVisualizations\Visualizations\JqplotGraph; +use Piwik\API\Request as ApiRequest; use Piwik\Common; use Piwik\DataTable; +use Piwik\Period; +use Piwik\Period\Factory; use Piwik\Period\Range; use Piwik\Plugins\CoreVisualizations\JqplotDataGenerator; use Piwik\Plugins\CoreVisualizations\Visualizations\JqplotGraph; @@ -43,7 +46,9 @@ class Evolution extends JqplotGraph public function beforeLoadDataTable() { - $this->calculateEvolutionDateRange(); + if (!$this->isComparing()) { + $this->calculateEvolutionDateRange(); + } parent::beforeLoadDataTable(); @@ -55,6 +60,33 @@ class Evolution extends JqplotGraph } $this->config->custom_parameters['columns'] = $this->config->columns_to_display; + + if ($this->isComparing()) { + $requestArray = $this->request->getRequestArray(); + $requestArray = ApiRequest::getRequestArrayFromString($requestArray); + + $requestingPeriod = Factory::build($requestArray['period'], $requestArray['date']); + + $this->requestConfig->request_parameters_to_modify['period'] = 'day'; + $this->requestConfig->request_parameters_to_modify['date'] = $requestingPeriod->getDateStart()->toString() . ',' . $requestingPeriod->getDateEnd()->toString(); + + if (!empty($requestArray['comparePeriods'])) { + foreach ($requestArray['comparePeriods'] as $index => $comparePeriod) { + $compareDate = $requestArray['compareDates'][$index]; + if (Period::isMultiplePeriod($compareDate, $comparePeriod)) { + continue; + } + + $comparePeriodObj = Factory::build($comparePeriod, $compareDate); + + $requestArray['comparePeriods'][$index] = 'day'; + $requestArray['compareDates'][$index] = $comparePeriodObj->getRangeString(); + } + + $this->requestConfig->request_parameters_to_modify['compareDates'] = $requestArray['compareDates']; + $this->requestConfig->request_parameters_to_modify['comparePeriods'] = $requestArray['comparePeriods']; + } + } } public function afterAllFiltersAreApplied() @@ -70,7 +102,7 @@ class Evolution extends JqplotGraph protected function makeDataGenerator($properties) { - return JqplotDataGenerator::factory('evolution', $properties); + return JqplotDataGenerator::factory('evolution', $properties, $this); } /** @@ -199,4 +231,9 @@ class Evolution extends JqplotGraph return ceil($paddedCount / $steps); } + + public function supportsComparison() + { + return true; + } } diff --git a/plugins/CoreVisualizations/Visualizations/JqplotGraph/Pie.php b/plugins/CoreVisualizations/Visualizations/JqplotGraph/Pie.php index 3fafdd5e7b..045047049f 100644 --- a/plugins/CoreVisualizations/Visualizations/JqplotGraph/Pie.php +++ b/plugins/CoreVisualizations/Visualizations/JqplotGraph/Pie.php @@ -55,6 +55,6 @@ class Pie extends JqplotGraph protected function makeDataGenerator($properties) { - return JqplotDataGenerator::factory('pie', $properties); + return JqplotDataGenerator::factory('pie', $properties, $this); } } diff --git a/plugins/CoreVisualizations/Visualizations/Sparkline.php b/plugins/CoreVisualizations/Visualizations/Sparkline.php index a2bdb09d2c..b59a89ba5d 100644 --- a/plugins/CoreVisualizations/Visualizations/Sparkline.php +++ b/plugins/CoreVisualizations/Visualizations/Sparkline.php @@ -11,7 +11,9 @@ namespace Piwik\Plugins\CoreVisualizations\Visualizations; use Exception; use Piwik\Common; use Piwik\DataTable; +use Piwik\Period; use Piwik\Plugin\ViewDataTable; +use Piwik\Site; /** * Reads the requested DataTable from the API and prepare data for the Sparkline view. @@ -21,6 +23,11 @@ class Sparkline extends ViewDataTable { const ID = 'sparkline'; + public function supportsComparison() + { + return true; + } + /** * @see ViewDataTable::main() * @return mixed @@ -29,22 +36,61 @@ class Sparkline extends ViewDataTable { // If period=range, we force the sparkline to draw daily data points $period = Common::getRequestVar('period'); - if ($period == 'range') { + $date = Common::getRequestVar('date'); + + if ($period == 'range' + || $this->isComparing() + ) { + $periodObj = Period\Factory::build($period, $date); $_GET['period'] = 'day'; + $_GET['date'] = $periodObj->getRangeString(); + } + + if ($this->isComparing()) { + $this->transformSingleComparisonPeriods(); } $this->loadDataTableFromAPI(); // then revert the hack for potentially subsequent getRequestVar $_GET['period'] = $period; + $_GET['date'] = $date; - $values = $this->getValuesFromDataTable($this->dataTable); - if (empty($values)) { - $values = array_fill(0, 30, 0); - } + $columnToPlot = $this->getColumnToPlot(); $graph = new \Piwik\Visualization\Sparkline(); - $graph->setValues($values); + + if ($this->isComparing()) { + $otherSeries = []; + + $comparisonSeries = $this->getComparisonSeries($this->dataTable); + foreach ($comparisonSeries as $seriesName) { + $otherSeries[$seriesName] = []; + } + + $this->dataTable->filter(function (DataTable $table) use ($comparisonSeries, &$otherSeries, $columnToPlot) { + foreach ($table->getRows() as $row) { + $comparisons = $row->getComparisons(); + if (empty($comparisons)) { + continue; + } + + foreach ($comparisons->getRows() as $comparisonRow) { + $compareSeriesPretty = $comparisonRow->getMetadata('compareSeriesPretty'); + $otherSeries[$compareSeriesPretty][] = $comparisonRow->getColumn($columnToPlot); + } + } + }); + + foreach ($otherSeries as $seriesValues) { + $seriesValues = $this->ensureValuesEvenIfEmpty($seriesValues); + $graph->addSeries($seriesValues); + } + } else { + $values = $this->getValuesFromDataTable($this->dataTable, $columnToPlot); + $values = $this->ensureValuesEvenIfEmpty($values); + $graph->addSeries($values); + } $height = Common::getRequestVar('height', 0, 'int'); if (!empty($height)) { @@ -100,7 +146,7 @@ class Sparkline extends ViewDataTable return $values; } - protected function getValuesFromDataTable($dataTable) + private function getColumnToPlot() { $columns = $this->config->columns_to_display; @@ -113,6 +159,11 @@ class Sparkline extends ViewDataTable } } + return $columnToPlot; + } + + protected function getValuesFromDataTable($dataTable, $columnToPlot) + { // a Set is returned when using the normal code path to request data from Archives, in all core plugins // however plugins can also return simple datatable, hence why the sparkline can accept both data types if ($this->dataTable instanceof DataTable\Map) { @@ -125,4 +176,41 @@ class Sparkline extends ViewDataTable return $values; } + + private function ensureValuesEvenIfEmpty(array $values) + { + if (empty($values)) { + return array_fill(0, 30, 0); + } + return $values; + } + + private function getComparisonSeries(DataTable\DataTableInterface $dataTable) + { + if ($dataTable instanceof DataTable\Map) { + return reset($dataTable->getDataTables())->getMetadata('comparisonSeries') ?: []; + } else { + return $dataTable->getMetadata('comparisonSeries') ?: []; + } + } + + private function transformSingleComparisonPeriods() + { + $comparePeriods = Common::getRequestVar('comparePeriods', $default = [], $type = 'array'); + $compareDates = Common::getRequestVar('compareDates', $default = [], $type = 'array'); + + foreach ($comparePeriods as $index => $comparePeriod) { + $compareDate = $compareDates[$index]; + if (Period::isMultiplePeriod($compareDate, $comparePeriod)) { + continue; + } + + $periodObj = Period\Factory::build($comparePeriod, $compareDate); + $comparePeriods[$index] = 'day'; + $compareDates[$index] = $periodObj->getRangeString(); + } + + $this->requestConfig->request_parameters_to_modify['comparePeriods'] = $comparePeriods; + $this->requestConfig->request_parameters_to_modify['compareDates'] = $compareDates; + } } diff --git a/plugins/CoreVisualizations/Visualizations/Sparklines.php b/plugins/CoreVisualizations/Visualizations/Sparklines.php index 2d11723bef..c3d8556e00 100644 --- a/plugins/CoreVisualizations/Visualizations/Sparklines.php +++ b/plugins/CoreVisualizations/Visualizations/Sparklines.php @@ -8,10 +8,14 @@ */ namespace Piwik\Plugins\CoreVisualizations\Visualizations; +use Piwik\API\Request; use Piwik\Common; use Piwik\DataTable; use Piwik\Metrics; +use Piwik\Period\Factory; use Piwik\Plugin\ViewDataTable; +use Piwik\Plugins\API\Filter\DataComparisonFilter; +use Piwik\SettingsPiwik; use Piwik\Url; use Piwik\View; @@ -48,6 +52,11 @@ class Sparklines extends ViewDataTable return new Sparklines\Config(); } + public function supportsComparison() + { + return true; + } + /** * @see ViewDataTable::main() * @return mixed @@ -82,6 +91,7 @@ class Sparklines extends ViewDataTable $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) { @@ -103,13 +113,35 @@ class Sparklines extends ViewDataTable } } - $translations = $this->config->translations; - $firstRow = $data->getFirstRow(); + $comparisons = $firstRow->getComparisons(); + + $originalDate = Common::getRequestVar('date'); + $originalPeriod = Common::getRequestVar('period'); + + if ($this->isComparing() && !empty($comparisons)) { + $comparisonRows = []; + foreach ($comparisons->getRows() as $comparisonRow) { + $segment = $comparisonRow->getMetadata('compareSegment'); + if ($segment === false) { + $segment = Request::getRawSegmentFromRequest() ?: '';; + } - foreach ($this->config->getSparklineMetrics() as $sparklineMetric) { + $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; @@ -120,31 +152,87 @@ class Sparklines extends ViewDataTable continue; } - if (!is_array($column)) { - $column = array($column); - } + $sparklineUrlParams = array( + 'columns' => $column, + 'module' => $this->requestConfig->getApiModuleToRequest(), + 'action' => $this->requestConfig->getApiMethodToRequest() + ); + + if ($this->isComparing() && !empty($comparisons)) { + $periodObj = Factory::build($originalPeriod, $originalDate); + + $sparklineUrlParams['compareSegments'] = []; + + $comparePeriods = $data->getMetadata('comparePeriods'); + $compareDates = $data->getMetadata('compareDates'); + + $compareSegments = $data->getMetadata('compareSegments'); + foreach ($compareSegments as $segmentIndex => $segment) { + $metrics = []; + $seriesIndices = []; - $values = array(); - $descriptions = array(); + foreach ($comparePeriods as $periodIndex => $period) { + $date = $compareDates[$periodIndex]; - foreach ($column as $col) { - $value = $firstRow->getColumn($col); + $compareRow = $comparisonRows[$segment][$period][$date]; + $segmentPretty = $compareRow->getMetadata('compareSegmentPretty'); + $periodPretty = $compareRow->getMetadata('comparePeriodPretty'); - if ($value === false) { - $value = 0; + $columnToUse = $this->removeUniqueVisitorsIfNotEnabledForPeriod($column, $period); + list($compareValues, $compareDescriptions, $evolutions) = $this->getValuesAndDescriptions($compareRow, $columnToUse); + + foreach ($compareValues as $i => $value) { + $metricInfo = [ + 'value' => $value, + 'description' => $compareDescriptions[$i], + 'group' => $periodPretty, + ]; + + if ($periodIndex > 0 + && 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, + 'period' => $periodObj->getLabel(), + 'date' => $periodObj->getRangeString(), + ]); + $this->config->addSparkline($params, $metrics, $desc = null, null, ($order * 100) + $segmentIndex, $title, $sparklineMetricIndex, $seriesIndices, $graphParams); } + } else { + list($values, $descriptions) = $this->getValuesAndDescriptions($firstRow, $column); - $values[] = $value; - $descriptions[] = isset($translations[$col]) ? $translations[$col] : $col; - } + $metrics = []; + foreach ($values as $i => $value) { + $newMetric = [ + 'value' => $value, + 'description' => $descriptions[$i], + ]; - $sparklineUrlParams = array( - 'columns' => $column, - 'module' => $this->requestConfig->getApiModuleToRequest(), - 'action' => $this->requestConfig->getApiMethodToRequest() - ); + $metrics[] = $newMetric; + } - $this->config->addSparkline($sparklineUrlParams, $values, $descriptions, null, $order); + $evolution = null; + + $computeEvolution = $this->config->compute_evolution; + if ($computeEvolution) { + $evolution = $computeEvolution(array_combine($column, $values)); + $newMetric['evolution'] = $evolution; + } + + $this->config->addSparkline($sparklineUrlParams, $metrics, $desc = null, $evolution, $order, $title = null, $group = $sparklineMetricIndex, $seriesIndices = null, $graphParams); + } } } @@ -161,4 +249,44 @@ class Sparklines extends ViewDataTable $table->applyQueuedFilters(); } + + private function getValuesAndDescriptions(DataTable\Row $firstRow, $columns) + { + if (!is_array($columns)) { + $columns = array($columns); + } + + $translations = $this->config->translations; + + $values = array(); + $descriptions = array(); + $evolutions = []; + + foreach ($columns as $col) { + $value = $firstRow->getColumn($col); + + if ($value === false) { + $value = 0; + } + + $evolution = $firstRow->getColumn($col . '_change'); // for comparison rows + if ($evolution !== false) { + $evolutions[] = ['percent' => ltrim($evolution, '+'), '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; + } + + return array_diff($columns, ['nb_users', 'nb_uniq_visitors']); + } } diff --git a/plugins/CoreVisualizations/Visualizations/Sparklines/Config.php b/plugins/CoreVisualizations/Visualizations/Sparklines/Config.php index 86944ccc5a..e5fe3da1c0 100644 --- a/plugins/CoreVisualizations/Visualizations/Sparklines/Config.php +++ b/plugins/CoreVisualizations/Visualizations/Sparklines/Config.php @@ -12,6 +12,7 @@ use Piwik\Common; use Piwik\DataTable\Filter\CalculateEvolutionFilter; use Piwik\Metrics; use Piwik\NoAccessException; +use Piwik\Period\Factory; use Piwik\Period\Range; use Piwik\Site; use Piwik\Url; @@ -46,6 +47,15 @@ class Config extends \Piwik\ViewDataTable\Config */ public $title_attributes = array(); + /** + * If supplied, this function is used to compute the evolution percent displayed next to non-comparison sparkline views. + * + * The function is passed an array mapping column names with column values. + * + * @var callable + */ + public $compute_evolution = null; + public function __construct() { parent::__construct(); @@ -145,12 +155,15 @@ class Config extends \Piwik\ViewDataTable\Config * @param string|array $metricName Either one metric name (eg 'nb_visits') or an array of metric names * @param int|null $order Defines the order. The lower the order the earlier the sparkline will be displayed. * By default the sparkline will be appended to the end. + * @param array $graphParams The params to use when changing an associated evolution graph. By default this is determined + * from the sparkline URL, but sometimes the sparkline API method may not match the evolution graph API method. */ - public function addSparklineMetric($metricName, $order = null) + public function addSparklineMetric($metricName, $order = null, $graphParams = null) { $this->sparkline_metrics[] = array( 'columns' => $metricName, - 'order' => $order + 'order' => $order, + 'graphParams' => $graphParams, ); } @@ -167,7 +180,10 @@ class Config extends \Piwik\ViewDataTable\Config $this->sparklines[] = array( 'url' => '', 'metrics' => array(), - 'order' => $this->getSparklineOrder($order) + 'order' => $this->getSparklineOrder($order), + + // adding this group ensures the sparkline will be placed between individual sparklines, and not in their own group together + 'group' => 'placeholder' . count($this->sparklines), ); } @@ -194,62 +210,87 @@ class Config extends \Piwik\ViewDataTable\Config * 'tooltip' => '10 visits in 2015-07-26 compared to 20 visits in 2015-07-25') * @param int $order Defines the order. The lower the order the earlier the sparkline will be * displayed. By default the sparkline will be appended to the end. + * @param string $title The title of this specific sparkline. It is displayed on the left above the sparkline image. + * @param string $group The ID of the group for this sparkline. + * @param int $seriesIndices The indexes of each series displayed in the sparkline. This determines what color is used for each series. Mainly used for comparison. + * @param array $graphParams The params to use when changing an associated evolution graph. By default this is determined + * from the sparkline URL, but sometimes the sparkline API method may not match the evolution graph API method. * @throws \Exception In case an evolution parameter is set but has wrong data structure */ - public function addSparkline($requestParamsForSparkline, $value, $description, $evolution = null, $order = null) + public function addSparkline($requestParamsForSparkline, $metricInfos, $description, $evolution = null, $order = null, $title = null, $group = '', $seriesIndices = null, $graphParams = null) { $metrics = array(); - if (is_array($value)) { - $values = $value; + if ($description === null && is_array($metricInfos)) { + $metrics = $metricInfos; } else { - $values = array($value); - } + $value = $metricInfos; - if (!is_array($description)) { - $description = array($description); + if (is_array($value)) { + $values = $value; + } else { + $values = array($value); + } + + if (!is_array($description)) { + $description = array($description); + } + + if (count($values) === count($description)) { + foreach ($values as $index => $value) { + $metrics[] = array( + 'value' => $value, + 'description' => $description[$index] + ); + } + } else { + $msg = 'The number of values and descriptions need to be the same to add a sparkline. '; + $msg .= 'Values: ' . implode(', ', $values). ' Descriptions: ' . implode(', ', $description); + throw new \Exception($msg); + } } if (!empty($requestParamsForSparkline['columns']) && is_array($requestParamsForSparkline['columns']) - && count($requestParamsForSparkline['columns']) === count($values)) { + && count($requestParamsForSparkline['columns']) === count($metrics)) { $columns = array_values($requestParamsForSparkline['columns']); } elseif (!empty($requestParamsForSparkline['columns']) && is_string($requestParamsForSparkline['columns']) - && count($values) === 1) { + && count($metrics) === 1) { $columns = array($requestParamsForSparkline['columns']); } else{ $columns = array(); } - if (count($values) === count($description)) { - foreach ($values as $index => $value) { - $metrics[] = array( - 'column' => isset($columns[$index]) ? $columns[$index] : '', - 'value' => $value, - 'description' => $description[$index] - ); - } - } else { - $msg = 'The number of values and descriptions need to be the same to add a sparkline. '; - $msg .= 'Values: ' . implode(', ', $values). ' Descriptions: ' . implode(', ', $description); - throw new \Exception($msg); + foreach ($metrics as $index => $metricInfo) { + $metrics[$index]['column'] = isset($columns[$index]) ? $columns[$index] : ''; } if (empty($metrics)) { return; } + $groupedMetrics = []; + foreach ($metrics as $metricInfo) { + $metricGroup = isset($metricInfo['group']) ? $metricInfo['group'] : ''; + $groupedMetrics[$metricGroup][] = $metricInfo; + } + $sparkline = array( 'url' => $this->getUrlSparkline($requestParamsForSparkline), - 'metrics' => $metrics, - 'order' => $this->getSparklineOrder($order) + 'metrics' => $groupedMetrics, + 'order' => $this->getSparklineOrder($order), + 'title' => $title, + 'group' => $group, + 'seriesIndices' => $seriesIndices, + 'graphParams' => $graphParams, ); if (!empty($evolution)) { if (!is_array($evolution) || !array_key_exists('currentValue', $evolution) || - !array_key_exists('pastValue', $evolution)) { + !array_key_exists('pastValue', $evolution) + ) { throw new \Exception('In order to show an evolution in the sparklines view a currentValue and pastValue array key needs to be present'); } @@ -300,7 +341,12 @@ class Config extends \Piwik\ViewDataTable\Config return ($a['order'] < $b['order']) ? -1 : 1; }); - return $this->sparklines; + $sparklines = []; + foreach ($this->sparklines as $sparkline) { + $group = $sparkline['group']; + $sparklines[$group][] = $sparkline; + } + return $sparklines; } private function getSparklineOrder($order) @@ -390,7 +436,13 @@ class Config extends \Piwik\ViewDataTable\Config throw new NoAccessException("Website not initialized, check that you are logged in and/or using the correct token_auth."); } - $paramDate = Range::getRelativeToEndDate($period, $range, $endDate, $site); + if (!isset($paramsToSet['date']) + || !Range::isMultiplePeriod($paramsToSet['date'], $period) + ) { + $paramDate = Range::getRelativeToEndDate($period, $range, $endDate, $site); + } else { + $paramDate = $paramsToSet['date']; + } $params = array_merge($paramsToSet, array('date' => $paramDate)); return $params; |