From f27801e36ba45285941d774a9b86264e43588b6a Mon Sep 17 00:00:00 2001 From: zhenghu <1831829219@qq.com> Date: Tue, 14 Apr 2026 15:55:02 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E7=BB=95=E8=BF=87=20Plotly=20add=5Fvlin?= =?UTF-8?q?e=20=E5=AF=B9=20datetime.date=20=E7=9A=84=E5=85=BC=E5=AE=B9?= =?UTF-8?q?=E6=80=A7=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 手动通过 add_shape + add_annotation 添加里程碑竖线标记, 替换 LAI 和生物量图表中原有的 add_vline 调用。 --- app.py | 37 +++++++++++++++++++++++-------------- 1 file changed, 23 insertions(+), 14 deletions(-) diff --git a/app.py b/app.py index bdf28cf..581f311 100644 --- a/app.py +++ b/app.py @@ -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)",