import 'package:fl_chart/fl_chart.dart'; import 'package:flutter/material.dart'; import 'package:intl/intl.dart'; import 'package:prasule/main.dart'; /// Monthly/Yearly expense/income [LineChart] class ExpensesChart extends StatelessWidget { const ExpensesChart( {super.key, required this.date, required this.locale, this.expenseData = const [], this.incomeData = const [], this.yearly = false}); final bool yearly; final DateTime date; final String locale; final List expenseData; List get expenseDataSorted { var list = List.from(expenseData); list.sort((a, b) => a.compareTo(b)); return list; } final List incomeData; List get incomeDataSorted { var list = List.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) { return LineChart( LineChartData( maxX: (yearly) ? 12 : DateTime(date.year, date.month, 0).day.toDouble(), maxY: maxY, minX: 1, minY: 0, backgroundColor: Theme.of(context).colorScheme.background, lineBarsData: [ 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, (index) => FlSpot(index.toDouble() + 1, incomeData[index]), ), ), 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]), ), ), ], // actual data titlesData: FlTitlesData( rightTitles: const AxisTitles( sideTitles: SideTitles(showTitles: false), ), topTitles: const AxisTitles( sideTitles: SideTitles(showTitles: false), ), bottomTitles: AxisTitles( sideTitles: SideTitles( showTitles: true, getTitlesWidget: (value, meta) { String text; if (yearly) { text = DateFormat.MMM(locale).format( DateTime(date.year, value.toInt(), 1), ); } else { text = value.toInt().toString(); } return SideTitleWidget( axisSide: meta.axisSide, child: Text(text)); }, ), ), ), // axis descriptions ), ); } }