diff options
author | Matthieu Aubry <matt@piwik.org> | 2015-02-23 06:14:40 +0300 |
---|---|---|
committer | Matthieu Aubry <matt@piwik.org> | 2015-02-23 06:14:40 +0300 |
commit | 03f08f4187d14cf0296c3a73663251ff08bfeede (patch) | |
tree | 4e6b2484bd58ac23a8ccd1108c8b9264891ecc4d | |
parent | d780c4495e0e4d42d304a76256eefec81aee5620 (diff) | |
parent | c07f896ac1dbfbbdcb21ba30f28fec83866ca56f (diff) |
Merge pull request #7262 from piwik/7247_graph_formatting_specialization
Fixes #7247, move default Formatter class to new Numeric metrics formatt.er and use in Graph visualization.
-rw-r--r-- | core/Metrics/Formatter.php | 52 | ||||
-rwxr-xr-x | core/Twig.php | 1 | ||||
-rw-r--r-- | plugins/CoreVisualizations/Metrics/Formatter/Numeric.php | 44 | ||||
-rw-r--r-- | plugins/CoreVisualizations/Visualizations/Graph.php | 3 | ||||
-rw-r--r-- | tests/UI/specs/EvolutionGraph_spec.js | 2 |
5 files changed, 81 insertions, 21 deletions
diff --git a/core/Metrics/Formatter.php b/core/Metrics/Formatter.php index bdf920e6a0..bce37e3be4 100644 --- a/core/Metrics/Formatter.php +++ b/core/Metrics/Formatter.php @@ -21,8 +21,6 @@ use Piwik\Tracker\GoalManager; /** * Contains methods to format metric values. Passed to the {@link \Piwik\Plugin\Metric::format()} * method when formatting Metrics. - * - * @api */ class Formatter { @@ -38,6 +36,7 @@ class Formatter * * @param number $value * @return string + * @api */ public function getPrettyNumber($value, $precision = 0) { @@ -58,6 +57,7 @@ class Formatter * @param bool $displayTimeAsSentence If set to true, will output `"5min 17s"`, if false `"00:05:17"`. * @param bool $round Whether to round to the nearest second or not. * @return string + * @api */ public function getPrettyTimeFromSeconds($numberOfSeconds, $displayTimeAsSentence = false, $round = false) { @@ -126,6 +126,7 @@ class Formatter * @param string $unit The specific unit to use, if any. If null, the unit is determined by $size. * @param int $precision The precision to use when rounding. * @return string eg, `'128 M'` or `'256 K'`. + * @api */ public function getPrettySizeFromBytes($size, $unit = null, $precision = 1) { @@ -133,23 +134,8 @@ class Formatter return '0 M'; } - $units = array('B', 'K', 'M', 'G', 'T'); - $numUnits = count($units) - 1; - - $currentUnit = null; - foreach ($units as $idx => $currentUnit) { - if ($unit && $unit !== $currentUnit) { - $size = $size / 1024; - } elseif ($unit && $unit === $currentUnit) { - break; - } elseif ($size >= 1024 && $idx != $numUnits) { - $size = $size / 1024; - } else { - break; - } - } - - return round($size, $precision) . " " . $currentUnit; + list($size, $sizeUnit) = $this->getPrettySizeFromBytesWithUnit($size, $unit, $precision); + return $size . " " . $sizeUnit; } /** @@ -158,6 +144,7 @@ class Formatter * @param int|string $value The monetary value to format. * @param int $idSite The ID of the site whose currency will be used. * @return string + * @api */ public function getPrettyMoney($value, $idSite) { @@ -199,6 +186,7 @@ class Formatter * * @param float $value * @return string + * @api */ public function getPrettyPercentFromQuotient($value) { @@ -211,6 +199,7 @@ class Formatter * * @param int $idSite The ID of the site to return the currency symbol for. * @return string eg, `'$'`. + * @api */ public static function getCurrencySymbol($idSite) { @@ -232,6 +221,7 @@ class Formatter * * @deprecated Use Piwik\Intl\Data\Provider\CurrencyDataProvider instead. * @see \Piwik\Intl\Data\Provider\CurrencyDataProvider::getCurrencyList() + * @api */ public static function getCurrencyList() { @@ -247,6 +237,7 @@ class Formatter * @param DataTable $dataTable The table to format metrics for. * @param Report|null $report The report the table belongs to. * @param string[]|null $metricsToFormat Whitelist of names of metrics to format. + * @api */ public function formatMetrics(DataTable $dataTable, Report $report = null, $metricsToFormat = null) { @@ -285,6 +276,29 @@ class Formatter } } + protected function getPrettySizeFromBytesWithUnit($size, $unit = null, $precision = 1) + { + $units = array('B', 'K', 'M', 'G', 'T'); + $numUnits = count($units) - 1; + + $currentUnit = null; + foreach ($units as $idx => $currentUnit) { + if ($unit && $unit !== $currentUnit) { + $size = $size / 1024; + } elseif ($unit && $unit === $currentUnit) { + break; + } elseif ($size >= 1024 && $idx != $numUnits) { + $size = $size / 1024; + } else { + break; + } + } + + $size = round($size, $precision); + + return array($size, $currentUnit); + } + private function makeRegexToMatchMetrics($metricsToFormat) { $metricsRegexParts = array(); diff --git a/core/Twig.php b/core/Twig.php index 8eeb34ffe4..c60b430d50 100755 --- a/core/Twig.php +++ b/core/Twig.php @@ -12,7 +12,6 @@ use Exception; use Piwik\Container\StaticContainer; use Piwik\DataTable\Filter\SafeDecodeLabel; use Piwik\Metrics\Formatter; -use Piwik\Translate; use Piwik\View\RenderTokenParser; use Piwik\Visualization\Sparkline; use Twig_Environment; diff --git a/plugins/CoreVisualizations/Metrics/Formatter/Numeric.php b/plugins/CoreVisualizations/Metrics/Formatter/Numeric.php new file mode 100644 index 0000000000..415dc40995 --- /dev/null +++ b/plugins/CoreVisualizations/Metrics/Formatter/Numeric.php @@ -0,0 +1,44 @@ +<?php +/** + * Piwik - free/libre analytics platform + * + * @link http://piwik.org + * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later + */ +namespace Piwik\Plugins\CoreVisualizations\Metrics\Formatter; + +use Piwik\Metrics\Formatter; + +/** + * A metrics formatter that prettifies metric values without returning string values. + * Results of this class can be converted to numeric values and processed further in + * some way. + */ +class Numeric extends Formatter +{ + public function getPrettyNumber($value, $precision = 0) + { + return round($value, $precision); + } + + public function getPrettyTimeFromSeconds($numberOfSeconds, $displayTimeAsSentence = false, $round = false) + { + return $round ? (int)$numberOfSeconds : (float)$numberOfSeconds; + } + + public function getPrettySizeFromBytes($size, $unit = null, $precision = 1) + { + list($size, $sizeUnit) = $this->getPrettySizeFromBytesWithUnit($size, $unit, $precision); + return $size; + } + + public function getPrettyMoney($value, $idSite) + { + return $value; + } + + public function getPrettyPercentFromQuotient($value) + { + return $value * 100; + } +}
\ No newline at end of file diff --git a/plugins/CoreVisualizations/Visualizations/Graph.php b/plugins/CoreVisualizations/Visualizations/Graph.php index f95712418d..3e87754817 100644 --- a/plugins/CoreVisualizations/Visualizations/Graph.php +++ b/plugins/CoreVisualizations/Visualizations/Graph.php @@ -10,6 +10,7 @@ namespace Piwik\Plugins\CoreVisualizations\Visualizations; use Piwik\DataTable; use Piwik\DataTable\Row; +use Piwik\Plugins\CoreVisualizations\Metrics\Formatter\Numeric; use Piwik\Piwik; use Piwik\Plugin\Visualization; @@ -60,6 +61,8 @@ abstract class Graph extends Visualization $this->requestConfig->request_parameters_to_modify['disable_queued_filters'] = 1; $this->requestConfig->request_parameters_to_modify['format_metrics'] = 1; + + $this->metricsFormatter = new Numeric(); } /** diff --git a/tests/UI/specs/EvolutionGraph_spec.js b/tests/UI/specs/EvolutionGraph_spec.js index c6576516a2..56f3b89cec 100644 --- a/tests/UI/specs/EvolutionGraph_spec.js +++ b/tests/UI/specs/EvolutionGraph_spec.js @@ -26,7 +26,7 @@ describe("EvolutionGraph", function () { it("should show percent metrics like bounce rate correctly", function (done) { expect.screenshot('bounce_rate').to.be.capture(function (page) { - page.load(url + "&columns=nb_visits,bounce_rate&filter_add_columns_when_show_all_columns=0"); + page.load(url + "&columns=nb_visits,bounce_rate,avg_time_on_site&filter_add_columns_when_show_all_columns=0"); }, done); }); |