feat: 统计页
This commit is contained in:
parent
917e08cb8a
commit
4ddb661f0e
1 changed files with 68 additions and 3 deletions
|
@ -1,7 +1,9 @@
|
|||
from PyQt6.QtWidgets import *
|
||||
from PyQt6.QtCharts import *
|
||||
from ._tab import TabPage
|
||||
from ._pg import PostgresTab
|
||||
from PyQt6.QtGui import QDoubleValidator
|
||||
import datetime
|
||||
import json
|
||||
|
||||
class StatTab(TabPage):
|
||||
|
@ -9,10 +11,73 @@ class StatTab(TabPage):
|
|||
super().__init__()
|
||||
self.initUI()
|
||||
self.pg = parent.pg
|
||||
self.rows = []
|
||||
|
||||
def initUI(self):
|
||||
# 按钮
|
||||
self.outButton = QPushButton('Expense')
|
||||
self.inButton = QPushButton('Income')
|
||||
buttonLayout = QHBoxLayout()
|
||||
buttonLayout.addWidget(self.outButton)
|
||||
buttonLayout.addWidget(self.inButton)
|
||||
|
||||
# 饼状图
|
||||
self.chart = QChart()
|
||||
self.chart.setTitle('Month Statistics')
|
||||
self.chart.setAnimationOptions(QChart.AnimationOption.SeriesAnimations)
|
||||
chartLayout = QVBoxLayout()
|
||||
chartLayout.addWidget(QChartView(self.chart))
|
||||
|
||||
leftLayout = QVBoxLayout()
|
||||
leftLayout.addLayout(buttonLayout)
|
||||
leftLayout.addLayout(chartLayout)
|
||||
|
||||
# 日历控件
|
||||
self.calendar = QCalendarWidget()
|
||||
self.calendar.setGridVisible(True)
|
||||
# 日历点击事件
|
||||
self.calendar.clicked.connect(self.onCalendarClicked)
|
||||
# 切换月份事件
|
||||
self.calendar.currentPageChanged.connect(self.onCalendarPageChanged)
|
||||
|
||||
calenderLayout = QHBoxLayout()
|
||||
calenderLayout.addWidget(self.calendar)
|
||||
|
||||
statLayout = QHBoxLayout()
|
||||
statLayout.addLayout(leftLayout)
|
||||
statLayout.addLayout(calenderLayout)
|
||||
self.setLayout(statLayout)
|
||||
|
||||
|
||||
|
||||
def onCalendarClicked(self, date):
|
||||
pass
|
||||
|
||||
def onXXXClicked(self):
|
||||
pass
|
||||
|
||||
def onCalendarPageChanged(self, year, month):
|
||||
print(year, month)
|
||||
|
||||
# 合计分类的交易量
|
||||
self.pg.execute("SELECT c_id, sum(amount)::numeric::float8 FROM transaction WHERE c_id IS NOT NULL and \
|
||||
time BETWEEN %s AND %s GROUP BY c_id ORDER BY c_id", \
|
||||
(datetime.date(year, month, 1), datetime.date(year, month+1, 1),))
|
||||
self.statData = self.pg.fetchall()
|
||||
print(self.statData)
|
||||
|
||||
# 获取此月所有交易类别
|
||||
self.pg.execute("SELECT c_id, meta->>'name' FROM category \
|
||||
WHERE c_id in (SELECT c_id FROM transaction WHERE time between %s and %s GROUP BY c_id) \
|
||||
ORDER BY c_id", \
|
||||
(datetime.date(year, month, 1), datetime.date(year, month+1, 1),))
|
||||
self.categoryData = self.pg.fetchall()
|
||||
print(self.categoryData)
|
||||
|
||||
# 填充饼状图
|
||||
self.chart.removeAllSeries()
|
||||
series = QPieSeries()
|
||||
for i in range(len(self.statData)):
|
||||
series.append(self.categoryData[i][1], self.statData[i][1])
|
||||
self.chart.addSeries(series)
|
||||
|
||||
def selected(self):
|
||||
# 刷新图表
|
||||
self.onCalendarPageChanged(self.calendar.yearShown(), self.calendar.monthShown())
|
||||
|
|
Loading…
Reference in a new issue