nova-issues icon indicating copy to clipboard operation
nova-issues copied to clipboard

Option to customize the precision when rounding values in a Trend class

Open Taronyuu opened this issue 8 years ago • 12 comments

Allow the user to set the precision to round values to in a Trend class. This will allow users to have a graph with values that use decimals.

Patch file to give an idea how to fix this:

diff --git a/Website/nova/src/Metrics/Trend.php b/Website/nova/src/Metrics/Trend.php
index 8b4ecf5..0769dc3 100755
--- a/Website/nova/src/Metrics/Trend.php
+++ b/Website/nova/src/Metrics/Trend.php
@@ -502,7 +502,7 @@ abstract class Trend extends RangedMetric
         $results = array_merge($possibleDateResults, $results->mapWithKeys(function ($result) use ($request, $unit) {
             return [$this->formatAggregateResultDate(
                 $result->date_result, $unit, $request->twelveHourTime === 'true'
-            ) => round($result->aggregate, 0)];
+            ) => round($result->aggregate, $this->getPrecision())];
         })->all());
 
         if (count($results) > $request->range) {
@@ -705,4 +705,14 @@ abstract class Trend extends RangedMetric
                         : __($date->format('F')).' '.$date->format('j').' - '.$date->format('G:i');
         }
     }
+
+    /**
+     * getPrecision function.
+     *
+     * @return int
+     */
+    protected function getPrecision(): int
+    {
+        return 0;
+    }
 }

Taronyuu avatar Aug 24 '18 17:08 Taronyuu

Would also be nice to have a 'displayAs()` option to do something like format cents to dollars, or similar.

reganjohnson avatar Aug 24 '18 17:08 reganjohnson

Agree. Maybe have the ability to use computed properties like fields do. As @reganjohnson says, I'm in a situation where I'm summing up a price column in my Orders table, but I can't convert it to dollars (since price is stored in cents).

public function calculate(Request $request)
{
    // can't convert to dollars
    return $this->sum($request, Order::class, 'price');
}

I can probably use return $this->result($sumNow)->previous($sumPrevious); as an alternative, but lose all the nice date ranges.

drehimself avatar Aug 27 '18 18:08 drehimself

@drehimself Did you happen to come up with a solution for the cents to dollars conversion?

alexpgates avatar Sep 01 '18 20:09 alexpgates

@alexpgates I have not, unfortunately. Let me know if you figure out a (not too hacky) way.

drehimself avatar Sep 03 '18 18:09 drehimself

Here's how I handled it:

public function calculate(Request $request)
{
    return $this->sum($request, Order::class, DB::raw('(price/100)'))->dollars();
}

Worked just fine.

hotmeteor avatar Sep 06 '18 13:09 hotmeteor

@hotmeteor Thank you! This solution works great.

drehimself avatar Sep 06 '18 13:09 drehimself

@hotmeteor how can i get other currencies?

willmkt avatar Sep 12 '18 18:09 willmkt

@hotmeteor how can i get other currencies?

Instead of dollars() use your prefix()

return $this->countByDays($request, User::class)->prefix('£');

RicLeP avatar Feb 20 '19 11:02 RicLeP

public function calculate(Request $request)
{
    return $this->sum($request, Order::class, DB::raw('(price/100)'))->dollars();
}

When I do this it’s rounding the result to the nearest pound so 2540 becomes £25

RicLeP avatar Feb 20 '19 11:02 RicLeP

This issue relates to an unsupported version of Nova. If you're still experiencing this in the latest version of Nova, please open a new ticket, thanks!

jbrooksuk avatar Aug 01 '19 08:08 jbrooksuk

@jbrooksuk I was going to open a new ticket as you suggested, but feature requests are closed by default so I will use this one. laravel/nova#681 should not mention this issue because it changed nothing for Trend metrics. I will create a PR fixing this when I have the time later, but it's as easy as applying the same changes you made on that 681 but to Trend instead of Value.

gpressutto5 avatar Apr 28 '20 19:04 gpressutto5

@gpressutto5 Thank you!!!

jonasbrenig avatar Sep 11 '20 13:09 jonasbrenig