insenseanalytics /
nova-bar-metrics
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Insenseanalytics\NovaBarMetrics; |
||
| 4 | |||
| 5 | use Laravel\Nova\Metrics\PartitionResult; |
||
| 6 | |||
| 7 | class BarChartMetricResult extends PartitionResult |
||
| 8 | { |
||
| 9 | /** |
||
| 10 | * The metric value prefix. |
||
| 11 | * |
||
| 12 | * @var string |
||
| 13 | */ |
||
| 14 | public $prefix; |
||
| 15 | |||
| 16 | /** |
||
| 17 | * The metric value suffix. |
||
| 18 | * |
||
| 19 | * @var string |
||
| 20 | */ |
||
| 21 | public $suffix; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * The precision of the average metric. |
||
| 25 | * |
||
| 26 | * @var int |
||
| 27 | */ |
||
| 28 | public $precision = 2; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Set the metric value prefix. |
||
| 32 | * |
||
| 33 | * @param string $prefix |
||
| 34 | * |
||
| 35 | * @return $this |
||
| 36 | */ |
||
| 37 | public function prefix($prefix) |
||
| 38 | { |
||
| 39 | $this->prefix = $prefix; |
||
| 40 | |||
| 41 | return $this; |
||
| 42 | } |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Set the metric value suffix. |
||
| 46 | * |
||
| 47 | * @param string $suffix |
||
| 48 | * |
||
| 49 | * @return $this |
||
| 50 | */ |
||
| 51 | public function suffix($suffix) |
||
| 52 | { |
||
| 53 | $this->suffix = $suffix; |
||
| 54 | |||
| 55 | return $this; |
||
| 56 | } |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Set the precision of the average metric. |
||
| 60 | * |
||
| 61 | * @param int $precision |
||
| 62 | * |
||
| 63 | * @return $this |
||
| 64 | */ |
||
| 65 | public function precision($precision) |
||
| 66 | { |
||
| 67 | $this->precision = $precision; |
||
| 68 | |||
| 69 | return $this; |
||
| 70 | } |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Indicate that the metric represents a dollar value. |
||
| 74 | * |
||
| 75 | * @param string $symbol |
||
| 76 | * |
||
| 77 | * @return $this |
||
| 78 | */ |
||
| 79 | public function dollars($symbol = '$') |
||
| 80 | { |
||
| 81 | return $this->prefix($symbol); |
||
| 82 | } |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Indicate that the metric represents a euro value. |
||
| 86 | * |
||
| 87 | * @param string $symbol |
||
| 88 | * |
||
| 89 | * @return $this |
||
| 90 | */ |
||
| 91 | public function euros($symbol = '€') |
||
| 92 | { |
||
| 93 | return $this->prefix($symbol); |
||
| 94 | } |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Indicate that the metric represents a rupee value. |
||
| 98 | * |
||
| 99 | * @param string $symbol |
||
| 100 | * |
||
| 101 | * @return $this |
||
| 102 | */ |
||
| 103 | public function rupees($symbol = '₹') |
||
| 104 | { |
||
| 105 | return $this->prefix($symbol); |
||
| 106 | } |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Prepare the metric result for JSON serialization. |
||
| 110 | * |
||
| 111 | * @return array |
||
| 112 | */ |
||
| 113 | public function jsonSerialize() |
||
| 114 | { |
||
| 115 | return [ |
||
| 116 | 'value' => collect($this->value ?? [])->map(function ($value, $label) { |
||
| 117 | return ['label' => $label, 'value' => $value]; |
||
| 118 | })->values()->all(), |
||
| 119 | 'prefix' => $this->prefix, |
||
| 120 | 'suffix' => $this->suffix, |
||
| 121 | 'precision' => $this->precision, |
||
| 122 | ]; |
||
| 123 | } |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Format the range labels to friendly numbers (K for 1000s, M for millions, B for billions). |
||
| 127 | * |
||
| 128 | * @return array |
||
| 129 | */ |
||
| 130 | public function withFormattedRangeLabels($labelPrecision = 0) |
||
| 131 | { |
||
| 132 | $newData = []; |
||
| 133 | |||
| 134 | foreach (($this->value ?? []) as $label => $value) { |
||
| 135 | $labelParts = explode('-', $label); |
||
| 136 | if (2 == count($labelParts)) { |
||
| 137 | $newLabel = $this->formatStrNumber($labelParts[0], $labelPrecision) . ' - ' . |
||
| 138 | $this->formatStrNumber($labelParts[1], $labelPrecision); |
||
| 139 | $newData[$newLabel] = isset($newData[$newLabel]) ? ($value + $newData[$newLabel]) : |
||
| 140 | $value; |
||
| 141 | } |
||
| 142 | } |
||
| 143 | |||
| 144 | $this->value = $newData; |
||
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
| 145 | |||
| 146 | return $this; |
||
|
0 ignored issues
–
show
|
|||
| 147 | } |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Helper function to format a string number. |
||
| 151 | * |
||
| 152 | * @return string |
||
| 153 | */ |
||
| 154 | protected function formatStrNumber($number, $labelPrecision) |
||
| 155 | { |
||
| 156 | $n = intval(trim($number)); |
||
| 157 | |||
| 158 | if ($n < 999) { |
||
| 159 | return $n . ''; |
||
| 160 | } elseif ($n < 999999) { |
||
| 161 | // anything less than a million |
||
| 162 | return number_format($n / 1000, $labelPrecision) . 'K'; |
||
| 163 | } elseif ($n < 999999999) { |
||
| 164 | // anything less than a billion |
||
| 165 | return number_format($n / 1000000, $labelPrecision) . 'M'; |
||
| 166 | } else { |
||
| 167 | // At least a billion |
||
| 168 | return number_format($n / 1000000000, $labelPrecision) . 'B'; |
||
| 169 | } |
||
| 170 | } |
||
| 171 | } |
||
| 172 |