Clock_out_Time_Calculator/ui/chart_widget.py
KINDNICK 606da976a0 v2.5.0: Phase 3 — weekly report, chart hover, clock-in distribution, leave calendar
- Weekly auto report on Monday (system alert + Discord push, dedupe)
- Matplotlib chart hover annotation for daily hours
- Clock-in time distribution histogram (30-min bins) with avg line
- Leave usage calendar with color-coded full/half/quarter days

Fixed: leave_view setLayout indentation regression

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-30 18:51:47 +09:00

191 lines
6.6 KiB
Python

"""
matplotlib 기반 차트 위젯.
stats_view에서 주간/월간 추세를 시각화. matplotlib 미설치 시
ImportError 안내 라벨로 fallback.
"""
from typing import List, Tuple
from PyQt5.QtWidgets import QWidget, QVBoxLayout, QLabel
from PyQt5.QtCore import Qt
try:
from matplotlib.figure import Figure
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
import matplotlib
matplotlib.rcParams['font.family'] = ['Malgun Gothic', 'Apple SD Gothic Neo', 'sans-serif']
matplotlib.rcParams['axes.unicode_minus'] = False
_MPL = True
except ImportError:
_MPL = False
class _Fallback(QWidget):
"""matplotlib 미설치 시 안내."""
def __init__(self, message: str):
super().__init__()
layout = QVBoxLayout()
label = QLabel(message)
label.setAlignment(Qt.AlignCenter)
label.setWordWrap(True)
label.setStyleSheet("color: #888; padding: 20px;")
layout.addWidget(label)
self.setLayout(layout)
def make_chart_widget(parent=None) -> QWidget:
"""차트가 그려질 빈 캔버스 위젯. matplotlib 없으면 fallback."""
if not _MPL:
return _Fallback("차트 표시에는 matplotlib가 필요합니다.\npip install matplotlib")
widget = QWidget(parent)
layout = QVBoxLayout()
layout.setContentsMargins(0, 0, 0, 0)
fig = Figure(figsize=(5, 3), dpi=100, tight_layout=True)
canvas = FigureCanvas(fig)
layout.addWidget(canvas)
widget.setLayout(layout)
widget._figure = fig
widget._canvas = canvas
return widget
def draw_daily_hours(widget: QWidget, records: List[dict]) -> None:
"""일별 근무시간 막대 그래프 (호버 시 정확한 수치 툴팁)."""
if not getattr(widget, '_figure', None):
return
fig = widget._figure
fig.clear()
if not records:
ax = fig.add_subplot(111)
ax.text(0.5, 0.5, '기록 없음', ha='center', va='center', transform=ax.transAxes)
widget._canvas.draw()
return
dates = [r['date'][5:] for r in records] # MM-DD만
full_dates = [r['date'] for r in records]
hours = [r.get('total_hours', 0) or 0 for r in records]
overtimes = [(r.get('overtime_minutes', 0) or 0) / 60 for r in records]
base = [max(h - o, 0) for h, o in zip(hours, overtimes)]
ax = fig.add_subplot(111)
bars_base = ax.bar(dates, base, label='정상', color='#4a90e2')
bars_ot = ax.bar(dates, overtimes, bottom=base, label='연장', color='#ff6b6b')
ax.set_ylabel('시간')
ax.legend(loc='upper left', fontsize=8)
ax.tick_params(axis='x', labelrotation=45, labelsize=8)
ax.grid(axis='y', alpha=0.3)
# 호버 annotation 설정
annot = ax.annotate(
"", xy=(0, 0), xytext=(15, 15),
textcoords="offset points",
bbox=dict(boxstyle="round,pad=0.4", fc="#222", ec="#888", alpha=0.95),
color="white", fontsize=9,
arrowprops=dict(arrowstyle="->", color="#888"),
)
annot.set_visible(False)
def on_hover(event):
if event.inaxes != ax:
if annot.get_visible():
annot.set_visible(False)
widget._canvas.draw_idle()
return
for bars, kind in ((bars_base, 'base'), (bars_ot, 'ot')):
for i, bar in enumerate(bars):
if bar.contains(event)[0]:
h = hours[i]; ot = overtimes[i]
text = f"{full_dates[i]}\n근무 {h:.1f}h"
if ot > 0:
text += f"\n연장 +{ot:.1f}h"
annot.xy = (bar.get_x() + bar.get_width() / 2, bar.get_height() + bar.get_y())
annot.set_text(text)
annot.set_visible(True)
widget._canvas.draw_idle()
return
if annot.get_visible():
annot.set_visible(False)
widget._canvas.draw_idle()
widget._canvas.mpl_connect("motion_notify_event", on_hover)
widget._canvas.draw()
def draw_clock_in_distribution(widget: QWidget, records: List[dict]) -> None:
"""출근 시각 분포 히스토그램 (30분 빈)."""
if not getattr(widget, '_figure', None):
return
fig = widget._figure
fig.clear()
if not records:
ax = fig.add_subplot(111)
ax.text(0.5, 0.5, '기록 없음', ha='center', va='center', transform=ax.transAxes)
widget._canvas.draw()
return
# 출근 시각을 분 단위로 (00:00=0)
minutes_list = []
for r in records:
ci = r.get('clock_in')
if not ci:
continue
parts = ci.split(':')
if len(parts) >= 2:
try:
minutes_list.append(int(parts[0]) * 60 + int(parts[1]))
except ValueError:
pass
if not minutes_list:
return
# 30분 빈
bin_size = 30
min_m = (min(minutes_list) // bin_size) * bin_size
max_m = ((max(minutes_list) // bin_size) + 1) * bin_size
bins = list(range(min_m, max_m + bin_size, bin_size))
ax = fig.add_subplot(111)
ax.hist(minutes_list, bins=bins, color='#4a90e2', edgecolor='white')
avg = sum(minutes_list) / len(minutes_list)
ax.axvline(avg, color='#ff6b6b', linestyle='--',
label=f'평균 {int(avg//60):02d}:{int(avg%60):02d}')
ax.set_xticks([m for m in bins if m % 60 == 0])
ax.set_xticklabels([f"{m//60:02d}:00" for m in bins if m % 60 == 0],
rotation=45, fontsize=8)
ax.set_ylabel('일수')
ax.set_title('출근 시각 분포')
ax.legend(loc='upper right', fontsize=8)
ax.grid(axis='y', alpha=0.3)
widget._canvas.draw()
def draw_weekday_avg(widget: QWidget, records: List[dict]) -> None:
"""요일별 평균 근무시간 막대 그래프."""
if not getattr(widget, '_figure', None):
return
fig = widget._figure
fig.clear()
from datetime import datetime as _dt
weekday_totals = [0.0] * 7
weekday_counts = [0] * 7
for r in records:
try:
d = _dt.strptime(r['date'], '%Y-%m-%d')
except (ValueError, TypeError):
continue
weekday_totals[d.weekday()] += r.get('total_hours', 0) or 0
weekday_counts[d.weekday()] += 1
avg = [(t / c) if c else 0 for t, c in zip(weekday_totals, weekday_counts)]
labels = ['', '', '', '', '', '', '']
ax = fig.add_subplot(111)
colors = ['#4a90e2'] * 5 + ['#ff6b6b'] * 2 # 주말 강조
ax.bar(labels, avg, color=colors)
ax.set_ylabel('평균 시간')
ax.set_title('요일별 평균 근무시간')
ax.grid(axis='y', alpha=0.3)
widget._canvas.draw()