Update app.py
Browse files
app.py
CHANGED
|
@@ -246,42 +246,45 @@ def create_plot(df, chart_type, x_column, y_column, group_column=None, size_colu
|
|
| 246 |
'solidity': 0.5
|
| 247 |
}
|
| 248 |
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
if agg_func == "count" or y_column == "計數":
|
| 256 |
-
grouped_df = df.groupby([x_column, group_column]).size().reset_index(name='__y__')
|
| 257 |
-
else:
|
| 258 |
-
grouped_df = df.groupby([x_column, group_column])[y_column].agg(agg_func).reset_index(name='__y__')
|
| 259 |
|
| 260 |
-
|
| 261 |
-
|
|
|
|
|
|
|
|
|
|
| 262 |
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
color = custom_colors[str(category)]
|
| 267 |
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
marker_pattern_shape=pattern_shape
|
| 278 |
-
))
|
| 279 |
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
|
| 284 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 285 |
else:
|
| 286 |
# 如果沒有分組欄,退回一般長條圖的做法
|
| 287 |
grouped_df = df.groupby(x_column)[y_column].agg(agg_func).reset_index()
|
|
@@ -1157,25 +1160,10 @@ CUSTOM_CSS = """
|
|
| 1157 |
/* 下拉選單位置修正 */
|
| 1158 |
.gradio-dropdown {
|
| 1159 |
position: relative !important;
|
| 1160 |
-
overflow: visible !important;
|
| 1161 |
-
z-index:
|
| 1162 |
-
}
|
| 1163 |
-
|
| 1164 |
-
.gradio-dropdown .choices__list--dropdown {
|
| 1165 |
-
position: absolute !important;
|
| 1166 |
-
top: 100% !important;
|
| 1167 |
-
left: 0 !important;
|
| 1168 |
-
z-index: 2000 !important;
|
| 1169 |
-
max-height: 300px !important;
|
| 1170 |
-
overflow-y: auto !important;
|
| 1171 |
-
background: white !important;
|
| 1172 |
-
border: 1px solid #ddd !important;
|
| 1173 |
-
border-radius: 4px !important;
|
| 1174 |
-
width: 100% !important;
|
| 1175 |
-
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1) !important;
|
| 1176 |
}
|
| 1177 |
|
| 1178 |
-
|
| 1179 |
.gradio-dropdown .choices__list--dropdown {
|
| 1180 |
position: absolute !important;
|
| 1181 |
top: 100% !important;
|
|
|
|
| 246 |
'solidity': 0.5
|
| 247 |
}
|
| 248 |
|
| 249 |
+
if chart_type == "堆疊長條圖":
|
| 250 |
+
if group_column and group_column in df.columns:
|
| 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],
|
| 282 |
+
name=str(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()
|
|
|
|
| 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 {
|
| 1168 |
position: absolute !important;
|
| 1169 |
top: 100% !important;
|