feat:增加统计图表模块
This commit is contained in:
@@ -54,8 +54,8 @@ class CalendarPageState extends State<CalendarPage> {
|
||||
void _showDialog(bool isEditing, Calendar? calendar) {
|
||||
final provider = Provider.of<CalendarProvider>(context, listen: false);
|
||||
|
||||
if (isEditing) {
|
||||
provider.initForm(calendar!);
|
||||
if (calendar != null) {
|
||||
provider.initForm(calendar);
|
||||
} else {
|
||||
provider.resetForm();
|
||||
}
|
||||
@@ -153,7 +153,7 @@ class CalendarPageState extends State<CalendarPage> {
|
||||
dataSource: AppointmentDataSource(_appointments),
|
||||
onAdd: () {
|
||||
if (_calendarController.view == CalendarView.week) {
|
||||
_showDialog(true, provider.formItem);
|
||||
_showDialog(false, provider.formItem);
|
||||
}
|
||||
},
|
||||
onEdit: (appointment) {
|
||||
|
||||
209
lib/pages/stats_page.dart
Normal file
209
lib/pages/stats_page.dart
Normal file
@@ -0,0 +1,209 @@
|
||||
import 'package:flisp_app/models/flisp.dart';
|
||||
import 'package:flisp_app/models/todo.dart';
|
||||
import 'package:flisp_app/service/flisp_service.dart';
|
||||
import 'package:flisp_app/service/todo_service.dart';
|
||||
import 'package:flisp_app/widgets/chart.dart';
|
||||
import 'package:flisp_app/widgets/common.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
class StatsPage extends StatefulWidget {
|
||||
const StatsPage({super.key});
|
||||
|
||||
@override
|
||||
StatsPageState createState() => StatsPageState();
|
||||
}
|
||||
|
||||
class StatsPageState extends State<StatsPage> {
|
||||
final double chartHeight = 400;
|
||||
final FlispService flispService = FlispService();
|
||||
final TodoService todoService = TodoService();
|
||||
|
||||
late List<ChartData> flispMonthlyStats;
|
||||
late List<ChartData> flispCategoryStats;
|
||||
late List<ChartData> todoMonthlyStats;
|
||||
late List<ChartData> todoMonthlyCompleteStats;
|
||||
late List<ChartData> todoPriorityStats;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
final allFlisps = flispService.getAllFlisps();
|
||||
getFlispMonthlyStats(allFlisps);
|
||||
getFlispCategoryStats(allFlisps);
|
||||
|
||||
final allTodos = todoService.getAllTodos();
|
||||
getTodoMonthlyStats(allTodos);
|
||||
getTodoMonthlyCompleteStats(allTodos);
|
||||
getTodoCategoryStats(allTodos);
|
||||
}
|
||||
|
||||
void getFlispMonthlyStats(List<Flisp> flisps) {
|
||||
Map<String, double> countMap = {};
|
||||
|
||||
for (var flisp in flisps) {
|
||||
String month = '${flisp.createTime.month}月';
|
||||
countMap[month] = (countMap[month] ?? 0) + 1;
|
||||
}
|
||||
|
||||
flispMonthlyStats = convert2ChartData(countMap);
|
||||
}
|
||||
|
||||
void getFlispCategoryStats(List<Flisp> flisps) {
|
||||
Map<String, double> countMap = {};
|
||||
|
||||
for (var flisp in flisps) {
|
||||
String category = flisp.tag.label;
|
||||
countMap[category] = (countMap[category] ?? 0) + 1;
|
||||
}
|
||||
|
||||
flispCategoryStats = convert2ChartData(countMap);
|
||||
}
|
||||
|
||||
void getTodoMonthlyStats(List<Todo> todos) {
|
||||
Map<String, double> countMap = {};
|
||||
|
||||
for (var todo in todos) {
|
||||
String month = '${todo.createTime.month}月';
|
||||
countMap[month] = (countMap[month] ?? 0) + 1;
|
||||
}
|
||||
|
||||
todoMonthlyStats = convert2ChartData(countMap);
|
||||
}
|
||||
|
||||
void getTodoMonthlyCompleteStats(List<Todo> todos) {
|
||||
Map<String, double> countMap = {};
|
||||
|
||||
for (var todo in todos) {
|
||||
if (todo.isCompleted) {
|
||||
String month = '${todo.createTime.month}月';
|
||||
countMap[month] = (countMap[month] ?? 0) + 1;
|
||||
}
|
||||
}
|
||||
|
||||
todoMonthlyCompleteStats = convert2ChartData(countMap);
|
||||
}
|
||||
|
||||
void getTodoCategoryStats(List<Todo> todos) {
|
||||
Map<String, double> countMap = {};
|
||||
|
||||
for (var todo in todos) {
|
||||
String priority = todo.priority.label;
|
||||
countMap[priority] = (countMap[priority] ?? 0) + 1;
|
||||
}
|
||||
|
||||
todoPriorityStats = convert2ChartData(countMap);
|
||||
}
|
||||
|
||||
List<ChartData> convert2ChartData(Map<String, double> countMap) {
|
||||
return countMap.entries
|
||||
.map((e) => ChartData(name: e.key, value: e.value))
|
||||
.toList();
|
||||
}
|
||||
|
||||
Widget _buildFlispMonthlyStats(BuildContext context) {
|
||||
return Column(
|
||||
children: [
|
||||
buildChartTitle(context, '每月闪灵数量统计'),
|
||||
const SizedBox(height: 3),
|
||||
buildChartDivider(context),
|
||||
const SizedBox(height: 3),
|
||||
Expanded(
|
||||
child: lineChart(
|
||||
context: context,
|
||||
xAxisName: '月份',
|
||||
yAxisName: '数量',
|
||||
unit: '篇',
|
||||
data: flispMonthlyStats,
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildFlispCategoryStats(BuildContext context) {
|
||||
return Column(
|
||||
children: [
|
||||
buildChartTitle(context, '闪灵分类统计'),
|
||||
const SizedBox(height: 3),
|
||||
buildChartDivider(context),
|
||||
const SizedBox(height: 3),
|
||||
Expanded(
|
||||
child: pieChart(
|
||||
context: context,
|
||||
unit: '篇',
|
||||
data: flispCategoryStats,
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildTodoMonthlyStats(BuildContext context) {
|
||||
return Column(
|
||||
children: [
|
||||
buildChartTitle(context, '每月待办数量统计'),
|
||||
const SizedBox(height: 3),
|
||||
buildChartDivider(context),
|
||||
const SizedBox(height: 3),
|
||||
Expanded(
|
||||
child: doubleBarChart(
|
||||
context: context,
|
||||
xAxisName: '月份',
|
||||
yAxisName: '数量',
|
||||
unit: '个',
|
||||
data1: todoMonthlyStats,
|
||||
data2: todoMonthlyCompleteStats,
|
||||
series1Name: '总数',
|
||||
series2Name: '完成数',
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildTodoPriorityStats(BuildContext context) {
|
||||
return Column(
|
||||
children: [
|
||||
buildChartTitle(context, '待办分类统计'),
|
||||
const SizedBox(height: 3),
|
||||
buildChartDivider(context),
|
||||
const SizedBox(height: 3),
|
||||
Expanded(
|
||||
child: pieChart(context: context, unit: '个', data: todoPriorityStats),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return SingleChildScrollView(
|
||||
child: Padding(
|
||||
padding: EdgeInsets.all(10),
|
||||
child: Column(
|
||||
children: [
|
||||
SizedBox(
|
||||
height: chartHeight,
|
||||
child: BuildCard(child: _buildFlispMonthlyStats(context)),
|
||||
),
|
||||
SizedBox(height: 10),
|
||||
SizedBox(
|
||||
height: chartHeight,
|
||||
child: BuildCard(child: _buildFlispCategoryStats(context)),
|
||||
),
|
||||
SizedBox(height: 10),
|
||||
SizedBox(
|
||||
height: chartHeight,
|
||||
child: BuildCard(child: _buildTodoMonthlyStats(context)),
|
||||
),
|
||||
SizedBox(height: 10),
|
||||
SizedBox(
|
||||
height: chartHeight,
|
||||
child: BuildCard(child: _buildTodoPriorityStats(context)),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user