Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import 'package:easy_localization/easy_localization.dart';
import 'package:fl_chart/fl_chart.dart';
import 'package:flutter/material.dart';
import 'package:scrobbles/config/app_colors.dart';
import 'package:scrobbles/models/heatmap.dart';
import 'package:scrobbles/utils/color_extensions.dart';
class ChartHeatmap extends StatelessWidget {
final HeatmapData chartData;
const ChartHeatmap({super.key, required this.chartData});
final double chartHeight = 150.0;
final double titleFontSize = 9;
final Color baseColor = AppColors.contentColorPink;
final double scale = 8.0;
final double darkenAmount = 50;
@override
Widget build(BuildContext context) {
if (this.chartData.data.keys.length == 0) {
return SizedBox(
height: this.chartHeight,
);
}
return AspectRatio(
aspectRatio: 3,
child: ScatterChart(
ScatterChartData(
scatterSpots: getSpots(),
minX: 0,
maxX: 24,
minY: 0,
maxY: 7,
borderData: FlBorderData(show: false),
gridData: FlGridData(show: false),
titlesData: getTitlesData(),
scatterTouchData: ScatterTouchData(enabled: false),
),
),
);
}
Color getColorFromNormalizedValue(double value) {
return baseColor.darken(1 + (darkenAmount * (1 - value)).toInt());
}
int getMaxCount() {
int maxValue = 0;
this.chartData.data.forEach((day, hours) {
hours.forEach((hour, count) {
if (count > maxValue) {
maxValue = count;
}
});
});
return maxValue;
}
List<ScatterSpot> getSpots() {
List<ScatterSpot> spots = [];
final int maxCount = getMaxCount();
this.chartData.data.forEach((day, hours) {
hours.forEach((hour, count) {
double normalizedValue = count / maxCount;
spots.add(ScatterSpot(
hour.toDouble(),
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
color: getColorFromNormalizedValue(normalizedValue),
radius: scale * normalizedValue,
));
});
});
return spots;
}
FlTitlesData getTitlesData() {
const AxisTitles none = const AxisTitles(
sideTitles: SideTitles(showTitles: false),
);
final AxisTitles verticalTitles = AxisTitles(
sideTitles: SideTitles(
showTitles: true,
reservedSize: 40,
getTitlesWidget: getVerticalTitlesWidget,
interval: 1,
),
);
final AxisTitles horizontalTitles = AxisTitles(
sideTitles: SideTitles(
showTitles: true,
reservedSize: 20,
getTitlesWidget: getHorizontalTitlesWidget,
interval: 4,
),
);
return FlTitlesData(
show: true,
bottomTitles: horizontalTitles,
leftTitles: verticalTitles,
topTitles: none,
rightTitles: none,
);
}
Widget getVerticalTitlesWidget(double value, TitleMeta meta) {
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
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: 10,
child: Text(
tr(dayShortName),
style: TextStyle(
color: AppColors.mainTextColor1,
fontSize: this.titleFontSize,
),
),
);
}
Widget getHorizontalTitlesWidget(double value, TitleMeta meta) {
String text = '';
if (value % 4 == 0 || value == 23) {
text = value.toInt().toString().padLeft(2, '0');
}
return SideTitleWidget(
axisSide: meta.axisSide,
space: 2,
child: Text(
text,
style: TextStyle(
color: AppColors.mainTextColor1,
fontSize: this.titleFontSize,
),
),
);
}
}