| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227 |
- import 'package:fl_chart/fl_chart.dart';
- import 'package:flutter/material.dart';
- import '../models/temperature_sample.dart';
- class TemperatureChart extends StatefulWidget {
- final List<double> history;
- final List<double> sensor2History;
- final double targetTemperature;
- final List<TemperatureSample> samples;
- const TemperatureChart({
- super.key,
- required this.history,
- required this.targetTemperature,
- this.sensor2History = const [],
- this.samples = const [],
- });
- @override
- State<TemperatureChart> createState() => _TemperatureChartState();
- }
- class _TemperatureChartState extends State<TemperatureChart> {
- int _windowMinutes = 5;
- DateTime? _windowEnd;
- double _minutesSince(DateTime timestamp, DateTime origin) =>
- timestamp.difference(origin).inMicroseconds /
- Duration.microsecondsPerMinute;
- @override
- Widget build(BuildContext context) {
- final samples = widget.samples;
- final timestamped = samples.isNotEmpty;
- if (!timestamped && widget.history.isEmpty) {
- return const Center(child: Text('No data yet'));
- }
- final origin = timestamped ? samples.first.timestamp : null;
- final latest = timestamped ? samples.last.timestamp : null;
- final duration = timestamped ? _minutesSince(latest!, origin!) : 0.0;
- final earliestEnd = duration < _windowMinutes
- ? duration
- : _windowMinutes.toDouble();
- final end = timestamped
- ? (_windowEnd == null ? duration : _minutesSince(_windowEnd!, origin!))
- .clamp(earliestEnd, duration)
- : 0.0;
- final minX = timestamped ? (end - _windowMinutes).clamp(0.0, end) : 0.0;
- final legacyLength = widget.history.length > widget.sensor2History.length
- ? widget.history.length
- : widget.sensor2History.length;
- // A single timestamp still needs a nonzero axis span.
- final maxX = timestamped
- ? (end > minX ? end : minX + 1 / 60)
- : (legacyLength > 1 ? legacyLength - 1.0 : 1.0);
- final visible = timestamped
- ? samples.where((sample) {
- final x = _minutesSince(sample.timestamp, origin!);
- return x >= minX && x <= maxX;
- }).toList()
- : <TemperatureSample>[];
- final boilerSpots = timestamped
- ? visible
- .map((s) => FlSpot(_minutesSince(s.timestamp, origin!), s.boiler))
- .toList()
- : widget.history
- .asMap()
- .entries
- .map((e) => FlSpot(e.key.toDouble(), e.value))
- .toList();
- final targetSpots = timestamped
- ? visible
- .map((s) => FlSpot(_minutesSince(s.timestamp, origin!), s.target))
- .toList()
- : [
- FlSpot(minX, widget.targetTemperature),
- FlSpot(maxX, widget.targetTemperature),
- ];
- final sensorSpots = timestamped
- ? visible
- .map(
- (s) => s.sensor2 == null
- ? FlSpot.nullSpot
- : FlSpot(_minutesSince(s.timestamp, origin!), s.sensor2!),
- )
- .toList()
- : widget.sensor2History
- .asMap()
- .entries
- .map((e) => FlSpot(e.key.toDouble(), e.value))
- .toList();
- final hasSensor2 = timestamped
- ? visible.any((s) => s.sensor2 != null)
- : widget.sensor2History.isNotEmpty;
- // Retained-history bounds keep the vertical scale steady while browsing.
- final values = timestamped
- ? samples.expand((s) => [s.boiler, s.target, ?s.sensor2])
- : [
- ...widget.history,
- ...widget.sensor2History,
- widget.targetTemperature,
- ];
- final dataMin = values.reduce((a, b) => a < b ? a : b);
- final dataMax = values.reduce((a, b) => a > b ? a : b);
- final minY = ((dataMin - 5) / 5).floorToDouble() * 5;
- final maxY = ((dataMax + 5) / 5).ceilToDouble() * 5;
- final chart = LineChart(
- LineChartData(
- minY: minY,
- maxY: maxY,
- minX: minX,
- maxX: maxX,
- clipData: const FlClipData.all(),
- lineBarsData: [
- _line(boilerSpots, Colors.redAccent, 3),
- LineChartBarData(
- spots: targetSpots,
- isCurved: false,
- color: Colors.blue.withValues(alpha: 0.5),
- barWidth: 1,
- dashArray: [5, 5],
- dotData: const FlDotData(show: false),
- ),
- if (hasSensor2) _line(sensorSpots, Colors.greenAccent, 2),
- ],
- titlesData: FlTitlesData(
- rightTitles: const AxisTitles(
- sideTitles: SideTitles(showTitles: false),
- ),
- topTitles: const AxisTitles(
- sideTitles: SideTitles(showTitles: false),
- ),
- bottomTitles: AxisTitles(
- sideTitles: SideTitles(
- showTitles: true,
- reservedSize: 28,
- getTitlesWidget: (value, meta) => Text(
- timestamped
- ? '${value.toStringAsFixed(1)}m'
- : value.toInt().toString(),
- style: const TextStyle(fontSize: 10),
- ),
- ),
- ),
- ),
- gridData: const FlGridData(show: true, drawVerticalLine: false),
- borderData: FlBorderData(show: true),
- ),
- duration: Duration.zero,
- );
- if (!timestamped) return chart;
- return Column(
- children: [
- Wrap(
- spacing: 4,
- children: [
- for (final minutes in [1, 5, 10, 20, 30])
- ChoiceChip(
- label: Text('$minutes min'),
- selected: _windowMinutes == minutes,
- onSelected: (_) => setState(() => _windowMinutes = minutes),
- visualDensity: VisualDensity.compact,
- ),
- ],
- ),
- Row(
- mainAxisAlignment: MainAxisAlignment.center,
- children: [
- TextButton(
- onPressed: end > earliestEnd
- ? () => setState(() {
- final older = (end - _windowMinutes).clamp(
- earliestEnd,
- duration,
- );
- _windowEnd = origin!.add(
- Duration(
- microseconds: (older * Duration.microsecondsPerMinute)
- .round(),
- ),
- );
- })
- : null,
- child: const Text('Older'),
- ),
- TextButton(
- onPressed: end < duration
- ? () => setState(() {
- final newer = (end + _windowMinutes).clamp(
- earliestEnd,
- duration,
- );
- _windowEnd = origin!.add(
- Duration(
- microseconds: (newer * Duration.microsecondsPerMinute)
- .round(),
- ),
- );
- })
- : null,
- child: const Text('Newer'),
- ),
- TextButton(
- onPressed: () => setState(() => _windowEnd = null),
- child: Text(_windowEnd == null ? 'Live' : 'Return to Live'),
- ),
- ],
- ),
- Expanded(child: chart),
- ],
- );
- }
- LineChartBarData _line(List<FlSpot> spots, Color color, double width) =>
- LineChartBarData(
- spots: spots,
- isCurved: false,
- color: color,
- barWidth: width,
- isStrokeCapRound: true,
- dotData: const FlDotData(show: false),
- );
- }
|