127 lines
4.3 KiB
Dart
127 lines
4.3 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 int xMax;
|
|
final int yMax;
|
|
final Color pointColor;
|
|
final int divisions;
|
|
|
|
ScatterPlotChart({
|
|
required this.points,
|
|
required this.xMax,
|
|
required this.yMax,
|
|
required this.pointColor,
|
|
required this.divisions,
|
|
});
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
// 计算向上取整后的最大值
|
|
// double xMaxCeil = (xMax / divisions).ceil() * divisions.toDouble();
|
|
double temp = (xMax / divisions).ceil().toDouble(); // 计算向上取整后的每个分区的最大值
|
|
double xMaxCeil = (((temp / 100).ceil()) * 100 * (divisions - 1))
|
|
.toDouble(); // 向百取整并乘以 divisions
|
|
double tempy = (yMax / divisions).ceil().toDouble(); // 计算向上取整后的每个分区的最大值
|
|
double yMaxCeil =
|
|
(((tempy / 100).ceil()) * 100 * (divisions - 1)).toDouble();
|
|
|
|
return SizedBox(
|
|
child: ScatterChart(
|
|
ScatterChartData(
|
|
backgroundColor: Colors.transparent,
|
|
gridData: FlGridData(
|
|
show: true,
|
|
horizontalInterval: yMaxCeil / divisions,
|
|
verticalInterval: xMaxCeil / divisions,
|
|
getDrawingHorizontalLine: (value) {
|
|
return FlLine(
|
|
color: themeController.currentColor.sc4, // 设置网格线颜色
|
|
strokeWidth: 0.5,
|
|
);
|
|
},
|
|
getDrawingVerticalLine: (value) {
|
|
return FlLine(
|
|
color: themeController.currentColor.sc4, // 设置网格线颜色
|
|
strokeWidth: 0.5,
|
|
);
|
|
},
|
|
),
|
|
titlesData: FlTitlesData(
|
|
show: true,
|
|
leftTitles: AxisTitles(
|
|
sideTitles: SideTitles(
|
|
showTitles: true,
|
|
reservedSize: 60.rpx, // 给 y 轴标签更多空间
|
|
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,
|
|
getTitlesWidget: (double value, TitleMeta meta) {
|
|
return Text(
|
|
value.toStringAsFixed(0),
|
|
style: TextStyle(
|
|
fontSize: 18.rpx,
|
|
color: themeController.currentColor.sc4,
|
|
),
|
|
maxLines: 1,
|
|
overflow: TextOverflow.ellipsis,
|
|
textAlign: TextAlign.center, // 右对齐
|
|
);
|
|
},
|
|
),
|
|
),
|
|
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, // x 坐标
|
|
point.y, // y 坐标
|
|
dotPainter: FlDotCirclePainter(
|
|
radius: 3.rpx, // 自定义大小
|
|
color: pointColor, // 自定义颜色
|
|
),
|
|
);
|
|
}).toList(),
|
|
minX: 0,
|
|
maxX: xMaxCeil,
|
|
minY: 0,
|
|
maxY: yMaxCeil,
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|