fix: 修复 Plotly add_vline 对 datetime.date 的兼容性问题
- 手动使用 shape + annotation 添加生育期 milestone 竖线 - 替换 LAI 和干物质两图中的 add_vline 调用 - 避免 datetime.date 类型触发 Plotly 内部异常
This commit is contained in:
31
app.py
31
app.py
@@ -17,6 +17,25 @@ def hex_to_rgba(hex_color: str, alpha: float = 0.1) -> str:
|
||||
return f"rgba({r}, {g}, {b}, {alpha})"
|
||||
|
||||
|
||||
def add_milestone_line(fig, x, color: str, text: str, xref: str = "x"):
|
||||
"""用 shape + annotation 手动添加竖线,绕过 Plotly add_vline 对 datetime.date 的 bug。"""
|
||||
fig.add_shape(
|
||||
type="line",
|
||||
x0=x, x1=x, y0=0, y1=1,
|
||||
xref=xref, yref="paper",
|
||||
line=dict(color=color, width=1.5, dash="dot"),
|
||||
)
|
||||
fig.add_annotation(
|
||||
x=x, y=1.02,
|
||||
xref=xref, yref="paper",
|
||||
text=text,
|
||||
showarrow=False,
|
||||
font=dict(color=color, size=10),
|
||||
bgcolor="rgba(255,255,255,0.8)",
|
||||
borderpad=2,
|
||||
)
|
||||
|
||||
|
||||
# ─── Page Config ────────────────────────────────────────────────────────────
|
||||
st.set_page_config(
|
||||
page_title="麦麦智农",
|
||||
@@ -289,13 +308,9 @@ with chart_left:
|
||||
# 添加生育期标记
|
||||
ms = summary.get("milestones", {})
|
||||
if "flowering" in ms:
|
||||
fig_lai.add_vline(x=ms["flowering"], line=dict(color="#d4a574", width=1.5, dash="dot"),
|
||||
annotation_text="开花", annotation_font_color="#7c5e42",
|
||||
annotation_position="top left")
|
||||
add_milestone_line(fig_lai, x=ms["flowering"], color="#d4a574", text="开花")
|
||||
if "maturity" in ms:
|
||||
fig_lai.add_vline(x=ms["maturity"], line=dict(color="#7c5e42", width=1.5, dash="dash"),
|
||||
annotation_text="成熟", annotation_font_color="#7c5e42",
|
||||
annotation_position="top right")
|
||||
add_milestone_line(fig_lai, x=ms["maturity"], color="#7c5e42", text="成熟")
|
||||
fig_lai.update_layout(
|
||||
xaxis=dict(title="日期", color="#5a5a5a", gridcolor="rgba(0,0,0,0.06)"),
|
||||
yaxis=dict(title="LAI", color="#5a5a5a", gridcolor="rgba(0,0,0,0.06)"),
|
||||
@@ -327,9 +342,9 @@ with chart_right:
|
||||
))
|
||||
ms = summary.get("milestones", {})
|
||||
if "flowering" in ms:
|
||||
fig_bio.add_vline(x=ms["flowering"], line=dict(color="#d4a574", width=1.5, dash="dot"))
|
||||
add_milestone_line(fig_bio, x=ms["flowering"], color="#d4a574", text="开花")
|
||||
if "maturity" in ms:
|
||||
fig_bio.add_vline(x=ms["maturity"], line=dict(color="#7c5e42", width=1.5, dash="dash"))
|
||||
add_milestone_line(fig_bio, x=ms["maturity"], color="#7c5e42", text="成熟")
|
||||
fig_bio.update_layout(
|
||||
xaxis=dict(title="日期", color="#5a5a5a", gridcolor="rgba(0,0,0,0.06)"),
|
||||
yaxis=dict(title="干物质 (kg/ha)", color="#5a5a5a", gridcolor="rgba(0,0,0,0.06)"),
|
||||
|
||||
Reference in New Issue
Block a user