Files
investbrain/resources/views/portfolio/portfolio-performance-chart.blade.php
T

173 lines
5.5 KiB
PHP
Raw Normal View History

2024-08-06 20:41:52 -05:00
<?php
2024-08-21 20:42:32 -05:00
use App\Models\DailyChange;
2024-08-06 20:41:52 -05:00
use App\Models\Portfolio;
use Livewire\Attributes\Lazy;
2024-08-06 20:41:52 -05:00
use Livewire\Volt\Component;
new #[Lazy] class extends Component
2025-03-10 21:17:24 -05:00
{
2024-08-06 20:41:52 -05:00
// props
2025-08-21 20:12:59 -05:00
public ?Portfolio $portfolio = null;
2025-03-10 21:17:24 -05:00
public string $name = 'portfolio';
public string $scope = 'YTD';
public array $scopeOptions = [
2024-08-21 20:42:32 -05:00
['id' => '1M', 'name' => '1 month', 'method' => 'subMonths', 'args' => [1]],
['id' => '3M', 'name' => '3 months', 'method' => 'subMonths', 'args' => [3]],
['id' => 'YTD', 'name' => 'Year to date', 'method' => 'startOfYear', 'args' => []],
['id' => '1Y', 'name' => '1 year', 'method' => 'subYears', 'args' => [1]],
['id' => '3Y', 'name' => '3 years', 'method' => 'subYears', 'args' => [3]],
2025-03-10 21:17:24 -05:00
['id' => 'ALL', 'name' => 'All time', 'method' => null],
2024-08-06 20:41:52 -05:00
];
// data
2025-03-10 21:17:24 -05:00
public array $chartSeries;
2024-08-06 20:41:52 -05:00
// methods
2025-03-10 21:17:24 -05:00
public function mount()
2024-08-06 20:41:52 -05:00
{
2024-08-21 20:42:32 -05:00
$this->chartSeries = $this->generatePerformanceData();
}
public function placeholder()
{
return <<<'HTML'
<div class="skeleton h-[395px] mb-5"></div>
HTML;
}
2024-08-21 20:42:32 -05:00
public function generatePerformanceData()
{
$filterMethod = collect($this->scopeOptions)->where('id', $this->scope)->first();
2024-08-21 20:42:32 -05:00
2025-04-09 19:25:15 -05:00
$dailyChangeQuery = DailyChange::withDailyPerformance();
2024-08-21 20:42:32 -05:00
if (isset($this->portfolio)) {
2025-03-10 21:17:24 -05:00
// portfolio
2025-08-19 21:54:18 -05:00
$dailyChangeQuery->portfolio($this->portfolio->id);
2024-08-21 20:47:06 -05:00
2024-08-21 20:42:32 -05:00
} else {
2024-08-22 22:45:17 -05:00
2025-03-10 21:17:24 -05:00
// dashboard
2025-08-19 21:54:18 -05:00
$dailyChangeQuery->myDailyChanges()->withoutWishlists();
2024-08-21 20:42:32 -05:00
}
if ($filterMethod['method']) {
2025-03-10 21:17:24 -05:00
2025-08-19 21:54:18 -05:00
$dailyChangeQuery->whereDate('daily_change.date', '>=', now()->{$filterMethod['method']}(...$filterMethod['args']));
2024-08-21 20:42:32 -05:00
}
2025-03-10 21:17:24 -05:00
2025-08-11 21:51:54 -05:00
$dailyChange = cache()->remember(
2025-08-19 21:47:27 -05:00
'graph-'.$this->scope.'-'.(isset($this->portfolio) ? $this->portfolio->id : request()->user()->id),
2025-08-21 20:12:59 -05:00
10,
2025-08-11 21:51:54 -05:00
function () use ($dailyChangeQuery) {
2025-08-21 20:54:14 -05:00
return $dailyChangeQuery->withMultipleDailyPerformance()->get();
2025-08-11 21:51:54 -05:00
}
);
2024-08-21 20:47:06 -05:00
2025-08-22 20:49:12 -05:00
$marketValueData = [];
$costBasisData = [];
$marketGainData = [];
foreach ($dailyChange as $data) {
$date = $data->date;
$marketGainData[] = [$date, round($data->total_market_gain, 2)];
2025-08-22 20:49:12 -05:00
$marketValueData[] = [$date, round($data->total_market_value, 2)];
$costBasisData[] = [$date, round($data->total_cost_basis, 2)];
2025-08-22 20:49:12 -05:00
// $dividendSeries[] = [$date, round($data->total_dividends_earned, 2)];
// $realizedGainSeries[] = [$date, round($data->realized_gains, 2)];
}
2024-08-21 20:42:32 -05:00
return [
2024-08-06 20:41:52 -05:00
'series' => [
[
'name' => __('Market Gain'),
'data' => $marketGainData,
],
2024-08-06 20:41:52 -05:00
[
2024-08-21 20:42:32 -05:00
'name' => __('Market Value'),
2025-08-22 20:49:12 -05:00
'data' => $marketValueData,
'hidden' => true,
2024-08-21 20:42:32 -05:00
],
2024-08-06 20:41:52 -05:00
[
2024-08-21 20:42:32 -05:00
'name' => __('Cost Basis'),
2025-08-22 20:49:12 -05:00
'data' => $costBasisData,
'hidden' => true,
2024-08-21 20:42:32 -05:00
],
2025-03-10 21:17:24 -05:00
// [
// 'name' => __('Dividends Earned'),
2025-08-22 20:49:12 -05:00
// 'data' => $dividendSeries
// ],
2024-08-22 22:45:17 -05:00
// [
// 'name' => __('Realized Gains'),
2025-08-22 20:49:12 -05:00
// 'data' => $realizedGainSeries
2024-08-22 22:45:17 -05:00
// ],
2025-03-10 21:17:24 -05:00
],
2024-08-06 20:41:52 -05:00
];
}
public function changeScope($scope)
{
$this->scope = $scope;
2025-08-21 20:12:59 -05:00
cache()->forget('graph-'.$this->scope.'-'.(isset($this->portfolio) ? $this->portfolio->id : request()->user()->id));
2025-08-11 21:51:54 -05:00
2024-08-21 20:42:32 -05:00
$this->chartSeries = $this->generatePerformanceData();
2024-08-06 20:41:52 -05:00
}
public function getScopeName($scope)
{
return collect($this->scopeOptions)->where('id', $scope)->first()['name'];
2024-08-06 20:41:52 -05:00
}
}; ?>
<x-ui.card class="mb-6">
2024-10-18 19:42:53 -05:00
<div class="flex flex-col md:flex-row md:justify-between mb-2">
2024-08-06 20:41:52 -05:00
2024-10-18 19:42:53 -05:00
<div class="flex flex-col md:flex-row items-start md:items-center">
2024-08-06 20:41:52 -05:00
2024-10-18 19:42:53 -05:00
<h2 class="text-xl mb-2 md:mb-0 md:mr-4">{{ __('Performance') }}</h2>
2024-10-01 13:58:17 -05:00
2024-10-18 19:42:53 -05:00
<div id="chart-legend-{{ $name }}" class="flex space-between whitespace-nowrap mb-2 md:mb-0"></div>
2024-08-06 20:41:52 -05:00
</div>
2024-08-06 23:12:57 -05:00
2024-09-19 21:31:54 -05:00
<div class="flex items-center" x-data="{ loading: false }">
{{-- <x-ui.button title="{{ __('Reset chart') }}" icon="o-arrow-path" class="btn-ghost btn-sm btn-circle mr-2" id="chart-reset-zoom-{{ $name }}" /> --}}
2024-08-06 20:41:52 -05:00
<x-ui.loading x-show="loading" x-cloak class="text-gray-400 ml-2" />
2024-09-19 21:31:54 -05:00
<x-ui.dropdown title="{{ __('Choose time period') }}" label="{{ $scope }}" class="btn-xs md:btn-sm btn-outline" x-bind:disabled="loading">
2024-08-06 23:12:57 -05:00
@foreach($scopeOptions as $option)
2024-08-07 18:28:57 -05:00
<x-ui.menu-item
2024-08-07 18:28:57 -05:00
title="{{ $option['name'] }}"
2024-09-19 21:31:54 -05:00
@click="
timeout = setTimeout(() => { loading = true }, 200);
$wire.changeScope('{{ $option['id'] }}').then(() => {
clearTimeout(timeout);
loading = false;
})
"
2024-08-07 18:28:57 -05:00
/>
@endforeach
2024-08-06 23:12:57 -05:00
</x-dropdown>
</div>
2024-08-06 20:41:52 -05:00
</div>
<div
class="h-[280px] mb-5"
>
<x-ui.apex-chart :series-data="$chartSeries" :name="$name" />
2024-08-06 20:41:52 -05:00
</div>
</x-ui.card>