@php
$fix = function($n) {
return is_float($n) || is_int($n) ? floatval($n) : floatval($n->jsonSerialize()['$numberDecimal']);
};
$fill_data_currency = function($days, $currency_id) use($fix) {
$out = [];
for($i = 0; $i < $days; $i++)
array_push($out, $fix(\Illuminate\Support\Facades\DB::table('invoices')
->where('created_at', '>=', \Carbon\Carbon::today()->subDays($i))
->where('created_at', '<', \Carbon\Carbon::today()->subDays($i - 1))
->where('currency', $currency_id)->where('status', 1)->sum('sum')));
return array_reverse($out);
};
$fill_labels = function($days) {
$out = [];
for($i = 0; $i < $days; $i++)
array_push($out, $i > 0 ? $i .' days ago' : 'Today');
return array_reverse($out);
};
$merge = [];
foreach (\App\Currency\Currency::all() as $currency) {
array_push($merge, [
'name' => $currency->name(),
'data' => $fill_data_currency(7, $currency->id())
]);
}
$chart = new \ArielMejiaDev\LarapexCharts\LarapexChart();
$chart->setTitle('One week')->setType('area')->setHeight(680)->setXAxis($fill_labels(7))->setDataset($merge);
@endphp
{{ $chart->script() }}