add first draft of progress page chart

feature/chart-progress-page
98spag 2023-05-31 13:36:44 +02:00
parent fcc607043e
commit 7a97f99496
3 changed files with 199 additions and 85 deletions

View File

@ -1 +1 @@
{"info":"This is a generated file; do not edit or check into version control.","plugins":{"ios":[{"name":"path_provider_foundation","path":"C:\\\\Users\\\\FUCHSLAU\\\\AppData\\\\Local\\\\Pub\\\\Cache\\\\hosted\\\\pub.dev\\\\path_provider_foundation-2.2.2\\\\","shared_darwin_source":true,"native_build":true,"dependencies":[]}],"android":[{"name":"path_provider_android","path":"C:\\\\Users\\\\FUCHSLAU\\\\AppData\\\\Local\\\\Pub\\\\Cache\\\\hosted\\\\pub.dev\\\\path_provider_android-2.0.27\\\\","native_build":true,"dependencies":[]}],"macos":[{"name":"path_provider_foundation","path":"C:\\\\Users\\\\FUCHSLAU\\\\AppData\\\\Local\\\\Pub\\\\Cache\\\\hosted\\\\pub.dev\\\\path_provider_foundation-2.2.2\\\\","shared_darwin_source":true,"native_build":true,"dependencies":[]}],"linux":[{"name":"path_provider_linux","path":"C:\\\\Users\\\\FUCHSLAU\\\\AppData\\\\Local\\\\Pub\\\\Cache\\\\hosted\\\\pub.dev\\\\path_provider_linux-2.1.10\\\\","native_build":false,"dependencies":[]}],"windows":[{"name":"path_provider_windows","path":"C:\\\\Users\\\\FUCHSLAU\\\\AppData\\\\Local\\\\Pub\\\\Cache\\\\hosted\\\\pub.dev\\\\path_provider_windows-2.1.6\\\\","native_build":false,"dependencies":[]}],"web":[]},"dependencyGraph":[{"name":"path_provider","dependencies":["path_provider_android","path_provider_foundation","path_provider_linux","path_provider_windows"]},{"name":"path_provider_android","dependencies":[]},{"name":"path_provider_foundation","dependencies":[]},{"name":"path_provider_linux","dependencies":[]},{"name":"path_provider_windows","dependencies":[]}],"date_created":"2023-05-30 20:39:45.634550","version":"3.10.2"}
{"info":"This is a generated file; do not edit or check into version control.","plugins":{"ios":[{"name":"path_provider_foundation","path":"C:\\\\Users\\\\FUCHSLAU\\\\AppData\\\\Local\\\\Pub\\\\Cache\\\\hosted\\\\pub.dev\\\\path_provider_foundation-2.2.2\\\\","shared_darwin_source":true,"native_build":true,"dependencies":[]}],"android":[{"name":"path_provider_android","path":"C:\\\\Users\\\\FUCHSLAU\\\\AppData\\\\Local\\\\Pub\\\\Cache\\\\hosted\\\\pub.dev\\\\path_provider_android-2.0.27\\\\","native_build":true,"dependencies":[]}],"macos":[{"name":"path_provider_foundation","path":"C:\\\\Users\\\\FUCHSLAU\\\\AppData\\\\Local\\\\Pub\\\\Cache\\\\hosted\\\\pub.dev\\\\path_provider_foundation-2.2.2\\\\","shared_darwin_source":true,"native_build":true,"dependencies":[]}],"linux":[{"name":"path_provider_linux","path":"C:\\\\Users\\\\FUCHSLAU\\\\AppData\\\\Local\\\\Pub\\\\Cache\\\\hosted\\\\pub.dev\\\\path_provider_linux-2.1.10\\\\","native_build":false,"dependencies":[]}],"windows":[{"name":"path_provider_windows","path":"C:\\\\Users\\\\FUCHSLAU\\\\AppData\\\\Local\\\\Pub\\\\Cache\\\\hosted\\\\pub.dev\\\\path_provider_windows-2.1.6\\\\","native_build":false,"dependencies":[]}],"web":[]},"dependencyGraph":[{"name":"path_provider","dependencies":["path_provider_android","path_provider_foundation","path_provider_linux","path_provider_windows"]},{"name":"path_provider_android","dependencies":[]},{"name":"path_provider_foundation","dependencies":[]},{"name":"path_provider_linux","dependencies":[]},{"name":"path_provider_windows","dependencies":[]}],"date_created":"2023-05-30 22:47:26.759130","version":"3.10.2"}

View File

