Skip to content

Commit 3b928d9

Browse files
committed
perf(battery_history): fix incorrect x ticks
1 parent 4bd7766 commit 3b928d9

File tree

3 files changed

+49
-19
lines changed

3 files changed

+49
-19
lines changed

app/components/battery_history_card.py

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -73,39 +73,52 @@ def calculate_x_ticks(self, unix_timestamp):
7373
return timestamp_xx00, timestamp_xx30
7474

7575
def updateBatteryHistory(self, battery_history_dic):
76+
self.battery_history_dic = battery_history_dic
7677
system_theme = str(cfg.get(cfg.themeMode))
7778
background_color = self.background_color_dic[system_theme]
7879
front_color = self.front_color_dic[system_theme]
7980
self.graphWidget.axes.set_facecolor(background_color)
8081
self.graphWidget.figure.patch.set_facecolor(background_color)
8182

82-
x_list = [int(x / 60) for x in battery_history_dic["time"]]
83+
x_list = [int(x) for x in battery_history_dic["time"]]
8384
y_labels_list = [10 * x for x in range(11)]
8485
battery_history_time_list = []
8586
x_ticks = []
86-
for x, y, charging, time in zip(x_list, battery_history_dic["battery"], battery_history_dic["charging"], battery_history_dic["time"]):
87+
stride = 15
88+
previous_timestamp = 0
89+
i = 0
90+
linewidth = min(5, self.width() / (len(x_list)) * 3)
91+
for x, y, charging in zip(x_list, battery_history_dic["battery"], battery_history_dic["charging"]):
8792
# vertical lines with color indicating charging status
88-
if charging:
89-
# green
90-
color = (0 / 255, 255 / 255, 54 / 255)
91-
else:
92-
# blue
93-
color = (53 / 255, 193 / 255, 241 / 255)
94-
self.graphWidget.axes.axvline(x=x, color=color, linestyle='-', linewidth=1, ymax=y / 100)
93+
# if consecutive records less than `stride`, then ignore
94+
if (x - previous_timestamp <= 20 and i % stride == 0) or x - previous_timestamp > 20:
95+
i += 1
96+
if charging:
97+
# green
98+
color = (0 / 255, 255 / 255, 54 / 255)
99+
else:
100+
# blue
101+
color = (53 / 255, 193 / 255, 241 / 255)
102+
self.graphWidget.axes.axvline(x=x, color=color, linestyle='-', linewidth=linewidth, ymin=0.05, ymax=y / 100, solid_capstyle='round')
103+
elif x - previous_timestamp <= 20 and i % stride != 0:
104+
i += 1
105+
106+
previous_timestamp = x
95107

96108
# calculate x ticks
97-
timestamp_xx00, timestamp_xx30 = self.calculate_x_ticks(time)
98-
if timestamp_xx00 not in battery_history_dic["time"]:
109+
timestamp_xx00, timestamp_xx30 = self.calculate_x_ticks(x)
110+
if timestamp_xx00 not in x_ticks:
99111
battery_history_time_list.append(self.convert_time(timestamp_xx00))
100112
battery_history_time_list.append(self.convert_time(timestamp_xx30))
101-
x_ticks.append(int(timestamp_xx00 / 60))
102-
x_ticks.append(int(timestamp_xx30 / 60))
103-
self.graphWidget.axes.plot(x_list, battery_history_dic["battery"], color=front_color)
113+
x_ticks.append(int(timestamp_xx00))
114+
x_ticks.append(int(timestamp_xx30))
104115

105116
self.graphWidget.axes.set_xticks(x_ticks)
106117
self.graphWidget.axes.set_xticklabels(battery_history_time_list, rotation=45, ha='right', color=front_color)
107118

108119
self.graphWidget.axes.set_ylim(0, 100)
120+
padding = 1800
121+
self.graphWidget.axes.set_xlim(x_list[-1] - padding, x_list[0] + padding)
109122

110123
# Customize y-axis tick labels.
111124
self.graphWidget.axes.set_yticks(range(0, 101, 10))
@@ -121,6 +134,11 @@ def updateBatteryHistory(self, battery_history_dic):
121134

122135
self.graphWidget.draw()
123136

137+
def resizeEvent(self, event):
138+
# Call your custom function when the window is resized
139+
self.updateBatteryHistory(self.battery_history_dic)
140+
event.accept()
141+
124142
class MplCanvas(FigureCanvasQTAgg):
125143

126144
def __init__(self, parent=None, width=5, height=4, dpi=100):

app/view/main_window.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,16 @@ def __init__(self, router):
4141
def update_monitoring_status(self):
4242
self.homeInterface.update_monitoring_status()
4343
self.monitoringStatusInterface.update_monitoring_status()
44-
self.batteryHistoryInterface.update_monitoring_status()
4544

4645
def update_traffic_statistics(self):
4746
self.homeInterface.update_traffic_statistics()
4847

4948
def update_month_statistics(self):
5049
self.homeInterface.update_month_statistics()
5150

51+
def update_battery_history(self):
52+
self.batteryHistoryInterface.update_monitoring_status()
53+
5254
def initLayout(self):
5355
signalBus.switchToSampleCard.connect(self.switchToSample)
5456

main.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,35 +84,45 @@ def show_window(button):
8484

8585
w.show()
8686

87-
async def update():
87+
def update():
8888
router.update_monitoring_status()
8989
icon = QIcon(router.get_battery_icon_path())
9090
tray.setIcon(icon)
9191
if w.isVisible():
9292
w.update_monitoring_status()
9393
w.update_month_statistics()
9494

95-
async def update_traffic_statistics():
95+
def update_traffic_statistics():
9696
if w.isVisible():
9797
router.update_traffic_statistics()
9898
w.update_traffic_statistics()
9999

100+
def update_battery_history():
101+
if w.isVisible():
102+
w.update_battery_history()
103+
100104
async def main():
101105
while True:
102-
await update()
106+
update()
103107
await asyncio.sleep(15) # Run update every 15 seconds
104108

105109
async def traffic_statistics_main():
106110
while True:
107-
await update_traffic_statistics()
111+
update_traffic_statistics()
108112
await asyncio.sleep(1) # Run update_traffic_statistics every 1 second
109113

114+
async def battery_history_main():
115+
while True:
116+
update_battery_history()
117+
await asyncio.sleep(15 * 60) # run every 15 mins
118+
110119
# Start the main and traffic statistics tasks
111120
loop = QEventLoop(app)
112121
asyncio.set_event_loop(loop)
113122

114123
loop.create_task(main())
115124
loop.create_task(traffic_statistics_main())
125+
loop.create_task(battery_history_main())
116126

117127
# Start the application event loop
118128
with loop:

0 commit comments

Comments
 (0)