diff options
Diffstat (limited to 'plugins/CoreVisualizations/Visualizations/Sparklines/Config.php')
-rw-r--r-- | plugins/CoreVisualizations/Visualizations/Sparklines/Config.php | 110 |
1 files changed, 81 insertions, 29 deletions
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; |