2023-12-25 19:03:52 +01:00
|
|
|
import 'package:currency_picker/currency_picker.dart';
|
2023-12-25 19:54:30 +01:00
|
|
|
import 'package:dynamic_color/dynamic_color.dart';
|
2023-11-21 20:23:14 +01:00
|
|
|
import 'package:fl_chart/fl_chart.dart';
|
|
|
|
import 'package:flutter/material.dart';
|
2023-12-25 19:03:52 +01:00
|
|
|
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
|
2023-12-29 21:39:54 +01:00
|
|
|
import 'package:intl/intl.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 {
|
2023-12-29 21:39:54 +01:00
|
|
|
/// Monthly/Yearly expense/income [LineChart]
|
|
|
|
const ExpensesLineChart({
|
|
|
|
required this.date,
|
|
|
|
required this.locale,
|
|
|
|
required this.expenseData,
|
|
|
|
required this.incomeData,
|
|
|
|
required this.currency,
|
|
|
|
super.key,
|
|
|
|
this.yearly = false,
|
|
|
|
});
|
|
|
|
|
|
|
|
/// If the graph will be shown yearly
|
2023-11-21 20:23:14 +01:00
|
|
|
final bool yearly;
|
2023-12-29 21:39:54 +01:00
|
|
|
|
|
|
|
/// Selected date
|
|
|
|
///
|
|
|
|
/// Used to get either month or year
|
2023-11-21 20:23:14 +01:00
|
|
|
final DateTime date;
|
2023-12-29 21:39:54 +01:00
|
|
|
|
|
|
|
/// Current locale
|
|
|
|
///
|
|
|
|
/// Used mainly for formatting
|
2023-11-21 20:23:14 +01:00
|
|
|
final String locale;
|
2023-12-29 21:39:54 +01:00
|
|
|
|
|
|
|
/// The expense data used for the graph
|
2023-11-22 15:09:05 +01:00
|
|
|
final List<double> expenseData;
|
2023-12-29 21:39:54 +01:00
|
|
|
|
|
|
|
/// Wallet currency
|
|
|
|
///
|
|
|
|
/// Used to show currency symbol
|
2023-12-25 19:03:52 +01:00
|
|
|
final Currency currency;
|
2023-11-21 20:23:14 +01:00
|
|
|
|
2023-12-29 21:39:54 +01:00
|
|
|
/// Expense data, but sorted
|
|
|
|
List<double> get expenseDataSorted =>
|
|
|
|
List<double>.from(expenseData)..sort((a, b) => a.compareTo(b));
|
|
|
|
|
|
|
|
/// Income data used for the graph
|
2023-11-22 15:09:05 +01:00
|
|
|
final List<double> incomeData;
|
|
|
|
|
2023-12-29 21:39:54 +01:00
|
|
|
/// Income data, but sorted
|
|
|
|
List<double> get incomeDataSorted =>
|
|
|
|
List<double>.from(incomeData)..sort((a, b) => a.compareTo(b));
|
|
|
|
|
|
|
|
/// Calculates maxY for the graph
|
2023-11-22 15:09:05 +01:00
|
|
|
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(
|
2023-12-25 19:26:46 +01:00
|
|
|
(spots[index].barIndex == 0)
|
|
|
|
? (yearly
|
|
|
|
? AppLocalizations.of(context).incomeForMonth(
|
2023-12-29 21:39:54 +01:00
|
|
|
DateFormat.MMMM(locale).format(
|
|
|
|
DateTime(
|
|
|
|
date.year,
|
|
|
|
spots[index].x.toInt() + 1,
|
|
|
|
),
|
|
|
|
),
|
2023-12-25 19:26:46 +01:00
|
|
|
NumberFormat.compactCurrency(
|
2023-12-29 21:39:54 +01:00
|
|
|
locale: locale,
|
|
|
|
symbol: currency.symbol,
|
|
|
|
name: currency.name,
|
|
|
|
).format(spots[index].y),
|
|
|
|
)
|
2023-12-25 19:26:46 +01:00
|
|
|
: AppLocalizations.of(context).incomeForDay(
|
|
|
|
NumberFormat.compactCurrency(
|
2023-12-29 21:39:54 +01:00
|
|
|
locale: locale,
|
|
|
|
symbol: currency.symbol,
|
|
|
|
name: currency.name,
|
|
|
|
).format(spots[index].y),
|
2023-12-25 19:26:46 +01:00
|
|
|
))
|
|
|
|
: (yearly
|
|
|
|
? AppLocalizations.of(context).expensesForMonth(
|
2023-12-29 21:39:54 +01:00
|
|
|
DateFormat.MMMM(locale).format(
|
|
|
|
DateTime(
|
|
|
|
date.year,
|
|
|
|
spots[index].x.toInt() + 1,
|
|
|
|
),
|
|
|
|
),
|
2023-12-25 19:26:46 +01:00
|
|
|
NumberFormat.compactCurrency(
|
2023-12-29 21:39:54 +01:00
|
|
|
locale: locale,
|
|
|
|
symbol: currency.symbol,
|
|
|
|
name: currency.name,
|
|
|
|
).format(spots[index].y),
|
|
|
|
)
|
2023-12-25 19:26:46 +01:00
|
|
|
: AppLocalizations.of(context).expensesForDay(
|
|
|
|
NumberFormat.compactCurrency(
|
2023-12-29 21:39:54 +01:00
|
|
|
locale: locale,
|
|
|
|
symbol: currency.symbol,
|
|
|
|
name: currency.name,
|
|
|
|
).format(spots[index].y),
|
2023-12-25 19:26:46 +01:00
|
|
|
)),
|
2023-12-25 19:03:52 +01:00
|
|
|
TextStyle(color: spots[index].bar.color),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
2023-11-22 15:09:05 +01:00
|
|
|
maxY: maxY,
|
2023-12-29 21:39:54 +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),
|
2023-12-29 21:39:54 +01:00
|
|
|
belowBarData: BarAreaData(),
|
2023-12-25 19:54:30 +01:00
|
|
|
color: Colors.green
|
|
|
|
.harmonizeWith(Theme.of(context).colorScheme.secondary),
|
2023-11-22 15:09:05 +01:00
|
|
|
spots: List.generate(
|
2023-12-29 21:39:54 +01:00
|
|
|
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),
|
2023-12-29 21:39:54 +01:00
|
|
|
belowBarData: BarAreaData(),
|
2023-12-25 19:54:30 +01:00
|
|
|
color: Colors.red
|
|
|
|
.harmonizeWith(Theme.of(context).colorScheme.secondary),
|
2023-11-22 15:09:05 +01:00
|
|
|
spots: List.generate(
|
2023-12-29 21:39:54 +01:00
|
|
|
yearly ? 12 : DateTime(date.year, date.month, 0).day,
|
2023-11-22 15:09:05 +01:00
|
|
|
(index) => FlSpot(index.toDouble() + 1, expenseData[index]),
|
|
|
|
),
|
2023-11-21 20:23:14 +01:00
|
|
|
),
|
|
|
|
], // actual data
|
|
|
|
titlesData: FlTitlesData(
|
2023-12-29 21:39:54 +01:00
|
|
|
rightTitles: const AxisTitles(),
|
|
|
|
topTitles: const AxisTitles(),
|
2023-11-21 20:23:14 +01:00
|
|
|
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-29 21:39:54 +01:00
|
|
|
DateTime(date.year, value.toInt() + 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(
|
2023-12-29 21:39:54 +01:00
|
|
|
axisSide: meta.axisSide,
|
|
|
|
child: Text(text),
|
|
|
|
);
|
2023-11-21 20:23:14 +01:00
|
|
|
},
|
|
|
|
),
|
|
|
|
),
|
|
|
|
), // axis descriptions
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2023-12-25 19:03:52 +01:00
|
|
|
|
2023-12-29 21:39:54 +01:00
|
|
|
/// Renders expenses/income as a [BarChart]
|
2023-12-25 19:03:52 +01:00
|
|
|
class ExpensesBarChart extends StatelessWidget {
|
2023-12-29 21:39:54 +01:00
|
|
|
/// Renders expenses/income as a [BarChart]
|
|
|
|
const ExpensesBarChart({
|
|
|
|
required this.yearly,
|
|
|
|
required this.date,
|
|
|
|
required this.locale,
|
|
|
|
required this.expenseData,
|
|
|
|
required this.incomeData,
|
|
|
|
required this.currency,
|
|
|
|
super.key,
|
|
|
|
});
|
|
|
|
|
|
|
|
/// If the graph will be shown yearly
|
2023-12-25 19:03:52 +01:00
|
|
|
final bool yearly;
|
2023-12-29 21:39:54 +01:00
|
|
|
|
|
|
|
/// Selected date
|
|
|
|
///
|
|
|
|
/// Used to get either month or year
|
2023-12-25 19:03:52 +01:00
|
|
|
final DateTime date;
|
2023-12-29 21:39:54 +01:00
|
|
|
|
|
|
|
/// Current locale
|
|
|
|
///
|
|
|
|
/// Used mainly for formatting
|
2023-12-25 19:03:52 +01:00
|
|
|
final String locale;
|
2023-12-29 21:39:54 +01:00
|
|
|
|
|
|
|
/// The expense data used for the graph
|
2023-12-25 19:03:52 +01:00
|
|
|
final List<double> expenseData;
|
|
|
|
|
2023-12-29 21:39:54 +01:00
|
|
|
/// Wallet currency
|
|
|
|
///
|
|
|
|
/// Used to show currency symbol
|
2023-12-25 19:03:52 +01:00
|
|
|
final Currency currency;
|
|
|
|
|
2023-12-29 21:39:54 +01:00
|
|
|
/// Expense data, but sorted
|
|
|
|
List<double> get expenseDataSorted =>
|
|
|
|
List<double>.from(expenseData)..sort((a, b) => a.compareTo(b));
|
|
|
|
|
|
|
|
/// Income data used for the graph
|
2023-12-25 19:03:52 +01:00
|
|
|
final List<double> incomeData;
|
|
|
|
|
2023-12-29 21:39:54 +01:00
|
|
|
/// Income data, but sorted
|
|
|
|
List<double> get incomeDataSorted =>
|
|
|
|
List<double>.from(incomeData)..sort((a, b) => a.compareTo(b));
|
|
|
|
|
|
|
|
/// Calculates maxY for the graph
|
2023-12-25 19:03:52 +01:00
|
|
|
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) =>
|
2023-12-29 21:39:54 +01:00
|
|
|
yearly // create custom tooltips for graph bars
|
2023-12-25 19:03:52 +01:00
|
|
|
? BarTooltipItem(
|
|
|
|
(rodIndex == 1)
|
|
|
|
? AppLocalizations.of(context).expensesForMonth(
|
|
|
|
DateFormat.MMMM(locale).format(
|
2023-12-29 21:39:54 +01:00
|
|
|
DateTime(date.year, groupIndex + 1),
|
|
|
|
),
|
2023-12-25 19:03:52 +01:00
|
|
|
NumberFormat.compactCurrency(
|
2023-12-29 21:39:54 +01:00
|
|
|
locale: locale,
|
|
|
|
symbol: currency.symbol,
|
|
|
|
name: currency.name,
|
|
|
|
).format(rod.toY),
|
2023-12-25 19:03:52 +01:00
|
|
|
)
|
|
|
|
: AppLocalizations.of(context).incomeForMonth(
|
|
|
|
DateFormat.MMMM(locale).format(
|
2023-12-29 21:39:54 +01:00
|
|
|
DateTime(date.year, groupIndex + 1),
|
|
|
|
),
|
2023-12-25 19:03:52 +01:00
|
|
|
NumberFormat.compactCurrency(
|
2023-12-29 21:39:54 +01:00
|
|
|
locale: locale,
|
|
|
|
symbol: currency.symbol,
|
|
|
|
name: currency.name,
|
|
|
|
).format(rod.toY),
|
2023-12-25 19:03:52 +01:00
|
|
|
),
|
|
|
|
TextStyle(color: rod.color),
|
|
|
|
)
|
|
|
|
: BarTooltipItem(
|
|
|
|
(rodIndex == 1)
|
|
|
|
? AppLocalizations.of(context).expensesForDay(
|
|
|
|
NumberFormat.compactCurrency(
|
2023-12-29 21:39:54 +01:00
|
|
|
locale: locale,
|
|
|
|
symbol: currency.symbol,
|
|
|
|
name: currency.name,
|
|
|
|
).format(rod.toY),
|
2023-12-25 19:03:52 +01:00
|
|
|
)
|
|
|
|
: AppLocalizations.of(context).incomeForDay(
|
|
|
|
NumberFormat.compactCurrency(
|
2023-12-29 21:39:54 +01:00
|
|
|
locale: locale,
|
|
|
|
symbol: currency.symbol,
|
|
|
|
name: currency.name,
|
|
|
|
).format(rod.toY),
|
2023-12-25 19:03:52 +01:00
|
|
|
),
|
|
|
|
TextStyle(color: rod.color),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
titlesData: FlTitlesData(
|
2023-12-29 21:39:54 +01:00
|
|
|
rightTitles: const AxisTitles(),
|
|
|
|
topTitles: const AxisTitles(),
|
2023-12-25 19:03:52 +01:00
|
|
|
bottomTitles: AxisTitles(
|
|
|
|
sideTitles: SideTitles(
|
|
|
|
showTitles: true,
|
|
|
|
reservedSize: 30,
|
|
|
|
getTitlesWidget: (value, meta) {
|
|
|
|
String text;
|
|
|
|
if (yearly) {
|
|
|
|
text = DateFormat.MMM(locale).format(
|
2023-12-29 21:39:54 +01:00
|
|
|
DateTime(date.year, value.toInt() + 1),
|
2023-12-25 19:03:52 +01:00
|
|
|
);
|
|
|
|
} else {
|
|
|
|
text = (value.toInt() + 1).toString();
|
|
|
|
}
|
|
|
|
return SideTitleWidget(
|
2023-12-29 21:39:54 +01:00
|
|
|
axisSide: meta.axisSide,
|
|
|
|
child: Text(text),
|
|
|
|
);
|
2023-12-25 19:03:52 +01:00
|
|
|
},
|
|
|
|
),
|
|
|
|
),
|
|
|
|
), // axis descriptions,
|
|
|
|
minY: 0,
|
|
|
|
maxY: maxY,
|
|
|
|
barGroups: List<BarChartGroupData>.generate(
|
2023-12-29 21:39:54 +01:00
|
|
|
yearly ? 12 : DateTime(date.year, date.month, 0).day,
|
2023-12-25 19:03:52 +01:00
|
|
|
(index) => BarChartGroupData(
|
|
|
|
x: index,
|
|
|
|
barRods: [
|
|
|
|
if (incomeData.isNotEmpty)
|
|
|
|
BarChartRodData(
|
|
|
|
toY: incomeData[index],
|
2023-12-25 19:54:30 +01:00
|
|
|
color: Colors.green
|
|
|
|
.harmonizeWith(Theme.of(context).colorScheme.secondary),
|
2023-12-25 19:03:52 +01:00
|
|
|
),
|
|
|
|
if (expenseData.isNotEmpty)
|
|
|
|
BarChartRodData(
|
|
|
|
toY: expenseData[index],
|
2023-12-25 19:54:30 +01:00
|
|
|
color: Colors.red
|
|
|
|
.harmonizeWith(Theme.of(context).colorScheme.secondary),
|
2023-12-25 19:03:52 +01:00
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|