From fa3d7effbb72dc40b6db2c46852d97f968daf7d4 Mon Sep 17 00:00:00 2001
From: Timo Besenreuther <timo.besenreuther@gmail.com>
Date: Tue, 23 Dec 2014 11:17:42 +0100
Subject: [PATCH] fixes #6895 - add a config option that causes the image
 graphs to show the evolution within the selected period instead of the
 evolution across the last n periods

---
 config/global.ini.php             |  4 ++++
 plugins/ImageGraph/ImageGraph.php | 28 ++++++++++++++++++++--------
 2 files changed, 24 insertions(+), 8 deletions(-)

diff --git a/config/global.ini.php b/config/global.ini.php
index c140cf21c9..00596b39ec 100644
--- a/config/global.ini.php
+++ b/config/global.ini.php
@@ -474,6 +474,10 @@ api_service_url = http://api.piwik.org
 ; eg. $period=range&date=previous10 becomes $period=day&date=previous10. Use this setting to override the $period value.
 graphs_default_period_to_plot_when_period_range = day
 
+; When the ImageGraph plugin is activated, enabling this option causes the image graphs to show the evolution
+; within the selected period instead of the evolution across the last n periods.
+graphs_show_evolution_within_selected_period = 0
+
 ; The Overlay plugin shows the Top X following pages, Top X downloads and Top X outlinks which followed
 ; a view of the current page. The value X can be set here.
 overlay_following_pages_limit = 300
diff --git a/plugins/ImageGraph/ImageGraph.php b/plugins/ImageGraph/ImageGraph.php
index 5bd61cc6a9..d38aa8fdde 100644
--- a/plugins/ImageGraph/ImageGraph.php
+++ b/plugins/ImageGraph/ImageGraph.php
@@ -16,6 +16,7 @@ use Piwik\Period\Range;
 use Piwik\Site;
 use Piwik\TaskScheduler;
 use Piwik\Url;
+use Piwik\Period\Factory as PeriodFactory;
 
 class ImageGraph extends \Piwik\Plugin
 {
@@ -88,16 +89,27 @@ class ImageGraph extends \Piwik\Plugin
 
             $piwikSite = new Site($idSite);
             if ($periodForSinglePeriodGraph == 'range') {
+                // for period=range, show the configured sub-periods
                 $periodForMultiplePeriodGraph = Config::getInstance()->General['graphs_default_period_to_plot_when_period_range'];
                 $dateForMultiplePeriodGraph = $dateForSinglePeriodGraph;
-            } else {
-                $periodForMultiplePeriodGraph = $periodForSinglePeriodGraph;
-                $dateForMultiplePeriodGraph = Range::getRelativeToEndDate(
-                    $periodForSinglePeriodGraph,
-                    'last' . self::GRAPH_EVOLUTION_LAST_PERIODS,
-                    $dateForSinglePeriodGraph,
-                    $piwikSite
-                );
+            } else if ($info['period'] == 'day' || !Config::getInstance()->General['graphs_show_evolution_within_selected_period']) {
+                // for period=day, always show the last n days
+                // if graphs_show_evolution_within_selected_period=false, show the last n periods
+				$periodForMultiplePeriodGraph = $periodForSinglePeriodGraph;
+				$dateForMultiplePeriodGraph = Range::getRelativeToEndDate(
+					$periodForSinglePeriodGraph,
+					'last' . self::GRAPH_EVOLUTION_LAST_PERIODS,
+					$dateForSinglePeriodGraph,
+					$piwikSite
+				);
+			} else {
+                // if graphs_show_evolution_within_selected_period=true, show the days withing the period
+                // (except if the period is day, see above)
+				$periodForMultiplePeriodGraph = 'day';
+				$period = PeriodFactory::build($info['period'], $info['date']);
+				$start = $period->getDateStart()->toString();
+				$end = $period->getDateEnd()->toString();
+				$dateForMultiplePeriodGraph = $start . ',' . $end;
             }
         }
 
-- 
GitLab