2023-12-25 19:03:52 +01:00
|
|
|
import 'package:currency_picker/currency_picker.dart';
|
2023-11-21 20:23:14 +01:00
|
|
|
import 'package:fl_chart/fl_chart.dart';
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:intl/intl.dart';
|
2023-12-25 19:03:52 +01:00
|
|
|
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
|
2023-11-21 20:23:14 +01:00
|
|
|
|
2023-11-22 15:09:05 +01:00
|
|
|
/// Monthly/Yearly expense/income [LineChart]
|
2023-12-25 19:03:52 +01:00
|
|
|
class ExpensesLineChart extends StatelessWidget {
|
|
|
|
const ExpensesLineChart(
|
2023-11-21 20:23:14 +01:00
|
|
|
{super.key,
|
|
|
|
required this.date,
|
|
|
|
required this.locale,
|
2023-12-25 19:03:52 +01:00
|
|
|
required this.expenseData,
|
|
|
|
required this.incomeData,
|
|
|
|
required this.currency,
|
2023-11-21 20:23:14 +01:00
|
|
|
this.yearly = false});
|
|
|
|
final bool yearly;
|
|
|
|
final DateTime date;
|
|
|
|
final String locale;
|
2023-11-22 15:09:05 +01:00
|
|
|
final List<double> expenseData;
|
2023-12-25 19:03:52 +01:00
|
|
|
final Currency currency;
|
2023-11-22 15:09:05 +01:00
|
|
|
List<double> get expenseDataSorted {
|
|
|
|
var list = List<double>.from(expenseData);
|
2023-11-21 20:23:14 +01:00
|
|
|
list.sort((a, b) => a.compareTo(b));
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
2023-11-22 15:09:05 +01:00
|
|
|
final List<double> incomeData;
|
|
|
|
List<double> get incomeDataSorted {
|
|
|
|
var list = List<double>.from(incomeData);
|
|
|
|
list.sort((a, b) => a.compareTo(b));
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
|
|
|
double get maxY {
|
|
|
|
if (incomeData.isEmpty) return expenseDataSorted.last;
|
|
|
|
if (expenseData.isEmpty) return incomeDataSorted.last;
|
|
|
|
if (expenseDataSorted.last > incomeDataSorted.last) {
|
|
|
|
return expenseDataSorted.last;
|
|
|
|
} else {
|
|
|
|
return incomeDataSorted.last;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-21 20:23:14 +01:00
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return LineChart(
|
|
|
|
LineChartData(
|
2023-12-25 19:03:52 +01:00
|
|
|
lineTouchData: LineTouchData(
|
|
|
|
touchTooltipData: LineTouchTooltipData(
|
|
|
|
getTooltipItems: (spots) => List<LineTooltipItem>.generate(
|
|
|
|
spots.length,
|
|
|
|
(index) => LineTooltipItem(
|
|
|
|
(yearly)
|
|
|
|
? AppLocalizations.of(context).expensesForMonth(
|
|
|
|
DateFormat.MMMM(locale).format(
|
|
|
|
DateTime(date.year, spots[index].x.toInt() + 1, 1)),
|
|
|
|
NumberFormat.compactCurrency(
|
|
|
|
locale: locale,
|
|
|
|
symbol: currency.symbol,
|
|
|
|
name: currency.name)
|
|
|
|
.format(spots[index].y))
|
|
|
|
: AppLocalizations.of(context).expensesForDay(
|
|
|
|
NumberFormat.compactCurrency(
|
|
|
|
locale: locale,
|
|
|
|
symbol: currency.symbol,
|
|
|
|
name: currency.name)
|
|
|
|
.format(spots[index].y),
|
|
|
|
),
|
|
|
|
TextStyle(color: spots[index].bar.color),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
2023-11-22 15:09:05 +01:00
|
|
|
maxY: maxY,
|
2023-12-25 19:03:52 +01:00
|
|
|
maxX: (yearly) ? 12 : DateTime(date.year, date.month, 0).day.toDouble(),
|
2023-11-21 20:23:14 +01:00
|
|
|
minY: 0,
|
2023-12-25 19:03:52 +01:00
|
|
|
minX: 0,
|
2023-11-21 20:23:14 +01:00
|
|
|
backgroundColor: Theme.of(context).colorScheme.background,
|
|
|
|
lineBarsData: [
|
2023-11-22 15:09:05 +01:00
|
|
|
if (incomeData.isNotEmpty)
|
|
|
|
LineChartBarData(
|
|
|
|
isCurved: true,
|
|
|
|
barWidth: 8,
|
|
|
|
isStrokeCapRound: true,
|
|
|
|
dotData: const FlDotData(show: false),
|
|
|
|
belowBarData: BarAreaData(show: false),
|
|
|
|
color: Theme.of(context).colorScheme.primary,
|
|
|
|
spots: List.generate(
|
|
|
|
(yearly) ? 12 : DateTime(date.year, date.month, 0).day,
|
2023-12-25 19:03:52 +01:00
|
|
|
(index) => FlSpot(index.toDouble(), incomeData[index]),
|
2023-11-22 15:09:05 +01:00
|
|
|
),
|
|
|
|
),
|
|
|
|
if (expenseData.isNotEmpty)
|
|
|
|
LineChartBarData(
|
|
|
|
isCurved: true,
|
|
|
|
barWidth: 8,
|
|
|
|
isStrokeCapRound: true,
|
|
|
|
dotData: const FlDotData(show: false),
|
|
|
|
belowBarData: BarAreaData(show: false),
|
|
|
|
color: Theme.of(context).colorScheme.error,
|
|
|
|
spots: List.generate(
|
|
|
|
(yearly) ? 12 : DateTime(date.year, date.month, 0).day,
|
|
|
|
(index) => FlSpot(index.toDouble() + 1, expenseData[index]),
|
|
|
|
),
|
2023-11-21 20:23:14 +01:00
|
|
|
),
|
|
|
|
], // actual data
|
|
|
|
titlesData: FlTitlesData(
|
|
|
|
rightTitles: const AxisTitles(
|
|
|
|
sideTitles: SideTitles(showTitles: false),
|
|
|
|
),
|
|
|
|
topTitles: const AxisTitles(
|
|
|
|
sideTitles: SideTitles(showTitles: false),
|
|
|
|
),
|
|
|
|
bottomTitles: AxisTitles(
|
|
|
|
sideTitles: SideTitles(
|
2023-12-25 19:03:52 +01:00
|
|
|
reservedSize: 30,
|
2023-11-21 20:23:14 +01:00
|
|
|
showTitles: true,
|
|
|
|
getTitlesWidget: (value, meta) {
|
|
|
|
String text;
|
|
|
|
if (yearly) {
|
|
|
|
text = DateFormat.MMM(locale).format(
|
2023-12-25 19:03:52 +01:00
|
|
|
DateTime(date.year, value.toInt() + 1, 1),
|
2023-11-21 20:23:14 +01:00
|
|
|
);
|
|
|
|
} else {
|
2023-12-25 19:03:52 +01:00
|
|
|
text = (value.toInt() + 1).toString();
|
2023-11-21 20:23:14 +01:00
|
|
|
}
|
|
|
|
return SideTitleWidget(
|
|
|
|
axisSide: meta.axisSide, child: Text(text));
|
|
|
|
},
|
|
|
|
),
|
|
|
|
),
|
|
|
|
), // axis descriptions
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2023-12-25 19:03:52 +01:00
|
|
|
|
|
|
|
class ExpensesBarChart extends StatelessWidget {
|
|
|
|
const ExpensesBarChart(
|
|
|
|
{super.key,
|
|
|
|
required this.yearly,
|
|
|
|
required this.date,
|
|
|
|
required this.locale,
|
|
|
|
required this.expenseData,
|
|
|
|
required this.incomeData,
|
|
|
|
required this.currency});
|
|
|
|
final bool yearly;
|
|
|
|
final DateTime date;
|
|
|
|
final String locale;
|
|
|
|
final List<double> expenseData;
|
|
|
|
List<double> get expenseDataSorted {
|
|
|
|
var list = List<double>.from(expenseData);
|
|
|
|
list.sort((a, b) => a.compareTo(b));
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
|
|
|
final Currency currency;
|
|
|
|
|
|
|
|
final List<double> incomeData;
|
|
|
|
List<double> get incomeDataSorted {
|
|
|
|
var list = List<double>.from(incomeData);
|
|
|
|
list.sort((a, b) => a.compareTo(b));
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
|
|
|
double get maxY {
|
|
|
|
if (incomeData.isEmpty) return expenseDataSorted.last;
|
|
|
|
if (expenseData.isEmpty) return incomeDataSorted.last;
|
|
|
|
if (expenseDataSorted.last > incomeDataSorted.last) {
|
|
|
|
return expenseDataSorted.last;
|
|
|
|
} else {
|
|
|
|
return incomeDataSorted.last;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) => BarChart(
|
|
|
|
BarChartData(
|
|
|
|
barTouchData: BarTouchData(
|
|
|
|
enabled: true,
|
|
|
|
touchTooltipData: BarTouchTooltipData(
|
|
|
|
getTooltipItem: (group, groupIndex, rod, rodIndex) =>
|
|
|
|
(yearly) // create custom tooltips for graph bars
|
|
|
|
? BarTooltipItem(
|
|
|
|
(rodIndex == 1)
|
|
|
|
? AppLocalizations.of(context).expensesForMonth(
|
|
|
|
DateFormat.MMMM(locale).format(
|
|
|
|
DateTime(date.year, groupIndex + 1, 1)),
|
|
|
|
NumberFormat.compactCurrency(
|
|
|
|
locale: locale,
|
|
|
|
symbol: currency.symbol,
|
|
|
|
name: currency.name)
|
|
|
|
.format(rod.toY),
|
|
|
|
)
|
|
|
|
: AppLocalizations.of(context).incomeForMonth(
|
|
|
|
DateFormat.MMMM(locale).format(
|
|
|
|
DateTime(date.year, groupIndex + 1, 1)),
|
|
|
|
NumberFormat.compactCurrency(
|
|
|
|
locale: locale,
|
|
|
|
symbol: currency.symbol,
|
|
|
|
name: currency.name)
|
|
|
|
.format(rod.toY),
|
|
|
|
),
|
|
|
|
TextStyle(color: rod.color),
|
|
|
|
)
|
|
|
|
: BarTooltipItem(
|
|
|
|
(rodIndex == 1)
|
|
|
|
? AppLocalizations.of(context).expensesForDay(
|
|
|
|
NumberFormat.compactCurrency(
|
|
|
|
locale: locale,
|
|
|
|
symbol: currency.symbol,
|
|
|
|
name: currency.name)
|
|
|
|
.format(rod.toY),
|
|
|
|
)
|
|
|
|
: AppLocalizations.of(context).incomeForDay(
|
|
|
|
NumberFormat.compactCurrency(
|
|
|
|
locale: locale,
|
|
|
|
symbol: currency.symbol,
|
|
|
|
name: currency.name)
|
|
|
|
.format(rod.toY),
|
|
|
|
),
|
|
|
|
TextStyle(color: rod.color),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
titlesData: FlTitlesData(
|
|
|
|
rightTitles: const AxisTitles(
|
|
|
|
sideTitles: SideTitles(showTitles: false),
|
|
|
|
),
|
|
|
|
topTitles: const AxisTitles(
|
|
|
|
sideTitles: SideTitles(showTitles: false),
|
|
|
|
),
|
|
|
|
bottomTitles: AxisTitles(
|
|
|
|
sideTitles: SideTitles(
|
|
|
|
showTitles: true,
|
|
|
|
reservedSize: 30,
|
|
|
|
getTitlesWidget: (value, meta) {
|
|
|
|
String text;
|
|
|
|
if (yearly) {
|
|
|
|
text = DateFormat.MMM(locale).format(
|
|
|
|
DateTime(date.year, value.toInt() + 1, 1),
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
text = (value.toInt() + 1).toString();
|
|
|
|
}
|
|
|
|
return SideTitleWidget(
|
|
|
|
axisSide: meta.axisSide, child: Text(text));
|
|
|
|
},
|
|
|
|
),
|
|
|
|
),
|
|
|
|
), // axis descriptions,
|
|
|
|
minY: 0,
|
|
|
|
maxY: maxY,
|
|
|
|
barGroups: List<BarChartGroupData>.generate(
|
|
|
|
(yearly) ? 12 : DateTime(date.year, date.month, 0).day,
|
|
|
|
(index) => BarChartGroupData(
|
|
|
|
x: index,
|
|
|
|
barRods: [
|
|
|
|
if (incomeData.isNotEmpty)
|
|
|
|
BarChartRodData(
|
|
|
|
toY: incomeData[index],
|
|
|
|
color: Theme.of(context).colorScheme.primary,
|
|
|
|
),
|
|
|
|
if (expenseData.isNotEmpty)
|
|
|
|
BarChartRodData(
|
|
|
|
toY: expenseData[index],
|
|
|
|
color: Theme.of(context).colorScheme.error,
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|