2016-10-11 86 views
0

我正在构建一个web应用程序,它将显示图像作为数据分析管道的一部分。为此,我需要动态更改散景中对象Figure的宽度和高度。动态改变散景的形状图

使用以下代码,Figure的形状已更改,但更改仅在调整浏览器窗口大小后才生效,即使浏览器窗口调整大小非常小。

import bokeh.plotting 
import bokeh.models 
import bokeh.layouts 

# set up the interface 
fig1 = bokeh.plotting.figure() 
button = bokeh.models.Button(label='scramble') 

# define a callback and connect it 
def callback(): 
    fig1.width = int(fig1.width * .8) 
button.on_click(callback) 

# add everything to the document 
bokeh.plotting.curdoc().add_root(bokeh.layouts.column(button, fig1)) 

是否有一些我需要运行的更新方法?我已经阅读了“下一次刻度线回调”,但我不明白这是否相关。发生

上述行为都与Firefox和铬我的GNOME系统上。

+0

上面的代码以“背景虚化服务” –

回答

1

发生这种情况的原因是因为布局未得到更新。虽然您的代码会更改图形的属性值,但您必须重新计算文档解算器中的所有值才能进行实际调整大小。

这里是BokehJS线,其中调整大小钩发生:

https://github.com/bokeh/bokeh/blob/master/bokehjs/src/coffee/document.coffee#L92

调整大小被称为在文档级别后,调整对象重新渲染:

https://github.com/bokeh/bokeh/blob/master/bokehjs/src/coffee/models/layouts/layout_dom.coffee#L61

的问题在于,据我所知,目前还没有一种公开的方式来重新触发文档调整大小事件。

但是,你可以做到这一点客户端。下面是使用CustomJS工作代码:

test.py

from bokeh.io import show 
from bokeh.layouts import column 
from bokeh.models import Button, CustomJS 
from bokeh.plotting import figure 


fig = figure() 
button = Button(label='scramble') 
button.callback = CustomJS(args=dict(fig=fig), code=""" 
    var old_width = fig.width; 
    var doc = fig.document; 
    fig.width = old_width * 0.8; 
    doc.resize(); 
""") 

col = column(button, fig) 
show(col) 

这可以用python test.py运行。

注意,你也可以与背景虚化服务器curdoc().add_root(col)替换最后一行show(col)做到这一点,但我没有做到这一点强调的是,这是一个客户端解决方案。

0

有一种方法可以通过内置功能动态调整散景图的大小。例如,

fig = plotting.figure(width=1200, height=900, title="Dynamic plot".format(chartType), sizing_mode='scale_width')

密钥选项是sizing_mode='scale_width'

width的和height命令作为初始值。 sizing_mode还有其他选项,所以我会研究一下。

+0

被执行我看着上浆模式但所描述的行为持续存在。 –