散景 NumerFormatter 瑞士法郎

问题描述

我目前正在使用 bokeh 开发一个包含瑞士法郎值表的 Web 应用程序。我不知道如何格式化以下样式的数字

1'000'000

所以用破折号代替逗号作为 1000 分隔符。向下看文档 bokeh 似乎仅限于此处给出的选项

https://docs.bokeh.org/en/latest/docs/reference/models/widgets.tables.html?highlight=numberfor#bokeh.models.widgets.tables.NumberFormatter

但 numbro 文档似乎暗示还有更多内容

http://numbrojs.com/format.html

我仍然找不到“de-CH”选项或类似的东西,即使我找到了,我也不知道如何在散景中交出它。有没有人有经验?

最好的问候。

解决方法

Bokeh 为您提供了使用 FuncTickFormatter 定义自己的刻度格式器的选项。有了它,您可以在 JavaScript 中定义自己的规则。

您可以在下面看到调整规则的示例。

from bokeh.plotting import figure,output_notebook,show
from bokeh.models import FuncTickFormatter
output_notebook()

# create a new plot with the toolbar below
p = figure(plot_width=400,plot_height=400,title=None,toolbar_location="below",)
x = [1e3,3e3,5e3,1e4,3e4]
y = [2,5,8,2,7]
p.circle(x,y,size=10)
p.xaxis.formatter = FuncTickFormatter(code=
    '''
    var num =  tick.toFixed(2).toString()
    var splitted = num.split(".")
    for (var i=splitted[0].length-3; 0<i; i+=-3){
      num = num.slice(0,i)+"'"+num.slice(i)
    }
    return num
    '''
)

对于一组不同的 xy

,输出看起来像这样
**Ticks 滴答声 2e6
x = [1e3,3e4] x = [1e6,5e6,9e6]
y = [2,7] y = [2,2]
Tick e3 Tick e6