# -*- coding: utf-8 -*-
"""图 5-33　输出甘特图（按教材图意用 Python 重绘，未裁用课本照片）
左侧任务表（任务名称/开始时间/完成时间），右侧按日期刻度的甘特条；
任务数据取教材图 5-31 的 28 行（摘要任务加粗、深色条）。
"""
import os, sys, datetime as dt
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from _draw import setup, canvas, save
import matplotlib.dates as mdates
import matplotlib.pyplot as plt

HERE = os.path.dirname(os.path.abspath(__file__))
OUT = os.path.join(HERE, '..', 'assets', '图5-33-输出甘特图.png')

T = [
 ('1.教务管理系统','2024-11-12','2025-02-05',0),('1.1系统需求','2024-11-12','2024-11-27',1),
 ('1.1.1需求收集','2024-11-12','2024-11-19',2),('1.1.1.1调研问卷设计','2024-11-12','2024-11-14',3),
 ('1.1.1.2访谈','2024-11-15','2024-11-18',3),('1.1.1.3需求讨论会','2024-11-19','2024-11-19',3),
 ('1.1.2需求分析','2024-11-20','2024-11-27',2),('1.1.2.1功能需求分析','2024-11-20','2024-11-25',3),
 ('1.1.2.2非功能需求分析','2024-11-20','2024-11-22',3),('1.1.2.3需求规格说明书','2024-11-26','2024-11-27',3),
 ('完成系统需求工作','2024-11-27','2024-11-27',2),('1.2系统设计','2024-11-28','2024-12-12',1),
 ('1.2.1系统架构设计','2024-11-28','2024-12-03',2),('1.2.2数据库设计','2024-12-04','2024-12-09',2),
 ('1.2.2.1数据库模型设计','2024-12-04','2024-12-05',3),('1.2.2.2数据库表结构设计','2024-12-06','2024-12-06',3),
 ('1.2.2.3数据库关系和约束','2024-12-09','2024-12-09',3),('1.2.3前端设计','2024-12-10','2024-12-11',2),
 ('1.2.4后端设计','2024-12-10','2024-12-12',2),('完成系统设计工作','2024-12-12','2024-12-12',2),
 ('1.3系统开发','2024-12-13','2025-01-14',1),('1.3.1前端开发','2024-12-13','2024-12-25',2),
 ('1.3.1.1登录页面','2024-12-13','2024-12-13',3),('1.3.1.2课程列表页面','2024-12-13','2024-12-16',3),
 ('1.3.1.3成绩查询页面','2024-12-17','2024-12-18',3),('1.3.1.4评教页面','2024-12-19','2024-12-20',3),
 ('1.3.1.5学位与毕业审核页面','2024-12-23','2024-12-25',3),('1.3.2后端开发','2024-12-13','2025-01-14',2),
]

def d(s):
    return dt.datetime.strptime(s, '%Y-%m-%d').date()

setup()
fig = plt.figure(figsize=(12.0, 6.4))
gs = fig.add_gridspec(1, 2, width_ratios=[1.05, 1.0], wspace=0.02)
axL = fig.add_subplot(gs[0]); axL.axis('off')
axL.set_xlim(0, 100); axL.set_ylim(0, len(T) + 1.6)
axL.text(2, len(T) + 0.9, '任务名称', fontsize=6.6, color='#1a365d', fontweight='bold', va='center')
axL.text(74, len(T) + 0.9, '开始时间', fontsize=6.6, color='#1a365d', fontweight='bold', va='center')
axL.text(88.5, len(T) + 0.9, '完成时间', fontsize=6.6, color='#1a365d', fontweight='bold', va='center')
axL.plot([1, 99], [len(T) + 0.45, len(T) + 0.45], color='#90cdf4', lw=0.9)
for i, (name, s, e, lvl) in enumerate(T):
    y = len(T) - i - 0.5
    bold = lvl <= 1
    axL.text(2 + lvl * 3.2, y, name, fontsize=5.0 if not bold else 5.2, color='#1a202c',
             va='center', fontweight='bold' if bold else 'normal')
    axL.text(74, y, s, fontsize=4.4, color='#4a5568', va='center')
    axL.text(88.5, y, e, fontsize=4.4, color='#4a5568', va='center')
    if i % 2 == 0:
        axL.add_patch(plt.Rectangle((1, y - 0.5), 98, 1.0, color='#f7fbff', zorder=0))
    axL.plot([1, 99], [y - 0.5, y - 0.5], color='#edf2f7', lw=0.4)

axR = fig.add_subplot(gs[1], sharey=axL)
axR.set_ylim(0, len(T) + 1.6)
for i, (name, s, e, lvl) in enumerate(T):
    y = len(T) - i - 0.5
    s0, e0 = mdates.date2num(d(s)), mdates.date2num(d(e)) + 1
    color = ['#1a365d', '#2b6cb0', '#63b3ed', '#90cdf4'][min(lvl, 3)]
    axR.barh(y, e0 - s0, left=s0, height=0.55 if lvl >= 2 else 0.75,
             color=color, edgecolor='white', linewidth=0.4)
axR.set_xlim(mdates.date2num(d('2024-11-01')), mdates.date2num(d('2025-02-10')))
axR.xaxis.set_major_locator(mdates.MonthLocator())
axR.xaxis.set_major_formatter(mdates.DateFormatter('%Y年%m月'))
axR.xaxis.set_minor_locator(mdates.DayLocator(bymonthday=[1, 8, 15, 22]))
axR.tick_params(axis='x', labelsize=5.6, length=2, pad=1)
axR.tick_params(axis='y', left=False, labelleft=False)
axR.grid(axis='x', color='#e2e8f0', lw=0.5)
for sp in axR.spines.values():
    sp.set_color('#cbd5e0'); sp.set_linewidth(0.8)
for i in range(1, len(T) + 1):
    axR.axhline(i - 0.5, color='#f1f5f9', lw=0.3, zorder=0)
save(fig, OUT)
