-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstats_module.py
More file actions
1220 lines (990 loc) · 50.4 KB
/
stats_module.py
File metadata and controls
1220 lines (990 loc) · 50.4 KB
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Importações Padrão
import sys
import calendar
import sqlite3
import numpy as np
# Importações de Terceiros
from PyQt5.QtWidgets import (
QWidget, QVBoxLayout, QLabel, QHBoxLayout, QTableWidget, QTableWidgetItem,
QListWidget, QPushButton, QComboBox, QMessageBox, QLineEdit, QFileDialog,
QTabWidget
)
from PyQt5.QtCore import QDate, pyqtSignal, Qt
from PyQt5.QtGui import QTextDocument
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
import matplotlib.pyplot as plt
# Constantes
DB_PATH = "data.db"
# --------------------------------------------------
# Definição da Classe StatsWidget
# --------------------------------------------------
class StatsWidget(QWidget):
"""Widget para exibir estatísticas com gráficos interativos."""
# Sinal para notificação de deleção de nota
note_deleted = pyqtSignal(str)
# Constante para paginação (não utilizada atualmente, mas mantida para futuras implementações)
NOTES_PER_PAGE = 10
def __init__(self, parent=None):
super().__init__(parent)
self.setWindowTitle("Estatísticas")
self.layout = QVBoxLayout(self)
# Configuração das abas
self.tabs = QTabWidget(self)
self.layout.addWidget(self.tabs)
# Inicializa as abas
self.init_graph_tab()
self.init_task_tab()
# Atualiza anos nas listas de filtros
self.update_years()
self.update_task_years()
# ------------------------------
# Métodos de Inicialização das Abas
# ------------------------------
def init_graph_tab(self):
"""Inicializa a aba de gráficos."""
self.graph_tab_layout = QVBoxLayout()
# Resumo numérico no topo
self.summary_label = QLabel(self)
self.graph_tab_layout.addWidget(self.summary_label)
# Filtros de anos, meses, categorias, tags e semanas
self.filter_layout = QHBoxLayout()
# Lista de anos
self.year_list = QListWidget(self)
self.year_list.setSelectionMode(QListWidget.MultiSelection)
self.year_label = QLabel("Anos:")
self.filter_layout.addWidget(self.year_label)
self.filter_layout.addWidget(self.year_list)
self.year_list.itemSelectionChanged.connect(self.update_months)
# Lista de meses
self.month_list = QListWidget(self)
self.month_list.setSelectionMode(QListWidget.MultiSelection)
self.month_label = QLabel("Meses:")
self.filter_layout.addWidget(self.month_label)
self.filter_layout.addWidget(self.month_list)
self.month_list.itemSelectionChanged.connect(self.update_weeks)
# Lista de semanas
self.week_list = QListWidget(self)
self.week_list.setSelectionMode(QListWidget.MultiSelection)
self.week_label = QLabel("Semanas:")
self.filter_layout.addWidget(self.week_label)
self.filter_layout.addWidget(self.week_list)
# Campo de categorias
self.category_input = QLineEdit(self)
self.category_input.setPlaceholderText("Categorias (separadas por vírgula)")
self.filter_layout.addWidget(QLabel("Categorias:"))
self.filter_layout.addWidget(self.category_input)
# Campo de tags
self.tag_input = QLineEdit(self)
self.tag_input.setPlaceholderText("Tags (separadas por vírgula)")
self.filter_layout.addWidget(QLabel("Tags:"))
self.filter_layout.addWidget(self.tag_input)
# Botões de ação
self.update_button = QPushButton("Atualizar Estatísticas", self)
self.update_button.clicked.connect(self.update_all)
self.filter_layout.addWidget(self.update_button)
self.export_button = QPushButton("Exportar Gráfico", self)
self.export_button.clicked.connect(self.export_chart)
self.export_button.setEnabled(False) # Desativado inicialmente
self.filter_layout.addWidget(self.export_button)
# Botões de seleção rápida
self.select_all_button = QPushButton("Selecionar Todo o Ano", self)
self.select_all_button.clicked.connect(self.select_all_years)
self.filter_layout.addWidget(self.select_all_button)
self.clear_selection_button = QPushButton("Limpar Seleção", self)
self.clear_selection_button.clicked.connect(self.clear_selection)
self.filter_layout.addWidget(self.clear_selection_button)
self.graph_tab_layout.addLayout(self.filter_layout)
# Seleção de tipo de gráfico
self.graph_type_combo = QComboBox(self)
self.graph_type_combo.addItems([
"Procrastinação (Dias Estudados vs Não Estudados)",
"Heatmap por Ano",
"Heatmap de Vários anos",
"Notas ao Longo do Tempo",
"Notas por Categoria",
"Notas por Tags",
"Progresso (Feito vs Pendentes)",
"Gráfico Comparativo",
# Novas opções adicionadas abaixo
"Comparativo de Notas por Dias da Semana - Barras",
"Comparativo de Notas por Dias da Semana - Linhas",
"Comparativo de Notas por Dias da Semana - Pizza"
])
self.graph_type_combo.currentIndexChanged.connect(self.handle_graph_type_change)
self.graph_tab_layout.addWidget(QLabel("Tipo de Gráfico:"))
self.graph_tab_layout.addWidget(self.graph_type_combo)
# Campo de entrada para ano (aparece somente para alguns gráficos)
self.year_input = QLineEdit(self)
self.year_input.setPlaceholderText("Digite o ano (ex: 2025)")
self.year_input.setVisible(False)
self.graph_tab_layout.addWidget(self.year_input)
# Feedback para o usuário
self.year_feedback = QLabel("Digite o ano e clique em 'Atualizar Estatísticas' para gerar o gráfico.", self)
self.year_feedback.setStyleSheet("color: blue; font-size: 15px; font-weight: bold;")
self.year_feedback.setVisible(False)
self.graph_tab_layout.addWidget(self.year_feedback)
# Área para exibir gráficos
self.figure = plt.figure(figsize=(12, 6))
self.canvas = FigureCanvas(self.figure)
self.graph_tab_layout.addWidget(self.canvas)
# Adiciona a aba de gráficos ao QTabWidget
graph_widget = QWidget(self)
graph_widget.setLayout(self.graph_tab_layout)
self.tabs.addTab(graph_widget, "Gráficos")
def init_task_tab(self):
"""Inicializa a aba de estatísticas de tarefas."""
self.task_tab_layout = QVBoxLayout()
# Resumo numérico
self.task_summary_label = QLabel(self)
self.task_tab_layout.addWidget(self.task_summary_label)
# Filtros de meses e anos para tarefas
self.task_filter_layout = QHBoxLayout()
# Lista de meses para tarefas
self.task_month_list = QListWidget(self)
self.task_month_list.setSelectionMode(QListWidget.MultiSelection)
for i in range(1, 13):
self.task_month_list.addItem(f"{i:02d}")
self.task_filter_layout.addWidget(QLabel("Meses:"))
self.task_filter_layout.addWidget(self.task_month_list)
# Lista de anos para tarefas
self.task_year_list = QListWidget(self)
self.task_year_list.setSelectionMode(QListWidget.MultiSelection)
self.task_filter_layout.addWidget(QLabel("Anos:"))
self.task_filter_layout.addWidget(self.task_year_list)
self.task_tab_layout.addLayout(self.task_filter_layout)
# Botão para atualizar estatísticas de tarefas
self.update_task_button = QPushButton("Atualizar Estatísticas de Tarefas", self)
self.update_task_button.clicked.connect(self.update_task_stats)
self.task_tab_layout.addWidget(self.update_task_button)
# Área para exibir gráficos de tarefas
self.task_figure = plt.figure(figsize=(12, 6))
self.task_canvas = FigureCanvas(self.task_figure)
self.task_tab_layout.addWidget(self.task_canvas)
# Adiciona a aba de tarefas ao QTabWidget
task_widget = QWidget(self)
task_widget.setLayout(self.task_tab_layout)
self.tabs.addTab(task_widget, "Estatísticas de Tarefas")
# ------------------------------
# Métodos de Atualização de Filtros
# ------------------------------
def update_years(self):
"""Atualiza a lista de anos com base nas notas existentes no banco de dados."""
with sqlite3.connect(DB_PATH) as conn:
cursor = conn.cursor()
cursor.execute("SELECT DISTINCT strftime('%Y', date) AS year FROM notes ORDER BY year")
years = [row[0] for row in cursor.fetchall()]
self.year_list.clear()
for year in years:
self.year_list.addItem(year)
def update_task_years(self):
"""Atualiza a lista de anos com base nas tarefas existentes no banco de dados."""
with sqlite3.connect(DB_PATH) as conn:
cursor = conn.cursor()
cursor.execute("SELECT DISTINCT strftime('%Y', creation_date) AS year FROM tasks ORDER BY year")
years = [row[0] for row in cursor.fetchall()]
self.task_year_list.clear()
for year in years:
self.task_year_list.addItem(year)
# ------------------------------
# Métodos de Atualização Dinâmica de Meses e Semanas
# ------------------------------
def update_months(self):
"""Atualiza a lista de meses com base nos anos selecionados."""
selected_years = [item.text() for item in self.year_list.selectedItems()]
if not selected_years:
# Se nenhum ano estiver selecionado, desabilita os meses e semanas
self.month_list.clear()
self.week_list.clear()
self.month_list.setEnabled(False)
self.week_list.setEnabled(False)
return
# Consulta os meses disponíveis nos anos selecionados
placeholders = ", ".join(["?"] * len(selected_years))
query = f"SELECT DISTINCT strftime('%m', date) FROM notes WHERE strftime('%Y', date) IN ({placeholders}) ORDER BY strftime('%m', date)"
with sqlite3.connect(DB_PATH) as conn:
cursor = conn.cursor()
cursor.execute(query, selected_years)
months = [row[0] for row in cursor.fetchall()]
# Atualiza a lista de meses
self.month_list.clear()
for month in sorted(months):
self.month_list.addItem(f"{int(month):02d}") # Exibir como '01', '02', etc.
self.month_list.setEnabled(True)
# Limpa e desabilita a lista de semanas até que os meses sejam selecionados
self.week_list.clear()
self.week_list.setEnabled(False)
def update_weeks(self):
"""Atualiza a lista de semanas com base nos meses selecionados."""
selected_years = [item.text() for item in self.year_list.selectedItems()]
selected_months = [item.text() for item in self.month_list.selectedItems()]
if not selected_years or not selected_months:
# Se não houver seleção de anos ou meses, desabilita as semanas
self.week_list.clear()
self.week_list.setEnabled(False)
return
# Para simplificar, consideraremos até 5 semanas por mês
self.week_list.clear()
for week in range(1, 6):
self.week_list.addItem(str(week))
self.week_list.setEnabled(True)
# ------------------------------
# Métodos de Filtragem e Atualização
# ------------------------------
def update_all(self):
"""Atualiza a filtragem, a tabela e o gráfico."""
self.filter_notes()
self.plot_dynamic_chart()
def filter_notes(self):
"""Filtra as notas com base nos filtros selecionados."""
selected_years = [item.text() for item in self.year_list.selectedItems()]
selected_months = [item.text() for item in self.month_list.selectedItems()]
selected_weeks = [int(item.text()) for item in self.week_list.selectedItems()]
categories = [cat.strip() for cat in self.category_input.text().split(",") if cat.strip()]
tags = [tag.strip() for tag in self.tag_input.text().split(",") if tag.strip()]
query = "SELECT date, text, categories, tags FROM notes"
conditions = []
params = []
# Filtrar por ano(s)
if selected_years:
placeholders = ", ".join(["?"] * len(selected_years))
conditions.append(f"strftime('%Y', date) IN ({placeholders})")
params.extend(selected_years)
# Filtrar por mês(es)
if selected_months:
placeholders = ", ".join(["?"] * len(selected_months))
conditions.append(f"strftime('%m', date) IN ({placeholders})")
params.extend(selected_months)
# Filtrar por semana(s) do mês
if selected_weeks:
# Calcular a semana do mês: ((dia - 1) / 7) + 1
week_conditions = []
for week in selected_weeks:
week_conditions.append(f"((CAST(strftime('%d', date) AS INTEGER) - 1) / 7 + 1) = {week}")
conditions.append("(" + " OR ".join(week_conditions) + ")")
# Filtrar por categorias
if categories:
category_conditions = " OR ".join(["categories LIKE ?"] * len(categories))
conditions.append(f"({category_conditions})")
params.extend([f"%{cat}%" for cat in categories])
# Filtrar por tags
if tags:
tag_conditions = " OR ".join(["tags LIKE ?"] * len(tags))
conditions.append(f"({tag_conditions})")
params.extend([f"%{tag}%" for tag in tags])
if conditions:
query += " WHERE " + " AND ".join(conditions)
with sqlite3.connect(DB_PATH) as conn:
cursor = conn.cursor()
cursor.execute(query, params)
self.filtered_notes = cursor.fetchall()
self.update_summary()
def update_summary(self):
"""Atualiza o resumo numérico no topo."""
total_notes = len(self.filtered_notes)
total_categories = len(set(cat for _, _, cats, _ in self.filtered_notes for cat in cats.split(",") if cats))
total_tags = len(set(tag for _, _, _, tags in self.filtered_notes for tag in tags.split(",") if tags))
total_done = sum(1 for _, _, _, tags in self.filtered_notes if "feito" in tags.split(","))
self.summary_label.setText(
f"Total de Notas: {total_notes} | Categorias Únicas: {total_categories} | "
f"Tags Únicas: {total_tags} | Feito: {total_done}"
)
# ------------------------------
# Métodos de Plotagem de Gráficos
# ------------------------------
def handle_graph_type_change(self):
"""Lida com a mudança no tipo de gráfico selecionado."""
chart_type = self.graph_type_combo.currentText()
# Mostrar ou esconder campos com base no tipo de gráfico
if chart_type == "Heatmap por Ano":
self.year_input.setVisible(True)
self.year_feedback.setVisible(True)
else:
self.year_input.setVisible(False)
self.year_feedback.setVisible(False)
def plot_dynamic_chart(self):
"""Desenha o gráfico com base no tipo selecionado."""
if not self.filtered_notes:
QMessageBox.information(self, "Sem dados", "Não há notas suficientes para exibir o gráfico.")
self.export_button.setEnabled(False)
self.figure.clear()
self.canvas.draw()
return
chart_type = self.graph_type_combo.currentText()
self.export_button.setEnabled(True) # Habilita o botão de exportação somente se houver dados
# Definir visibilidade do campo de ano e feedback
if chart_type == "Heatmap por Ano":
self.year_input.setVisible(True)
self.year_feedback.setVisible(True)
else:
self.year_input.setVisible(False)
self.year_feedback.setVisible(False)
# Limpa a figura atual
self.figure.clear()
# Seleciona o método de plotagem com base no tipo de gráfico
plot_methods = {
"Heatmap por Ano": self.plot_heatmap_for_year,
"Heatmap de Vários anos": self.plot_heatmap,
"Notas ao Longo do Tempo": self.plot_notes_over_time,
"Notas por Categoria": self.plot_notes_by_category,
"Notas por Tags": self.plot_notes_by_tags,
"Progresso (Feito vs Pendentes)": self.plot_progress_chart,
"Procrastinação (Dias Estudados vs Não Estudados)": self.plot_procrastination_chart,
"Gráfico Comparativo": self.plot_comparative_chart,
# Novos métodos adicionados abaixo
"Comparativo de Notas por Dias da Semana - Barras": self.plot_weekday_bar_chart,
"Comparativo de Notas por Dias da Semana - Linhas": self.plot_weekday_line_chart,
"Comparativo de Notas por Dias da Semana - Pizza": self.plot_weekday_pie_chart
}
plot_method = plot_methods.get(chart_type, None)
if plot_method:
if chart_type == "Heatmap por Ano":
year_text = self.year_input.text().strip()
if not year_text:
QMessageBox.warning(self, "Entrada Inválida", "Por favor, digite um ano antes de atualizar.")
return
elif year_text.isdigit():
year = int(year_text)
plot_method(year)
else:
QMessageBox.warning(self, "Entrada Inválida", "Por favor, digite um ano válido.")
return
else:
plot_method()
# Atualiza o canvas
self.canvas.draw()
# ------------------------------
# Métodos de Plotagem dos Novos Gráficos
# ------------------------------
def plot_weekday_bar_chart(self):
"""Plota um gráfico de barras mostrando o número de notas por dia da semana."""
# Contar notas por dia da semana
weekday_counts = {day:0 for day in calendar.day_name} # Monday, Tuesday, etc.
for date_str, _, _, _ in self.filtered_notes:
date = QDate.fromString(date_str, "yyyy-MM-dd")
weekday = calendar.day_name[date.dayOfWeek() - 1] # QDate.dayOfWeek() retorna 1 (Monday) to 7 (Sunday)
weekday_counts[weekday] += 1
days = list(weekday_counts.keys())
counts = list(weekday_counts.values())
ax = self.figure.add_subplot(111)
bars = ax.bar(days, counts, color='skyblue')
ax.set_title("Número de Notas por Dia da Semana")
ax.set_xlabel("Dias da Semana")
ax.set_ylabel("Quantidade de Notas")
ax.set_ylim(0, max(counts) + 5)
# Adicionar rótulos acima das barras
for bar in bars:
height = bar.get_height()
ax.annotate('{}'.format(height),
xy=(bar.get_x() + bar.get_width() / 2, height),
xytext=(0, 3), # 3 pontos verticalmente
textcoords="offset points",
ha='center', va='bottom')
def plot_weekday_line_chart(self):
"""Plota um gráfico de linhas mostrando a tendência de notas por dia da semana ao longo das semanas."""
# Agrupar notas por semana e dia da semana
from collections import defaultdict
# Estrutura: {week_number: {day_name: count}}
week_day_counts = defaultdict(lambda: defaultdict(int))
for date_str, _, _, _ in self.filtered_notes:
date = QDate.fromString(date_str, "yyyy-MM-dd")
week_number = date.weekNumber()[0] # (week_number, is_last_week)
weekday = calendar.day_name[date.dayOfWeek() - 1]
week_day_counts[week_number][weekday] += 1
if not week_day_counts:
QMessageBox.warning(self, "Sem dados", "Não há notas para exibir no gráfico de linhas.")
return
weeks = sorted(week_day_counts.keys())
days = list(calendar.day_name)
# Preparar dados para cada dia
day_trends = {day: [] for day in days}
for week in weeks:
for day in days:
day_trends[day].append(week_day_counts[week].get(day, 0))
# Plotar as linhas
ax = self.figure.add_subplot(111)
for day, counts in day_trends.items():
ax.plot(weeks, counts, marker='o', label=day)
ax.set_title("Tendência de Notas por Dia da Semana ao Longo das Semanas")
ax.set_xlabel("Número da Semana")
ax.set_ylabel("Quantidade de Notas")
ax.legend(title="Dias da Semana")
ax.grid(True)
def plot_weekday_pie_chart(self):
"""Plota um gráfico de pizza mostrando a proporção de notas por dia da semana."""
# Contar notas por dia da semana
weekday_counts = {day:0 for day in calendar.day_name} # Monday, Tuesday, etc.
for date_str, _, _, _ in self.filtered_notes:
date = QDate.fromString(date_str, "yyyy-MM-dd")
weekday = calendar.day_name[date.dayOfWeek() - 1]
weekday_counts[weekday] += 1
labels = list(weekday_counts.keys())
sizes = list(weekday_counts.values())
if sum(sizes) == 0:
QMessageBox.warning(self, "Sem dados", "Não há notas para exibir no gráfico de pizza.")
return
ax = self.figure.add_subplot(111)
ax.pie(sizes, labels=labels, autopct='%1.1f%%', startangle=140, colors=plt.cm.Paired.colors)
ax.set_title("Proporção de Notas por Dia da Semana")
ax.axis('equal') # Equal aspect ratio ensures that pie is drawn as a circle.
def plot_heatmap_for_year(self, year):
"""Plota um heatmap de notas para um único ano, com cada linha representando um mês."""
# Coletar as datas das notas filtradas
dates = [
QDate.fromString(date, "yyyy-MM-dd") for date, _, _, _ in self.filtered_notes
if QDate.fromString(date, "yyyy-MM-dd").year() == year
]
if not dates:
QMessageBox.warning(self, "Sem dados", f"Não há notas para exibir no ano {year}.")
return
# Criar uma matriz 12x31 (12 meses, até 31 dias por mês)
heatmap_data = np.zeros((12, 31), dtype=int)
for date in dates:
month_index = date.month() - 1
day_index = date.day() - 1
heatmap_data[month_index, day_index] += 1 # Incrementa a contagem de notas no dia correspondente
# Plotar o heatmap
ax = self.figure.add_subplot(111)
cax = ax.imshow(heatmap_data, cmap='YlGn', aspect='auto', vmin=0, vmax=heatmap_data.max())
# Configurar rótulos dos eixos
ax.set_xticks(np.arange(31))
ax.set_xticklabels([str(i + 1) for i in range(31)])
ax.set_yticks(np.arange(12))
ax.set_yticklabels([calendar.month_name[i + 1] for i in range(12)])
ax.set_title(f"Heatmap de Notas - {year}")
ax.set_xlabel("Dias do Mês")
ax.set_ylabel("Meses")
# Adicionar a barra de cores
self.figure.colorbar(cax, orientation='vertical', ticks=range(0, heatmap_data.max()+1), label='Quantidade de Notas')
def plot_heatmap(self):
"""Plota um heatmap com a contagem de notas por mês ao longo de vários anos."""
# Coletar as datas filtradas
dates = [QDate.fromString(date, "yyyy-MM-dd") for date, _, _, _ in self.filtered_notes]
if not dates:
QMessageBox.warning(self, "Sem dados", "Não há notas para exibir no período selecionado.")
return
# Coletar os anos únicos presentes nas datas
selected_years = sorted(set(date.year() for date in dates))
months = list(range(1, 13)) # Meses de 1 a 12
# Criar uma matriz de contagem de notas (linhas: anos, colunas: meses)
heatmap_data = np.zeros((len(selected_years), len(months)), dtype=int)
for date in dates:
year_index = selected_years.index(date.year())
month_index = date.month() - 1
heatmap_data[year_index, month_index] += 1 # Incrementa a contagem de notas no mês correspondente
# Plotar o heatmap
ax = self.figure.add_subplot(111)
cax = ax.imshow(heatmap_data, cmap='YlGn', aspect='auto')
# Configurar rótulos dos eixos
ax.set_xticks(np.arange(len(months)))
ax.set_xticklabels([calendar.month_abbr[m] for m in months]) # Abreviações dos meses
ax.set_yticks(np.arange(len(selected_years)))
ax.set_yticklabels([str(year) for year in selected_years])
ax.set_title("Heatmap de Notas por Mês e Ano")
ax.set_xlabel("Meses")
ax.set_ylabel("Anos")
# Adicionar a barra de cores
self.figure.colorbar(cax, orientation='vertical', label='Quantidade de Notas')
def plot_notes_over_time(self):
"""Plota um gráfico de linha: Notas ao longo do tempo."""
dates = sorted([QDate.fromString(date, "yyyy-MM-dd") for date, _, _, _ in self.filtered_notes])
counts = list(range(1, len(dates) + 1))
date_strings = [date.toString("yyyy-MM-dd") for date in dates]
ax = self.figure.add_subplot(111)
ax.plot(date_strings, counts, marker='o', linestyle='-', color='purple')
ax.set_title("Notas ao Longo do Tempo")
ax.set_xlabel("Data")
ax.set_ylabel("Quantidade de Notas")
ax.tick_params(axis='x', rotation=45)
def plot_notes_by_category(self):
"""Plota um gráfico de barras horizontal: Notas por categoria."""
categories = {}
for _, _, cats, _ in self.filtered_notes:
for category in cats.split(","):
if category.strip():
categories[category.strip()] = categories.get(category.strip(), 0) + 1
if not categories:
QMessageBox.information(self, "Sem dados", "Não há categorias para exibir.")
return
ax = self.figure.add_subplot(111)
ax.barh(list(categories.keys()), list(categories.values()), color='skyblue')
ax.set_title("Notas por Categoria")
ax.set_xlabel("Quantidade")
ax.set_ylabel("Categoria")
def plot_notes_by_tags(self):
"""Plota um gráfico de barras horizontal: Notas por tags."""
tags = {}
for _, _, _, tags_str in self.filtered_notes:
for tag in tags_str.split(","):
if tag.strip():
tags[tag.strip()] = tags.get(tag.strip(), 0) + 1
if not tags:
QMessageBox.information(self, "Sem dados", "Não há tags para exibir.")
return
ax = self.figure.add_subplot(111)
ax.barh(list(tags.keys()), list(tags.values()), color='lightgreen')
ax.set_title("Notas por Tags")
ax.set_xlabel("Quantidade")
ax.set_ylabel("Tags")
def plot_progress_chart(self):
"""Plota um gráfico de pizza: Progresso (feito vs pendentes)."""
total_done = sum(1 for _, _, _, tags in self.filtered_notes if "feito" in tags.split(","))
total_pending = len(self.filtered_notes) - total_done
if total_done == 0 and total_pending == 0:
QMessageBox.information(self, "Sem dados", "Não há progresso para exibir.")
return
ax = self.figure.add_subplot(111)
ax.pie([total_done, total_pending], labels=["Feito", "Pendente"], autopct="%1.1f%%",
colors=["#66b3ff", "#ff9999"])
ax.set_title("Progresso (Feito vs Pendentes)")
def plot_procrastination_chart(self):
"""Plota um gráfico de pizza: Procrastinação (dias estudados vs não estudados)."""
dates = [QDate.fromString(date, "yyyy-MM-dd") for date, _, _, _ in self.filtered_notes]
studied_days = set((date.year(), date.month(), date.day()) for date in dates)
if not dates:
QMessageBox.information(self, "Sem dados", "Não há dias estudados para exibir.")
return
current_date = QDate.currentDate()
# Considerando o mês atual para cálculo de dias
total_days = current_date.daysInMonth()
procrastinated_days = total_days - len({day for (_, _, day) in studied_days if day <= total_days})
if procrastinated_days < 0:
procrastinated_days = 0
ax = self.figure.add_subplot(111)
ax.pie([len(studied_days), procrastinated_days], labels=["Estudados", "Não Estudados"],
autopct="%1.1f%%", colors=["#66b3ff", "#ff9999"])
ax.set_title("Procrastinação (Dias Estudados vs Não Estudados)")
def plot_comparative_chart(self):
"""Plota um gráfico comparativo de categorias e tags."""
categories = {}
tags = {}
for _, _, cats, tags_str in self.filtered_notes:
for category in cats.split(","):
if category.strip():
categories[category.strip()] = categories.get(category.strip(), 0) + 1
for tag in tags_str.split(","):
if tag.strip():
tags[tag.strip()] = tags.get(tag.strip(), 0) + 1
if not categories and not tags:
QMessageBox.information(self, "Sem dados", "Não há categorias ou tags para exibir.")
return
labels = list(categories.keys()) + list(tags.keys())
values = list(categories.values()) + list(tags.values())
ax = self.figure.add_subplot(111)
ax.bar(labels, values, color=['skyblue'] * len(categories) + ['lightgreen'] * len(tags))
ax.set_title("Gráfico Comparativo (Categorias vs Tags)")
ax.set_xlabel("Categorias e Tags")
ax.set_ylabel("Quantidade")
ax.tick_params(axis='x', rotation=45)
# ------------------------------
# Métodos de Plotagem dos Novos Gráficos por Dia da Semana
# ------------------------------
def plot_weekday_bar_chart(self):
"""Plota um gráfico de barras mostrando o número de notas por dia da semana."""
# Contar notas por dia da semana
weekday_counts = {day:0 for day in calendar.day_name} # Monday, Tuesday, etc.
for date_str, _, _, _ in self.filtered_notes:
date = QDate.fromString(date_str, "yyyy-MM-dd")
weekday = calendar.day_name[date.dayOfWeek() - 1] # QDate.dayOfWeek() retorna 1 (Monday) to 7 (Sunday)
weekday_counts[weekday] += 1
days = list(weekday_counts.keys())
counts = list(weekday_counts.values())
ax = self.figure.add_subplot(111)
bars = ax.bar(days, counts, color='skyblue')
ax.set_title("Número de Notas por Dia da Semana")
ax.set_xlabel("Dias da Semana")
ax.set_ylabel("Quantidade de Notas")
ax.set_ylim(0, max(counts) + 5)
# Adicionar rótulos acima das barras
for bar in bars:
height = bar.get_height()
ax.annotate('{}'.format(height),
xy=(bar.get_x() + bar.get_width() / 2, height),
xytext=(0, 3), # 3 pontos verticalmente
textcoords="offset points",
ha='center', va='bottom')
def plot_weekday_line_chart(self):
"""Plota um gráfico de linhas mostrando a tendência de notas por dia da semana ao longo das semanas."""
# Agrupar notas por semana e dia da semana
from collections import defaultdict
# Estrutura: {week_number: {day_name: count}}
week_day_counts = defaultdict(lambda: defaultdict(int))
for date_str, _, _, _ in self.filtered_notes:
date = QDate.fromString(date_str, "yyyy-MM-dd")
week_number = date.weekNumber()[0] # (week_number, is_last_week)
weekday = calendar.day_name[date.dayOfWeek() - 1]
week_day_counts[week_number][weekday] += 1
if not week_day_counts:
QMessageBox.warning(self, "Sem dados", "Não há notas para exibir no gráfico de linhas.")
return
weeks = sorted(week_day_counts.keys())
days = list(calendar.day_name)
# Preparar dados para cada dia
day_trends = {day: [] for day in days}
for week in weeks:
for day in days:
day_trends[day].append(week_day_counts[week].get(day, 0))
# Plotar as linhas
ax = self.figure.add_subplot(111)
for day, counts in day_trends.items():
ax.plot(weeks, counts, marker='o', label=day)
ax.set_title("Tendência de Notas por Dia da Semana ao Longo das Semanas")
ax.set_xlabel("Número da Semana")
ax.set_ylabel("Quantidade de Notas")
ax.legend(title="Dias da Semana")
ax.grid(True)
def plot_weekday_pie_chart(self):
"""Plota um gráfico de pizza mostrando a proporção de notas por dia da semana."""
# Contar notas por dia da semana
weekday_counts = {day:0 for day in calendar.day_name} # Monday, Tuesday, etc.
for date_str, _, _, _ in self.filtered_notes:
date = QDate.fromString(date_str, "yyyy-MM-dd")
weekday = calendar.day_name[date.dayOfWeek() - 1]
weekday_counts[weekday] += 1
labels = list(weekday_counts.keys())
sizes = list(weekday_counts.values())
if sum(sizes) == 0:
QMessageBox.warning(self, "Sem dados", "Não há notas para exibir no gráfico de pizza.")
return
ax = self.figure.add_subplot(111)
ax.pie(sizes, labels=labels, autopct='%1.1f%%', startangle=140, colors=plt.cm.Paired.colors)
ax.set_title("Proporção de Notas por Dia da Semana")
ax.axis('equal') # Equal aspect ratio ensures that pie is drawn as a circle.
def plot_heatmap_for_year(self, year):
"""Plota um heatmap de notas para um único ano, com cada linha representando um mês."""
# Coletar as datas das notas filtradas
dates = [
QDate.fromString(date, "yyyy-MM-dd") for date, _, _, _ in self.filtered_notes
if QDate.fromString(date, "yyyy-MM-dd").year() == year
]
if not dates:
QMessageBox.warning(self, "Sem dados", f"Não há notas para exibir no ano {year}.")
return
# Criar uma matriz 12x31 (12 meses, até 31 dias por mês)
heatmap_data = np.zeros((12, 31), dtype=int)
for date in dates:
month_index = date.month() - 1
day_index = date.day() - 1
heatmap_data[month_index, day_index] += 1 # Incrementa a contagem de notas no dia correspondente
# Plotar o heatmap
ax = self.figure.add_subplot(111)
cax = ax.imshow(heatmap_data, cmap='YlGn', aspect='auto', vmin=0, vmax=heatmap_data.max())
# Configurar rótulos dos eixos
ax.set_xticks(np.arange(31))
ax.set_xticklabels([str(i + 1) for i in range(31)])
ax.set_yticks(np.arange(12))
ax.set_yticklabels([calendar.month_name[i + 1] for i in range(12)])
ax.set_title(f"Heatmap de Notas - {year}")
ax.set_xlabel("Dias do Mês")
ax.set_ylabel("Meses")
# Adicionar a barra de cores
self.figure.colorbar(cax, orientation='vertical', ticks=range(0, heatmap_data.max()+1), label='Quantidade de Notas')
def plot_heatmap(self):
"""Plota um heatmap com a contagem de notas por mês ao longo de vários anos."""
# Coletar as datas filtradas
dates = [QDate.fromString(date, "yyyy-MM-dd") for date, _, _, _ in self.filtered_notes]
if not dates:
QMessageBox.warning(self, "Sem dados", "Não há notas para exibir no período selecionado.")
return
# Coletar os anos únicos presentes nas datas
selected_years = sorted(set(date.year() for date in dates))
months = list(range(1, 13)) # Meses de 1 a 12
# Criar uma matriz de contagem de notas (linhas: anos, colunas: meses)
heatmap_data = np.zeros((len(selected_years), len(months)), dtype=int)
for date in dates:
year_index = selected_years.index(date.year())
month_index = date.month() - 1
heatmap_data[year_index, month_index] += 1 # Incrementa a contagem de notas no mês correspondente
# Plotar o heatmap
ax = self.figure.add_subplot(111)
cax = ax.imshow(heatmap_data, cmap='YlGn', aspect='auto')
# Configurar rótulos dos eixos
ax.set_xticks(np.arange(len(months)))
ax.set_xticklabels([calendar.month_abbr[m] for m in months]) # Abreviações dos meses
ax.set_yticks(np.arange(len(selected_years)))
ax.set_yticklabels([str(year) for year in selected_years])
ax.set_title("Heatmap de Notas por Mês e Ano")
ax.set_xlabel("Meses")
ax.set_ylabel("Anos")
# Adicionar a barra de cores
self.figure.colorbar(cax, orientation='vertical', label='Quantidade de Notas')
def plot_notes_over_time(self):
"""Plota um gráfico de linha: Notas ao longo do tempo."""
dates = sorted([QDate.fromString(date, "yyyy-MM-dd") for date, _, _, _ in self.filtered_notes])
counts = list(range(1, len(dates) + 1))
date_strings = [date.toString("yyyy-MM-dd") for date in dates]
ax = self.figure.add_subplot(111)
ax.plot(date_strings, counts, marker='o', linestyle='-', color='purple')
ax.set_title("Notas ao Longo do Tempo")
ax.set_xlabel("Data")
ax.set_ylabel("Quantidade de Notas")
ax.tick_params(axis='x', rotation=45)
def plot_notes_by_category(self):
"""Plota um gráfico de barras horizontal: Notas por categoria."""
categories = {}
for _, _, cats, _ in self.filtered_notes:
for category in cats.split(","):
if category.strip():
categories[category.strip()] = categories.get(category.strip(), 0) + 1
if not categories:
QMessageBox.information(self, "Sem dados", "Não há categorias para exibir.")
return
ax = self.figure.add_subplot(111)
ax.barh(list(categories.keys()), list(categories.values()), color='skyblue')
ax.set_title("Notas por Categoria")
ax.set_xlabel("Quantidade")
ax.set_ylabel("Categoria")
def plot_notes_by_tags(self):
"""Plota um gráfico de barras horizontal: Notas por tags."""
tags = {}
for _, _, _, tags_str in self.filtered_notes:
for tag in tags_str.split(","):
if tag.strip():
tags[tag.strip()] = tags.get(tag.strip(), 0) + 1
if not tags:
QMessageBox.information(self, "Sem dados", "Não há tags para exibir.")
return
ax = self.figure.add_subplot(111)
ax.barh(list(tags.keys()), list(tags.values()), color='lightgreen')
ax.set_title("Notas por Tags")
ax.set_xlabel("Quantidade")
ax.set_ylabel("Tags")
def plot_progress_chart(self):
"""Plota um gráfico de pizza: Progresso (feito vs pendentes)."""
total_done = sum(1 for _, _, _, tags in self.filtered_notes if "feito" in tags.split(","))
total_pending = len(self.filtered_notes) - total_done
if total_done == 0 and total_pending == 0:
QMessageBox.information(self, "Sem dados", "Não há progresso para exibir.")
return
ax = self.figure.add_subplot(111)
ax.pie([total_done, total_pending], labels=["Feito", "Pendente"], autopct="%1.1f%%",
colors=["#66b3ff", "#ff9999"])
ax.set_title("Progresso (Feito vs Pendentes)")
def plot_procrastination_chart(self):
"""Plota um gráfico de pizza: Procrastinação (dias estudados vs não estudados)."""
dates = [QDate.fromString(date, "yyyy-MM-dd") for date, _, _, _ in self.filtered_notes]
studied_days = set((date.year(), date.month(), date.day()) for date in dates)
if not dates:
QMessageBox.information(self, "Sem dados", "Não há dias estudados para exibir.")
return
current_date = QDate.currentDate()
# Considerando o mês atual para cálculo de dias
total_days = current_date.daysInMonth()
procrastinated_days = total_days - len({day for (_, _, day) in studied_days if day <= total_days})
if procrastinated_days < 0:
procrastinated_days = 0
ax = self.figure.add_subplot(111)
ax.pie([len(studied_days), procrastinated_days], labels=["Estudados", "Não Estudados"],
autopct="%1.1f%%", colors=["#66b3ff", "#ff9999"])
ax.set_title("Procrastinação (Dias Estudados vs Não Estudados)")
def plot_comparative_chart(self):
"""Plota um gráfico comparativo de categorias e tags."""
categories = {}
tags = {}
for _, _, cats, tags_str in self.filtered_notes:
for category in cats.split(","):
if category.strip():
categories[category.strip()] = categories.get(category.strip(), 0) + 1
for tag in tags_str.split(","):
if tag.strip():
tags[tag.strip()] = tags.get(tag.strip(), 0) + 1
if not categories and not tags:
QMessageBox.information(self, "Sem dados", "Não há categorias ou tags para exibir.")
return
labels = list(categories.keys()) + list(tags.keys())
values = list(categories.values()) + list(tags.values())
ax = self.figure.add_subplot(111)
ax.bar(labels, values, color=['skyblue'] * len(categories) + ['lightgreen'] * len(tags))
ax.set_title("Gráfico Comparativo (Categorias vs Tags)")
ax.set_xlabel("Categorias e Tags")
ax.set_ylabel("Quantidade")
ax.tick_params(axis='x', rotation=45)
# ------------------------------
# Métodos de Plotagem dos Novos Gráficos por Dia da Semana
# ------------------------------
def plot_weekday_bar_chart(self):
"""Plota um gráfico de barras mostrando o número de notas por dia da semana."""
# Contar notas por dia da semana
weekday_counts = {day:0 for day in calendar.day_name} # Monday, Tuesday, etc.
for date_str, _, _, _ in self.filtered_notes:
date = QDate.fromString(date_str, "yyyy-MM-dd")
weekday = calendar.day_name[date.dayOfWeek() - 1] # QDate.dayOfWeek() retorna 1 (Monday) to 7 (Sunday)
weekday_counts[weekday] += 1
days = list(weekday_counts.keys())
counts = list(weekday_counts.values())