175 lines
5.7 KiB
Dart
175 lines
5.7 KiB
Dart
import 'package:flutter/material.dart';
|
|
|
|
class ChartPoint {
|
|
final String label; // shown in the tooltip and under the first/last point
|
|
final double value;
|
|
|
|
const ChartPoint(this.label, this.value);
|
|
}
|
|
|
|
/// Single-series line chart: 2px line, 8px points, three faint gridlines.
|
|
/// Tap (or drag) to show the nearest point's label and value.
|
|
class ProgressChart extends StatefulWidget {
|
|
final List<ChartPoint> points;
|
|
final Color color;
|
|
final Color textColor;
|
|
final Color gridColor;
|
|
|
|
const ProgressChart({
|
|
super.key,
|
|
required this.points,
|
|
required this.color,
|
|
required this.textColor,
|
|
required this.gridColor,
|
|
});
|
|
|
|
@override
|
|
State<ProgressChart> createState() => _ProgressChartState();
|
|
}
|
|
|
|
class _ProgressChartState extends State<ProgressChart> {
|
|
int? _selected;
|
|
|
|
void _select(Offset position, Size size) {
|
|
final n = widget.points.length;
|
|
if (n == 0) return;
|
|
final plotWidth = size.width - _ChartPainter.leftAxis - _ChartPainter.pad * 2;
|
|
final x = (position.dx - _ChartPainter.leftAxis - _ChartPainter.pad) / plotWidth;
|
|
setState(() => _selected = (x * (n - 1)).round().clamp(0, n - 1));
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return LayoutBuilder(
|
|
builder: (context, constraints) {
|
|
final size = Size(constraints.maxWidth, 170);
|
|
return GestureDetector(
|
|
behavior: HitTestBehavior.opaque,
|
|
onTapDown: (d) => _select(d.localPosition, size),
|
|
onHorizontalDragUpdate: (d) => _select(d.localPosition, size),
|
|
child: CustomPaint(
|
|
size: size,
|
|
painter: _ChartPainter(
|
|
points: widget.points,
|
|
color: widget.color,
|
|
textColor: widget.textColor,
|
|
gridColor: widget.gridColor,
|
|
selected: _selected,
|
|
),
|
|
),
|
|
);
|
|
},
|
|
);
|
|
}
|
|
}
|
|
|
|
class _ChartPainter extends CustomPainter {
|
|
static const double leftAxis = 32; // room for y labels
|
|
static const double pad = 10; // keeps end points inside the plot
|
|
static const double top = 34; // room for the tooltip
|
|
static const double bottom = 20; // room for x labels
|
|
|
|
final List<ChartPoint> points;
|
|
final Color color;
|
|
final Color textColor;
|
|
final Color gridColor;
|
|
final int? selected;
|
|
|
|
_ChartPainter({
|
|
required this.points,
|
|
required this.color,
|
|
required this.textColor,
|
|
required this.gridColor,
|
|
required this.selected,
|
|
});
|
|
|
|
@override
|
|
void paint(Canvas canvas, Size size) {
|
|
if (points.isEmpty) return;
|
|
final values = points.map((p) => p.value);
|
|
var minV = values.reduce((a, b) => a < b ? a : b);
|
|
var maxV = values.reduce((a, b) => a > b ? a : b);
|
|
if (maxV - minV < 1) {
|
|
minV -= 0.5;
|
|
maxV += 0.5;
|
|
}
|
|
final plot = Rect.fromLTRB(leftAxis + pad, top, size.width - pad, size.height - bottom);
|
|
|
|
Offset at(int i) => Offset(
|
|
points.length == 1 ? plot.center.dx : plot.left + plot.width * i / (points.length - 1),
|
|
plot.bottom - plot.height * (points[i].value - minV) / (maxV - minV),
|
|
);
|
|
|
|
// Gridlines + y labels (min, mid, max)
|
|
final grid = Paint()
|
|
..color = gridColor
|
|
..strokeWidth = 1;
|
|
for (final f in [0.0, 0.5, 1.0]) {
|
|
final y = plot.bottom - plot.height * f;
|
|
canvas.drawLine(Offset(leftAxis, y), Offset(size.width, y), grid);
|
|
_text(canvas, (minV + (maxV - minV) * f).toStringAsFixed(1), Offset(0, y - 7), 11, textColor);
|
|
}
|
|
|
|
// x labels: first and last only
|
|
_text(canvas, points.first.label, Offset(plot.left - pad, plot.bottom + 5), 11, textColor);
|
|
if (points.length > 1) {
|
|
_text(canvas, points.last.label, Offset(plot.right, plot.bottom + 5), 11, textColor, alignRight: true);
|
|
}
|
|
|
|
// Line
|
|
final path = Path()..moveTo(at(0).dx, at(0).dy);
|
|
for (int i = 1; i < points.length; i++) {
|
|
path.lineTo(at(i).dx, at(i).dy);
|
|
}
|
|
canvas.drawPath(
|
|
path,
|
|
Paint()
|
|
..color = color
|
|
..strokeWidth = 2
|
|
..style = PaintingStyle.stroke
|
|
..strokeJoin = StrokeJoin.round,
|
|
);
|
|
|
|
// Points with a surface ring so they read on top of the line
|
|
for (int i = 0; i < points.length; i++) {
|
|
final isSel = i == selected;
|
|
canvas.drawCircle(at(i), isSel ? 7 : 5, Paint()..color = Colors.white);
|
|
canvas.drawCircle(at(i), isSel ? 5.5 : 4, Paint()..color = color);
|
|
}
|
|
|
|
// Tooltip for the selected point
|
|
if (selected != null) {
|
|
final p = at(selected!);
|
|
canvas.drawLine(Offset(p.dx, plot.top), Offset(p.dx, plot.bottom), grid);
|
|
final label = '${points[selected!].label} ${points[selected!].value.toStringAsFixed(2)}';
|
|
final tp = _layout(label, 12, Colors.white, bold: true);
|
|
final w = tp.width + 16;
|
|
final left = (p.dx - w / 2).clamp(0.0, size.width - w);
|
|
canvas.drawRRect(
|
|
RRect.fromRectAndRadius(Rect.fromLTWH(left, 2, w, 24), const Radius.circular(6)),
|
|
Paint()..color = Colors.grey[900]!,
|
|
);
|
|
tp.paint(canvas, Offset(left + 8, 2 + (24 - tp.height) / 2));
|
|
}
|
|
}
|
|
|
|
TextPainter _layout(String text, double size, Color color, {bool bold = false}) {
|
|
return TextPainter(
|
|
text: TextSpan(
|
|
text: text,
|
|
style: TextStyle(fontSize: size, color: color, fontWeight: bold ? FontWeight.w600 : FontWeight.normal),
|
|
),
|
|
textDirection: TextDirection.ltr,
|
|
)..layout();
|
|
}
|
|
|
|
void _text(Canvas canvas, String text, Offset at, double size, Color color, {bool alignRight = false}) {
|
|
final tp = _layout(text, size, color);
|
|
tp.paint(canvas, alignRight ? at.translate(-tp.width, 0) : at);
|
|
}
|
|
|
|
@override
|
|
bool shouldRepaint(_ChartPainter old) =>
|
|
old.points != points || old.selected != selected || old.color != color || old.textColor != textColor;
|
|
}
|