diff --git a/src/budget/_stat.py b/src/budget/_stat.py index 6a352cd..09462ba 100644 --- a/src/budget/_stat.py +++ b/src/budget/_stat.py @@ -6,6 +6,7 @@ from PyQt6.QtGui import QDoubleValidator import datetime import json + class StatTab(TabPage): def __init__(self, parent): super().__init__() @@ -25,10 +26,11 @@ class StatTab(TabPage): # 饼状图 self.chart = QChart() - self.chart.setTitle('Month Statistics') self.chart.setAnimationOptions(QChart.AnimationOption.SeriesAnimations) chartLayout = QVBoxLayout() chartLayout.addWidget(QChartView(self.chart)) + # 关闭图例 + self.chart.legend().setVisible(False) leftLayout = QVBoxLayout() leftLayout.addLayout(buttonLayout) @@ -48,6 +50,9 @@ class StatTab(TabPage): statLayout = QHBoxLayout() statLayout.addLayout(leftLayout) statLayout.addLayout(calenderLayout) + # 设置左右空间占比为2:1 + statLayout.setStretchFactor(leftLayout, 2) + statLayout.setStretchFactor(calenderLayout, 1) self.setLayout(statLayout) def onOutButtonClicked(self): @@ -59,7 +64,38 @@ class StatTab(TabPage): self.selected() def onCalendarClicked(self, date): - pass + # 清除饼状图 + self.chart.removeAllSeries() + # 构建日期查询 + startTime = datetime.datetime(date.year(), date.month(), date.day(), 0, 0, 0) + endTime = datetime.datetime(date.year(), date.month(), date.day(), 23, 59, 59) + # 查询交易 + 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", \ + (startTime, endTime,)) + self.statData = self.pg.fetchall() + + # 获取此日所有交易类别 + self.pg.execute("SELECT c_id, meta->>'type', 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", \ + (startTime, endTime,)) + self.categoryData = self.pg.fetchall() + + # 填充饼状图 + self.chart.setTitle('Day Statistics') + self.chart.removeAllSeries() + series = QPieSeries() + + for i in range(len(self.statData)): + if (self.categoryData[i][1] == self.mode): + series.append(self.categoryData[i][2], self.statData[i][1]) + self.chart.addSeries(series) + + # 在饼状图内显示数额 + for slice in series.slices(): + slice.setLabelVisible(True) + slice.setLabel(slice.label()+" "+"{:.2f}%".format(100 * slice.percentage())) def onCalendarPageChanged(self, year, month): @@ -77,13 +113,19 @@ class StatTab(TabPage): self.categoryData = self.pg.fetchall() # 填充饼状图 + self.chart.setTitle('Month Statistics') self.chart.removeAllSeries() series = QPieSeries() for i in range(len(self.statData)): if (self.categoryData[i][1] == self.mode): series.append(self.categoryData[i][2], self.statData[i][1]) - self.chart.addSeries(series) + self.chart.addSeries(series) + + # 在饼状图内显示数额 + for slice in series.slices(): + slice.setLabelVisible(True) + slice.setLabel(slice.label()+" "+"{:.2f}%".format(100 * slice.percentage())) def selected(self): # 刷新图表