#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
生成出货单长度-数量汇总图表
"""

import matplotlib.pyplot as plt
import matplotlib
matplotlib.rcParams['font.sans-serif'] = ['DejaVu Sans', 'SimHei', 'Arial Unicode MS']
matplotlib.rcParams['axes.unicode_minus'] = False

# 数据：长度(米)和数量(支)
data = [
    (0.528, 204), (0.578, 204), (0.628, 204), (0.678, 204), (0.728, 204),
    (0.778, 204), (0.828, 204), (0.878, 204), (0.928, 204), (0.978, 204),
    (1.028, 204), (1.078, 204), (1.128, 204), (1.178, 204), (1.228, 204),
    (1.278, 204), (1.328, 204), (1.378, 204), (1.428, 204), (1.478, 204),
    (1.528, 614), (2.028, 614), (3.000, 614)
]

lengths = [d[0] for d in data]
quantities = [d[1] for d in data]

# 创建图表
fig, ax = plt.subplots(figsize=(14, 8))

# 使用不同颜色区分数量
colors = ['#4472C4' if q == 204 else '#ED7D31' for q in quantities]

bars = ax.bar(range(len(lengths)), quantities, color=colors, edgecolor='white', linewidth=0.5)

# 设置X轴标签
ax.set_xticks(range(len(lengths)))
ax.set_xticklabels([f'{l:.3f}' for l in lengths], rotation=45, ha='right', fontsize=9)

# 设置标题和标签
ax.set_title('智耀星空出货单 - 长度与数量汇总\n单号: CK26053015 | 日期: 2026-05-30', 
             fontsize=16, fontweight='bold', pad=20)
ax.set_xlabel('Length (m)', fontsize=12)
ax.set_ylabel('Quantity (pcs)', fontsize=12)

# 在柱状图上显示数值
for i, (bar, qty) in enumerate(zip(bars, quantities)):
    height = bar.get_height()
    ax.text(bar.get_x() + bar.get_width()/2., height + 10,
            f'{qty}', ha='center', va='bottom', fontsize=8)

# 添加图例
from matplotlib.patches import Patch
legend_elements = [
    Patch(facecolor='#4472C4', label='204 pcs (20 items)'),
    Patch(facecolor='#ED7D31', label='614 pcs (3 items)')
]
ax.legend(handles=legend_elements, loc='upper left', fontsize=10)

# 添加汇总信息
summary_text = f'Total: 5,922 pcs | Items: 23 | Model: ZH-10X10SJ'
ax.text(0.98, 0.98, summary_text, transform=ax.transAxes, 
        fontsize=11, verticalalignment='top', horizontalalignment='right',
        bbox=dict(boxstyle='round', facecolor='wheat', alpha=0.5))

# 添加网格线
ax.yaxis.grid(True, linestyle='--', alpha=0.7)
ax.set_axisbelow(True)

# 设置Y轴范围
ax.set_ylim(0, 700)

plt.tight_layout()
plt.savefig('/mnt/data/we-smart/users/limaolin/exports/shipment_length_quantity_chart.png', 
            dpi=150, bbox_inches='tight', facecolor='white')
print("图表已保存: /mnt/data/we-smart/users/limaolin/exports/shipment_length_quantity_chart.png")
