Newer
Older
import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart';
import 'package:fl_chart/fl_chart.dart';
import 'package:scrobbles/config/app_colors.dart';
import 'package:scrobbles/models/counts_by_day.dart';
import 'package:scrobbles/ui/widgets/abstracts/custom_bar_chart.dart';
class ChartCountsByDay extends CustomBarChart {
const ChartCountsByDay({super.key, required this.chartData});
final double verticalTicksInterval = 5;
final String verticalAxisTitleSuffix = '%';
@override
Widget build(BuildContext context) {
if (this.chartData.data.keys.length == 0) {
return SizedBox(
height: this.chartHeight,
);
}
child: LayoutBuilder(
builder: (context, constraints) {
final double maxWidth = constraints.maxWidth;
final int barsCount = this.chartData.data.keys.length;
return getBarChart(
barWidth: this.getBarWidth(maxWidth, barsCount),
backgroundColor: Theme.of(context).colorScheme.onSurface,
);
},
),
);
}
double getMaxCountsValue() {
double maxValue = 0;
this.chartData.data.keys.forEach((key) {
double? counts = this.chartData.data[key];
if (counts != null) {
if (counts > maxValue) {
maxValue = counts;
}
}
});
return maxValue;
}
Color getColorFromIndex(int index) {
Color barColor = AppColors.contentColorBlack;
switch (index) {
case 1:
barColor = Color.fromARGB(255, 255, 99, 132);
break;
case 2:
barColor = Color.fromARGB(255, 255, 159, 64);
break;
case 3:
barColor = Color.fromARGB(255, 255, 205, 86);
break;
case 4:
barColor = Color.fromARGB(255, 75, 192, 192);
break;
case 5:
barColor = Color.fromARGB(255, 54, 162, 235);
break;
case 6:
barColor = Color.fromARGB(255, 153, 102, 255);
break;
case 7:
barColor = Color.fromARGB(255, 201, 203, 207);
break;
default:
}
return barColor;
List<BarChartGroupData> getDataCounts(double barWidth) {
List<BarChartGroupData> data = [];
this.chartData.data.keys.forEach((day) {
final double? counts = this.chartData.data[day];
if (counts != null) {
final Color barColor = this.getColorFromIndex(day);
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
));
}
});
return data;
}
Widget getHorizontalTitlesWidget(double value, TitleMeta meta) {
final int day = value.toInt();
String dayShortName = '';
switch (day) {
case 1:
dayShortName = 'MON';
break;
case 2:
dayShortName = 'TUE';
break;
case 3:
dayShortName = 'WED';
break;
case 4:
dayShortName = 'THU';
break;
case 5:
dayShortName = 'FRI';
break;
case 6:
dayShortName = 'SAT';
break;
case 7:
dayShortName = 'SUN';
break;
default:
}
return SideTitleWidget(
axisSide: meta.axisSide,
space: 2,
child: Text(
style: const TextStyle(
color: AppColors.mainTextColor1,
fontSize: 11,
),
),
);
}
}