KonectarApp/lib/widgets/piechart.dart

231 lines
8.2 KiB
Dart
Raw Permalink Normal View History

2024-09-06 06:30:31 +00:00
import 'package:fl_chart/fl_chart.dart';
import 'package:flutter/material.dart';
2024-10-07 12:45:45 +00:00
import 'package:konectar_events/model/specialtymodel.dart';
2024-09-06 06:30:31 +00:00
import 'package:konectar_events/utils/appcolors.dart';
import 'package:konectar_events/widgets/indicators.dart';
class CustomPieChart extends StatefulWidget {
2024-10-07 12:45:45 +00:00
List<Specialty> specialtyList;
CustomPieChart({super.key, required this.specialtyList});
2024-09-06 06:30:31 +00:00
@override
State<StatefulWidget> createState() => CustomPieChartState();
}
2024-10-07 12:45:45 +00:00
class CustomPieChartState extends State<CustomPieChart> {
2024-09-06 06:30:31 +00:00
int touchedIndex = -1;
@override
Widget build(BuildContext context) {
return AspectRatio(
aspectRatio: 1.3,
child: Column(
children: <Widget>[
const SizedBox(
height: 18,
),
Expanded(
child: AspectRatio(
aspectRatio: 1.9,
child: PieChart(
PieChartData(
pieTouchData: PieTouchData(
touchCallback: (FlTouchEvent event, pieTouchResponse) {
setState(() {
if (!event.isInterestedForInteractions ||
pieTouchResponse == null ||
pieTouchResponse.touchedSection == null) {
touchedIndex = -1;
return;
}
touchedIndex = pieTouchResponse
.touchedSection!.touchedSectionIndex;
});
},
),
borderData: FlBorderData(
show: false,
),
sectionsSpace: 0,
centerSpaceRadius: 60,
2024-10-07 12:45:45 +00:00
sections: showingSections(widget.specialtyList),
2024-09-06 06:30:31 +00:00
),
),
),
),
2024-10-07 12:45:45 +00:00
// SizedBox(
// height: 5,
// ),
2024-09-06 06:30:31 +00:00
Padding(
2024-10-07 12:45:45 +00:00
padding: const EdgeInsets.only(top: 8.0, bottom: 8.0, left: 8.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: List.generate(widget.specialtyList.length, (i) {
return Indicator(
size: 10,
color: AppColors().appcolors[i],
text: widget.specialtyList[i].specialtyName,
isSquare: true,
);
})
// <Widget>[
// Indicator(
// size: 8,
// color: AppColors.contentColorBlue,
// text: 'Medical Oncology',
// isSquare: true,
// ),
// SizedBox(
// height: 2,
// ),
// Indicator(
// size: 8,
// color: AppColors.contentColorYellow,
// text: 'Hematology/Oncology',
// isSquare: true,
// ),
// SizedBox(
// height: 2,
// ),
// Indicator(
// size: 8,
// color: AppColors.contentColorPurple,
// text: 'Internal Medicine',
// isSquare: true,
// ),
// SizedBox(
// height: 2,
// ),
// Indicator(
// size: 8,
// color: AppColors.contentColorGreen,
// text: 'Radiation Oncology',
// isSquare: true,
// ),
// SizedBox(
// height: 2,
// ),
// Indicator(
// size: 8,
// color: AppColors.contentColorOrange,
// text: 'Pediatric Hematology/Oncology',
// isSquare: true,
// ),
// SizedBox(
// height: 5,
// ),
// ],
2024-09-06 06:30:31 +00:00
),
),
const SizedBox(
width: 9,
),
],
),
);
}
2024-10-07 12:45:45 +00:00
List<PieChartSectionData> showingSections(List<Specialty> specialtyList) {
double total = 0.0;
for (var obj in specialtyList) {
total += double.parse(obj.specialtyCount);
}
return List.generate(specialtyList.length, (i) {
2024-09-06 06:30:31 +00:00
final isTouched = i == touchedIndex;
2024-10-07 12:45:45 +00:00
final fontSize = isTouched ? 14.0 : 12.0;
2024-09-06 06:30:31 +00:00
final radius = isTouched ? 60.0 : 50.0;
const shadows = [Shadow(color: Colors.black, blurRadius: 2)];
2024-10-07 12:45:45 +00:00
return PieChartSectionData(
color: AppColors().appcolors[i],
value: ((double.parse(specialtyList[i].specialtyCount) / total) * 100)
.roundToDouble(),
title:
'${((double.parse(specialtyList[i].specialtyCount) / total) * 100).roundToDouble()}%',
radius: radius,
titleStyle: TextStyle(
fontSize: fontSize,
fontWeight: FontWeight.bold,
color: AppColors.mainTextColor1,
shadows: shadows,
),
);
// switch (i) {
// case 0:
// return PieChartSectionData(
// color: AppColors.contentColorBlue,
// value:
// (double.parse(specialtyList[i].specialtyCount) * total) / 100,
// title:
// '${(double.parse(specialtyList[i].specialtyCount) * total) / 100}%',
// radius: radius,
// titleStyle: TextStyle(
// fontSize: fontSize,
// fontWeight: FontWeight.bold,
// color: AppColors.mainTextColor1,
// shadows: shadows,
// ),
// );
// case 1:
// return PieChartSectionData(
// color: AppColors.contentColorYellow,
// value: double.parse(specialtyList[i].specialtyCount) / 100,
// title: '${double.parse(specialtyList[i].specialtyCount) / 100}%',
// radius: radius,
// titleStyle: TextStyle(
// fontSize: fontSize,
// fontWeight: FontWeight.bold,
// color: AppColors.mainTextColor1,
// shadows: shadows,
// ),
// );
// case 2:
// return PieChartSectionData(
// color: AppColors.contentColorPurple,
// value: double.parse(specialtyList[i].specialtyCount) / 100,
// title: '${double.parse(specialtyList[i].specialtyCount) / 100}%',
// radius: radius,
// titleStyle: TextStyle(
// fontSize: fontSize,
// fontWeight: FontWeight.bold,
// color: AppColors.mainTextColor1,
// shadows: shadows,
// ),
// );
// case 3:
// return PieChartSectionData(
// color: AppColors.contentColorGreen,
// value: double.parse(specialtyList[i].specialtyCount) / 100,
// title: '${double.parse(specialtyList[i].specialtyCount) / 100}%',
// radius: radius,
// titleStyle: TextStyle(
// fontSize: fontSize,
// fontWeight: FontWeight.bold,
// color: AppColors.mainTextColor1,
// shadows: shadows,
// ),
// );
// case 4:
// return PieChartSectionData(
// color: AppColors.contentColorOrange,
// value: double.parse(specialtyList[i].specialtyCount) / 100,
// title: '${double.parse(specialtyList[i].specialtyCount)}%',
// radius: radius,
// titleStyle: TextStyle(
// fontSize: fontSize,
// fontWeight: FontWeight.bold,
// color: AppColors.mainTextColor1,
// shadows: shadows,
// ),
// );
2024-09-06 06:30:31 +00:00
2024-10-07 12:45:45 +00:00
// default:
// throw Error();
// }
2024-09-06 06:30:31 +00:00
});
}
}