-
Notifications
You must be signed in to change notification settings - Fork 7.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: chart tool can't display chinese (#9686)
- Loading branch information
Showing
2 changed files
with
23 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,77 +1,36 @@ | ||
import matplotlib.pyplot as plt | ||
from fontTools.ttLib import TTFont | ||
from matplotlib.font_manager import findSystemFonts | ||
from matplotlib.font_manager import FontProperties | ||
|
||
from core.tools.errors import ToolProviderCredentialValidationError | ||
from core.tools.provider.builtin.chart.tools.line import LinearChartTool | ||
from core.tools.provider.builtin_tool_provider import BuiltinToolProviderController | ||
|
||
# use a business theme | ||
plt.style.use("seaborn-v0_8-darkgrid") | ||
plt.rcParams["axes.unicode_minus"] = False | ||
|
||
|
||
def init_fonts(): | ||
fonts = findSystemFonts() | ||
|
||
popular_unicode_fonts = [ | ||
def set_chinese_font(): | ||
font_list = [ | ||
"PingFang SC", | ||
"SimHei", | ||
"Microsoft YaHei", | ||
"STSong", | ||
"SimSun", | ||
"Arial Unicode MS", | ||
"DejaVu Sans", | ||
"DejaVu Sans Mono", | ||
"DejaVu Serif", | ||
"FreeMono", | ||
"FreeSans", | ||
"FreeSerif", | ||
"Liberation Mono", | ||
"Liberation Sans", | ||
"Liberation Serif", | ||
"Noto Mono", | ||
"Noto Sans", | ||
"Noto Serif", | ||
"Open Sans", | ||
"Roboto", | ||
"Source Code Pro", | ||
"Source Sans Pro", | ||
"Source Serif Pro", | ||
"Ubuntu", | ||
"Ubuntu Mono", | ||
"Noto Sans CJK SC", | ||
"Noto Sans CJK JP", | ||
] | ||
|
||
supported_fonts = [] | ||
for font in font_list: | ||
chinese_font = FontProperties(font) | ||
if chinese_font.get_name() == font: | ||
return chinese_font | ||
|
||
for font_path in fonts: | ||
try: | ||
font = TTFont(font_path) | ||
# get family name | ||
family_name = font["name"].getName(1, 3, 1).toUnicode() | ||
if family_name in popular_unicode_fonts: | ||
supported_fonts.append(family_name) | ||
except: | ||
pass | ||
return FontProperties() | ||
|
||
plt.rcParams["font.family"] = "sans-serif" | ||
# sort by order of popular_unicode_fonts | ||
for font in popular_unicode_fonts: | ||
if font in supported_fonts: | ||
plt.rcParams["font.sans-serif"] = font | ||
break | ||
|
||
|
||
init_fonts() | ||
# use a business theme | ||
plt.style.use("seaborn-v0_8-darkgrid") | ||
plt.rcParams["axes.unicode_minus"] = False | ||
font_properties = set_chinese_font() | ||
plt.rcParams["font.family"] = font_properties.get_name() | ||
|
||
|
||
class ChartProvider(BuiltinToolProviderController): | ||
def _validate_credentials(self, credentials: dict) -> None: | ||
try: | ||
LinearChartTool().fork_tool_runtime( | ||
runtime={ | ||
"credentials": credentials, | ||
} | ||
).invoke( | ||
user_id="", | ||
tool_parameters={ | ||
"data": "1,3,5,7,9,2,4,6,8,10", | ||
}, | ||
) | ||
except Exception as e: | ||
raise ToolProviderCredentialValidationError(str(e)) | ||
pass |