90 lines
3.1 KiB
Dart
90 lines
3.1 KiB
Dart
|
import 'package:flutter/material.dart';
|
||
|
import 'package:fl_chart/fl_chart.dart';
|
||
|
|
||
|
class ChartPart extends StatelessWidget {
|
||
|
final List<List<Color>> groupColors; // 接收每个组的颜色
|
||
|
|
||
|
ChartPart({required this.groupColors}); // 构造函数
|
||
|
|
||
|
@override
|
||
|
Widget build(BuildContext context) {
|
||
|
return OrientationBuilder(builder: (context, orientation) {
|
||
|
final size = MediaQuery.of(context).size;
|
||
|
|
||
|
// 使用静态示例数据来绘制图表框架
|
||
|
List<BarChartGroupData> sampleData = [];
|
||
|
for (int i = 0; i < groupColors.length; i++) {
|
||
|
// 确保每个组至少有两个颜色
|
||
|
if (groupColors[i].isNotEmpty &&
|
||
|
groupColors[i][0] != Colors.transparent) {
|
||
|
sampleData.add(
|
||
|
BarChartGroupData(
|
||
|
x: i,
|
||
|
barRods: [
|
||
|
BarChartRodData(
|
||
|
toY: (4 + i).toDouble(), color: groupColors[i][0]),
|
||
|
],
|
||
|
),
|
||
|
);
|
||
|
}
|
||
|
}
|
||
|
return OrientationBuilder(
|
||
|
builder: (context, orientation) {
|
||
|
return SizedBox(
|
||
|
width: size.width,
|
||
|
child: Padding(
|
||
|
padding: const EdgeInsets.all(10),
|
||
|
child: BarChart(
|
||
|
BarChartData(
|
||
|
maxY: 10,
|
||
|
barTouchData: BarTouchData(enabled: false),
|
||
|
titlesData: FlTitlesData(
|
||
|
show: true,
|
||
|
bottomTitles: AxisTitles(
|
||
|
sideTitles: SideTitles(
|
||
|
showTitles: true,
|
||
|
reservedSize: 42,
|
||
|
getTitlesWidget: (value, meta) {
|
||
|
// 根据方向设置标题
|
||
|
return Text(orientation == Orientation.portrait
|
||
|
? 'Group $value'
|
||
|
: 'Group $value'); // 可以根据需要调整
|
||
|
},
|
||
|
),
|
||
|
),
|
||
|
topTitles: const AxisTitles(
|
||
|
sideTitles: SideTitles(
|
||
|
showTitles: false,
|
||
|
),
|
||
|
),
|
||
|
rightTitles: const AxisTitles(
|
||
|
sideTitles: SideTitles(
|
||
|
showTitles: false,
|
||
|
),
|
||
|
),
|
||
|
leftTitles: AxisTitles(
|
||
|
sideTitles: SideTitles(
|
||
|
showTitles: true,
|
||
|
reservedSize: 40,
|
||
|
getTitlesWidget: (value, meta) {
|
||
|
return RotatedBox(
|
||
|
quarterTurns: 0, // 旋转 270 度
|
||
|
child: Text(value.toString()),
|
||
|
);
|
||
|
},
|
||
|
),
|
||
|
),
|
||
|
),
|
||
|
borderData: FlBorderData(show: false),
|
||
|
barGroups: sampleData,
|
||
|
alignment: BarChartAlignment.spaceAround,
|
||
|
),
|
||
|
),
|
||
|
),
|
||
|
);
|
||
|
},
|
||
|
);
|
||
|
});
|
||
|
}
|
||
|
}
|