Option to customize the precision when rounding values in a Trend class
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;
+ }
}
Would also be nice to have a 'displayAs()` option to do something like format cents to dollars, or similar.
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 Did you happen to come up with a solution for the cents to dollars conversion?
@alexpgates I have not, unfortunately. Let me know if you figure out a (not too hacky) way.
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 Thank you! This solution works great.
@hotmeteor how can i get other currencies?
@hotmeteor how can i get other currencies?
Instead of dollars() use your prefix()
return $this->countByDays($request, User::class)->prefix('£');
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
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 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 Thank you!!!