@ -1,7 +1,9 @@
import 'dart:math';
import 'package:ernaehrung/android/components/meal_page_text/days_component.dart';
import 'package:ernaehrung/android/config/cast_helper.dart';
import 'package:fl_chart/fl_chart.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:hive/hive.dart';
import '../models/food.dart';
import 'format_helper.dart';
@ -16,7 +18,6 @@ class StatisticsService {
StatisticsService._internal() {
initBoxes();
}
final String reducedStatisticsBoxName = 'STATISTICS_REDUCED';
final String mainStatisticsBoxName = 'STATISTICS_MAIN';
final String progressStatisticsBoxName = 'STATISTICS_PROGRESS';
@ -24,8 +25,7 @@ class StatisticsService {
ValueNotifier<List<double>> ingredients = ValueNotifier<List<double>>([0,0,0]);
ValueNotifier<int> dailyAverageForCurrentWeek = ValueNotifier<int>(0);
ValueNotifier<List<Food>> weeklyCaloryRanking = ValueNotifier<List<Food>>([]);
ValueNotifier<List<BarChartGroupData>> barChartData = ValueNotifier<List<BarChartGroupData>>([]);
initBoxes()async{
Box reducedBox = Hive.box(reducedStatisticsBoxName);
@ -223,6 +223,8 @@ class StatisticsService {
dailyAverageForCurrentWeek.notifyListeners();
weeklyCaloryRanking.value = getWeeklyCaloryRanking();
weeklyCaloryRanking.notifyListeners();
barChartData.value = getBarChartData();
barChartData.notifyListeners();
}
void updateProgressBoxValues(){
@ -258,10 +260,170 @@ class StatisticsService {
return getListOfDistinctElements(allFoodsOfWeek);
}
int getSumOfCaloriesByFoodList(List<Food> foods){
int sum = 0;
for(int i = 0; i < foods.length;i++){
sum += foods[i].calories as int;
}
return sum;
}
List<BarChartGroupData> getBarChartData(){
int timestamp = getTimestampFromNow();
List<int> currentWeek = getTimestampsByTimestampAndTimespan(TimeSpan.week,timestamp);
int maxValue = 0;
List<List<int>> result =
[
[0,0,0],
[0,0,0],
[0,0,0],
[0,0,0],
[0,0,0],
[0,0,0],
[0,0,0],
]; //[[breakfast,lunch,dinner],[breakfast,lunch,dinner] ...]
for(int i = 0;i < currentWeek.length;i++){
Map<String,List<Food>> foodMapFromMainBoxByTimestamp = getFoodMapForGivenTimestampFromMainBox(currentWeek[i]);
Iterable<String> keys = foodMapFromMainBoxByTimestamp.keys;
if(keys.isNotEmpty){
for(int j = 0; j < keys.length;j++){
int value = getSumOfCaloriesByFoodList(foodMapFromMainBoxByTimestamp[keys.elementAt(j)] ?? []);
if(value > maxValue){
maxValue = value;
}
result[i][j] = (value * 175) ~/ 100;
}
}
}
return [
BarChartGroupData(
x: 0,
barRods: [BarChartRodData(
width: 7.5, // Adjust the width of the bar if needed
color: Colors.yellow,
toY: result[0][0].toDouble(), // First segment color
), BarChartRodData(
width: 7.5, // Adjust the width of the bar if needed
color: Colors.red,
toY: result[0][1].toDouble(), // Second segment color
), BarChartRodData(
width: 7.5, // Adjust the width of the bar if needed
color: Colors.green,
toY: result[0][2].toDouble(), // Second segment color
),],
showingTooltipIndicators: [0],
groupVertically: true
),
BarChartGroupData(
x: 1,
barRods: [BarChartRodData(
width: 7.5, // Adjust the width of the bar if needed
color: Colors.yellow,
toY: result[1][0].toDouble(),
// First segment color
), BarChartRodData(
width: 7.5, // Adjust the width of the bar if needed
color: Colors.red,
toY: result[1][1].toDouble(), // Second segment color
), BarChartRodData(
width: 7.5, // Adjust the width of the bar if needed
color: Colors.green,
toY: result[1][2].toDouble(), // Second segment color
),],
showingTooltipIndicators: [0],
),
BarChartGroupData(
x: 2,
barRods: [BarChartRodData(
width: 7.5, // Adjust the width of the bar if needed
color: Colors.yellow,
toY: result[2][0].toDouble(), // First segment color
), BarChartRodData(
width: 7.5, // Adjust the width of the bar if needed
color: Colors.red,
toY: result[2][1].toDouble(), // Second segment color
), BarChartRodData(
width: 7.5, // Adjust the width of the bar if needed
color: Colors.green,
toY: result[2][2].toDouble(), // Second segment color
),],
showingTooltipIndicators: [0],
),
BarChartGroupData(
x: 3,
barRods: [BarChartRodData(
width: 7.5, // Adjust the width of the bar if needed
color: Colors.yellow,
toY: result[3][0].toDouble(), // First segment color
), BarChartRodData(
width: 7.5, // Adjust the width of the bar if needed
color: Colors.red,
toY: result[3][1].toDouble(), // Second segment color
), BarChartRodData(
width: 7.5, // Adjust the width of the bar if needed
color: Colors.green,
toY: result[3][2].toDouble(), // Second segment color
),],
showingTooltipIndicators: [0],
),
BarChartGroupData(
x: 4,
barRods: [BarChartRodData(
width: 7.5, // Adjust the width of the bar if needed
color: Colors.yellow,
toY: result[4][0].toDouble(), // First segment color
), BarChartRodData(
width: 7.5, // Adjust the width of the bar if needed
color: Colors.red,
toY: result[4][1].toDouble(), // Second segment color
), BarChartRodData(
width: 7.5, // Adjust the width of the bar if needed
color: Colors.green,
toY: result[4][2].toDouble(), // Second segment color
),],
showingTooltipIndicators: [0],
),
BarChartGroupData(
x: 5,
barRods: [BarChartRodData(
width: 7.5, // Adjust the width of the bar if needed
color: Colors.yellow,
toY: result[5][0].toDouble(), // First segment color
), BarChartRodData(
width: 7.5, // Adjust the width of the bar if needed
color: Colors.red,
toY: result[5][1].toDouble(), // Second segment color
), BarChartRodData(
width: 7.5, // Adjust the width of the bar if needed
color: Colors.green,
toY: result[5][2].toDouble(), // Second segment color
),], showingTooltipIndicators: [0],
),
BarChartGroupData(
x: 6,
barRods: [BarChartRodData(
width: 7.5, // Adjust the width of the bar if needed
color: Colors.yellow,
toY: result[6][0].toDouble(), // First segment color
), BarChartRodData(
width: 7.5, // Adjust the width of the bar if needed
color: Colors.red,
toY: result[6][1].toDouble(), // Second segment color
), BarChartRodData(
width: 7.5, // Adjust the width of the bar if needed
color: Colors.green,
toY: result[6][2].toDouble(), // Second segment color
),], showingTooltipIndicators: [0],
),
];
}
getTimestampFromNow(){
DateTime now = DateTime.now();
return now.millisecondsSinceEpoch.toInt() ~/ 1000;
}
showItems(){
print("Statistics.dart - showItems() - ITEMS");
//Hive.box(boxName).clear();
@ -273,4 +435,6 @@ class StatisticsService {
}
}
}

View File

@ -35,7 +35,8 @@ class ProgressPage extends StatelessWidget {
valueListenable: StatisticsService.instance.dailyAverageForCurrentWeek,
builder: (context, value, child) {
return Container(
height: 284,
height: 100,
width: 400,
decoration: BoxDecoration(
border: Border.all(),
),
@ -48,6 +49,19 @@ class ProgressPage extends StatelessWidget {
),
const SecondaryTextComponent("Durchschnittlich"),
SecondaryBigTextComponent(value.toString()),
],
),
);
},
),
ValueListenableBuilder(
valueListenable: StatisticsService.instance.barChartData,
builder: (context, value, child) {
return SizedBox(
height: 205,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
SizedBox(
height: 200,
child: BarChart(
@ -55,7 +69,7 @@ class ProgressPage extends StatelessWidget {
barTouchData: barTouchData,
titlesData: titlesData,
borderData: borderData,
barGroups: barGroups,
barGroups: value,
gridData: FlGridData(show: false),
alignment: BarChartAlignment.spaceAround,
maxY: 200,
@ -127,25 +141,25 @@ class ProgressPage extends StatelessWidget {
String text;
switch (value.toInt()) {
case 0:
text = 'Mn';
text = 'M';
break;
case 1:
text = 'Te';
text = 'T';
break;
case 2:
text = 'Wd';
text = 'W';
break;
case 3:
text = 'Tu';
text = 'T';
break;
case 4:
text = 'Fr';
text = 'F';
break;
case 5:
text = 'St';
text = 'S';
break;
case 6:
text = 'Sn';
text = 'S';
break;
default:
text = '';
@ -190,77 +204,13 @@ class ProgressPage extends StatelessWidget {
begin: Alignment.bottomCenter,
end: Alignment.topCenter,
);
LinearGradient get _breakfastGradient => const LinearGradient(
colors: [
Colors.green,
Colors.yellow,
],
begin: Alignment.bottomCenter,
end: Alignment.topCenter,
);
List<BarChartGroupData> get barGroups => [
BarChartGroupData(
x: 0,
barRods: [
BarChartRodData(
toY: 8,
gradient: _barsGradient,
)
],
showingTooltipIndicators: [0],
),
BarChartGroupData(
x: 1,
barRods: [
BarChartRodData(
toY: 10,
gradient: _barsGradient,
)
],
showingTooltipIndicators: [0],
),
BarChartGroupData(
x: 2,
barRods: [
BarChartRodData(
toY: 14,
gradient: _barsGradient,
)
],
showingTooltipIndicators: [0],
),
BarChartGroupData(
x: 3,
barRods: [
BarChartRodData(
toY: 150,
gradient: _barsGradient,
)
],
showingTooltipIndicators: [0],
),
BarChartGroupData(
x: 4,
barRods: [
BarChartRodData(
toY: 13,
gradient: _barsGradient,
)
],
showingTooltipIndicators: [0],
),
BarChartGroupData(
x: 5,
barRods: [
BarChartRodData(
toY: 10,
gradient: _barsGradient,
)
],
showingTooltipIndicators: [0],
),
BarChartGroupData(
x: 6,
barRods: [
BarChartRodData(
toY: 16,
gradient: _barsGradient,
)
],
showingTooltipIndicators: [0],
),
];
}