Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -41,8 +41,12 @@ selected_df = load_data(urls[dataset_choice])
|
|
41 |
st.write("### 爬取的資料預覽")
|
42 |
st.dataframe(selected_df.head())
|
43 |
|
|
|
|
|
|
|
|
|
44 |
# 允許用戶選擇用於繪製圖表的列
|
45 |
-
column_choice = st.selectbox("選擇欄位來繪製圖表",
|
46 |
|
47 |
# 添加一個生成圖表的按鈕
|
48 |
if st.button("生成圖表"):
|
@@ -52,39 +56,37 @@ if st.button("生成圖表"):
|
|
52 |
time.sleep(0.01)
|
53 |
progress_bar.progress(i + 1)
|
54 |
|
55 |
-
#
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
else:
|
87 |
-
st.error("選定的欄位不是數值類型,無法繪製圖表。")
|
88 |
|
89 |
# 下載並設置自定義字體以顯示中文字符
|
90 |
font_url = "https://drive.google.com/uc?id=1eGAsTN1HBpJAkeVM57_C7ccp7hbgSz3_&export=download"
|
|
|
41 |
st.write("### 爬取的資料預覽")
|
42 |
st.dataframe(selected_df.head())
|
43 |
|
44 |
+
# 過濾出數值類型的列,排除 '出表日期' 和 '報告年度'
|
45 |
+
numeric_columns = selected_df.select_dtypes(include=['float64', 'int64']).columns
|
46 |
+
numeric_columns = [col for col in numeric_columns if col not in ['出表日期', '報告年度']]
|
47 |
+
|
48 |
# 允許用戶選擇用於繪製圖表的列
|
49 |
+
column_choice = st.selectbox("選擇欄位來繪製圖表", numeric_columns)
|
50 |
|
51 |
# 添加一個生成圖表的按鈕
|
52 |
if st.button("生成圖表"):
|
|
|
56 |
time.sleep(0.01)
|
57 |
progress_bar.progress(i + 1)
|
58 |
|
59 |
+
# 創建一個標籤頁布局
|
60 |
+
tab1, tab2 = st.tabs(["圓餅圖", "長條圖"])
|
61 |
+
|
62 |
+
with tab1:
|
63 |
+
# 使用 plotly 創建圓餅圖
|
64 |
+
fig_pie = px.pie(selected_df, names=selected_df['公司名稱'], values=column_choice,
|
65 |
+
title=f"{dataset_choice} - {column_choice} 圓餅圖",
|
66 |
+
color_discrete_sequence=px.colors.qualitative.Pastel)
|
67 |
+
fig_pie.update_traces(textposition='inside', textinfo='percent+label')
|
68 |
+
fig_pie.update_layout(
|
69 |
+
font=dict(size=14),
|
70 |
+
legend=dict(orientation="h", yanchor="bottom", y=1.02, xanchor="right", x=1)
|
71 |
+
)
|
72 |
+
st.plotly_chart(fig_pie, use_container_width=True)
|
73 |
+
|
74 |
+
with tab2:
|
75 |
+
# 使用 plotly 創建長條圖
|
76 |
+
fig_bar = px.bar(selected_df, x='公司名稱', y=column_choice,
|
77 |
+
title=f"{dataset_choice} - {column_choice} 長條圖",
|
78 |
+
color='公司名稱', # 為每個公司使用不同的顏色
|
79 |
+
color_discrete_sequence=px.colors.qualitative.Pastel)
|
80 |
+
fig_bar.update_layout(
|
81 |
+
xaxis_title="企業",
|
82 |
+
yaxis_title=column_choice,
|
83 |
+
font=dict(size=14),
|
84 |
+
xaxis_tickangle=-45,
|
85 |
+
showlegend=False # 隱藏圖例,因為顏色已經用於區分不同的公司
|
86 |
+
)
|
87 |
+
st.plotly_chart(fig_bar, use_container_width=True)
|
88 |
+
|
89 |
+
st.success("圖表生成完成!")
|
|
|
|
|
90 |
|
91 |
# 下載並設置自定義字體以顯示中文字符
|
92 |
font_url = "https://drive.google.com/uc?id=1eGAsTN1HBpJAkeVM57_C7ccp7hbgSz3_&export=download"
|