提问人:srt111 提问时间:10/15/2023 最后编辑:srt111 更新时间:10/15/2023 访问量:55
在 HoverToolTip 中显示“FILTERED”Dataframe(散景)
Displaying 'FILTERED' Dataframe in HoverToolTip (Bokeh)
问:
在 Bokeh(3.2.2) 中,当尝试通过回调在悬停工具提示中显示过滤后的 pandas 数据帧时,cb_data.source/cb_data.x/cb_data.y = undefined 。
如何在Javascript中检索当前的x,y属性?
错误
cb_data.source = undefined
cb_data.value = undefined
cb_data.x/cb_data.y = undefined/undefined
法典:
import pandas as pd
from bokeh.models import HoverTool, ColumnDataSource, DatetimeTickFormatter, CustomJS
from bokeh.plotting import figure, show
data = {
"Time": ["12:03", "12:03", "12:03", "12:03", "12:12", "12:12", "12:12", "12:12", "12:17", "12:17", "12:17", "12:17"],
"Spot": [19653, 19653, 19653, 19653, 19629, 19629, 19629, 19629, 19640, 19640, 19640, 19640],
"Strike": [19500, 19600, 19700, 19800, 19500, 19600, 19700, 19800, 19500, 19600, 19700, 19800],
"cLtp": [99, 174, 167, 167, 91, 165, 161, 93, 95, 16, 12, 31],
"pLtp": [49, 24, 42, 25, 51, 74, 36, 53, 21, 29, 75, 32],
"cSellOi": [851500, 267500, 379050, 382350, 1324350, 389700, 438850, 1318250, 1365900, 449000, 433200, 433200],
"pSellOi": [2530350, 1615600, 1656000, 1606500, 2542100, 1598450, 1646950, 2519050, 2544450, 1488200, 1460400, 1460400],
}
df = pd.DataFrame(data)
# Convert the 'Time' column to datetime
df['Time'] = pd.to_datetime(df['Time'], format='%H:%M')
df.sort_values(by='Time', inplace=True)
source = ColumnDataSource(df)
plot = figure(
x_axis_label="Time",
y_axis_label="Spot",
x_axis_type="datetime",
title="Dataframe in Tooltip",
)
plot.line(x='Time', y='Spot', source=source, line_width=3, line_color="blue")
plot.xaxis.formatter = DatetimeTickFormatter(
days="%Y-%m-%d",
hours="%H:%M:%S",
minutes="%H:%M"
)
# Create a custom JavaScript callback for the hover tool
jsHover = """
var data = source.data;
var hover = hover;
hover.tooltips = [];
var tooltipString = ""
console.log( "cb_data.source = " + cb_data['source'] );
console.log( "cb_data.value = " + cb_data['value']);
console.log( "cb_data.x/cb_data.y = " + cb_data['x'] +"/" + cb_data['y']);
for (var i = 0; i < data['Strike'].length; i++) {
tooltipString += data['Strike'][i] + ": (" + data['cLtp'][i] + ", " + data['pLtp'][i] + ")[";
tooltipString += data['cSellOi'][i] + ", " + data['pSellOi'][i] + "]\\n";
}
hover.tooltips = tooltipString;
hover.change.emit();
console.log('jsHover Callback Finished');
console.log(hover.tooltips);
hoverTool = HoverTool(tooltips=None)
cb = CustomJS(args={'source': source,'hover': hoverTool}, code=jsHover)
hoverTool.callback = cb
plot.add_tools(hoverTool)
show(plot)
"""
答: 暂无答案
上一个:散景粘性十字准线
评论