filter - 根据 RangeTool 中的日期时间散景过滤 CDSView
问题描述
我正在尝试根据另一个图中的 rangetool 控制的日期范围过滤散点图 (p1) 的数据。
这将是此处已经显示的内容的变体:https ://docs.bokeh.org/en/latest/docs/gallery/range_tool.html
这里是一个 MWE(不适用于 p1):
import numpy as np
from bokeh.io import show
from bokeh.layouts import column
from bokeh.models import ColumnDataSource, RangeTool
from bokeh.plotting import figure
from bokeh.sampledata.stocks import AAPL
dates = np.array(AAPL['date'], dtype=np.datetime64)
source = ColumnDataSource(data=dict(date=dates, close=AAPL['adj_close'], volume=AAPL['volume']))
####################### p
p = figure(plot_height=300, plot_width=800, tools="xpan", toolbar_location=None,
x_axis_type="datetime", x_axis_location="above",
background_fill_color="#efefef", x_range=(dates[1500], dates[2500]))
p.line('date', 'close', source=source)
p.yaxis.axis_label = 'Price'
################ p1
p1 = figure(plot_height=300, plot_width=800, tools="xpan", toolbar_location=None,
background_fill_color="#efefef")
p1.circle (x='close', y='volume', source=source)
##################### select (rangeslider)
select = figure(title="Drag the middle and edges of the selection box to change the range above",
plot_height=130, plot_width=800, y_range=p.y_range,
x_axis_type="datetime", y_axis_type=None,
tools="", toolbar_location=None, background_fill_color="#efefef")
range_tool = RangeTool(x_range=p.x_range)
range_tool.overlay.fill_color = "navy"
range_tool.overlay.fill_alpha = 0.2
select.line('date', 'close', source=source)
select.ygrid.grid_line_color = None
select.add_tools(range_tool)
select.toolbar.active_multi = range_tool
#####################
show(column(p,p1, select))
我想使用“RangeTool(x_range=p.x_range)”来控制 p1 源的过滤器。任何帮助将不胜感激
解决方案
这是一个例子:
import numpy as np
from bokeh.layouts import column
from bokeh.models import ColumnDataSource, RangeTool, CustomJS, CDSView, CustomJSFilter
from bokeh.plotting import figure, show
from bokeh.sampledata.stocks import AAPL
dates = np.array(AAPL['date'], dtype=np.datetime64)
source = ColumnDataSource(data=dict(date=dates, close=AAPL['adj_close'], volume=AAPL['volume']))
p = figure(plot_height=300, x_axis_type="datetime", x_range=(dates[1500], dates[2500]))
p.line('date', 'close', source=source)
range_tool = RangeTool(x_range=p.x_range)
p.x_range.callback = CustomJS(args=dict(source=source), code="source.change.emit();")
date_filter = CustomJSFilter(args=dict(source=source, x_range=p.x_range), code="""
let start=x_range.start;
let end=x_range.end;
let dates = source.data['date'];
let indices = [];
for (var i = 0; i <= dates.length; i++){
if (dates[i] >= start && dates[i] <= end) indices.push(i);
}
return indices;
""")
view = CDSView(source=source, filters=[date_filter])
p1 = figure(plot_height=300)
p1.circle (x='close', y='volume', source=source, view=view)
select = figure(plot_height=130, x_axis_type="datetime")
select.line('date', 'close', source=source)
select.add_tools(range_tool)
show(column(p, p1, select))
推荐阅读
- git - 使用 git add -p 时如何配置 git 以显示移动的行?
- oracle-data-integrator - 如何在 ODI 中的 2 个表之间进行多个联接?
- java - 捕获 ARCore ArSceneView 的位图
- java - 使用嵌套方法的方法引用运算符很热门?
- maven - Maven UnknownArchetype 异常
- multithreading - 在 pthread_cond_timedwait 和 pthread_mutex_lock 上崩溃
- git - GitHub 拉取请求与 Git 命令行合并
- javascript - 将 agSetColumnFilter 与服务器端行模型一起使用
- r - 为什么此应用函数返回与循环不同的值?
- java - Android中的Animate Drawable图标 - ClassCastException VectorDrawable无法转换为Animatable