Lashtw commited on
Commit
6b3fa2a
·
verified ·
1 Parent(s): debc832

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -10
app.py CHANGED
@@ -3,7 +3,6 @@ import pandas as pd
3
  import plotly.graph_objects as go
4
  import os
5
  from zipfile import ZipFile
6
- import plotly.io as pio
7
  try:
8
  import kaleido
9
  except ImportError:
@@ -59,7 +58,7 @@ def create_radar_chart(df, selected_rows, selected_columns):
59
  radialaxis=dict(
60
  visible=True,
61
  range=[0, df[selected_columns].values.max() * 1.1],
62
- tickfont=dict(size=12, color='black', family="Microsoft JhengHei, Noto Sans CJK, Arial") # 調整刻度文字顏色為黑色並設定字體
63
  ),
64
  angularaxis=dict(
65
  tickfont=dict(size=16, color='black', family="Microsoft JhengHei, Noto Sans CJK, Arial") # 放大字體,顏色為黑色並設定字體
@@ -67,7 +66,7 @@ def create_radar_chart(df, selected_rows, selected_columns):
67
  ),
68
  showlegend=True,
69
  legend=dict(
70
- font=dict(size=14, color='black', family="Microsoft JhengHei, Noto Sans CJK, Arial") # 圖例文字加大並改為黑色並設定字體
71
  ),
72
  title='學生成績雷達圖',
73
  plot_bgcolor='white', # 背景為白色
@@ -77,6 +76,30 @@ def create_radar_chart(df, selected_rows, selected_columns):
77
 
78
  return fig
79
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
80
 
81
  def save_all_radar_charts(df, selected_columns, comparison_rows):
82
  """批次生成並儲存每個學生與指定比較對象的雷達圖"""
@@ -88,15 +111,10 @@ def save_all_radar_charts(df, selected_columns, comparison_rows):
88
  continue
89
 
90
  fig = create_radar_chart(df, [student] + comparison_rows, selected_columns)
 
91
 
92
- # 這裡使用 plotly.io.to_image 直接生成圖片
93
- img_bytes = pio.to_image(fig, format="png",
94
- engine="kaleido",
95
- font_family="Microsoft JhengHei, Noto Sans CJK, Arial"
96
- )
97
  file_path = os.path.join(output_dir, f"{student}_comparison.png")
98
- with open(file_path, "wb") as f:
99
- f.write(img_bytes)
100
 
101
  # 壓縮所有圖表
102
  zip_path = "radar_charts.zip"
 
3
  import plotly.graph_objects as go
4
  import os
5
  from zipfile import ZipFile
 
6
  try:
7
  import kaleido
8
  except ImportError:
 
58
  radialaxis=dict(
59
  visible=True,
60
  range=[0, df[selected_columns].values.max() * 1.1],
61
+ tickfont=dict(size=12, color='black', family="Microsoft JhengHei, Noto Sans CJK, Arial") # 調整刻度文字顏色為黑色並設定字體
62
  ),
63
  angularaxis=dict(
64
  tickfont=dict(size=16, color='black', family="Microsoft JhengHei, Noto Sans CJK, Arial") # 放大字體,顏色為黑色並設定字體
 
66
  ),
67
  showlegend=True,
68
  legend=dict(
69
+ font=dict(size=14, color='black', family="Microsoft JhengHei, Noto Sans CJK, Arial") # 圖例文字加大並改為黑色並設定字體
70
  ),
71
  title='學生成績雷達圖',
72
  plot_bgcolor='white', # 背景為白色
 
76
 
77
  return fig
78
 
79
+ def apply_font_to_all_text(fig):
80
+ """強制設定圖表內所有文字元素的字型"""
81
+ for trace in fig.data:
82
+ if hasattr(trace, 'textfont'):
83
+ trace.textfont.family = "Microsoft JhengHei, Noto Sans CJK, Arial"
84
+ if hasattr(trace, 'marker') and hasattr(trace.marker, 'textfont'):
85
+ trace.marker.textfont.family = "Microsoft JhengHei, Noto Sans CJK, Arial"
86
+
87
+ fig.update_layout(
88
+ font=dict(
89
+ family="Microsoft JhengHei, Noto Sans CJK, Arial"
90
+ )
91
+ )
92
+
93
+ if hasattr(fig, 'layout') and hasattr(fig.layout, 'xaxis'):
94
+ fig.layout.xaxis.tickfont.family = "Microsoft JhengHei, Noto Sans CJK, Arial"
95
+ if hasattr(fig, 'layout') and hasattr(fig.layout, 'yaxis'):
96
+ fig.layout.yaxis.tickfont.family = "Microsoft JhengHei, Noto Sans CJK, Arial"
97
+ if hasattr(fig, 'layout') and hasattr(fig.layout, 'polar') and hasattr(fig.layout.polar, 'radialaxis'):
98
+ fig.layout.polar.radialaxis.tickfont.family = "Microsoft JhengHei, Noto Sans CJK, Arial"
99
+ if hasattr(fig, 'layout') and hasattr(fig.layout, 'polar') and hasattr(fig.layout.polar, 'angularaxis'):
100
+ fig.layout.polar.angularaxis.tickfont.family = "Microsoft JhengHei, Noto Sans CJK, Arial"
101
+
102
+ return fig
103
 
104
  def save_all_radar_charts(df, selected_columns, comparison_rows):
105
  """批次生成並儲存每個學生與指定比較對象的雷達圖"""
 
111
  continue
112
 
113
  fig = create_radar_chart(df, [student] + comparison_rows, selected_columns)
114
+ fig = apply_font_to_all_text(fig)
115
 
 
 
 
 
 
116
  file_path = os.path.join(output_dir, f"{student}_comparison.png")
117
+ fig.write_image(file_path, engine="kaleido")
 
118
 
119
  # 壓縮所有圖表
120
  zip_path = "radar_charts.zip"