173 lines
5.1 KiB
Dart
173 lines
5.1 KiB
Dart
import 'package:flutter/material.dart';
|
|
|
|
import '../layout/chart_config.dart';
|
|
import '../layout/layout_controller.dart';
|
|
import '../session/session_controller.dart';
|
|
|
|
/// Stateless per-frame painter. Reads everything fresh from the controllers
|
|
/// each `paint()` — no internal state.
|
|
class ChartPainter extends CustomPainter {
|
|
ChartPainter({
|
|
required this.channel,
|
|
required this.session,
|
|
required this.layout,
|
|
});
|
|
|
|
final int channel;
|
|
final SessionController session;
|
|
final LayoutController layout;
|
|
|
|
@override
|
|
void paint(Canvas canvas, Size size) {
|
|
final padLeft = 32.0, padRight = 4.0, padTop = 4.0, padBottom = 16.0;
|
|
final plotW = size.width - padLeft - padRight;
|
|
final plotH = size.height - padTop - padBottom;
|
|
if (plotW <= 0 || plotH <= 0) return;
|
|
|
|
// Background.
|
|
final bgPaint = Paint()..color = const Color(0xFFF7F7F4);
|
|
canvas.drawRect(
|
|
Rect.fromLTWH(padLeft, padTop, plotW, plotH),
|
|
bgPaint,
|
|
);
|
|
|
|
// Compute window.
|
|
final newest = session.packets.newest;
|
|
final oldestUs = session.packets.oldest?.timestampUs.toInt() ?? 0;
|
|
final newestUs = newest?.timestampUs.toInt() ??
|
|
DateTime.now().microsecondsSinceEpoch;
|
|
final win = session.viewState.currentWindow(
|
|
nowUs: newestUs,
|
|
oldestUs: oldestUs,
|
|
newestUs: newestUs,
|
|
);
|
|
|
|
// Decimate.
|
|
final cols = session.decimator.decimate(
|
|
channel: channel,
|
|
buffer: session.packets,
|
|
startUs: win.startUs,
|
|
endUs: win.endUs,
|
|
pixelWidth: plotW.round(),
|
|
);
|
|
final gaps = session.decimator.computeGaps(
|
|
channel: channel,
|
|
buffer: session.packets,
|
|
startUs: win.startUs,
|
|
endUs: win.endUs,
|
|
pixelWidth: plotW.round(),
|
|
);
|
|
|
|
// Y range.
|
|
final cfg = layout.configFor(channel);
|
|
var yMin = cfg.yMin, yMax = cfg.yMax;
|
|
if (cfg.yMode == YMode.auto) {
|
|
double lo = double.infinity, hi = double.negativeInfinity;
|
|
for (final c in cols) {
|
|
if (!c.hasData) continue;
|
|
if (c.min < lo) lo = c.min;
|
|
if (c.max > hi) hi = c.max;
|
|
}
|
|
if (lo.isFinite && hi.isFinite && hi > lo) {
|
|
yMin = lo;
|
|
yMax = hi;
|
|
}
|
|
}
|
|
final ySpan = (yMax - yMin).abs() < 1e-12 ? 1.0 : (yMax - yMin);
|
|
|
|
double xForCol(int col) => padLeft + col + 0.5;
|
|
double yForVal(double v) =>
|
|
padTop + plotH * (1 - (v - yMin) / ySpan);
|
|
|
|
// Hatched gap rectangles.
|
|
final hatchPaint = Paint()
|
|
..color = const Color(0x55993C1D)
|
|
..style = PaintingStyle.fill;
|
|
for (final g in gaps.hatchedRanges) {
|
|
final x0 = xForCol(g.startPx);
|
|
final x1 = xForCol(g.endPx + 1);
|
|
canvas.drawRect(
|
|
Rect.fromLTRB(x0, padTop, x1, padTop + plotH),
|
|
hatchPaint,
|
|
);
|
|
}
|
|
|
|
// Polyline.
|
|
final linePaint = Paint()
|
|
..color = const Color(0xFF185FA5)
|
|
..strokeWidth = 1.2
|
|
..style = PaintingStyle.stroke;
|
|
final path = Path();
|
|
bool started = false;
|
|
for (var i = 0; i < cols.length; i++) {
|
|
final c = cols[i];
|
|
if (!c.hasData) {
|
|
started = false;
|
|
continue;
|
|
}
|
|
final x = xForCol(i);
|
|
final yMinPx = yForVal(c.max); // Higher value = smaller y.
|
|
final yMaxPx = yForVal(c.min);
|
|
if (!started) {
|
|
path.moveTo(x, (yMinPx + yMaxPx) / 2);
|
|
started = true;
|
|
} else {
|
|
path.lineTo(x, (yMinPx + yMaxPx) / 2);
|
|
}
|
|
// Min/max vertical span to surface intra-column variation.
|
|
if ((yMaxPx - yMinPx).abs() > 0.5) {
|
|
canvas.drawLine(Offset(x, yMinPx), Offset(x, yMaxPx), linePaint);
|
|
}
|
|
}
|
|
canvas.drawPath(path, linePaint);
|
|
|
|
// Sub-pixel marker lines.
|
|
final markerPaint = Paint()
|
|
..color = const Color(0xAA993C1D)
|
|
..strokeWidth = 1;
|
|
for (final px in gaps.markerPixels) {
|
|
final x = xForCol(px);
|
|
canvas.drawLine(
|
|
Offset(x, padTop),
|
|
Offset(x, padTop + plotH),
|
|
markerPaint,
|
|
);
|
|
}
|
|
|
|
// Axes labels.
|
|
_drawText(canvas, '${yMax.toStringAsFixed(2)}',
|
|
Offset(padLeft - 4, padTop), align: TextAlign.right);
|
|
_drawText(canvas, '${yMin.toStringAsFixed(2)}',
|
|
Offset(padLeft - 4, padTop + plotH - 12), align: TextAlign.right);
|
|
_drawText(canvas, _fmtTime(win.startUs),
|
|
Offset(padLeft, size.height - 14));
|
|
_drawText(canvas, _fmtTime(win.endUs),
|
|
Offset(size.width - padRight, size.height - 14),
|
|
align: TextAlign.right);
|
|
}
|
|
|
|
void _drawText(Canvas c, String s, Offset where,
|
|
{TextAlign align = TextAlign.left}) {
|
|
final tp = TextPainter(
|
|
text: TextSpan(
|
|
text: s,
|
|
style: const TextStyle(fontSize: 9, color: Colors.grey),
|
|
),
|
|
textDirection: TextDirection.ltr,
|
|
textAlign: align,
|
|
)..layout();
|
|
final dx = align == TextAlign.right ? where.dx - tp.width : where.dx;
|
|
tp.paint(c, Offset(dx, where.dy));
|
|
}
|
|
|
|
String _fmtTime(int us) {
|
|
final d = DateTime.fromMicrosecondsSinceEpoch(us);
|
|
return '${d.hour.toString().padLeft(2, '0')}:'
|
|
'${d.minute.toString().padLeft(2, '0')}:'
|
|
'${d.second.toString().padLeft(2, '0')}.'
|
|
'${d.millisecond.toString().padLeft(3, '0')}';
|
|
}
|
|
|
|
@override
|
|
bool shouldRepaint(ChartPainter old) => true; // Per-frame repaint.
|
|
} |