2016-10-11 83 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

被執行我看着上漿模式但所描述的行爲持續存在。 –