Added Station Filter without function and refactored
This commit is contained in:
115
lib/widgets/reading_graph.dart
Normal file
115
lib/widgets/reading_graph.dart
Normal file
@@ -0,0 +1,115 @@
|
||||
import 'package:fl_chart/fl_chart.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:intl/intl.dart' as intl;
|
||||
import 'dart:math';
|
||||
|
||||
import '../model/reading.dart';
|
||||
|
||||
class ReadingGraph extends StatelessWidget {
|
||||
const ReadingGraph({super.key, required List<Reading> readings})
|
||||
: _readings = readings;
|
||||
final List<Reading> _readings;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final spots = _readings
|
||||
.map<FlSpot>(
|
||||
(e) => FlSpot(
|
||||
e.dateTime.millisecondsSinceEpoch.toDouble() / 1000 / 60,
|
||||
e.value),
|
||||
)
|
||||
.toList();
|
||||
|
||||
return LineChart(
|
||||
LineChartData(
|
||||
maxY: _readings.map<double>((e) => e.value).reduce(max) + 0.05,
|
||||
minY: _readings.map<double>((e) => e.value).reduce(min) - 0.05,
|
||||
lineBarsData: [
|
||||
LineChartBarData(
|
||||
isCurved: true,
|
||||
color: Colors.cyan,
|
||||
barWidth: 2,
|
||||
isStrokeCapRound: true,
|
||||
dotData: const FlDotData(show: false),
|
||||
spots: _readings
|
||||
.map<FlSpot>(
|
||||
(e) => FlSpot(
|
||||
e.dateTime.millisecondsSinceEpoch.toDouble() / 1000 / 60,
|
||||
e.value),
|
||||
)
|
||||
.toList(),
|
||||
)
|
||||
],
|
||||
titlesData: FlTitlesData(
|
||||
bottomTitles: getBottomTitles(spots),
|
||||
leftTitles: getLeftTitles(spots),
|
||||
topTitles: const AxisTitles(sideTitles: SideTitles()),
|
||||
rightTitles: const AxisTitles(sideTitles: SideTitles()),
|
||||
),
|
||||
lineTouchData: LineTouchData(
|
||||
touchTooltipData: LineTouchTooltipData(
|
||||
getTooltipItems: (touchedSpots) {
|
||||
return touchedSpots.map((touchedSpot) {
|
||||
return LineTooltipItem(
|
||||
'${touchedSpot.y.toString()}\n${getLongDate(touchedSpot.x)}',
|
||||
TextStyle(
|
||||
color: Theme.of(context).colorScheme.onPrimary,
|
||||
fontWeight: FontWeight.bold));
|
||||
}).toList();
|
||||
},
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
AxisTitles getBottomTitles(List<FlSpot> spots) {
|
||||
return AxisTitles(
|
||||
sideTitles: SideTitles(
|
||||
interval: 90,
|
||||
reservedSize: 40,
|
||||
showTitles: true,
|
||||
maxIncluded: false,
|
||||
getTitlesWidget: (value, meta) => SideTitleWidget(
|
||||
meta: meta,
|
||||
child: Text(getDate(value)),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
AxisTitles getLeftTitles(List<FlSpot> spots) {
|
||||
return AxisTitles(
|
||||
sideTitles: SideTitles(
|
||||
reservedSize: 50,
|
||||
showTitles: true,
|
||||
maxIncluded: false,
|
||||
minIncluded: false,
|
||||
getTitlesWidget: (value, meta) => SideTitleWidget(
|
||||
meta: meta,
|
||||
child: Text(value.toStringAsFixed(2)),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
String getDate(double value) {
|
||||
intl.DateFormat hmFormat = intl.DateFormat('Hm');
|
||||
return hmFormat.format(
|
||||
DateTime.fromMillisecondsSinceEpoch((value * 1000 * 60).toInt()));
|
||||
}
|
||||
|
||||
String getLongDate(double value) {
|
||||
DateTime date =
|
||||
DateTime.fromMillisecondsSinceEpoch((value * 1000 * 60).toInt());
|
||||
int daysDifference = (DateTime.now().weekday - date.weekday + 7) % 7;
|
||||
|
||||
if (daysDifference == 0) {
|
||||
return 'Today ${intl.DateFormat('Hm').format(date)}';
|
||||
} else if (daysDifference == 1) {
|
||||
return 'Yesterday ${intl.DateFormat('Hm').format(date)}';
|
||||
}
|
||||
|
||||
return intl.DateFormat('yyyy-MM-dd H:m').format(date);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user