Roberta2024 commited on
Commit
98655f8
·
verified ·
1 Parent(s): 17a30d8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -34
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("選擇欄位來繪製圖表", selected_df.columns)
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
- if pd.api.types.is_numeric_dtype(selected_df[column_choice]):
57
- # 創建一個標籤頁布局
58
- tab1, tab2 = st.tabs(["圓餅圖", "長條圖"])
59
-
60
- with tab1:
61
- # 使用 plotly 創建圓餅圖
62
- fig_pie = px.pie(selected_df, names=selected_df.index, values=column_choice,
63
- title=f"{dataset_choice} - {column_choice} 圓餅圖",
64
- color_discrete_sequence=px.colors.qualitative.Pastel)
65
- fig_pie.update_traces(textposition='inside', textinfo='percent+label')
66
- fig_pie.update_layout(
67
- font=dict(size=14),
68
- legend=dict(orientation="h", yanchor="bottom", y=1.02, xanchor="right", x=1)
69
- )
70
- st.plotly_chart(fig_pie, use_container_width=True)
71
-
72
- with tab2:
73
- # 使用 plotly 創建長條圖
74
- fig_bar = px.bar(selected_df, x=selected_df.index, y=column_choice,
75
- title=f"{dataset_choice} - {column_choice} 長條圖",
76
- color_discrete_sequence=px.colors.qualitative.Pastel)
77
- fig_bar.update_layout(
78
- xaxis_title="企業",
79
- yaxis_title=column_choice,
80
- font=dict(size=14),
81
- xaxis_tickangle=-45
82
- )
83
- st.plotly_chart(fig_bar, use_container_width=True)
84
-
85
- st.success("圖表生成完成!")
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"