@@ -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+
124142class MplCanvas (FigureCanvasQTAgg ):
125143
126144 def __init__ (self , parent = None , width = 5 , height = 4 , dpi = 100 ):
0 commit comments