pyecharts 原生 Javascript

2023-02-17 11:12 更新

pyecharts 支持傳入原生 JS 函數(shù),這些函數(shù)大都用于在設(shè)置 formatter 的時(shí)候使用,如 ?Geo ?圖的 formatter 默認(rèn)就是通過(guò)回調(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? 浮點(diǎn)數(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')")

打開瀏覽器控制臺(tái)就可以看到輸出了 ?hello world?。


以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)