-
Notifications
You must be signed in to change notification settings - Fork 0
/
plotter.py
341 lines (274 loc) · 11.1 KB
/
plotter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
import os.path
from datetime import datetime, timedelta
import matplotlib.pyplot as plt
from matplotlib.dates import DateFormatter
from matplotlib.font_manager import fontManager, FontProperties
from matplotlib.offsetbox import OffsetImage, AnnotationBbox
from custom_logger import Logger
from database import DatabaseReader
from gwaff.database_events import DatabaseEvents
from utils import request_img
logger = Logger('gwaff.plotter')
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
PRIMARY_FONT_PATH = os.path.join(BASE_DIR, 'assets/gg sans Semibold.ttf')
EMOJI_FONT_PATH = os.path.join(BASE_DIR, 'assets/NotoEmoji-Regular.ttf')
fontManager.addfont(PRIMARY_FONT_PATH) # gg sans
fontManager.addfont(EMOJI_FONT_PATH) # Noto Emoji
p_font = FontProperties(fname=PRIMARY_FONT_PATH)
e_font = FontProperties(fname=EMOJI_FONT_PATH)
fonts = [FontProperties(fname=PRIMARY_FONT_PATH).get_name(),
FontProperties(fname=EMOJI_FONT_PATH).get_name()]
plt.rcParams['font.family'] = 'sans-serif'
plt.rcParams['font.sans-serif'] = fonts + plt.rcParams['font.sans-serif']
WINDOW_SIZE = (15, 7)
GAP_MIN_SIZE = 3
GRAPH_DEFAULT_USERS = 15
GRAPH_SEPERATOR = 0.03
GRAPH_IMAGE_WIDTH = 0.018
RANK_DEFAULT_THRESHOLD = 30
class Colours:
"""
A class to hold color constants used in the plot.
"""
missing = '#505050'
text = '#FFFFFF'
outside = '#2B2D31'
inside = '#313338'
highlight = '#3D3F46'
class ResponsiveDateFormat:
"""
A class to determine the date format based on the range of dates.
Attributes:
start_date (datetime): The start date of the range.
end_date (datetime): The end date of the range.
difference (int): The difference in days between start_date and end_date.
formatter (DateFormatter): The date formatter based on the difference.
"""
def __init__(self, start_date, end_date):
"""
Initializes the ResponsiveDateFormat with start and end dates.
Args:
start_date (datetime): The start date of the range.
end_date (datetime): The end date of the range.
"""
self.start_date = start_date
self.end_date = end_date
self.difference = abs((end_date - start_date).days)
if self.difference <= 3:
self.formatter = DateFormatter('%d %H')
elif self.difference <= 180:
self.formatter = DateFormatter('%y-%m-%d')
elif self.difference <= 365:
self.formatter = DateFormatter('%Y-%m')
else:
self.formatter = DateFormatter('%Y-%m')
def __str__(self):
"""
Returns a string representation of the date format.
Returns:
str: The date format as a string.
"""
if self.difference <= 3:
return 'DD HH AEST'
elif self.difference <= 7:
return 'YY-MM-DD AEST'
elif self.difference <= 180:
return 'YY-MM-DD'
elif self.difference <= 365:
return 'YYYY-MM'
else:
return 'YYYY-MM'
class Plotter:
"""
A class to create and manage plots of XP data over time.
Attributes:
fig (Figure): The matplotlib figure object.
ax (Axes): The matplotlib axes object.
active_threshold (int): The threshold for active users.
annotations (list): A list to store annotations.
start_date (datetime): The start date for the plot.
end_date (datetime): The end date for the plot.
special (bool): A flag for special plots.
title (str): The title of the plot.
"""
def __init__(self,
start_date: datetime = None,
end_date: datetime = None,
active_threshold: int = RANK_DEFAULT_THRESHOLD,
special: bool = False,
title: str = "XP Over Time"):
"""
Initializes the Plotter with optional parameters.
Args:
start_date (datetime, optional): The start date for the plot. Defaults to None.
end_date (datetime, optional): The end date for the plot. Defaults to None.
active_threshold (int, optional): The threshold for active users. Defaults to RANK_DEFAULT_THRESHOLD.
special (bool, optional): A flag for special plots. Defaults to False.
title (str, optional): The title of the plot. Defaults to "XP Over Time".
"""
self.fig, self.ax = plt.subplots(figsize=WINDOW_SIZE)
self.active_threshold = active_threshold
self.annotations = []
self.start_date = start_date
self.end_date = end_date
self.special = special
self.title = title
self.max_xp = 0
self.min_xp = 0
def get_data(self, limit: int, include: set[int] = None) -> list:
"""
Retrieves data from the database within the specified range.
Args:
limit (int): The maximum number of data points to retrieve.
include (list[int], optional): A list of user IDs to include. Defaults to None.
Returns:
list: The data retrieved from the database.
"""
dbr = DatabaseReader()
return dbr.get_data_in_range(self.start_date, self.end_date, limit=limit, include=include)
def draw(self, limit: int = GRAPH_DEFAULT_USERS,
include: set[int] = None) -> None:
"""
Draws the plot with the specified parameters.
Args:
limit (int, optional): The maximum number of users to plot. Defaults to GRAPH_DEFAULT_USERS.
include (set[int], optional): If specified, only includes the specified user IDs. Defaults to None.
"""
max_count = limit if not include else len(include)
count: int = 0
for profile, xs, ys in self.get_data(max_count, include):
id, name, colour, avatar = profile
if include and id not in include:
continue
if len(xs) <= 1:
continue
self.annotations.append(
(ys[-1], name, colour, avatar, ys[0]))
plt.plot(xs, ys, color=colour)
count += 1
if count >= max_count:
break
if count < max_count:
logger.info(f"{count} profiles shown")
def draw_events(self) -> None:
"""
Draws event spans on the plot.
"""
now = datetime.now()
dbe = DatabaseEvents()
events = dbe.get_events_in_range(self.start_date, self.end_date)
for event in events:
start = max(event.start_time, self.start_date)
end = min(event.end_time or now, self.end_date or now)
mult = event.multiplier
alpha = max(min(mult - 1, 1), 0)
plt.axvspan(start, end, color=Colours.highlight, alpha=alpha)
plt.annotate(f"{mult}x", (start + (end - start) / 2, 1),
xycoords=('data', 'axes fraction'),
color=Colours.text,
ha='center',
va='top',
family=fonts)
def annotate(self) -> None:
"""
Adds names to the graph.
Ensures the names are separated by at least 'GRAPH_SEPERATOR'.
Requires at least 1 annotation.
"""
# Determine how to convert from xp to axes fraction.
sorted_annotations = sorted(self.annotations, key=lambda x: x[0])
self.max_xp = sorted_annotations[-1][0]
self.min_xp = sorted_annotations[0][4]
if len(self.annotations) > 1:
def xp_to_axes(xp):
return (xp - self.min_xp) / (self.max_xp - self.min_xp)
def axes_to_data(h):
return h * (self.max_xp - self.min_xp) + self.min_xp
# Each point defaults to next to line.
# Moves up to avoid lower labels.
heights = [-GRAPH_SEPERATOR]
for item in sorted_annotations:
height = item[0]
position = 1.001 + GRAPH_IMAGE_WIDTH
if len(self.annotations) > 1:
label_height = xp_to_axes(height)
if label_height - heights[-1] < GRAPH_SEPERATOR:
label_height = heights[-1] + GRAPH_SEPERATOR
heights.append(label_height)
label_height = axes_to_data(label_height)
else:
label_height = height
did_img = self.annotate_image(item[3], label_height)
label_position = position if did_img else position - GRAPH_IMAGE_WIDTH
plt.annotate(item[1], (position, height),
xytext=(label_position, label_height),
xycoords=('axes fraction', 'data'),
color=item[2],
va='center',
family=fonts)
def annotate_image(self, avatar: str, height: float) -> bool:
"""
Adds an image at the given height.
Args:
avatar (str): URL to the avatar image.
height (float): Height in data units to position the image.
Returns:
bool: True if the image was added successfully, False otherwise.
"""
image = request_img(avatar)
if image is None:
return False
image = plt.imread(image, format='jpeg')
image = OffsetImage(image, zoom=0.1)
annotation = AnnotationBbox(image, (1 + GRAPH_IMAGE_WIDTH / 2, height),
xycoords=('axes fraction', 'data'),
frameon=False)
self.ax.add_artist(annotation)
return True
def configure(self) -> None:
"""
Configures the plot with labels, colors, and other settings.
"""
end = self.end_date or datetime.now()
self.ax.set_xlim([self.start_date, end])
dateformat = ResponsiveDateFormat(self.start_date, end)
self.ax.xaxis.set_major_formatter(dateformat.formatter)
self.ax.set_xlabel(f"Date ({str(dateformat)})", color=Colours.text)
self.ax.set_ylabel("Total XP", color=Colours.text)
self.fig.patch.set_facecolor(Colours.outside)
self.ax.set_facecolor(Colours.inside)
self.ax.tick_params(colors=Colours.text)
for spine in self.ax.spines.values():
spine.set_visible(False)
plt.grid(visible=True, axis='y', color=Colours.outside)
self.fig.subplots_adjust(left=0.06, bottom=0.08, top=0.94, right=0.83)
if self.title:
plt.title(self.title, color=Colours.text)
def show(self):
"""
Displays the plot.
"""
plt.show()
def save(self, name="out.png"):
"""
Saves the plot to a file.
Args:
name (str, optional): The name of the file. Defaults to "out.png".
"""
name = os.path.join(BASE_DIR, 'generated', name)
plt.savefig(name)
return name
def close(self):
"""
Closes the plot.
"""
plt.close()
if __name__ == '__main__':
plot = Plotter(start_date=datetime.now() - timedelta(days=7))
plot.draw()
plot.draw_events()
plot.annotate()
plot.configure()
plot.save()
plot.show()
plot.close()