-
Notifications
You must be signed in to change notification settings - Fork 0
/
transactions_chart.php
41 lines (34 loc) · 1.18 KB
/
transactions_chart.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<?php
$data = file_get_contents('transactions.json');
$transactions = json_decode($data, true);
$grouped_transactions = array_reduce($transactions, function ($result, $transaction) {
$category = $transaction['category'];
if (!isset($result[$category])) {
$result[$category] = ['label' => $category, 'data' => 0];
}
$result[$category]['data'] += $transaction['amount'];
return $result;
}, []);
$chart_data = [
'labels' => array_keys($grouped_transactions),
'datasets' => [
[
'label' => 'Montante financeiro',
'data' => array_map(function ($category) {
return $category['data'];
}, $grouped_transactions),
'backgroundColor' => [
'#FF6384',
'#36A2EB',
'#FFCE56',
'#8BFF82',
'#EE82EE'
]
]
]
];
echo '<canvas class="myChart" id="myChart"></canvas>';
echo '<script>';
echo 'var ctx = document.getElementById("myChart").getContext("2d");';
echo 'var myChart = new Chart(ctx, { type: "bar", data: ' . json_encode($chart_data) . ' });';
echo '</script>';