2017-09-25 16 views
1

我有一个简单的ColumnDataSource多列,每列表示一个模拟的不同日子,每一行代表具有状态a,b,c等的实体的数量。我希望能够用滑块擦洗日子(列)。使用带滑块的CustomJS回调绘制不同的列

我已经采取一看12和散景docs的信息,但我一直没能成功地得到它的工作。我有以下代码(最小):

from bokeh.plotting import figure 
from bokeh.layouts import column, widgetbox 
from bokeh.models import CustomJS, Slider, ColumnDataSource, ranges 
from bokeh.io import output_file, show 

output_file("barplot.html") 
sim_time=4 

data = {'x': ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'], 
     '0': [2860.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], 
     '1': [2860.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], 
     '2': [0.0, 2860.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], 
     '3': [0.0, 2860.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]} 

source = ColumnDataSource(data) 

barplot = figure(plot_width=800, plot_height=600, tools='pan', x_axis_label='Status', x_range=source.data['x'], y_range=ranges.Range1d(start=0, end=3000)) 
barplot.vbar(source=source, x='x', top='0', width=0.6) 

callback = CustomJS(args={'source':source}, code=""" 
    console.log(' changed selected time', cb_obj.value); 
    var data = source.data; 
    data['0'] = data[cb_obj.value]; 
    source.change.emit() 
""") 

time_slider = Slider(start=0, end=sim_time-1, value=1, step=1, callback=callback) 
callback.args["time"] = time_slider 

show(column(barplot, time_slider)) 

即我无法使用滑块遍历列。

我在做什么错?

干杯

回答

0

试试这个:

data = {'x': ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'], 
     'y': [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], 
     '0': [0.0, 0.0, 0.0, 0.0, 500.0, 0.0, 0.0, 0.0], 
     '1': [0.0, 1000.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], 
     '2': [0.0, 0.0, 1000.0, 0.0, 0.0, 0.0, 0.0, 0.0], 
     '3': [0.0, 0.0, 0.0, 1000.0, 0.0, 0.0, 0.0, 0.0]} 

source = ColumnDataSource(data) 

barplot = figure(plot_width=800, plot_height=600, tools='pan', 
       x_axis_label='Status', x_range=source.data['x'], 
       y_range=ranges.Range1d(start=0, end=3000)) 
barplot.vbar(source=source, x='x', top='y', width=0.6) 

callback = CustomJS(args=dict(source=source), code=""" 
    console.log(' changed selected time', cb_obj.value); 
    var data = source.data; 
    data['y'] = data[cb_obj.value]; 
    source.change.emit() 
""") 

time_slider = Slider(start=0, end=sim_time-1, value=0, step=1, callback=callback) 
callback.args["time"] = time_slider  
show(column(barplot, time_slider)) 

有你的代码的两个问题。您的酒吧为0123是相同的。在选择2和3之间不会发生任何变化。另外,您还可以使用0列来存储数据。但是你用你的JS代码覆盖0中的数据。因此,数据会丢失。因此,在滑块上切换回0不会产生任何影响。这就是为什么我添加了一个新的(空)列y到您的数据。这只是一个占位符,用于绘制保存当前选定的数据。

+0

我很欣赏快速回复!我应用了所有建议,但擦洗仍然无效。在查看浏览器控制台时,它告诉我source.change.emit()抛出了“undefined不是对象”的TypeErrors。有什么想法吗? – okurniawan

+0

解决了这个问题 - 这个功能在散景0.12.5上被打破,但在0.12.7上功能完整。 – okurniawan