Fixed: Charts label formatting..

This commit is contained in:
Cüneyt Şentürk 2022-06-24 15:56:29 +03:00
parent eb7ab67eb8
commit ad03af63cb
3 changed files with 84 additions and 2 deletions

View File

@ -3,6 +3,7 @@
namespace App\Traits; namespace App\Traits;
use Akaunting\Apexcharts\Chart; use Akaunting\Apexcharts\Chart;
use Balping\JsonRaw\Raw;
trait Charts trait Charts
{ {
@ -89,4 +90,79 @@ trait Charts
return $chart; 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;
}
} }

View File

@ -5,13 +5,14 @@ namespace App\Widgets;
use Akaunting\Apexcharts\Chart; use Akaunting\Apexcharts\Chart;
use App\Abstracts\Widget; use App\Abstracts\Widget;
use App\Models\Banking\Transaction; use App\Models\Banking\Transaction;
use App\Traits\Charts;
use App\Traits\Currencies; use App\Traits\Currencies;
use App\Traits\DateTime; use App\Traits\DateTime;
use App\Utilities\Date; use App\Utilities\Date;
class CashFlow extends Widget class CashFlow extends Widget
{ {
use Currencies, DateTime; use Charts, Currencies, DateTime;
public $default_name = 'widgets.cash_flow'; public $default_name = 'widgets.cash_flow';
@ -53,6 +54,11 @@ class CashFlow extends Widget
'legend' => [ 'legend' => [
'position' => 'top', 'position' => 'top',
], ],
'yaxis' => [
'labels' => [
'formatter' => $this->getFormatLabel('percent'),
],
],
]; ];
$chart = new Chart(); $chart = new Chart();

View File

@ -1,5 +1,5 @@
<script type="text/javascript"><!-- <script type="text/javascript"><!--
var options = {!! json_encode($chart->getOptions()) !!}; var options = {!! $chart->getOptions() !!};
var chart_{{ $chart->getId() }} = new ApexCharts(document.querySelector("#{!! $chart->getId() !!}"), options); var chart_{{ $chart->getId() }} = new ApexCharts(document.querySelector("#{!! $chart->getId() !!}"), options);