Skip to content

Commit

Permalink
Merge pull request #26 from cdterry87/18--add-charts
Browse files Browse the repository at this point in the history
18  add charts
  • Loading branch information
cdterry87 authored Aug 11, 2024
2 parents 32307c3 + 5252a89 commit 338db42
Show file tree
Hide file tree
Showing 10 changed files with 304 additions and 59 deletions.
106 changes: 105 additions & 1 deletion app/Livewire/Home.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,84 @@
use Livewire\Component;
use App\Traits\WithAlerts;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Auth;
use Asantibanez\LivewireCharts\Models\PieChartModel;
use Asantibanez\LivewireCharts\Models\LineChartModel;
use Asantibanez\LivewireCharts\Models\ColumnChartModel;

class Home extends Component
{
use WithAlerts;

private $chartJsonConfig = [
'title' => [
'style' => [
'fontSize' => '18px',
'color' => '#444444'
]
],
'legend' => [
'fontSize' => '12px',
'fontWeight' => 'bold',
'labels' => [
'colors' => [
'#444444'
]
]
],
'tooltip' => [
'theme' => 'light',
],
'xaxis' => [
'labels' => [
'style' => [
'fontSize' => '10px',
'colors' => '#444444'
]
]
],
'yaxis' => [
'labels' => [
'style' => [
'fontSize' => '10px',
'colors' => '#444444'
]
]
],
'dataLabels' => [
'total' => [
'style' => [
'fontSize' => '10px',
'colors' => '#444444'
]
]
]
];

public function render()
{
// Get income summary
$yearlyIncome = auth()->user()->getYearlyIncome();
$monthlyIncome = auth()->user()->getMonthlyIncome();
$biWeeklyIncome = auth()->user()->getBiWeeklyIncome();
$weeklyIncome = auth()->user()->getWeeklyIncome();

// Get notifications
$notifications = $this->getNotifications();

// Get charts
$incomeVsBillsChart = $this->getIncomeVsBillsChart();
$topCategoriesChart = $this->getTopCategoriesChart();

return view('livewire.home', [
'yearlyIncome' => $yearlyIncome,
'monthlyIncome' => $monthlyIncome,
'biWeeklyIncome' => $biWeeklyIncome,
'weeklyIncome' => $weeklyIncome,
'notifications' => $notifications
'notifications' => $notifications,
'incomeVsBillsChart' => $incomeVsBillsChart,
'topCategoriesChart' => $topCategoriesChart,
]);
}

Expand Down Expand Up @@ -59,4 +116,51 @@ public function getNotifications()

return $notifications;
}

public function getIncomeVsBillsChart()
{
$income = auth()->user()->getMonthlyIncomeRaw();
$billsTotal = auth()->user()->currentMonthBills()->sum('amount');

return (new ColumnChartModel())
->withDataLabels()
->setTitle('Income vs Bills')
->addColumn('Income', $income, '#047857')
->addColumn('Expenses', $billsTotal, '#be123c')
->setJsonConfig($this->chartJsonConfig);
}

public function getTopCategoriesChart()
{
// Get currentMonthBills for User, group by category, sum amount for each category, and order by amount desc
$billCategories = auth()->user()->currentMonthBills()
->groupBy('category')
->select('category', DB::raw('sum(amount) as total'))
->orderBy('total', 'desc')
->limit(5)
->get();

$categories = [];
foreach ($billCategories as $billCategory) {
$categories[$billCategory->category] = $billCategory->total;
}

$chart = (new ColumnChartModel())
->withDataLabels()
->setTitle('Top Bill Categories')
->setJsonConfig($this->chartJsonConfig);

$colors = [
'#047857',
'#1d4ed8',
'#6d28d9',
'#be123c',
'#f43f5e',
];
foreach ($categories as $label => $value) {
$chart->addColumn(ucwords($label), $value, array_shift($colors));
}

return $chart;
}
}
24 changes: 24 additions & 0 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,30 @@ public function bills()
return $this->hasMany(Bill::class);
}

public function currentMonthBills()
{
return $this->bills()
->where(function ($query) {
$currentMonth = strtolower(now()->format('F'));

$query->where($currentMonth, true)
->orWhere(function ($query) {
$query->where('january', false)
->where('february', false)
->where('march', false)
->where('april', false)
->where('may', false)
->where('june', false)
->where('july', false)
->where('august', false)
->where('september', false)
->where('october', false)
->where('november', false)
->where('december', false);
});
});
}

public function getYearlyIncome(): string
{
$income = round($this->income * $this->frequency, 2);
Expand Down
16 changes: 13 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
"name": "laravel/laravel",
"type": "project",
"description": "The skeleton application for the Laravel framework.",
"keywords": ["laravel", "framework"],
"keywords": [
"laravel",
"framework"
],
"license": "MIT",
"require": {
"php": "^8.2",
Expand All @@ -11,7 +14,8 @@
"laravel/framework": "^11.9",
"laravel/sanctum": "^4.0",
"laravel/tinker": "^2.9",
"livewire/livewire": "^3.5"
"livewire/livewire": "^3.5",
"asantibanez/livewire-charts": "dev-l11-compatibility"
},
"require-dev": {
"barryvdh/laravel-debugbar": "^3.13",
Expand All @@ -22,6 +26,12 @@
"nunomaduro/collision": "^8.0",
"phpunit/phpunit": "^11.0.1"
},
"repositories": [
{
"type": "vcs",
"url": "https://github.com/laravel-shift/livewire-charts.git"
}
],
"autoload": {
"psr-4": {
"App\\": "app/",
Expand Down Expand Up @@ -67,4 +77,4 @@
},
"minimum-stability": "stable",
"prefer-stable": true
}
}
Loading

0 comments on commit 338db42

Please sign in to comment.