更新睡眠报告

This commit is contained in:
wyf
2025-05-29 20:20:49 +08:00
parent b34737dbe8
commit 7a816922fa
41 changed files with 4604 additions and 2394 deletions

View File

@@ -164,6 +164,114 @@ class SnoreWaveform extends StatelessWidget {
}
}
// class SnoreWaveformPainter extends CustomPainter {
// final List<dynamic> snoreValues;
// final int startTime;
// final int endTime;
// SnoreWaveformPainter({
// required this.snoreValues,
// required this.startTime,
// required this.endTime,
// });
// @override
// void paint(Canvas canvas, Size size) {
// final double width = size.width;
// final double height = size.height;
// final double centerY = height / 2;
// final double totalDuration = (endTime - startTime).toDouble();
// final double pixelPerMs = width / totalDuration;
// final Paint wavePaint = Paint()
// ..color = stringToColor("#8E7DEF")
// ..strokeWidth = 1.5
// ..style = PaintingStyle.stroke;
// final Path upperPath = Path();
// final Path lowerPath = Path();
// const double scaleY = 0.5; //波形图比例
// for (int i = 0; i < snoreValues.length; i++) {
// final timestamp = snoreValues[i]["st"];
// final value = snoreValues[i]["value"]?.toDouble() ?? 0;
// final x = (timestamp - startTime) * pixelPerMs;
// final y = centerY - value * scaleY;
// final yMirror = centerY + value * scaleY;
// if (i == 0) {
// upperPath.moveTo(x, y);
// lowerPath.moveTo(x, yMirror);
// } else {
// upperPath.lineTo(x, y);
// lowerPath.lineTo(x, yMirror);
// }
// }
// canvas.drawPath(upperPath, wavePaint);
// canvas.drawPath(lowerPath, wavePaint);
// final Paint axisPaint = Paint()
// ..color = Colors.grey
// ..strokeWidth = 0.5;
// // 画中心线
// canvas.drawLine(Offset(0, centerY), Offset(width, centerY), axisPaint);
// // 时间刻度绘制
// final textPainter = TextPainter(
// textAlign: TextAlign.center,
// textDirection: ui.TextDirection.ltr,
// );
// final int hourMs = 60 * 60 * 1000;
// // 循环绘制整点小时标签(不包含终点)
// for (int t = startTime; t < endTime; t += hourMs) {
// double x = (t - startTime) * pixelPerMs;
// DateTime dt = DateTime.fromMillisecondsSinceEpoch(t);
// String label;
// if (t == startTime) {
// label = DateFormat('HH:mm').format(dt); // 起点显示 HH:mm
// } else {
// label = DateFormat('h').format(dt); // 中间显示小时不带前导0
// }
// textPainter.text = TextSpan(
// text: label,
// style: TextStyle(fontSize: 10, color: Colors.grey),
// );
// textPainter.layout();
// textPainter.paint(
// canvas,
// Offset(x - textPainter.width / 2, height + 20.rpx),
// );
// }
// // 单独绘制终点时间标签,确保显示具体时分
// {
// double x = (endTime - startTime) * pixelPerMs;
// DateTime dt = DateTime.fromMillisecondsSinceEpoch(endTime);
// String label = DateFormat('HH:mm').format(dt);
// textPainter.text = TextSpan(
// text: label,
// style: TextStyle(fontSize: 10, color: Colors.grey),
// );
// textPainter.layout();
// textPainter.paint(
// canvas,
// Offset(x - textPainter.width / 2, height + 20.rpx),
// );
// }
// }
// @override
// bool shouldRepaint(covariant CustomPainter oldDelegate) => true;
// }
class SnoreWaveformPainter extends CustomPainter {
final List<dynamic> snoreValues;
final int startTime;
@@ -184,13 +292,22 @@ class SnoreWaveformPainter extends CustomPainter {
final double pixelPerMs = width / totalDuration;
final Paint wavePaint = Paint()
..color = stringToColor("#8E7DEF")
..color = stringToColor("#8E7DEF").withOpacity(0.8)
..strokeWidth = 1.5
..style = PaintingStyle.stroke;
final Path upperPath = Path();
final Path lowerPath = Path();
const double scaleY = 0.5; //波形图比例
// ✅ 获取最大值用于自适应比例
double maxValue = snoreValues.fold<double>(0, (prev, e) {
final value = e["value"]?.toDouble() ?? 0;
return value > prev ? value : prev;
});
// ✅ 自适应缩放比例,限制波形最大高度为 height * 0.45
final double maxWaveHeight = height * 1;
final double scaleY = maxValue > 0 ? (maxWaveHeight / maxValue) : 1;
for (int i = 0; i < snoreValues.length; i++) {
final timestamp = snoreValues[i]["st"];
@@ -212,32 +329,26 @@ class SnoreWaveformPainter extends CustomPainter {
canvas.drawPath(upperPath, wavePaint);
canvas.drawPath(lowerPath, wavePaint);
// ✅ 最后绘制中心线,防止被覆盖
final Paint axisPaint = Paint()
..color = Colors.grey
..color = Colors.grey.withOpacity(0.6)
..strokeWidth = 0.5;
// 画中心线
canvas.drawLine(Offset(0, centerY), Offset(width, centerY), axisPaint);
// 时间刻度绘制
// 时间刻度绘制
final textPainter = TextPainter(
textAlign: TextAlign.center,
textDirection: ui.TextDirection.ltr,
);
final int hourMs = 60 * 60 * 1000;
// 循环绘制整点小时标签(不包含终点)
for (int t = startTime; t < endTime; t += hourMs) {
double x = (t - startTime) * pixelPerMs;
DateTime dt = DateTime.fromMillisecondsSinceEpoch(t);
String label;
if (t == startTime) {
label = DateFormat('HH:mm').format(dt); // 起点显示 HH:mm
} else {
label = DateFormat('h').format(dt); // 中间显示小时不带前导0
}
String label = t == startTime
? DateFormat('HH:mm').format(dt)
: DateFormat('h').format(dt); // 12小时制
textPainter.text = TextSpan(
text: label,
@@ -246,11 +357,11 @@ class SnoreWaveformPainter extends CustomPainter {
textPainter.layout();
textPainter.paint(
canvas,
Offset(x - textPainter.width / 2, height + 20.rpx),
Offset(x - textPainter.width / 2, height + 2), // 标签显示在底部
);
}
// 单独绘制终点时间标签,确保显示具体时分
// ✅ 画终点时间
{
double x = (endTime - startTime) * pixelPerMs;
DateTime dt = DateTime.fromMillisecondsSinceEpoch(endTime);
@@ -263,7 +374,7 @@ class SnoreWaveformPainter extends CustomPainter {
textPainter.layout();
textPainter.paint(
canvas,
Offset(x - textPainter.width / 2, height + 20.rpx),
Offset(x - textPainter.width / 2, height + 2),
);
}
}