Files
tuiche/lib/pages/sleep_report/chart/ScatterPlotChart.dart
2026-03-14 10:44:28 +08:00

262 lines
8.2 KiB
Dart

import 'package:fl_chart/fl_chart.dart';
import 'package:flutter/material.dart';
import 'package:vbvs_app/common/util/FitTool.dart';
import 'package:vbvs_app/common/util/MyUtils.dart';
class ScatterPlotChart extends StatelessWidget {
final List<ScatterSpot> points;
final double xMin;
final double xMax;
final double yMin;
final double yMax;
final Color pointColor;
ScatterPlotChart({
required this.points,
required this.xMin,
required this.xMax,
required this.yMin,
required this.yMax,
required this.pointColor,
});
@override
Widget build(BuildContext context) {
const double interval = 400; // 默认间隔
return SizedBox(
child: ScatterChart(
ScatterChartData(
backgroundColor: Colors.transparent,
gridData: FlGridData(
show: true,
horizontalInterval: interval,
verticalInterval: interval,
getDrawingHorizontalLine: (value) {
return FlLine(
color: themeController.currentColor.sc4,
strokeWidth: 1.rpx,
dashArray: [5, 5], // 设置虚线 [实线长度, 空白长度]
);
},
getDrawingVerticalLine: (value) {
return FlLine(
color: themeController.currentColor.sc4,
strokeWidth: 1.rpx,
dashArray: [5, 5], // 设置虚线
);
},
),
titlesData: FlTitlesData(
show: true,
leftTitles: AxisTitles(
sideTitles: SideTitles(
showTitles: true,
reservedSize: 70.rpx,
interval: 400, // 强制 400 一个刻度
getTitlesWidget: (double value, TitleMeta meta) {
// 只显示 400 的倍数
if (value % 400 != 0) return Container();
return Padding(
padding: EdgeInsets.only(right: 14.rpx),
child: Text(
value.toStringAsFixed(0),
style: TextStyle(
fontSize: 18.rpx,
color: themeController.currentColor.sc4,
),
maxLines: 1,
overflow: TextOverflow.ellipsis,
textAlign: TextAlign.center,
),
);
},
),
),
bottomTitles: AxisTitles(
sideTitles: SideTitles(
showTitles: true,
getTitlesWidget: (double value, TitleMeta meta) {
// 只显示 400 的倍数
if (value % 400 != 0) return Container();
return Text(
value.toStringAsFixed(0),
style: TextStyle(
fontSize: 18.rpx,
color: themeController.currentColor.sc4,
),
);
},
interval: 400, // 强制间隔 400
),
),
rightTitles: AxisTitles(
sideTitles: SideTitles(showTitles: false),
),
topTitles: AxisTitles(
sideTitles: SideTitles(showTitles: false),
),
),
borderData: FlBorderData(
show: true,
border: Border.all(
color: themeController.currentColor.sc4,
width: 0.5,
),
),
scatterSpots: points.map((point) {
return ScatterSpot(
point.x,
point.y,
dotPainter: FlDotCirclePainter(
radius: 3.rpx,
color: pointColor,
),
);
}).toList(),
minX: xMin,
maxX: xMax,
minY: yMin,
maxY: yMax,
),
),
);
}
}
// import 'dart:math' as math;
// import 'package:fl_chart/fl_chart.dart';
// import 'package:flutter/material.dart';
// import 'package:vbvs_app/common/util/FitTool.dart';
// import 'package:vbvs_app/common/util/MyUtils.dart';
// class ScatterPlotChart extends StatelessWidget {
// final List<ScatterSpot> points;
// final double xMin;
// final double xMax;
// final double yMin;
// final double yMax;
// final Color pointColor;
// ScatterPlotChart({
// required this.points,
// required this.xMin,
// required this.xMax,
// required this.yMin,
// required this.yMax,
// required this.pointColor,
// });
// // 计算固定6格的间隔
// double _calculateInterval(double min, double max) {
// double range = max - min;
// if (range <= 0) return 100; // 默认值
// // 固定分成6格
// return range / 6;
// }
// @override
// Widget build(BuildContext context) {
// double xInterval = _calculateInterval(xMin, xMax);
// double yInterval = _calculateInterval(yMin, yMax);
// return SizedBox(
// child: ScatterChart(
// ScatterChartData(
// backgroundColor: Colors.transparent,
// gridData: FlGridData(
// show: true,
// horizontalInterval: yInterval,
// verticalInterval: xInterval,
// getDrawingHorizontalLine: (value) {
// return FlLine(
// color: themeController.currentColor.sc4,
// strokeWidth: 1.rpx,
// dashArray: [5, 5], // 设置虚线 [实线长度, 空白长度]
// );
// },
// getDrawingVerticalLine: (value) {
// return FlLine(
// color: themeController.currentColor.sc4,
// strokeWidth: 1.rpx,
// dashArray: [5, 5], // 设置虚线
// );
// },
// ),
// titlesData: FlTitlesData(
// show: true,
// leftTitles: AxisTitles(
// sideTitles: SideTitles(
// showTitles: true,
// reservedSize: 70.rpx,
// interval: yInterval,
// getTitlesWidget: (double value, TitleMeta meta) {
// // 显示所有刻度值
// return Padding(
// padding: EdgeInsets.only(right: 14.rpx),
// child: Text(
// value.toStringAsFixed(0),
// style: TextStyle(
// fontSize: 18.rpx,
// color: themeController.currentColor.sc4,
// ),
// maxLines: 1,
// overflow: TextOverflow.ellipsis,
// textAlign: TextAlign.center,
// ),
// );
// },
// ),
// ),
// bottomTitles: AxisTitles(
// sideTitles: SideTitles(
// showTitles: true,
// interval: xInterval,
// getTitlesWidget: (double value, TitleMeta meta) {
// return Text(
// value.toStringAsFixed(0),
// style: TextStyle(
// fontSize: 18.rpx,
// color: themeController.currentColor.sc4,
// ),
// );
// },
// ),
// ),
// rightTitles: AxisTitles(
// sideTitles: SideTitles(showTitles: false),
// ),
// topTitles: AxisTitles(
// sideTitles: SideTitles(showTitles: false),
// ),
// ),
// borderData: FlBorderData(
// show: true,
// border: Border.all(
// color: themeController.currentColor.sc4,
// width: 0.5,
// ),
// ),
// scatterSpots: points.map((point) {
// return ScatterSpot(
// point.x,
// point.y,
// dotPainter: FlDotCirclePainter(
// radius: 3.rpx,
// color: pointColor,
// ),
// );
// }).toList(),
// minX: xMin,
// maxX: xMax,
// minY: yMin,
// maxY: yMax,
// ),
// ),
// );
// }
// }