prasule/lib/util/graphs.dart

75 lines
2.3 KiB
Dart
Raw Normal View History

import 'package:fl_chart/fl_chart.dart';
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
/// Monthly/Yearly expenses [LineChart]
class ExpensesChart extends StatelessWidget {
const ExpensesChart(
{super.key,
required this.date,
required this.locale,
this.data = const [],
this.yearly = false});
final bool yearly;
final DateTime date;
final String locale;
final List<double> data;
List<double> get dataSorted {
var list = List<double>.from(data);
list.sort((a, b) => a.compareTo(b));
return list;
}
@override
Widget build(BuildContext context) {
return LineChart(
LineChartData(
maxX: (yearly) ? 12 : DateTime(date.year, date.month, 0).day.toDouble(),
maxY: dataSorted.last,
minX: 1,
minY: 0,
backgroundColor: Theme.of(context).colorScheme.background,
lineBarsData: [
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, data[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
),
);
}
}