kovinar update code
This commit is contained in:
+160
-92
@@ -1,24 +1,36 @@
|
||||
import 'dart:math';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
class ChartPoint {
|
||||
final String label; // shown in the tooltip and under the first/last point
|
||||
final double value;
|
||||
enum ChartAxis { left, right, none }
|
||||
|
||||
const ChartPoint(this.label, this.value);
|
||||
/// One line on the chart. Each series has its own scale, so values of very
|
||||
/// different size (average per shot vs. session total) can share the chart.
|
||||
/// [axis] says which side labels its scale.
|
||||
class ChartSeries {
|
||||
final String name;
|
||||
final Color color;
|
||||
final List<double> values; // one per label
|
||||
final int decimals;
|
||||
final ChartAxis axis;
|
||||
|
||||
const ChartSeries(this.name, this.color, this.values,
|
||||
{this.decimals = 2, this.axis = ChartAxis.none});
|
||||
|
||||
String format(double v) => v.toStringAsFixed(decimals);
|
||||
}
|
||||
|
||||
/// Single-series line chart: 2px line, 8px points, three faint gridlines.
|
||||
/// Tap (or drag) to show the nearest point's label and value.
|
||||
/// Line chart: 2px lines, 8px points, three faint gridlines, up to one labelled
|
||||
/// axis per side. Tap (or drag) to show every series' value at the nearest point.
|
||||
class ProgressChart extends StatefulWidget {
|
||||
final List<ChartPoint> points;
|
||||
final Color color;
|
||||
final List<String> labels; // shown in the tooltip and under the first/last point
|
||||
final List<ChartSeries> series; // legend and tooltip order
|
||||
final Color textColor;
|
||||
final Color gridColor;
|
||||
|
||||
const ProgressChart({
|
||||
super.key,
|
||||
required this.points,
|
||||
required this.color,
|
||||
required this.labels,
|
||||
required this.series,
|
||||
required this.textColor,
|
||||
required this.gridColor,
|
||||
});
|
||||
@@ -31,120 +43,180 @@ class _ProgressChartState extends State<ProgressChart> {
|
||||
int? _selected;
|
||||
|
||||
void _select(Offset position, Size size) {
|
||||
final n = widget.points.length;
|
||||
final n = widget.labels.length;
|
||||
if (n == 0) return;
|
||||
final plotWidth = size.width - _ChartPainter.leftAxis - _ChartPainter.pad * 2;
|
||||
final x = (position.dx - _ChartPainter.leftAxis - _ChartPainter.pad) / plotWidth;
|
||||
final plot = _ChartPainter.plotRect(size);
|
||||
final x = (position.dx - plot.left) / plot.width;
|
||||
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,
|
||||
),
|
||||
),
|
||||
);
|
||||
},
|
||||
return Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
children: [
|
||||
LayoutBuilder(
|
||||
builder: (context, constraints) {
|
||||
final size = Size(constraints.maxWidth, 190);
|
||||
return GestureDetector(
|
||||
behavior: HitTestBehavior.opaque,
|
||||
onTapDown: (d) => _select(d.localPosition, size),
|
||||
onHorizontalDragUpdate: (d) => _select(d.localPosition, size),
|
||||
child: CustomPaint(
|
||||
size: size,
|
||||
painter: _ChartPainter(
|
||||
labels: widget.labels,
|
||||
series: widget.series,
|
||||
textColor: widget.textColor,
|
||||
gridColor: widget.gridColor,
|
||||
selected: _selected,
|
||||
),
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
// Legend, centred under the graph
|
||||
Wrap(
|
||||
alignment: WrapAlignment.center,
|
||||
spacing: 16,
|
||||
runSpacing: 4,
|
||||
children: [
|
||||
for (final s in widget.series)
|
||||
Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Container(
|
||||
width: 14,
|
||||
height: 3,
|
||||
decoration: BoxDecoration(color: s.color, borderRadius: BorderRadius.circular(2)),
|
||||
),
|
||||
const SizedBox(width: 6),
|
||||
Text(s.name, style: TextStyle(fontSize: 12, color: widget.textColor)),
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _ChartPainter extends CustomPainter {
|
||||
static const double leftAxis = 32; // room for y labels
|
||||
static const double axis = 34; // room for y labels on each side
|
||||
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;
|
||||
static Rect plotRect(Size size) =>
|
||||
Rect.fromLTRB(axis + pad, top, size.width - axis - pad, size.height - bottom);
|
||||
|
||||
final List<String> labels;
|
||||
final List<ChartSeries> series;
|
||||
final Color textColor;
|
||||
final Color gridColor;
|
||||
final int? selected;
|
||||
|
||||
_ChartPainter({
|
||||
required this.points,
|
||||
required this.color,
|
||||
required this.labels,
|
||||
required this.series,
|
||||
required this.textColor,
|
||||
required this.gridColor,
|
||||
required this.selected,
|
||||
});
|
||||
|
||||
// Min and max of a series, widened so a flat line sits in the middle
|
||||
static (double, double) _range(List<double> values, double minSpan) {
|
||||
var lo = values.reduce(min);
|
||||
var hi = values.reduce(max);
|
||||
if (hi - lo < minSpan) {
|
||||
final mid = (hi + lo) / 2;
|
||||
lo = mid - minSpan / 2;
|
||||
hi = mid + minSpan / 2;
|
||||
}
|
||||
return (lo, hi);
|
||||
}
|
||||
|
||||
@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);
|
||||
if (labels.isEmpty || series.isEmpty) return;
|
||||
final plot = plotRect(size);
|
||||
final n = labels.length;
|
||||
final ranges = [
|
||||
for (final s in series) _range(s.values, s.values.reduce(max).abs() * 0.1 + 0.2),
|
||||
];
|
||||
|
||||
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),
|
||||
);
|
||||
double xAt(int i) => n == 1 ? plot.center.dx : plot.left + plot.width * i / (n - 1);
|
||||
double yAt(double v, (double, double) r) => plot.bottom - plot.height * (v - r.$1) / (r.$2 - r.$1);
|
||||
|
||||
// Gridlines + y labels (min, mid, max)
|
||||
// Gridlines + y labels in each axis series' colour
|
||||
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);
|
||||
canvas.drawLine(Offset(axis, y), Offset(size.width - axis, y), grid);
|
||||
for (int k = 0; k < series.length; k++) {
|
||||
if (series[k].axis == ChartAxis.none) continue;
|
||||
final right = series[k].axis == ChartAxis.right;
|
||||
final r = ranges[k];
|
||||
final v = r.$1 + (r.$2 - r.$1) * f;
|
||||
final text = series[k].decimals == 0 ? v.round().toString() : v.toStringAsFixed(1);
|
||||
_text(canvas, text, Offset(right ? size.width : 0, y - 7), 11, series[k].color, alignRight: right);
|
||||
}
|
||||
}
|
||||
|
||||
// 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);
|
||||
_text(canvas, labels.first, Offset(plot.left - pad, plot.bottom + 5), 11, textColor);
|
||||
if (n > 1) {
|
||||
_text(canvas, labels.last, Offset(plot.right + pad, 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);
|
||||
// Lines, last series first so the main one stays on top
|
||||
for (int k = series.length - 1; k >= 0; k--) {
|
||||
final s = series[k];
|
||||
final r = ranges[k];
|
||||
final path = Path()..moveTo(xAt(0), yAt(s.values[0], r));
|
||||
for (int i = 1; i < n; i++) {
|
||||
path.lineTo(xAt(i), yAt(s.values[i], r));
|
||||
}
|
||||
canvas.drawPath(
|
||||
path,
|
||||
Paint()
|
||||
..color = s.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 < n; i++) {
|
||||
final isSel = i == selected;
|
||||
final p = Offset(xAt(i), yAt(s.values[i], r));
|
||||
canvas.drawCircle(p, isSel ? 6 : 4.5, Paint()..color = Colors.white);
|
||||
canvas.drawCircle(p, isSel ? 4.5 : 3.5, Paint()..color = s.color);
|
||||
}
|
||||
}
|
||||
|
||||
// Tooltip for the selected point
|
||||
// Tooltip for the selected point: label, then each value in its colour
|
||||
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 i = selected!;
|
||||
canvas.drawLine(Offset(xAt(i), plot.top), Offset(xAt(i), plot.bottom), grid);
|
||||
final tp = TextPainter(
|
||||
text: TextSpan(
|
||||
style: const TextStyle(fontSize: 12, fontWeight: FontWeight.w600, color: Colors.white),
|
||||
children: [
|
||||
TextSpan(text: labels[i]),
|
||||
for (final s in series)
|
||||
TextSpan(
|
||||
text: ' ${s.format(s.values[i])}',
|
||||
style: TextStyle(color: Color.lerp(s.color, Colors.white, 0.35)),
|
||||
),
|
||||
],
|
||||
),
|
||||
textDirection: TextDirection.ltr,
|
||||
)..layout(maxWidth: size.width - 16);
|
||||
final w = tp.width + 16;
|
||||
final left = (p.dx - w / 2).clamp(0.0, size.width - w);
|
||||
final left = (xAt(i) - 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]!,
|
||||
@@ -153,22 +225,18 @@ class _ChartPainter extends CustomPainter {
|
||||
}
|
||||
}
|
||||
|
||||
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),
|
||||
),
|
||||
void _text(Canvas canvas, String text, Offset at, double size, Color color, {bool alignRight = false}) {
|
||||
final tp = TextPainter(
|
||||
text: TextSpan(text: text, style: TextStyle(fontSize: size, color: color)),
|
||||
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;
|
||||
old.labels != labels ||
|
||||
old.series != series ||
|
||||
old.selected != selected ||
|
||||
old.textColor != textColor;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user