temperature_chart.dart 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. import 'package:fl_chart/fl_chart.dart';
  2. import 'package:flutter/material.dart';
  3. import '../models/temperature_sample.dart';
  4. class TemperatureChart extends StatefulWidget {
  5. final List<double> history;
  6. final List<double> sensor2History;
  7. final double targetTemperature;
  8. final List<TemperatureSample> samples;
  9. const TemperatureChart({
  10. super.key,
  11. required this.history,
  12. required this.targetTemperature,
  13. this.sensor2History = const [],
  14. this.samples = const [],
  15. });
  16. @override
  17. State<TemperatureChart> createState() => _TemperatureChartState();
  18. }
  19. class _TemperatureChartState extends State<TemperatureChart> {
  20. int _windowMinutes = 5;
  21. DateTime? _windowEnd;
  22. double _minutesSince(DateTime timestamp, DateTime origin) =>
  23. timestamp.difference(origin).inMicroseconds /
  24. Duration.microsecondsPerMinute;
  25. @override
  26. Widget build(BuildContext context) {
  27. final samples = widget.samples;
  28. final timestamped = samples.isNotEmpty;
  29. if (!timestamped && widget.history.isEmpty) {
  30. return const Center(child: Text('No data yet'));
  31. }
  32. final origin = timestamped ? samples.first.timestamp : null;
  33. final latest = timestamped ? samples.last.timestamp : null;
  34. final duration = timestamped ? _minutesSince(latest!, origin!) : 0.0;
  35. final earliestEnd = duration < _windowMinutes
  36. ? duration
  37. : _windowMinutes.toDouble();
  38. final end = timestamped
  39. ? (_windowEnd == null ? duration : _minutesSince(_windowEnd!, origin!))
  40. .clamp(earliestEnd, duration)
  41. : 0.0;
  42. final minX = timestamped ? (end - _windowMinutes).clamp(0.0, end) : 0.0;
  43. final legacyLength = widget.history.length > widget.sensor2History.length
  44. ? widget.history.length
  45. : widget.sensor2History.length;
  46. // A single timestamp still needs a nonzero axis span.
  47. final maxX = timestamped
  48. ? (end > minX ? end : minX + 1 / 60)
  49. : (legacyLength > 1 ? legacyLength - 1.0 : 1.0);
  50. final visible = timestamped
  51. ? samples.where((sample) {
  52. final x = _minutesSince(sample.timestamp, origin!);
  53. return x >= minX && x <= maxX;
  54. }).toList()
  55. : <TemperatureSample>[];
  56. final boilerSpots = timestamped
  57. ? visible
  58. .map((s) => FlSpot(_minutesSince(s.timestamp, origin!), s.boiler))
  59. .toList()
  60. : widget.history
  61. .asMap()
  62. .entries
  63. .map((e) => FlSpot(e.key.toDouble(), e.value))
  64. .toList();
  65. final targetSpots = timestamped
  66. ? visible
  67. .map((s) => FlSpot(_minutesSince(s.timestamp, origin!), s.target))
  68. .toList()
  69. : [
  70. FlSpot(minX, widget.targetTemperature),
  71. FlSpot(maxX, widget.targetTemperature),
  72. ];
  73. final sensorSpots = timestamped
  74. ? visible
  75. .map(
  76. (s) => s.sensor2 == null
  77. ? FlSpot.nullSpot
  78. : FlSpot(_minutesSince(s.timestamp, origin!), s.sensor2!),
  79. )
  80. .toList()
  81. : widget.sensor2History
  82. .asMap()
  83. .entries
  84. .map((e) => FlSpot(e.key.toDouble(), e.value))
  85. .toList();
  86. final hasSensor2 = timestamped
  87. ? visible.any((s) => s.sensor2 != null)
  88. : widget.sensor2History.isNotEmpty;
  89. // Retained-history bounds keep the vertical scale steady while browsing.
  90. final values = timestamped
  91. ? samples.expand((s) => [s.boiler, s.target, ?s.sensor2])
  92. : [
  93. ...widget.history,
  94. ...widget.sensor2History,
  95. widget.targetTemperature,
  96. ];
  97. final dataMin = values.reduce((a, b) => a < b ? a : b);
  98. final dataMax = values.reduce((a, b) => a > b ? a : b);
  99. final minY = ((dataMin - 5) / 5).floorToDouble() * 5;
  100. final maxY = ((dataMax + 5) / 5).ceilToDouble() * 5;
  101. final chart = LineChart(
  102. LineChartData(
  103. minY: minY,
  104. maxY: maxY,
  105. minX: minX,
  106. maxX: maxX,
  107. clipData: const FlClipData.all(),
  108. lineBarsData: [
  109. _line(boilerSpots, Colors.redAccent, 3),
  110. LineChartBarData(
  111. spots: targetSpots,
  112. isCurved: false,
  113. color: Colors.blue.withValues(alpha: 0.5),
  114. barWidth: 1,
  115. dashArray: [5, 5],
  116. dotData: const FlDotData(show: false),
  117. ),
  118. if (hasSensor2) _line(sensorSpots, Colors.greenAccent, 2),
  119. ],
  120. titlesData: FlTitlesData(
  121. rightTitles: const AxisTitles(
  122. sideTitles: SideTitles(showTitles: false),
  123. ),
  124. topTitles: const AxisTitles(
  125. sideTitles: SideTitles(showTitles: false),
  126. ),
  127. bottomTitles: AxisTitles(
  128. sideTitles: SideTitles(
  129. showTitles: true,
  130. reservedSize: 28,
  131. getTitlesWidget: (value, meta) => Text(
  132. timestamped
  133. ? '${value.toStringAsFixed(1)}m'
  134. : value.toInt().toString(),
  135. style: const TextStyle(fontSize: 10),
  136. ),
  137. ),
  138. ),
  139. ),
  140. gridData: const FlGridData(show: true, drawVerticalLine: false),
  141. borderData: FlBorderData(show: true),
  142. ),
  143. duration: Duration.zero,
  144. );
  145. if (!timestamped) return chart;
  146. return Column(
  147. children: [
  148. Wrap(
  149. spacing: 4,
  150. children: [
  151. for (final minutes in [1, 5, 10, 20, 30])
  152. ChoiceChip(
  153. label: Text('$minutes min'),
  154. selected: _windowMinutes == minutes,
  155. onSelected: (_) => setState(() => _windowMinutes = minutes),
  156. visualDensity: VisualDensity.compact,
  157. ),
  158. ],
  159. ),
  160. Row(
  161. mainAxisAlignment: MainAxisAlignment.center,
  162. children: [
  163. TextButton(
  164. onPressed: end > earliestEnd
  165. ? () => setState(() {
  166. final older = (end - _windowMinutes).clamp(
  167. earliestEnd,
  168. duration,
  169. );
  170. _windowEnd = origin!.add(
  171. Duration(
  172. microseconds: (older * Duration.microsecondsPerMinute)
  173. .round(),
  174. ),
  175. );
  176. })
  177. : null,
  178. child: const Text('Older'),
  179. ),
  180. TextButton(
  181. onPressed: end < duration
  182. ? () => setState(() {
  183. final newer = (end + _windowMinutes).clamp(
  184. earliestEnd,
  185. duration,
  186. );
  187. _windowEnd = origin!.add(
  188. Duration(
  189. microseconds: (newer * Duration.microsecondsPerMinute)
  190. .round(),
  191. ),
  192. );
  193. })
  194. : null,
  195. child: const Text('Newer'),
  196. ),
  197. TextButton(
  198. onPressed: () => setState(() => _windowEnd = null),
  199. child: Text(_windowEnd == null ? 'Live' : 'Return to Live'),
  200. ),
  201. ],
  202. ),
  203. Expanded(child: chart),
  204. ],
  205. );
  206. }
  207. LineChartBarData _line(List<FlSpot> spots, Color color, double width) =>
  208. LineChartBarData(
  209. spots: spots,
  210. isCurved: false,
  211. color: color,
  212. barWidth: width,
  213. isStrokeCapRound: true,
  214. dotData: const FlDotData(show: false),
  215. );
  216. }