Update app.py
Browse files
app.py
CHANGED
|
@@ -251,29 +251,31 @@ def create_plot(df, chart_type, x_column, y_column, group_column=None, size_colu
|
|
| 251 |
# 明確將字符串列轉換為類別型
|
| 252 |
df[x_column] = df[x_column].astype('category')
|
| 253 |
df[group_column] = df[group_column].astype('category')
|
| 254 |
-
|
| 255 |
-
#
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
|
|
|
|
|
|
| 264 |
categories = pivot_df.columns.tolist()
|
| 265 |
categories.remove(x_column)
|
| 266 |
-
|
| 267 |
-
#
|
| 268 |
for i, category in enumerate(categories):
|
| 269 |
color = colors[i % len(colors)]
|
| 270 |
if str(category) in custom_colors:
|
| 271 |
color = custom_colors[str(category)]
|
| 272 |
-
|
| 273 |
pattern_shape = None
|
| 274 |
if patterns and i < len(patterns) and patterns[i] != "無":
|
| 275 |
pattern_shape = patterns[i]
|
| 276 |
-
|
| 277 |
fig.add_trace(go.Bar(
|
| 278 |
x=pivot_df[x_column],
|
| 279 |
y=pivot_df[category],
|
|
@@ -281,13 +283,12 @@ def create_plot(df, chart_type, x_column, y_column, group_column=None, size_colu
|
|
| 281 |
marker_color=color,
|
| 282 |
marker_pattern_shape=pattern_shape
|
| 283 |
))
|
| 284 |
-
|
| 285 |
fig.update_layout(barmode='stack')
|
| 286 |
else:
|
| 287 |
-
#
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
fig = px.bar(counts, x=x_column, y='count', **fig_params)
|
| 291 |
|
| 292 |
elif chart_type == "群組長條圖":
|
| 293 |
if group_column and group_column in df.columns:
|
|
@@ -1159,6 +1160,8 @@ CUSTOM_CSS = """
|
|
| 1159 |
/* 下拉選單位置修正 */
|
| 1160 |
.gradio-dropdown {
|
| 1161 |
position: relative !important;
|
|
|
|
|
|
|
| 1162 |
}
|
| 1163 |
|
| 1164 |
.gradio-dropdown .choices__list--dropdown {
|
|
@@ -1172,6 +1175,7 @@ CUSTOM_CSS = """
|
|
| 1172 |
border: 1px solid #ddd !important;
|
| 1173 |
border-radius: 4px !important;
|
| 1174 |
width: 100% !important;
|
|
|
|
| 1175 |
}
|
| 1176 |
|
| 1177 |
/* 確保下拉項目可見 */
|
|
@@ -1179,6 +1183,7 @@ CUSTOM_CSS = """
|
|
| 1179 |
padding: 8px 10px !important;
|
| 1180 |
cursor: pointer !important;
|
| 1181 |
}
|
|
|
|
| 1182 |
"""
|
| 1183 |
|
| 1184 |
# 現代化UI界面
|
|
|
|
| 251 |
# 明確將字符串列轉換為類別型
|
| 252 |
df[x_column] = df[x_column].astype('category')
|
| 253 |
df[group_column] = df[group_column].astype('category')
|
| 254 |
+
|
| 255 |
+
# 根據使用者選的聚合函數進行分組計算
|
| 256 |
+
if agg_func == "count" or y_column == "計數":
|
| 257 |
+
grouped_df = df.groupby([x_column, group_column]).size().reset_index(name='__y__')
|
| 258 |
+
else:
|
| 259 |
+
grouped_df = df.groupby([x_column, group_column])[y_column].agg(agg_func).reset_index(name='__y__')
|
| 260 |
+
|
| 261 |
+
# 建立樞紐表
|
| 262 |
+
pivot_df = grouped_df.pivot_table(index=x_column, columns=group_column,
|
| 263 |
+
values='__y__', aggfunc='sum').reset_index().fillna(0)
|
| 264 |
+
|
| 265 |
+
# 類別清單
|
| 266 |
categories = pivot_df.columns.tolist()
|
| 267 |
categories.remove(x_column)
|
| 268 |
+
|
| 269 |
+
# 加入每一組 Bar
|
| 270 |
for i, category in enumerate(categories):
|
| 271 |
color = colors[i % len(colors)]
|
| 272 |
if str(category) in custom_colors:
|
| 273 |
color = custom_colors[str(category)]
|
| 274 |
+
|
| 275 |
pattern_shape = None
|
| 276 |
if patterns and i < len(patterns) and patterns[i] != "無":
|
| 277 |
pattern_shape = patterns[i]
|
| 278 |
+
|
| 279 |
fig.add_trace(go.Bar(
|
| 280 |
x=pivot_df[x_column],
|
| 281 |
y=pivot_df[category],
|
|
|
|
| 283 |
marker_color=color,
|
| 284 |
marker_pattern_shape=pattern_shape
|
| 285 |
))
|
| 286 |
+
|
| 287 |
fig.update_layout(barmode='stack')
|
| 288 |
else:
|
| 289 |
+
# 如果沒有分組欄,退回一般長條圖的做法
|
| 290 |
+
grouped_df = df.groupby(x_column)[y_column].agg(agg_func).reset_index()
|
| 291 |
+
fig = px.bar(grouped_df, x=x_column, y=y_column, color_discrete_sequence=colors, **fig_params)
|
|
|
|
| 292 |
|
| 293 |
elif chart_type == "群組長條圖":
|
| 294 |
if group_column and group_column in df.columns:
|
|
|
|
| 1160 |
/* 下拉選單位置修正 */
|
| 1161 |
.gradio-dropdown {
|
| 1162 |
position: relative !important;
|
| 1163 |
+
overflow: visible !important; /* ✅ 關鍵補充 */
|
| 1164 |
+
z-index: 10 !important; /* 確保浮在上層 */
|
| 1165 |
}
|
| 1166 |
|
| 1167 |
.gradio-dropdown .choices__list--dropdown {
|
|
|
|
| 1175 |
border: 1px solid #ddd !important;
|
| 1176 |
border-radius: 4px !important;
|
| 1177 |
width: 100% !important;
|
| 1178 |
+
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1) !important;
|
| 1179 |
}
|
| 1180 |
|
| 1181 |
/* 確保下拉項目可見 */
|
|
|
|
| 1183 |
padding: 8px 10px !important;
|
| 1184 |
cursor: pointer !important;
|
| 1185 |
}
|
| 1186 |
+
|
| 1187 |
"""
|
| 1188 |
|
| 1189 |
# 現代化UI界面
|