You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何从Bokeh RangeTool选区内获取y轴数值?

问题:获取Bokeh RangeTool选区内的温度统计值

我正在开发一款展示气象站温度记录的应用,采用Bokeh 3.5.1实现了两个图表:主图表搭配搭载RangeTool的辅助图表用于数据范围选择。我需要在JavaScript客户端获取选区内的y轴数值(最高、最低及平均温度)并计算算术平均值。

触发RangesUpdate事件时,我检查了回调函数中的RangeTool对象(rngt参数)、Plot对象(select参数)和ColumnDataSource对象(source参数),但找不到获取RangeTool选区起止索引的方法或属性。RangeTool的x_range对象返回的是选区视图的时间格式日期,但这些数值并不存在于源数据中,执行source.data['x_axis'].indexOf(rngt.x_range.min)返回-1。

相关代码如下:

def _configure_main_graph(self, dict_keys, station_data, bok_data, legend):
    """Configure the main graph to plot into the html file."""
    station_id = station_data[1]
    station_name = station_data[2].capitalize()
    cn = station_data[3]
    lat = station_data[4]
    lon = station_data[5]
    height = station_data[6]
    country = countries.get(alpha_2=cn)

    # Adapted code from Bokeh official docs RangeTool example
    # Normal graph
    lat = self.dd_to_dms(float(lat), 'lat')
    lon = self.dd_to_dms(float(lon), 'lon')
    
    title = f'Station: {station_id} - {station_name} - Country: {country.name} - Latitude: {lat} - Longitude: {lon} - Height: {height} m.'

    maing = figure(tools="xpan", toolbar_location=None,
               x_axis_type="datetime", x_axis_location="above",
               background_fill_color="#efefef", sizing_mode="scale_both", title=title)

    line = None
    legend_items = []
    tooltips = []
    tooltips.append(("Date:", "@x_axis{%F}"))
    renderers = []

    # For each y axis list of data, plot a line
    for ind, y_name in enumerate(dict_keys):
        line = maing.line('x_axis', y_name, source=bok_data, line_color=self.graph_lines_colors[ind])

        legend_items.append((legend[ind], [line]))

        format_y = f"@{y_name}"
        format_y += '{0.0} ºC'
        tooltips.append((f"Temp. {legend[ind]}:", format_y))

    renderers.append(line)
        
    maing.add_tools(HoverTool(tooltips=tooltips, renderers=renderers, formatters={"@x_axis": "datetime"}, mode="vline"))

    lines_legend = Legend(items=legend_items, location="center")
    maing.add_layout(lines_legend, 'right')

    # Set the y axis label
    maing.yaxis.axis_label = 'Temperature ºC'

    return maing

def _configure_selection_graph(self, maing, dict_keys, bok_data):
    # Range selection graph
    select = figure(title="Drag the middle and edges of the selection box to change the range above",
                    height=300, width_policy="max", y_range=maing.y_range,
                    x_axis_type="datetime", y_axis_type=None,
                    tools="", toolbar_location=None, background_fill_color="#efefef")

    # For each y axis list of data, plot a line
    for ind, y_name in enumerate(dict_keys):
        select.line(x='x_axis', y=y_name, source=bok_data, line_color=self.graph_lines_colors[ind])

    select.ygrid.grid_line_color = None

    # Selection tool
    range_tool = RangeTool(x_range=maing.x_range, start_gesture="pan")
    range_tool.overlay.fill_color = "navy"
    range_tool.overlay.fill_alpha = 0.2

    select.add_tools(range_tool)

    callback = CustomJS(args=dict(source=bok_data, rngt=range_tool, select=select), code="""debugger;""")

    select.js_on_event(RangesUpdate, callback)
    
    if self.acknowledgement:
        select.add_layout(Title(text=self.acknowledgement, align="center", text_align="center", text_font_style="normal", text_color="gray"), "below")
        
    return select

图表截图:
气象站温度图表及RangeTool选区

内容的提问来源于stack exchange,提问作者quimm2003

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.18 17:55:56