pyecharts 支持傳入原生 JS 函數(shù),這些函數(shù)大都用于在設(shè)置 formatter 的時候使用,如 ?Geo
?圖的 formatter 默認(rèn)就是通過回調(diào)函數(shù)設(shè)置的
使用回調(diào)函數(shù)設(shè)置 ?Tooltip Formatter
?
GEO = """function (params) {
return params.name + ' : ' + params.value[2];
}"""
Note: 想使用 ?\n
?, ?\t
? 字符串的話,需要轉(zhuǎn)換為 ?\\n
?, ?\\t
?
然后在 ?set_global_opts
?中配置,所有的 JS 函數(shù)均要使用 ?utils.JsCode
? 類封裝
from pyecharts.commons import utils
geo.set_global_opts(
opts.TooltipOpts(formatter=utils.JsCode(TooltipFormatterType.GEO)),
)
使用回調(diào)函數(shù)設(shè)置 ?Label Formatter
? 浮點數(shù)位數(shù)
FORMATTER = """function (params) {
return window.parseFloat(params.value).toFixed(2)
}
"""
或者可以在任何圖表上附加 JS 代碼
from pyecharts.charts import Bar
bar = Bar()
bar.add_js_funcs("console.log('hello world')")
打開瀏覽器控制臺就可以看到輸出了 ?hello world
?。
更多建議: