Spaces:
Sleeping
Sleeping
feat(rank): independent top-N / bottom-N counts (either may be 0)
Browse filesTwo sliders (0-11) replace the single "N each" slider; legend only lists
the classes that are used and is re-centred; bottom never overlaps top.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
1863 unmodified lines18641865186618671868186918708 unmodified lines187918801881188218831884188518861887188818891890189187 unmodified lines19791980198119821983198419851986198770 unmodified lines205820592060206120622063206420652066206720682069572 unmodified lines2642264326442645264626472648144 unmodified lines27932794279527962797279827991863 unmodified linesov.text(x + wl + (w - wl) / 2, y, value_text, ha="center", va="center", fontsize=fs, fontweight="bold",color="#111827" if ranked else "#9ca3af", zorder=9)def render_rank_infographic(entries, title, subtitle, unit_note, footnote, descending, top_n, colors,figsize=(14.0, 9.6)):"""資訊圖表風的縣市排名圖:中間是臺灣本島(依名次著色),左右兩欄與上下排是膠囊標籤8 unmodified linesvalues = {n: v for n, (_, v) in entries.items()}ranked = sorted(values, key=lambda n: values[n], reverse=descending)total = len(ranked)n_each = min(top_n, total // 2) if total > 1 else 0rank_no = {n: 1 + sum(1 for m in ranked if (values[m] > values[n] if descending else values[m] < values[n]))for n in ranked}def klass(n):if n not in values: return "none"i = ranked.index(n)return "top" if i < n_each else "bottom" if i >= total - n_each else "other"color_of = {"top": c_top, "bottom": c_bottom, "other": c_other, "none": "#e5e7eb"}# --- 版面(單位:英吋)---87 unmodified linest_main.set_x(start)if t_sub: t_sub.set_x(start + w1 + 0.15)n_label = n_each if n_each else top_nlegend = [(c_top, f"排名前{n_label}"), (c_bottom, f"排名後{n_label}"), (c_other, "其他")]lx = W / 2 - 3.3for color, text in legend:ov.add_patch(FancyBboxPatch((lx, H - 1.28), 1.5, 0.1, boxstyle="round,pad=0,rounding_size=0.05", fc=color,ec=color, zorder=5))70 unmodified lines"r", name="註解")prs.save(path)def _rank_figure(input_df, store, title, subtitle, unit_note, footnote, order, top_n, c_top, c_bottom, c_other):"""回傳 (matplotlib 圖, scene);scene 供匯出原生 PPTX 使用"""store = merge_table_into_store(input_df, store)entries = _county_numeric_entries(store)return render_rank_infographic(entries, title, subtitle, unit_note, footnote, descending=(order == INFO_ORDERS[0]), top_n=int(top_n),colors=(c_top or INFO_DEFAULT_COLORS[0], c_bottom or INFO_DEFAULT_COLORS[1], c_other or INFO_DEFAULT_COLORS[2]))def preview_rank_infographic(*args):572 unmodified linesrk_unit = gr.Textbox(label="單位說明(圖例右側的小字)", value="每千人的新生人口數(‰)")rk_note = gr.Textbox(label="註解(右下角)", value="")rk_order = gr.Radio(INFO_ORDERS, label="名次排序", value=INFO_ORDERS[0])rk_topn = gr.Slider(1, 8, value=5, step=1, label="標示前/後幾名")with gr.Row():rk_c1 = gr.ColorPicker(label="排名前段", value=INFO_DEFAULT_COLORS[0])rk_c2 = gr.ColorPicker(label="排名後段", value=INFO_DEFAULT_COLORS[1])144 unmodified linesinputs=[manual_input_df, manual_store, manual_title, manual_color, manual_label_mode, manual_label_size,manual_ppt_overview, manual_ppt_table],outputs=manual_file)rank_opts = [manual_input_df, manual_store, rk_title, rk_sub, rk_unit, rk_note, rk_order, rk_topn,rk_c1, rk_c2, rk_c3]btn_rk.click(fn=preview_rank_infographic, inputs=rank_opts, outputs=rk_preview)for btn, fmt in ((btn_rk_png, "png"), (btn_rk_svg, "svg"), (btn_rk_pptx, "pptx")):1863 unmodified lines18641865186618671868186918708 unmodified lines1879188018811882188318841885188618871888188918901891189287 unmodified lines19801981198219831984198519861987198870 unmodified lines2059206020612062206320642065206620672068206920702071572 unmodified lines264426452646264726482649265026512652144 unmodified lines27972798279928002801280228031863 unmodified linesov.text(x + wl + (w - wl) / 2, y, value_text, ha="center", va="center", fontsize=fs, fontweight="bold",color="#111827" if ranked else "#9ca3af", zorder=9)def render_rank_infographic(entries, title, subtitle, unit_note, footnote, descending, top_n, bottom_n, colors,figsize=(14.0, 9.6)):"""資訊圖表風的縣市排名圖:中間是臺灣本島(依名次著色),左右兩欄與上下排是膠囊標籤8 unmodified linesvalues = {n: v for n, (_, v) in entries.items()}ranked = sorted(values, key=lambda n: values[n], reverse=descending)total = len(ranked)top_k = max(0, min(int(top_n), total)) # 標示前幾名(0=不標)bottom_k = max(0, min(int(bottom_n), total - top_k)) # 標示後幾名;不和前段重疊rank_no = {n: 1 + sum(1 for m in ranked if (values[m] > values[n] if descending else values[m] < values[n]))for n in ranked}def klass(n):if n not in values: return "none"i = ranked.index(n)return "top" if i < top_k else "bottom" if bottom_k and i >= total - bottom_k else "other"color_of = {"top": c_top, "bottom": c_bottom, "other": c_other, "none": "#e5e7eb"}# --- 版面(單位:英吋)---87 unmodified linest_main.set_x(start)if t_sub: t_sub.set_x(start + w1 + 0.15)legend = ([(c_top, f"排名前{top_k}")] if top_k else []) + ([(c_bottom, f"排名後{bottom_k}")] if bottom_k else []) \+ [(c_other, "其他")]lx = W / 2 - (len(legend) * 2.2 - 0.7) / 2 # 圖例項目數不固定,置中排列for color, text in legend:ov.add_patch(FancyBboxPatch((lx, H - 1.28), 1.5, 0.1, boxstyle="round,pad=0,rounding_size=0.05", fc=color,ec=color, zorder=5))70 unmodified lines"r", name="註解")prs.save(path)def _rank_figure(input_df, store, title, subtitle, unit_note, footnote, order, top_n, bottom_n,c_top, c_bottom, c_other):"""回傳 (matplotlib 圖, scene);scene 供匯出原生 PPTX 使用"""store = merge_table_into_store(input_df, store)entries = _county_numeric_entries(store)return render_rank_infographic(entries, title, subtitle, unit_note, footnote, descending=(order == INFO_ORDERS[0]), top_n=int(top_n), bottom_n=int(bottom_n),colors=(c_top or INFO_DEFAULT_COLORS[0], c_bottom or INFO_DEFAULT_COLORS[1], c_other or INFO_DEFAULT_COLORS[2]))def preview_rank_infographic(*args):572 unmodified linesrk_unit = gr.Textbox(label="單位說明(圖例右側的小字)", value="每千人的新生人口數(‰)")rk_note = gr.Textbox(label="註解(右下角)", value="")rk_order = gr.Radio(INFO_ORDERS, label="名次排序", value=INFO_ORDERS[0])with gr.Row():rk_topn = gr.Slider(0, 11, value=5, step=1, label="標示前幾名(0=不標)")rk_botn = gr.Slider(0, 11, value=5, step=1, label="標示後幾名(0=不標)")with gr.Row():rk_c1 = gr.ColorPicker(label="排名前段", value=INFO_DEFAULT_COLORS[0])rk_c2 = gr.ColorPicker(label="排名後段", value=INFO_DEFAULT_COLORS[1])144 unmodified linesinputs=[manual_input_df, manual_store, manual_title, manual_color, manual_label_mode, manual_label_size,manual_ppt_overview, manual_ppt_table],outputs=manual_file)rank_opts = [manual_input_df, manual_store, rk_title, rk_sub, rk_unit, rk_note, rk_order, rk_topn, rk_botn,rk_c1, rk_c2, rk_c3]btn_rk.click(fn=preview_rank_infographic, inputs=rank_opts, outputs=rk_preview)for btn, fmt in ((btn_rk_png, "png"), (btn_rk_svg, "svg"), (btn_rk_pptx, "pptx")):
app.py+14-10