diff --git a/lib/features/home/presentation/pages/running_status_page.dart b/lib/features/home/presentation/pages/running_status_page.dart index 6f0aac4f..994c036f 100644 --- a/lib/features/home/presentation/pages/running_status_page.dart +++ b/lib/features/home/presentation/pages/running_status_page.dart @@ -24,8 +24,13 @@ class RunningStatusPage extends StatefulWidget { class _RunningStatusPageState extends State { bool _isCardView = true; - bool _chipGaugeMode = true; + // 仪表盘/折线图切换状态 + bool _voltageGaugeMode = true; + bool _chipTempGaugeMode = true; + bool _knifeSpeedGaugeMode = false; + + // 历史数据 final List _leftMeasureHistory = []; final List _rightMeasureHistory = []; final List _leftTargetHistory = []; @@ -93,7 +98,7 @@ class _RunningStatusPageState extends State { _rightTempHistory.add(FlSpot(_timeIndex, status.rightMotorTemp)); _knifeHistory.add(FlSpot(_timeIndex, double.tryParse(status.knifeCuttingSpeed) ?? 0)); _chipTempHistory.add(FlSpot(_timeIndex, status.chipTemp)); - _voltageHistory.add(FlSpot(_timeIndex, double.tryParse(status.battery) ?? 0)); + _voltageHistory.add(FlSpot(_timeIndex, status.voltage)); _timeIndex += 1; @@ -137,8 +142,8 @@ class _RunningStatusPageState extends State { ); } - // ====================== 半圆仪表盘(电压/芯片温度) ====================== - Widget _semiCircleGauge({ + // ====================== 大半个圆仪表盘(电压/芯片温度) ====================== + Widget _largeArcGauge({ required double value, required double maxValue, required String unit, @@ -146,13 +151,13 @@ class _RunningStatusPageState extends State { List majorTicks = const [0, 25, 50, 75, 100, 125, 150, 175, 200, 225, 250], }) { return SizedBox( - height: 200, + height: 220, child: Stack( alignment: Alignment.center, children: [ CustomPaint( - painter: SemiCircleGaugePainter(value: value / maxValue, maxValue: maxValue, majorTicks: majorTicks), - size: const Size(280, 140), + painter: LargeArcGaugePainter(value: value / maxValue, maxValue: maxValue, majorTicks: majorTicks), + size: const Size(300, 180), ), Positioned( bottom: 10, @@ -193,7 +198,13 @@ class _RunningStatusPageState extends State { required double yAxisMax, double yAxisMin = 0, List bottomLabels = const ["t", "t+1", "t+2", "t+3", "t+4", "t+5"], + List leftTicks = const [0, 1000, 2000, 3000], }) { + // 获取实际显示的点位数量 + final int showSpotCount = lines.isNotEmpty ? lines.first.spots.length : 0; + // 计算底部标签的间隔,避免标签过多溢出 + final double bottomInterval = showSpotCount > 6 ? showSpotCount / 6 : 1; + return Column( children: [ if (lines.length > 1) @@ -208,44 +219,68 @@ class _RunningStatusPageState extends State { ], ), ), - SizedBox( - height: 200, - child: LineChart( - LineChartData( - minY: yAxisMin, - maxY: yAxisMax, - gridData: FlGridData( - show: true, - horizontalInterval: (yAxisMax - yAxisMin) / 6, - getDrawingHorizontalLine: (value) => const FlLine(color: Color(0xFFE5E5E5), strokeWidth: 1), - drawVerticalLine: false, - ), - titlesData: FlTitlesData( - leftTitles: AxisTitles( - sideTitles: SideTitles( - showTitles: true, - interval: (yAxisMax - yAxisMin) / 6, - getTitlesWidget: (value, meta) => Text(value.toStringAsFixed(0), style: const TextStyle(fontSize: 12, color: Colors.grey)), - ), + // 用Expanded适配宽度,避免溢出 + Expanded( + child: SizedBox( + height: 200, + child: LineChart( + LineChartData( + minY: yAxisMin, + maxY: yAxisMax, + gridData: FlGridData( + show: true, + horizontalInterval: (yAxisMax - yAxisMin) / leftTicks.length, + getDrawingHorizontalLine: (value) => const FlLine(color: Color(0xFFE5E5E5), strokeWidth: 1), + drawVerticalLine: false, ), - bottomTitles: AxisTitles( - sideTitles: SideTitles( - showTitles: true, - interval: 1, - getTitlesWidget: (value, meta) { - int idx = value.toInt(); - if (idx >= 0 && idx < bottomLabels.length) { - return Text(bottomLabels[idx], style: const TextStyle(fontSize: 12, color: Colors.grey)); - } - return const Text(""); - }, + titlesData: FlTitlesData( + leftTitles: AxisTitles( + sideTitles: SideTitles( + showTitles: true, + interval: (yAxisMax - yAxisMin) / leftTicks.length, + getTitlesWidget: (value, meta) { + if (leftTicks.contains(value)) { + return Text(value.toStringAsFixed(0), style: const TextStyle(fontSize: 12, color: Colors.grey)); + } + return const SizedBox.shrink(); + }, + // 限制左侧标签宽度 + reservedSize: 40, + ), ), + bottomTitles: AxisTitles( + sideTitles: SideTitles( + showTitles: true, + interval: bottomInterval, + // 限制底部标签高度和宽度 + reservedSize: 30, + getTitlesWidget: (value, meta) { + int idx = value.toInt(); + if (idx >= 0 && idx < bottomLabels.length) { + return Container( + width: 40, + alignment: Alignment.center, + child: Text( + bottomLabels[idx], + style: const TextStyle(fontSize: 12, color: Colors.grey), + overflow: TextOverflow.ellipsis, + ), + ); + } + return const SizedBox.shrink(); + }, + ), + ), + rightTitles: const AxisTitles(sideTitles: SideTitles(showTitles: false)), + topTitles: const AxisTitles(sideTitles: SideTitles(showTitles: false)), ), - rightTitles: const AxisTitles(sideTitles: SideTitles(showTitles: false)), - topTitles: const AxisTitles(sideTitles: SideTitles(showTitles: false)), + borderData: FlBorderData(show: false), + lineBarsData: lines, + // 设置图表边距,避免内容贴边 + minX: 0, + maxX: bottomLabels.length - 1.toDouble(), + extraLinesData: ExtraLinesData(horizontalLines: []), ), - borderData: FlBorderData(show: false), - lineBarsData: lines, ), ), ), @@ -272,14 +307,16 @@ class _RunningStatusPageState extends State { } LineChartBarData _lineData(List data, Color color) { - // 替换 takeLast 为自定义的 _takeLast 方法 return LineChartBarData( - spots: _takeLast(data, 6), // 兼容低版本 Dart + spots: _takeLast(data, 6), isCurved: false, color: color, barWidth: 2, isStrokeCapRound: true, - dotData: FlDotData(show: true, getDotPainter: (spot, percent, barData, index) => FlDotCirclePainter(radius: 4, color: Colors.white, strokeWidth: 2)), + dotData: FlDotData( + show: true, + getDotPainter: (spot, percent, barData, index) => FlDotCirclePainter(radius: 4, color: color, strokeWidth: 2, strokeColor: Colors.white), + ), belowBarData: BarAreaData(show: false), ); } @@ -296,90 +333,248 @@ class _RunningStatusPageState extends State { padding: const EdgeInsets.all(16), child: Column( children: [ - // 电压仪表盘 + // 电压仪表盘/折线图 _chartCard( title: "电压 (V)", - child: _semiCircleGauge( - value: status.voltage, - maxValue: 250, - unit: "V", - label: "电压", - majorTicks: const [0, 25, 50, 75, 100, 125, 150, 175, 200, 225, 250], + child: Column( + children: [ + Row( + mainAxisAlignment: MainAxisAlignment.end, + children: [ + GestureDetector( + onTap: () => setState(() => _voltageGaugeMode = true), + child: Text( + "仪表盘", + style: TextStyle( + color: _voltageGaugeMode ? const Color(0xFF1677FF) : Colors.grey, + fontSize: 14, + fontWeight: _voltageGaugeMode ? FontWeight.bold : FontWeight.normal, + ), + ), + ), + const SizedBox(width: 16), + GestureDetector( + onTap: () => setState(() => _voltageGaugeMode = false), + child: Text( + "折线图", + style: TextStyle( + color: !_voltageGaugeMode ? const Color(0xFF1677FF) : Colors.grey, + fontSize: 14, + fontWeight: !_voltageGaugeMode ? FontWeight.bold : FontWeight.normal, + ), + ), + ), + ], + ), + const SizedBox(height: 16), + _voltageGaugeMode + ? _largeArcGauge( + value: status.voltage, + maxValue: 250, + unit: "V", + label: "电压", + majorTicks: const [0, 25, 50, 75, 100, 125, 150, 175, 200, 225, 250], + ) + : SizedBox( + height: 200, + child: _styledLineChart( + title: "电压", + lines: [_lineData(_voltageHistory, const Color(0xFF4CAF50))], + yAxisMax: 250, + leftTicks: const [0, 50, 100, 150, 200, 250], + ), + ), + ], ), ), _gap(), + // 芯片温度仪表盘/折线图 + _chartCard( + title: "芯片温度 (°C)", + child: Column( + children: [ + Row( + mainAxisAlignment: MainAxisAlignment.end, + children: [ + GestureDetector( + onTap: () => setState(() => _chipTempGaugeMode = true), + child: Text( + "仪表盘", + style: TextStyle( + color: _chipTempGaugeMode ? const Color(0xFF1677FF) : Colors.grey, + fontSize: 14, + fontWeight: _chipTempGaugeMode ? FontWeight.bold : FontWeight.normal, + ), + ), + ), + const SizedBox(width: 16), + GestureDetector( + onTap: () => setState(() => _chipTempGaugeMode = false), + child: Text( + "折线图", + style: TextStyle( + color: !_chipTempGaugeMode ? const Color(0xFF1677FF) : Colors.grey, + fontSize: 14, + fontWeight: !_chipTempGaugeMode ? FontWeight.bold : FontWeight.normal, + ), + ), + ), + ], + ), + const SizedBox(height: 16), + _chipTempGaugeMode + ? _largeArcGauge( + value: status.chipTemp, + maxValue: 100, + unit: "°C", + label: "芯片温度", + majorTicks: const [0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100], + ) + : SizedBox( + height: 200, + child: _styledLineChart( + title: "芯片温度", + lines: [_lineData(_chipTempHistory, const Color(0xFF4CAF50))], + yAxisMax: 100, + leftTicks: const [0, 20, 40, 60, 80, 100], + ), + ), + ], + ), + ), + _gap(), + + // 割刀速度仪表盘/折线图 + _chartCard( + title: "割刀速度 (rpm)", + child: Column( + children: [ + Row( + mainAxisAlignment: MainAxisAlignment.end, + children: [ + GestureDetector( + onTap: () => setState(() => _knifeSpeedGaugeMode = true), + child: Text( + "仪表盘", + style: TextStyle( + color: _knifeSpeedGaugeMode ? const Color(0xFF1677FF) : Colors.grey, + fontSize: 14, + fontWeight: _knifeSpeedGaugeMode ? FontWeight.bold : FontWeight.normal, + ), + ), + ), + const SizedBox(width: 16), + GestureDetector( + onTap: () => setState(() => _knifeSpeedGaugeMode = false), + child: Text( + "折线图", + style: TextStyle( + color: !_knifeSpeedGaugeMode ? const Color(0xFF1677FF) : Colors.grey, + fontSize: 14, + fontWeight: !_knifeSpeedGaugeMode ? FontWeight.bold : FontWeight.normal, + ), + ), + ), + ], + ), + const SizedBox(height: 16), + _knifeSpeedGaugeMode + ? _largeArcGauge( + value: double.tryParse(status.knifeCuttingSpeed) ?? 0, + maxValue: 3000, + unit: "rpm", + label: "割刀速度", + majorTicks: const [0, 500, 1000, 1500, 2000, 2500, 3000], + ) + : SizedBox( + height: 200, + child: _styledLineChart( + title: "割刀速度", + lines: [_lineData(_knifeHistory, const Color(0xFF4CAF50))], + yAxisMax: 3000, + yAxisMin: -3000, + leftTicks: const [-3000, -2000, -1000, 0, 1000, 2000, 3000], + ), + ), + ], + ), + ), + _gap(), + // 测量速度对比(左轮 + 右轮在一张图) + _chartCard( + title: "左右轮测量速度对比 (rpm)", + child: SizedBox( + height: 200, + child: _styledLineChart( + title: "左右轮测量速度对比", + lines: [ + _lineData(_leftMeasureHistory, const Color(0xFF3F51B5)), // 左轮 + _lineData(_rightMeasureHistory, const Color(0xFF8BC34A)), // 右轮 + ], + yAxisMax: 3000, + yAxisMin: -3000, + leftTicks: const [-3000, -2000, -1000, 0, 1000, 2000, 3000], + ), + ), + ), + + _gap(), + // 目标速度对比(左轮 + 右轮在一张图) + _chartCard( + title: "左右轮目标速度对比 (rpm)", + child: SizedBox( + height: 200, + child: _styledLineChart( + title: "左右轮目标速度对比", + lines: [ + _lineData(_leftTargetHistory, const Color(0xFF3F51B5)), // 左轮 + _lineData(_rightTargetHistory, const Color(0xFF8BC34A)), // 右轮 + ], + yAxisMax: 3000, + yAxisMin: -3000, + leftTicks: const [-3000, -2000, -1000, 0, 1000, 2000, 3000], + ), + ), + ), + + _gap(), + + // 电流对比 + _chartCard( + title: "电流对比 (A)", + child: SizedBox( + height: 200, + child: _styledLineChart( + title: "电流对比", + lines: [_lineData(_leftCurrentHistory, const Color(0xFF3F51B5)), _lineData(_rightCurrentHistory, const Color(0xFF8BC34A))], + yAxisMax: 100, + leftTicks: const [0, 20, 40, 60, 80, 100], + ), + ), + ), + _gap(), + + // 电机温度对比 + _chartCard( + title: "电机温度对比 (°C)", + child: SizedBox( + height: 200, + child: _styledLineChart( + title: "电机温度对比", + lines: [_lineData(_leftTempHistory, const Color(0xFF3F51B5)), _lineData(_rightTempHistory, const Color(0xFF8BC34A))], + yAxisMax: 100, + leftTicks: const [0, 20, 40, 60, 80, 100], + ), + ), + ), + // 航向角仪表盘 _chartCard( title: "航向角 (°)", child: _compassGauge(yaw: status.yaw), ), _gap(), - - // 芯片温度仪表盘 - _chartCard( - title: "芯片温度 (°C)", - child: _semiCircleGauge( - value: status.chipTemp, - maxValue: 100, - unit: "°C", - label: "芯片温度", - majorTicks: const [0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100], - ), - ), - _gap(), - - // 割刀速度折线图 - _chartCard( - title: "割刀速度 (rpm)", - child: _styledLineChart(title: "割刀速度", lines: [_lineData(_knifeHistory, const Color(0xFF4CAF50))], yAxisMax: 3000, yAxisMin: -3000), - ), - _gap(), - - // 电流对比折线图 - _chartCard( - title: "电流对比 (A)", - child: _styledLineChart( - title: "电流对比", - lines: [_lineData(_leftCurrentHistory, const Color(0xFF3F51B5)), _lineData(_rightCurrentHistory, const Color(0xFF8BC34A))], - yAxisMax: 100, - ), - ), - _gap(), - - // 电机温度对比折线图 - _chartCard( - title: "电机温度对比 (°C)", - child: _styledLineChart( - title: "电机温度对比", - lines: [_lineData(_leftTempHistory, const Color(0xFF3F51B5)), _lineData(_rightTempHistory, const Color(0xFF8BC34A))], - yAxisMax: 100, - ), - ), - _gap(), - - // 右轮速度对比折线图 - _chartCard( - title: "右轮速度对比 (rpm)", - child: _styledLineChart( - title: "右轮速度对比", - lines: [_lineData(_rightTargetHistory, const Color(0xFF3F51B5)), _lineData(_rightMeasureHistory, const Color(0xFF8BC34A))], - yAxisMax: 3000, - yAxisMin: -3000, - ), - ), - _gap(), - - // 左轮速度对比折线图 - _chartCard( - title: "左轮速度对比 (rpm)", - child: _styledLineChart( - title: "左轮速度对比", - lines: [_lineData(_leftTargetHistory, const Color(0xFF3F51B5)), _lineData(_leftMeasureHistory, const Color(0xFF8BC34A))], - yAxisMax: 3000, - yAxisMin: -3000, - ), - ), ], ), ); @@ -659,27 +854,27 @@ class _RunningStatusPageState extends State { } } -// ====================== 半圆仪表盘绘制器 ====================== -class SemiCircleGaugePainter extends CustomPainter { +// ====================== 大半个圆仪表盘绘制器 ====================== +class LargeArcGaugePainter extends CustomPainter { final double value; final double maxValue; final List majorTicks; - SemiCircleGaugePainter({required this.value, required this.maxValue, required this.majorTicks}); + LargeArcGaugePainter({required this.value, required this.maxValue, required this.majorTicks}); @override void paint(Canvas canvas, Size size) { final center = Offset(size.width / 2, size.height); final radius = size.width / 2 - 20; - // 背景弧 + // 背景弧(大半个圆,从 -135° 到 135°) final bgPaint = Paint() ..color = const Color(0xFFE3F2FD) ..style = PaintingStyle.stroke ..strokeWidth = 20 ..strokeCap = StrokeCap.round; - canvas.drawArc(Rect.fromCircle(center: center, radius: radius), math.pi, math.pi, false, bgPaint); + canvas.drawArc(Rect.fromCircle(center: center, radius: radius), -135 * math.pi / 180, 270 * math.pi / 180, false, bgPaint); // 进度弧 final progressPaint = Paint() @@ -688,15 +883,15 @@ class SemiCircleGaugePainter extends CustomPainter { ..strokeWidth = 20 ..strokeCap = StrokeCap.round; - canvas.drawArc(Rect.fromCircle(center: center, radius: radius), math.pi, math.pi * value, false, progressPaint); + canvas.drawArc(Rect.fromCircle(center: center, radius: radius), -135 * math.pi / 180, 270 * math.pi / 180 * value, false, progressPaint); // 刻度 final tickPaint = Paint() ..color = Colors.black54 ..strokeWidth = 2; - for (int i = 0; i <= 40; i++) { - final angle = math.pi + (math.pi / 40) * i; + for (int i = 0; i <= 45; i++) { + final angle = -135 * math.pi / 180 + (270 * math.pi / 180) * (i / 45); final x1 = center.dx + (radius - 15) * math.cos(angle); final y1 = center.dy + (radius - 15) * math.sin(angle); final x2 = center.dx + radius * math.cos(angle); @@ -704,13 +899,13 @@ class SemiCircleGaugePainter extends CustomPainter { canvas.drawLine(Offset(x1, y1), Offset(x2, y2), tickPaint); // 主刻度 - if (majorTicks.contains(i * (maxValue / 40))) { + if (majorTicks.contains(i * (maxValue / 45))) { final x3 = center.dx + (radius - 25) * math.cos(angle); final y3 = center.dy + (radius - 25) * math.sin(angle); canvas.drawLine(Offset(x1, y1), Offset(x3, y3), tickPaint..strokeWidth = 4); // 刻度值 - final textValue = (i * (maxValue / 40)).toStringAsFixed(0); + final textValue = (i * (maxValue / 45)).toStringAsFixed(0); final textPainter = TextPainter( text: TextSpan( text: textValue, @@ -724,16 +919,16 @@ class SemiCircleGaugePainter extends CustomPainter { } } - // 指针 - final needleAngle = math.pi + math.pi * value; + // 指针(修正方向) + final needleAngle = -135 * math.pi / 180 + 270 * math.pi / 180 * value; final needlePaint = Paint() ..color = const Color(0xFF90A4AE) ..style = PaintingStyle.fill; final needlePath = Path() ..moveTo(center.dx, center.dy) - ..lineTo(center.dx + radius * 0.8 * math.cos(needleAngle - 0.05), center.dy + radius * 0.8 * math.sin(needleAngle - 0.05)) - ..lineTo(center.dx + radius * 0.8 * math.cos(needleAngle + 0.05), center.dy + radius * 0.8 * math.sin(needleAngle + 0.05)) + ..lineTo(center.dx + radius * 0.8 * math.cos(needleAngle - 0.03), center.dy + radius * 0.8 * math.sin(needleAngle - 0.03)) + ..lineTo(center.dx + radius * 0.8 * math.cos(needleAngle + 0.03), center.dy + radius * 0.8 * math.sin(needleAngle + 0.03)) ..close(); canvas.drawPath(needlePath, needlePaint);