Skip to content

Commit

Permalink
fix: chart tool chinese font display and raise error (#11058)
Browse files Browse the repository at this point in the history
  • Loading branch information
hjlarry authored Nov 25, 2024
1 parent 2e00829 commit 56e361a
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions api/core/tools/provider/builtin/chart/chart.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import matplotlib
import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties, fontManager

from core.tools.provider.builtin_tool_provider import BuiltinToolProviderController


def set_chinese_font():
font_list = [
to_find_fonts = [
"PingFang SC",
"SimHei",
"Microsoft YaHei",
Expand All @@ -15,16 +16,16 @@ def set_chinese_font():
"Noto Sans CJK SC",
"Noto Sans CJK JP",
]

for font in font_list:
if font in fontManager.ttflist:
chinese_font = FontProperties(font)
if chinese_font.get_name() == font:
return chinese_font
installed_fonts = frozenset(fontInfo.name for fontInfo in fontManager.ttflist)
for font in to_find_fonts:
if font in installed_fonts:
return FontProperties(font)

return FontProperties()


# use non-interactive backend to prevent `RuntimeError: main thread is not in main loop`
matplotlib.use("Agg")
# use a business theme
plt.style.use("seaborn-v0_8-darkgrid")
plt.rcParams["axes.unicode_minus"] = False
Expand Down

0 comments on commit 56e361a

Please sign in to comment.