dashPi: a self-hosted home dashboard, its sync server and phone app
The dashboard (Raspberry Pi 3.5" screen + web pages), the end-to-end encrypted sync mailbox (PHP + SQLite) and the Android companion app (Flutter), with docs, clean-install checks and screenshots made from demo data. Built mostly with an AI coding assistant; not yet reviewed line by line by a human (see README and SECURITY.md). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
commit
fb3c01cdd7
333 changed files with 45151 additions and 0 deletions
212
app/lib/widgets/line_chart.dart
Normal file
212
app/lib/widgets/line_chart.dart
Normal file
|
|
@ -0,0 +1,212 @@
|
|||
// A small time chart for the health pages: lines (or dots) over dates, an optional band (the
|
||||
// healthy range), hour / day / month ticks to suit the span, a few value ticks; tap or drag shows
|
||||
// the nearest point. With [range]: the shared Health period as a chip in the corner (tap = change
|
||||
// it for every Health chart).
|
||||
import 'dart:math';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import '../app/theme.dart';
|
||||
import '../health/range.dart';
|
||||
|
||||
class Series {
|
||||
const Series(this.name, this.points, this.color);
|
||||
final String name;
|
||||
final List<(DateTime, double)> points;
|
||||
final Color color;
|
||||
}
|
||||
|
||||
class LineChart extends StatefulWidget {
|
||||
const LineChart({super.key, required this.series, required this.from, required this.to, this.unit = '',
|
||||
this.band, this.limits, this.height = 190, this.decimals = 1, this.range});
|
||||
final List<Series> series;
|
||||
final DateTime from, to;
|
||||
final String unit;
|
||||
final (double, double)? band;
|
||||
final (double, double)? limits; // a fixed scale (stretched if a value falls outside), e.g. 85–100 % for SpO₂
|
||||
final double height;
|
||||
final int decimals;
|
||||
final HealthRange? range;
|
||||
@override
|
||||
State<LineChart> createState() => _LineChartState();
|
||||
}
|
||||
|
||||
class _LineChartState extends State<LineChart> {
|
||||
(Series, (DateTime, double))? hit;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final chart = _chart(context);
|
||||
if (widget.range == null) return chart;
|
||||
return Column(crossAxisAlignment: CrossAxisAlignment.stretch, children: [Align(alignment: Alignment.centerRight, child: RangeChip(widget.range!)), const SizedBox(height: 4), chart]);
|
||||
}
|
||||
|
||||
Widget _chart(BuildContext context) {
|
||||
final pts = [for (final s in widget.series) for (final p in s.points) if (!p.$1.isBefore(widget.from)) p];
|
||||
if (pts.isEmpty) {
|
||||
return SizedBox(height: 60, child: Center(child: Text('Nothing measured in this period', style: TextStyle(color: muted.withValues(alpha: .8)))));
|
||||
}
|
||||
return LayoutBuilder(builder: (context, box) {
|
||||
final painter = _Painter(widget, hit);
|
||||
void pick(Offset o) {
|
||||
(Series, (DateTime, double))? best;
|
||||
var bd = double.infinity;
|
||||
for (final s in widget.series) {
|
||||
for (final p in s.points) {
|
||||
if (p.$1.isBefore(widget.from)) continue;
|
||||
final d = (painter.pos(p, box.maxWidth) - o).distance;
|
||||
if (d < bd) {
|
||||
bd = d;
|
||||
best = (s, p);
|
||||
}
|
||||
}
|
||||
}
|
||||
setState(() => hit = bd < 60 ? best : null);
|
||||
}
|
||||
|
||||
return Column(crossAxisAlignment: CrossAxisAlignment.stretch, children: [
|
||||
GestureDetector(
|
||||
onTapDown: (d) => pick(d.localPosition),
|
||||
onHorizontalDragUpdate: (d) => pick(d.localPosition),
|
||||
child: CustomPaint(size: Size(box.maxWidth, widget.height), painter: painter),
|
||||
),
|
||||
SizedBox(
|
||||
height: 20,
|
||||
child: hit == null
|
||||
? null
|
||||
: Text('${hit!.$1.name}: ${hit!.$2.$2.toStringAsFixed(widget.decimals)} ${widget.unit} · '
|
||||
'${hit!.$2.$1.day}.${hit!.$2.$1.month}.${hit!.$2.$1.year}'
|
||||
'${hit!.$2.$1.hour + hit!.$2.$1.minute > 0 ? ' ${hit!.$2.$1.toIso8601String().substring(11, 16)}' : ''}',
|
||||
textAlign: TextAlign.center, style: TextStyle(color: hit!.$1.color, fontSize: 13)),
|
||||
),
|
||||
]);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
class _Painter extends CustomPainter {
|
||||
_Painter(this.w, this.hit) {
|
||||
final vs = [for (final s in w.series) for (final p in s.points) if (!p.$1.isBefore(w.from)) p.$2];
|
||||
var lo = vs.reduce(min), hi = vs.reduce(max);
|
||||
if (w.band != null && w.band!.$1 < hi && w.band!.$2 > lo) { // the band only stretches the scale where it meets the data
|
||||
lo = min(lo, w.band!.$1);
|
||||
hi = max(hi, w.band!.$2);
|
||||
}
|
||||
if (w.limits != null) {
|
||||
lo = min(lo, w.limits!.$1);
|
||||
hi = max(hi, w.limits!.$2);
|
||||
} else {
|
||||
final pad = max((hi - lo) * 0.1, 0.5);
|
||||
lo -= pad;
|
||||
hi += pad;
|
||||
}
|
||||
step = _nice((hi - lo) / 4);
|
||||
this.lo = (lo / step).floorToDouble() * step;
|
||||
this.hi = (hi / step).ceilToDouble() * step;
|
||||
}
|
||||
|
||||
final LineChart w;
|
||||
final (Series, (DateTime, double))? hit;
|
||||
late final double lo, hi, step;
|
||||
static const ml = 40.0, mb = 18.0, mt = 6.0;
|
||||
|
||||
static double _nice(double x) {
|
||||
final e = pow(10, (log(x) / ln10).floor()).toDouble();
|
||||
final f = x / e;
|
||||
return (f <= 1 ? 1 : f <= 2 ? 2 : f <= 5 ? 5 : 10) * e;
|
||||
}
|
||||
|
||||
double get span => max(1, w.to.difference(w.from).inHours / 24);
|
||||
|
||||
Offset pos((DateTime, double) p, double width) {
|
||||
final x = ml + (p.$1.difference(w.from).inHours / 24) / span * (width - ml - 8);
|
||||
final y = mt + (1 - (p.$2 - lo) / (hi - lo)) * (w.height - mt - mb);
|
||||
return Offset(x, y);
|
||||
}
|
||||
|
||||
@override
|
||||
void paint(Canvas c, Size size) {
|
||||
final grid = Paint()..color = card2..strokeWidth = 1;
|
||||
final txt = TextStyle(color: muted, fontSize: 10.5);
|
||||
void label(String s, Offset at, {bool right = false, bool center = false}) {
|
||||
final tp = TextPainter(text: TextSpan(text: s, style: txt), textDirection: TextDirection.ltr)..layout();
|
||||
tp.paint(c, at - Offset(right ? tp.width : center ? tp.width / 2 : 0, tp.height / 2));
|
||||
}
|
||||
|
||||
if (w.band != null) {
|
||||
final top = max(mt, pos((w.from, w.band!.$2), size.width).dy), bottom = min(w.height - mb, pos((w.to, w.band!.$1), size.width).dy);
|
||||
if (bottom > top) c.drawRect(Rect.fromLTRB(ml, top, size.width - 8, bottom), Paint()..color = good.withValues(alpha: 0.08));
|
||||
}
|
||||
for (var v = lo; v <= hi + step / 2; v += step) {
|
||||
final y = pos((w.from, v), size.width).dy;
|
||||
c.drawLine(Offset(ml, y), Offset(size.width - 8, y), grid);
|
||||
label(v.toStringAsFixed(step < 1 ? 1 : 0), Offset(ml - 6, y), right: true);
|
||||
}
|
||||
// x ticks to suit the span: hours (≤ 2 days), days (≤ 45), months
|
||||
final days = span;
|
||||
const mon = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
|
||||
void tick(DateTime d, String text) {
|
||||
if (d.isBefore(w.from) || d.isAfter(w.to)) return;
|
||||
label(text, Offset(pos((d, lo), size.width).dx, w.height - mb / 2 + 2), center: true);
|
||||
}
|
||||
if (days <= 2) {
|
||||
for (var d = DateTime.utc(w.from.year, w.from.month, w.from.day); !d.isAfter(w.to); d = d.add(const Duration(hours: 4))) {
|
||||
tick(d, d.hour == 0 ? '${d.day} ${mon[d.month - 1]}' : '${d.hour.toString().padLeft(2, '0')}:00');
|
||||
}
|
||||
} else if (days <= 45) {
|
||||
final every = (days / 7).ceil();
|
||||
for (var d = DateTime.utc(w.from.year, w.from.month, w.from.day), i = 0; !d.isAfter(w.to); d = d.add(const Duration(days: 1)), i++) {
|
||||
if (i % every == 0) tick(d, '${d.day} ${mon[d.month - 1]}');
|
||||
}
|
||||
} else {
|
||||
final every = days > 800 ? 6 : days > 400 ? 3 : 1;
|
||||
for (var d = DateTime.utc(w.from.year, w.from.month, 1); !d.isAfter(w.to); d = DateTime.utc(d.year, d.month + 1, 1)) {
|
||||
if ((d.month - 1) % every == 0) tick(d, d.month == 1 ? '${d.year}' : mon[d.month - 1]);
|
||||
}
|
||||
}
|
||||
for (final s in w.series) {
|
||||
final ps = [for (final p in s.points) if (!p.$1.isBefore(w.from)) pos(p, size.width)];
|
||||
final line = Paint()..color = s.color..strokeWidth = 2..style = PaintingStyle.stroke;
|
||||
if (ps.length > 1) {
|
||||
final path = Path()..moveTo(ps.first.dx, ps.first.dy);
|
||||
for (final p in ps.skip(1)) {
|
||||
path.lineTo(p.dx, p.dy);
|
||||
}
|
||||
c.drawPath(path, line);
|
||||
}
|
||||
for (final p in ps) {
|
||||
c.drawCircle(p, ps.length > 40 ? 1.8 : 3, Paint()..color = s.color);
|
||||
}
|
||||
}
|
||||
if (hit != null) {
|
||||
final p = pos(hit!.$2, size.width);
|
||||
c.drawCircle(p, 6, Paint()..color = hit!.$1.color.withValues(alpha: .35));
|
||||
c.drawLine(Offset(p.dx, mt), Offset(p.dx, w.height - mb), Paint()..color = muted.withValues(alpha: .4));
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
bool shouldRepaint(covariant _Painter old) => true;
|
||||
}
|
||||
|
||||
/// The shared Health period as a small chip (tap = change it for every Health chart).
|
||||
class RangeChip extends StatelessWidget {
|
||||
const RangeChip(this.r, {super.key});
|
||||
final HealthRange r;
|
||||
@override
|
||||
Widget build(BuildContext context) => PopupMenuButton<String>(
|
||||
tooltip: 'Time period (every Health chart)',
|
||||
color: card2,
|
||||
initialValue: r.value,
|
||||
onSelected: r.set,
|
||||
itemBuilder: (_) => [for (final (k, l, _) in ranges) PopupMenuItem(value: k, child: Text(l))],
|
||||
child: Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 4),
|
||||
decoration: BoxDecoration(color: card2, borderRadius: BorderRadius.circular(8)),
|
||||
child: Row(mainAxisSize: MainAxisSize.min, children: [
|
||||
Text(r.label, style: const TextStyle(color: accent, fontSize: 13)),
|
||||
const Icon(Icons.arrow_drop_down, color: accent, size: 18),
|
||||
]),
|
||||
),
|
||||
);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue