Skip to content

Commit

Permalink
Update graph.dart
Browse files Browse the repository at this point in the history
Updated Graphs (Weekly, Monthly and yearly)
  • Loading branch information
arafaatqureshi authored Sep 22, 2023
1 parent abc4bc3 commit c9299de
Showing 1 changed file with 203 additions and 129 deletions.
332 changes: 203 additions & 129 deletions lib/pages/graph.dart
Original file line number Diff line number Diff line change
@@ -1,183 +1,257 @@
import 'package:flutter/material.dart';
import 'package:flutter_svg/svg.dart';
import 'package:hexcolor/hexcolor.dart';
import 'package:intl/intl.dart';
import 'package:syncfusion_flutter_charts/charts.dart';
import 'package:intl/intl.dart';
import '../db/Notes_database.dart';

import 'Settings.dart';
List<int> weekdatesData= [0,0,0,0,0,0,0];
var average = 0.0;
class Graph extends StatefulWidget {
const Graph({Key? key}) : super(key: key);

@override
State<Graph> createState() => _GraphState();
}

class _GraphState extends State<Graph> {
class _GraphState extends State<Graph> with TickerProviderStateMixin{
late TooltipBehavior _tooltipBehavior;


late List<int> weekdatesData;
late List<int> monthDatesData;
late List<int> yearDatesData;
late double average;
late String dateRange;
late TabController _tabController;

@override
void initState(){
_tooltipBehavior = TooltipBehavior(enable: true);
void initState() {
super.initState();
_tooltipBehavior = TooltipBehavior(enable: true);
weekdatesData = List<int>.filled(7, 0);
monthDatesData = List<int>.filled(30, 0); // Assuming 30 days for a month
yearDatesData = List<int>.filled(12, 0); // Assuming 12 months for a year
average = 0.0;
dateRange = calculateCurrentWeekDateRange();
_tabController = TabController(length: 3, vsync: this);
getDatesData();
_tabController.addListener(_handleTabSelection); // Listen for tab selection
}
@override
void dispose() {
_tabController.dispose(); // Dispose the tab controller
super.dispose();
}
Future<void> getDatesData() async {
print('loadinggg........');

List<String> previousWeekDates = getPreviousWeekDates();
var avgCount = 0;

for (int i = 0; i < previousWeekDates.length; i++) {
int noteCount = await NotesDatabase.instance.getDocumentCountByDate(previousWeekDates[i]);
print("Count for ${previousWeekDates[i]}: $noteCount");
weekdatesData[i] = noteCount;
avgCount += noteCount;
print("fayes: " + noteCount.toString());
}

// Calculate monthly and yearly data similarly

setState(() {
average = avgCount / 7;
});
}
void _handleTabSelection() {
setState(() {
// Update the data and date range based on the selected tab
if (_tabController.index == 0) {
dateRange = calculateCurrentWeekDateRange();
// Load weekly data here
} else if (_tabController.index == 1) {
dateRange = calculateCurrentMonthDateRange();
// Load monthly data here
} else if (_tabController.index == 2) {
dateRange = calculateCurrentYearDateRange();
// Load yearly data here
}
});
}

List<String> getPreviousWeekDates() {
List<String> previousWeekDates = [];

// Get the current date
DateTime currentDate = DateTime.now();

// Calculate the starting date of the previous week (6 days ago from the current date)
DateTime previousWeekStartDate = currentDate.subtract(Duration(days: 6));

// Iterate through the previous week's dates and add them to the list
for (int i = 0; i < 7; i++) {
// Calculate each date by adding 'i' days to the previous week's starting date
DateTime date = previousWeekStartDate.add(Duration(days: i));

// Format the date in the desired pattern 'July 17, 2023' and add it to the list
String formattedDate = DateFormat('MMMM d, y').format(date);
previousWeekDates.add(formattedDate);
}

return previousWeekDates;
}

getDatesData() async {
print('loadinggg........');

List<String> previousWeekDates = getPreviousWeekDates();
var avgCount=0;
for (int i = 0; i < previousWeekDates.length; i++) {
int noteCount = await NotesDatabase.instance.getDocumentCountByDate(previousWeekDates[i]);
print("Count for ${previousWeekDates[i]}: $noteCount");
weekdatesData[i] = noteCount;
avgCount = avgCount+noteCount;
print("fayes: "+noteCount.toString());
}
setState(() {
weekdatesData;
average = avgCount/7;
String calculateCurrentWeekDateRange() {
DateTime currentDate = DateTime.now();
DateTime firstDayOfWeek = currentDate.subtract(Duration(days: currentDate.weekday - 1));
DateTime lastDayOfWeek = firstDayOfWeek.add(Duration(days: 6));
String formattedFirstDay = DateFormat('d MMMM').format(firstDayOfWeek);
String formattedLastDay = DateFormat('d MMMM').format(lastDayOfWeek);
return '$formattedFirstDay - $formattedLastDay';
}

});
String calculateCurrentMonthDateRange() {
DateTime currentDate = DateTime.now();
DateTime firstDayOfMonth = DateTime(currentDate.year, currentDate.month, 1);
DateTime lastDayOfMonth = DateTime(currentDate.year, currentDate.month + 1, 0);
String formattedFirstDay = DateFormat('d MMMM').format(firstDayOfMonth);
String formattedLastDay = DateFormat('d MMMM').format(lastDayOfMonth);
return '$formattedFirstDay - $formattedLastDay';
}

String calculateCurrentYearDateRange() {
DateTime currentDate = DateTime.now();
DateTime firstDayOfYear = DateTime(currentDate.year, 1, 1);
DateTime lastDayOfYear = DateTime(currentDate.year, 12, 31);
String formattedFirstDay = DateFormat('d MMMM').format(firstDayOfYear);
String formattedLastDay = DateFormat('d MMMM').format(lastDayOfYear);
return '$formattedFirstDay - $formattedLastDay';
}
@override
Widget build(BuildContext context) {

// ... Rest of your code for fetching and displaying data ...

final List<ChartData> chartData = [
ChartData('M', weekdatesData[0]),
ChartData('TU', weekdatesData[1]),
ChartData('W', weekdatesData[2]),
ChartData('TH', weekdatesData[3]),
ChartData('F', weekdatesData[4]),
ChartData('ST', weekdatesData[5]),
ChartData('SN', weekdatesData[6]),

];

return
SafeArea(
child: Scaffold(
body: Column(

children:[
Container(
margin: EdgeInsets.all(20),
child: SizedBox(
height: 50,
child: Row(
children: [

Expanded(
child: OutlinedButton(
onPressed: (){
Navigator.push(context,
MaterialPageRoute(builder: (context) => Settings()),
);

},
child: Center(child: Text('W'))
@override
Widget build(BuildContext context) {
List<ChartData> chartData;

if (_tabController.index == 0) {
chartData = weekdatesData
.asMap()
.entries
.map((entry) => ChartData('Day ${entry.key}', entry.value))
.toList();
} else if (_tabController.index == 1) {
chartData = monthDatesData
.asMap()
.entries
.map((entry) => ChartData('Day ${entry.key}', entry.value))
.toList();
} else {
chartData = yearDatesData
.asMap()
.entries
.map((entry) => ChartData('Month ${entry.key}', entry.value))
.toList();
}

return SafeArea(
child: Scaffold(
body: Column(
children: [
SizedBox(height: 19,),
TabBar(
controller: _tabController, // Connect the TabBar to the TabController
indicator: BoxDecoration(), // Set an empty BoxDecoration for the indicator
tabs: [
Tab(
child: Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(10),
border: Border.all(color: Colors.grey),
),
child: Center(
child: Text(
'W',
style: TextStyle(
color: Colors.blue,
),
),
Expanded(
child: OutlinedButton(onPressed: (){

}, child: Center(child: Text('M'))),
),
Expanded(
child: OutlinedButton(onPressed: (){

}, child: Center(child: Text('Y'))),
),
),
),
Tab(
child: Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(10),
border: Border.all(color: Colors.grey),
),
child: Center(
child: Text(
'M',
style: TextStyle(
color: Colors.blue,
),
),

],
),
),
),
),
Container(
child: Row(
children: [
SizedBox(width: 20,),
Text(double.parse(average.toStringAsFixed(1)).toString(),style: TextStyle(fontSize: 25,fontWeight: FontWeight. bold)),
SizedBox(width: 8,),
Text('fucks given on average',style: TextStyle(fontSize: 20)),
],
Tab(
child: Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(10),
border: Border.all(color: Colors.grey),
),
child: Center(
child: Text(
'Y',
style: TextStyle(
color: Colors.blue,
),
),
),
),
),
],
),
// Your UI widgets here

Container(
margin: EdgeInsets.all(20),
child: SizedBox(
height: 20,

),
SizedBox(
height:5,
),
Container(
child: Row(
children: [
SizedBox(width: 20,),
Text('5 - 12 September',style: TextStyle(fontSize: 17)),
],
),
),
Container(
child: Row(
children: [
SizedBox(width: 20,),
Text(average.toStringAsFixed(1), style: TextStyle(fontSize: 25, fontWeight: FontWeight.bold)),
SizedBox(width: 8,),
Text('fucks given on average', style: TextStyle(fontSize: 20)),
],
),
SizedBox(
height:5,
),
SizedBox(height: 5,),
Container(
child: Row(
children: [
SizedBox(width: 20,),
Text(dateRange, style: TextStyle(fontSize: 17)),
],
),
Container(
child: SfCartesianChart(
primaryXAxis: CategoryAxis(),
series: <CartesianSeries>[
LineSeries<ChartData, String>(
color:HexColor("#29A331"),
dataSource: chartData,
xValueMapper: (ChartData data, _) => data.x,
yValueMapper: (ChartData data, _) => data.y,
markerSettings: MarkerSettings(
isVisible: true,

shape: DataMarkerType.circle

)
)
]
)
),
SizedBox(height: 5,),
Container(
child: SfCartesianChart(
primaryXAxis: CategoryAxis(),
series: <ChartSeries>[
LineSeries<ChartData, String>(
color: Colors.blue, // Change the color as needed
dataSource: chartData,
xValueMapper: (ChartData data, _) => data.x,
yValueMapper: (ChartData data, _) => data.y!,
markerSettings: MarkerSettings(
isVisible: true,
shape: DataMarkerType.circle,
),
)





],
),

],
),
),
],
),
);
),
);
}
}

class ChartData {
ChartData(this.x, this.y);
final String x;
Expand Down

0 comments on commit c9299de

Please sign in to comment.