Update app.py
Browse files
app.py
CHANGED
|
@@ -246,45 +246,42 @@ 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 |
-
|
| 256 |
-
|
| 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 |
-
|
| 263 |
-
|
|
|
|
| 264 |
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 288 |
else:
|
| 289 |
# 如果沒有分組欄,退回一般長條圖的做法
|
| 290 |
grouped_df = df.groupby(x_column)[y_column].agg(agg_func).reset_index()
|
|
@@ -1160,10 +1157,25 @@ CUSTOM_CSS = """
|
|
| 1160 |
/* 下拉選單位置修正 */
|
| 1161 |
.gradio-dropdown {
|
| 1162 |
position: relative !important;
|
| 1163 |
-
overflow: visible !important;
|
| 1164 |
-
z-index:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1165 |
}
|
| 1166 |
|
|
|
|
| 1167 |
.gradio-dropdown .choices__list--dropdown {
|
| 1168 |
position: absolute !important;
|
| 1169 |
top: 100% !important;
|
|
|
|
| 246 |
'solidity': 0.5
|
| 247 |
}
|
| 248 |
|
| 249 |
+
|
| 250 |
+
elif chart_type == "堆疊長條圖":
|
| 251 |
+
if group_column and group_column in df.columns:
|
| 252 |
+
df[x_column] = df[x_column].astype('category')
|
| 253 |
+
df[group_column] = df[group_column].astype('category')
|
| 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 |
+
pivot_df = grouped_df.pivot(index=x_column, columns=group_column, values='__y__').fillna(0).reset_index()
|
| 261 |
+
categories = [col for col in pivot_df.columns if col != x_column]
|
|
|
|
|
|
|
|
|
|
| 262 |
|
| 263 |
+
for i, category in enumerate(categories):
|
| 264 |
+
color = colors[i % len(colors)]
|
| 265 |
+
if str(category) in custom_colors:
|
| 266 |
+
color = custom_colors[str(category)]
|
| 267 |
|
| 268 |
+
pattern_shape = None
|
| 269 |
+
if patterns and i < len(patterns) and patterns[i] != "無":
|
| 270 |
+
pattern_shape = patterns[i]
|
| 271 |
|
| 272 |
+
fig.add_trace(go.Bar(
|
| 273 |
+
x=pivot_df[x_column],
|
| 274 |
+
y=pivot_df[category],
|
| 275 |
+
name=str(category),
|
| 276 |
+
marker_color=color,
|
| 277 |
+
marker_pattern_shape=pattern_shape
|
| 278 |
+
))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 279 |
|
| 280 |
+
fig.update_layout(barmode='stack')
|
| 281 |
+
else:
|
| 282 |
+
grouped_df = df.groupby(x_column)[y_column].agg(agg_func).reset_index()
|
| 283 |
+
fig = px.bar(grouped_df, x=x_column, y=y_column, color_discrete_sequence=colors, **fig_params)
|
| 284 |
+
|
| 285 |
else:
|
| 286 |
# 如果沒有分組欄,退回一般長條圖的做法
|
| 287 |
grouped_df = df.groupby(x_column)[y_column].agg(agg_func).reset_index()
|
|
|
|
| 1157 |
/* 下拉選單位置修正 */
|
| 1158 |
.gradio-dropdown {
|
| 1159 |
position: relative !important;
|
| 1160 |
+
overflow: visible !important;
|
| 1161 |
+
z-index: 1000 !important;
|
| 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;
|