fix: 绕过 Plotly add_vline 对 datetime.date 的兼容性问题

手动通过 add_shape + add_annotation 添加里程碑竖线标记,
  替换 LAI 和生物量图表中原有的 add_vline 调用。
This commit is contained in:
zhenghu
2026-04-14 15:55:02 +08:00
parent bd3d73d140
commit f27801e36b

37
app.py
View File

@@ -21,6 +21,25 @@ st.set_page_config(
)
def add_milestone_line(fig, x, color: str, text: str):
"""手动添加竖线标记,绕过 Plotly add_vline 对 datetime.date 的 bug。"""
fig.add_shape(
type="line",
x0=x, x1=x, y0=0, y1=1,
xref="x", yref="paper",
line=dict(color=color, width=1.5, dash="dot"),
)
fig.add_annotation(
x=x, y=1.02,
xref="x", yref="paper",
text=text,
showarrow=False,
font=dict(color=color, size=10),
bgcolor="rgba(255,255,255,0.8)",
borderpad=2,
)
# ─── Sidebar ────────────────────────────────────────────────────────────────
with st.sidebar:
st.header("🌾 麦麦智农")
@@ -104,19 +123,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_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_position="top right",
)
add_milestone_line(fig_lai, x=ms["maturity"], color="#7c5e42", text="成熟")
fig_lai.update_layout(
xaxis_title="日期",
yaxis_title="LAI",
@@ -149,9 +158,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_title="日期",
yaxis_title="干物质 (kg/ha)",