mirror of
				https://github.com/firefly-iii/firefly-iii.git
				synced 2025-10-29 22:47:42 +00:00 
			
		
		
		
	Fixed category chart
This commit is contained in:
		| @@ -37,6 +37,13 @@ interface CategoryChartGeneratorInterface | |||||||
|      */ |      */ | ||||||
|     public function all(Collection $entries): array; |     public function all(Collection $entries): array; | ||||||
|  |  | ||||||
|  |     /** | ||||||
|  |      * @param array $entries | ||||||
|  |      * | ||||||
|  |      * @return array | ||||||
|  |      */ | ||||||
|  |     public function mainReportChart(array $entries): array; | ||||||
|  |  | ||||||
|     /** |     /** | ||||||
|      * @param Collection $categories |      * @param Collection $categories | ||||||
|      * @param Collection $entries |      * @param Collection $entries | ||||||
|   | |||||||
| @@ -118,6 +118,51 @@ class ChartJsCategoryChartGenerator implements CategoryChartGeneratorInterface | |||||||
|         return $data; |         return $data; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |     /** | ||||||
|  |      * @param array $entries | ||||||
|  |      * | ||||||
|  |      * @return array | ||||||
|  |      */ | ||||||
|  |     public function mainReportChart(array $entries): array | ||||||
|  |     { | ||||||
|  |  | ||||||
|  |         $data = [ | ||||||
|  |             'count'    => 0, | ||||||
|  |             'labels'   => array_keys($entries), | ||||||
|  |             'datasets' => [], | ||||||
|  |         ]; | ||||||
|  |  | ||||||
|  |  | ||||||
|  |         foreach ($entries as $row) { | ||||||
|  |             foreach ($row['in'] as $categoryId => $amount) { | ||||||
|  |                 // get in: | ||||||
|  |                 $data['datasets'][$categoryId . 'in']['data'][] = round($amount, 2); | ||||||
|  |  | ||||||
|  |                 // get out: | ||||||
|  |                 $opposite                                        = $row['out'][$categoryId]; | ||||||
|  |                 $data['datasets'][$categoryId . 'out']['data'][] = round($opposite, 2); | ||||||
|  |  | ||||||
|  |                 // set name: | ||||||
|  |                 $data['datasets'][$categoryId . 'out']['label'] = $row['name'][$categoryId] . ' (' . strtolower(strval(trans('firefly.expenses'))) . ')'; | ||||||
|  |                 $data['datasets'][$categoryId . 'in']['label']  = $row['name'][$categoryId] . ' (' . strtolower(strval(trans('firefly.income'))) . ')'; | ||||||
|  |  | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         // remove empty rows: | ||||||
|  |         foreach ($data['datasets'] as $key => $content) { | ||||||
|  |             if (array_sum($content['data']) === 0.0) { | ||||||
|  |                 unset($data['datasets'][$key]); | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         // re-key the datasets array: | ||||||
|  |         $data['datasets'] = array_values($data['datasets']); | ||||||
|  |         $data['count']    = count($data['datasets']); | ||||||
|  |  | ||||||
|  |         return $data; | ||||||
|  |     } | ||||||
|  |  | ||||||
|     /** |     /** | ||||||
|      * |      * | ||||||
|      * @param Collection $entries |      * @param Collection $entries | ||||||
|   | |||||||
| @@ -19,12 +19,14 @@ use FireflyIII\Generator\Chart\Category\CategoryChartGeneratorInterface; | |||||||
| use FireflyIII\Generator\Report\Category\MonthReportGenerator; | use FireflyIII\Generator\Report\Category\MonthReportGenerator; | ||||||
| use FireflyIII\Helpers\Collector\JournalCollector; | use FireflyIII\Helpers\Collector\JournalCollector; | ||||||
| use FireflyIII\Http\Controllers\Controller; | use FireflyIII\Http\Controllers\Controller; | ||||||
|  | use FireflyIII\Models\Category; | ||||||
| use FireflyIII\Models\Transaction; | use FireflyIII\Models\Transaction; | ||||||
| use FireflyIII\Models\TransactionType; | use FireflyIII\Models\TransactionType; | ||||||
| use FireflyIII\Repositories\Account\AccountRepositoryInterface; | use FireflyIII\Repositories\Account\AccountRepositoryInterface; | ||||||
| use FireflyIII\Repositories\Category\CategoryRepositoryInterface; | use FireflyIII\Repositories\Category\CategoryRepositoryInterface; | ||||||
| use Illuminate\Support\Collection; | use Illuminate\Support\Collection; | ||||||
| use Log; | use Log; | ||||||
|  | use Navigation; | ||||||
| use Response; | use Response; | ||||||
|  |  | ||||||
|  |  | ||||||
| @@ -260,6 +262,57 @@ class CategoryReportController extends Controller | |||||||
|         return Response::json($data); |         return Response::json($data); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |     /** | ||||||
|  |      * @param Collection $accounts | ||||||
|  |      * @param Collection $categories | ||||||
|  |      * @param Carbon     $start | ||||||
|  |      * @param Carbon     $end | ||||||
|  |      * | ||||||
|  |      * @return \Illuminate\Http\JsonResponse | ||||||
|  |      */ | ||||||
|  |     public function mainChart(Collection $accounts, Collection $categories, Carbon $start, Carbon $end) | ||||||
|  |     { | ||||||
|  |         // determin optimal period: | ||||||
|  |         $period = '1D'; | ||||||
|  |         $format = 'month_and_day'; | ||||||
|  |         if ($start->diffInMonths($end) > 1) { | ||||||
|  |             $period = '1M'; | ||||||
|  |             $format = 'month'; | ||||||
|  |         } | ||||||
|  |         if ($start->diffInMonths($end) > 13) { | ||||||
|  |             $period = '1Y'; | ||||||
|  |             $format = 'year'; | ||||||
|  |         } | ||||||
|  |         Log::debug(sprintf('Period is %s', $period)); | ||||||
|  |         $data    = []; | ||||||
|  |         $current = clone $start; | ||||||
|  |         while ($current < $end) { | ||||||
|  |             $currentEnd   = Navigation::endOfPeriod($current, $period); | ||||||
|  |             $expenses     = $this->groupByCategory($this->getExpenses($accounts, $categories, $current, $currentEnd)); | ||||||
|  |             $income       = $this->groupByCategory($this->getIncome($accounts, $categories, $current, $currentEnd)); | ||||||
|  |             $label        = $current->formatLocalized(strval(trans('config.' . $format))); | ||||||
|  |             $data[$label] = [ | ||||||
|  |                 'in'  => [], | ||||||
|  |                 'out' => [], | ||||||
|  |             ]; | ||||||
|  |  | ||||||
|  |             /** @var Category $category */ | ||||||
|  |             foreach ($categories as $category) { | ||||||
|  |                 // get sum, and get label: | ||||||
|  |                 $categoryId                       = $category->id; | ||||||
|  |                 $data[$label]['name'][$categoryId]             = $category->name; | ||||||
|  |                 $data[$label]['in'][$categoryId]  = $income[$categoryId] ?? '0'; | ||||||
|  |                 $data[$label]['out'][$categoryId] = $expenses[$categoryId] ?? '0'; | ||||||
|  |             } | ||||||
|  |  | ||||||
|  |             $current = Navigation::addPeriod($current, $period, 0); | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         $data = $this->generator->mainReportChart($data); | ||||||
|  |         return Response::json($data); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |  | ||||||
|     /** |     /** | ||||||
|      * @param Collection $accounts |      * @param Collection $accounts | ||||||
|      * @param Collection $categories |      * @param Collection $categories | ||||||
|   | |||||||
| @@ -42,12 +42,15 @@ function drawChart() { | |||||||
|     "use strict"; |     "use strict"; | ||||||
|  |  | ||||||
|     // month view: |     // month view: | ||||||
|  |     stackedColumnChart('chart/category-report-in-out/' + accountIds + '/' + categoryIds + '/' + startDate + '/' + endDate, 'in-out-chart'); | ||||||
|  |  | ||||||
|     // draw pie chart of income, depending on "show other transactions too": |     // draw pie chart of income, depending on "show other transactions too": | ||||||
|     redrawPieChart('categories-in-pie-chart', catInUri); |     redrawPieChart('categories-in-pie-chart', catInUri); | ||||||
|     redrawPieChart('categories-out-pie-chart', catOutUri); |     redrawPieChart('categories-out-pie-chart', catOutUri); | ||||||
|     redrawPieChart('accounts-in-pie-chart', accInUri); |     redrawPieChart('accounts-in-pie-chart', accInUri); | ||||||
|     redrawPieChart('accounts-out-pie-chart', accOutUri); |     redrawPieChart('accounts-out-pie-chart', accOutUri); | ||||||
|  |  | ||||||
|  |  | ||||||
| } | } | ||||||
|  |  | ||||||
| function redrawPieChart(container, uri) { | function redrawPieChart(container, uri) { | ||||||
|   | |||||||
| @@ -148,15 +148,21 @@ | |||||||
|  |  | ||||||
|     <div class="row"> |     <div class="row"> | ||||||
|         <div class="col-lg-12"> |         <div class="col-lg-12"> | ||||||
|  |                 <div class="box"> | ||||||
|              |                     <div class="box-header with-border"> | ||||||
|             big chart here |                         <h3 class="box-title">{{ 'income_and_expenses'|_ }}</h3> | ||||||
|  |                     </div> | ||||||
|             Show income / expenses per period. Differs per report: month = per day, year = per month, multi-year = per year. |                     <div class="box-body"> | ||||||
|             In a bar chart, possibly grouped by expense/revenue account. |                         <canvas id="in-out-chart" style="margin:0 auto;" height="300"></canvas> | ||||||
|  |                     </div> | ||||||
|  |                 </div> | ||||||
|         </div> |         </div> | ||||||
|     </div> |     </div> | ||||||
|  |     {# | ||||||
|  |             big chart here | ||||||
|  |             Show income / expenses per period. Differs per report: month = per day, year = per month, multi-year = per year. | ||||||
|  |             In a bar chart, possibly grouped by expense/revenue account. | ||||||
|  |             #} | ||||||
|  |  | ||||||
|     <div class="row"> |     <div class="row"> | ||||||
|         <div class="col-lg-3"> |         <div class="col-lg-3"> | ||||||
|   | |||||||
| @@ -227,12 +227,8 @@ Route::group( | |||||||
|         ['uses' => 'Chart\CategoryReportController@accountExpense'] |         ['uses' => 'Chart\CategoryReportController@accountExpense'] | ||||||
|     ); |     ); | ||||||
|     Route::get( |     Route::get( | ||||||
|         '/chart/category-report-income/{accountList}/{categoryList}/{start_date}/{end_date}', |         '/chart/category-report-in-out/{accountList}/{categoryList}/{start_date}/{end_date}', | ||||||
|         ['uses' => 'Chart\CategoryReportController@mainIncomeChart'] |         ['uses' => 'Chart\CategoryReportController@mainChart'] | ||||||
|     ); |  | ||||||
|     Route::get( |  | ||||||
|         '/chart/category-report-expenses/{accountList}/{categoryList}/{start_date}/{end_date}', |  | ||||||
|         ['uses' => 'Chart\CategoryReportController@mainExpenseChart'] |  | ||||||
|     ); |     ); | ||||||
|  |  | ||||||
|     // piggy banks: |     // piggy banks: | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user