diff --git a/app/Traits/Charts.php b/app/Traits/Charts.php index 130f6fad7..be223e77f 100644 --- a/app/Traits/Charts.php +++ b/app/Traits/Charts.php @@ -3,6 +3,7 @@ namespace App\Traits; use Akaunting\Apexcharts\Chart; +use Balping\JsonRaw\Raw; trait Charts { @@ -89,4 +90,79 @@ trait Charts return $chart; } + + public function getFormatLabel($type = 'money', $position = null) + { + $label = ''; + $decimal_mark = str_replace("'", "\\'", config('money.' . setting('default.currency') . '.decimal_mark')); + $thousands_separator = str_replace("'", "\\'", config('money.' . setting('default.currency') . '.thousands_separator')); + $symbol = str_replace("'", "\\'", config('money.' . setting('default.currency') . '.symbol')); + $symbol_first = str_replace("'", "\\'", config('money.' . setting('default.currency') . '.symbol_first')); + $precision = str_replace("'", "\\'", config('money.' . setting('default.currency') . '.precision')); + $percent_position = $position ?: setting('localisation.percent_position'); + + switch ($type) { + case 'percent': + $label = new Raw("function(value) { + " . ($percent_position == 'right' ? "return value + '%';" : "return '%' + value;") . " + }"); + break; + default: + $label = new Raw("function(value) { + const moneySettings = { + decimal: '" . $decimal_mark . "', + thousands: '". $thousands_separator . "', + symbol: '" . $symbol . "', + isPrefix: '" . $symbol_first . "', + precision: '" . $precision . "', + }; + + const formattedCurrency = function (input, opt = moneySettings) { + if (typeof input === 'number') { + input = input.toFixed(fixed(opt.precision)) + } + + function fixed (precision) { + return Math.max(0, Math.min(precision, 20)); + }; + + function toStr(value) { + return value ? value.toString() : ''; + }; + + function numbersToCurrency(numbers, precision) { + var exp = Math.pow(10, precision); + var float = parseFloat(numbers) / exp; + + return float.toFixed(fixed(precision)); + }; + + function joinIntegerAndDecimal (integer, decimal, separator) { + return decimal ? integer + separator + decimal : integer; + }; + + if (typeof input === 'number') { + input = input.toFixed(fixed(opt.precision)); + }; + + var negative = input.indexOf('-') >= 0 ? '-' : ''; + var numbers = toStr(input).replace(/\D+/g, '') || '0'; + var currency = numbersToCurrency(numbers, opt.precision); + var parts = toStr(currency).split('.'); + var integer = parts[0].replace(/(\d)(?=(?:\d{3})+\b)/gm, ('$1' + opt.thousands)); + var decimal = parts[1]; + + if (opt.isPrefix == 1) { + return opt.symbol + negative + joinIntegerAndDecimal(integer, decimal, opt.decimal); + } + + return negative + joinIntegerAndDecimal(integer, decimal, opt.decimal) + opt.symbol; + }; + + return formattedCurrency(value, moneySettings); + }"); + } + + return $label; + } } diff --git a/app/Widgets/CashFlow.php b/app/Widgets/CashFlow.php index 570864757..7ed298ddb 100644 --- a/app/Widgets/CashFlow.php +++ b/app/Widgets/CashFlow.php @@ -5,13 +5,14 @@ namespace App\Widgets; use Akaunting\Apexcharts\Chart; use App\Abstracts\Widget; use App\Models\Banking\Transaction; +use App\Traits\Charts; use App\Traits\Currencies; use App\Traits\DateTime; use App\Utilities\Date; class CashFlow extends Widget { - use Currencies, DateTime; + use Charts, Currencies, DateTime; public $default_name = 'widgets.cash_flow'; @@ -53,6 +54,11 @@ class CashFlow extends Widget 'legend' => [ 'position' => 'top', ], + 'yaxis' => [ + 'labels' => [ + 'formatter' => $this->getFormatLabel('percent'), + ], + ], ]; $chart = new Chart(); diff --git a/resources/views/vendor/apexcharts/script.blade.php b/resources/views/vendor/apexcharts/script.blade.php index 53af546cd..ba9e23b65 100644 --- a/resources/views/vendor/apexcharts/script.blade.php +++ b/resources/views/vendor/apexcharts/script.blade.php @@ -1,5 +1,5 @@