Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The chart did not update when the slider was dragged #13581

Closed
wjquant opened this issue Dec 5, 2023 · 3 comments
Closed

The chart did not update when the slider was dragged #13581

wjquant opened this issue Dec 5, 2023 · 3 comments

Comments

@wjquant
Copy link

wjquant commented Dec 5, 2023

Software versions

Name: bokeh
Version: 3.3.0
Summary: Interactive plots and applications in the browser from Python
Home-page:
Author: Bokeh Team

Browser name and version

Chrome 119.0.6045.200(正式版本) (64 位)

Jupyter notebook / Jupyter Lab version

No response

Expected behavior

The chart update when the slider was dragged

Observed behavior

The chart did not update when the slider was dragged

Example code

from bokeh.plotting import figure, curdoc
from bokeh.models import ColumnDataSource, HoverTool, Slider
import pandas as pd
from bokeh.layouts import gridplot
import os


script_dir = os.path.dirname(os.path.abspath(__file__))
# 加载5分钟数据
csv_file_5min = os.path.join(script_dir, 'sz123228_5min.csv')
df_5min = pd.read_csv(csv_file_5min)
df_5min["date"] = pd.to_datetime(df_5min["date"])
# 创建5分钟图表
p_5min = figure(x_axis_type="datetime", title=f"sz123228_5min", width=1000, height=300, y_axis_location="right")
p_5min.xaxis.major_label_overrides = {i: date.strftime('%m/%d %H:%M') for i, date in zip(df_5min.index, df_5min["date"])}
inc = df_5min.close >= df_5min.open
dec = df_5min.open > df_5min.close
source_inc_5min = ColumnDataSource(df_5min[inc])
source_dec_5min = ColumnDataSource(df_5min[dec])
p_5min.segment(x0='index', y0='high', x1='index', y1='low', source=source_inc_5min, color="red")
p_5min.segment(x0='index', y0='high', x1='index', y1='low', source=source_dec_5min, color="green")
p_5min.vbar(x='index', width=0.5, top='open', bottom='close', source=source_inc_5min, fill_color="white", line_color="red", line_width=1)
p_5min.vbar(x='index', width=0.5, top='open', bottom='close', source=source_dec_5min, fill_color="white", line_color="green", line_width=1)

# 加载15分钟终表数据
csv_file_15min_chart = os.path.join(script_dir, 'sz123228_15min_chart.csv')
df_15min_chart = pd.read_csv(csv_file_15min_chart)
df_15min_chart["date"] = pd.to_datetime(df_15min_chart["date"])
# 创建15分钟终表图表
p_15min = figure(x_axis_type="datetime", title=f"sz123228_15min_chart", width=1000, height=300, y_axis_location="right")
p_15min.xaxis.major_label_overrides = {i: date.strftime('%m/%d %H:%M') for i, date in zip(df_15min_chart.index, df_15min_chart["date"])}
inc_15min = df_15min_chart.close >= df_15min_chart.open
dec_15min = df_15min_chart.open > df_15min_chart.close
source_inc_15min = ColumnDataSource(df_15min_chart[inc_15min])
source_dec_15min = ColumnDataSource(df_15min_chart[dec_15min])
p_15min.segment(x0='index', y0='high', x1='index', y1='low', source=source_inc_15min, color="red")
p_15min.segment(x0='index', y0='high', x1='index', y1='low', source=source_dec_15min, color="green")
p_15min.vbar(x='index', width=0.5, top='open', bottom='close', source=source_inc_15min, fill_color="white", line_color="red", line_width=1)
p_15min.vbar(x='index', width=0.5, top='open', bottom='close', source=source_dec_15min, fill_color="white", line_color="green", line_width=1)

hover = HoverTool()
hover.tooltips = [("日期", "@date{%F %H:%M}"), ("开盘价", "@open{0,0.000}"), ("最高价", "@high{0,0.000}"), ("最低价", "@low{0,0.000}"), ("收盘价", "@close{0,0.000}")]
hover.formatters = {'@date': 'datetime'}
p_5min.add_tools(hover)
p_15min.add_tools(hover)

initial_date = df_5min["date"].iloc[0].strftime('%m/%d %H:%M')
slider = Slider(start=0, end=len(df_5min), value=len(df_5min), step=1, title=f'起点: {initial_date}', width=1000)

# 加载15分钟中间过程数据
csv_file_15min_process = os.path.join(script_dir, 'sz123228_15min.csv')
df_15min_process = pd.read_csv(csv_file_15min_process)
df_15min_process["date"] = pd.to_datetime(df_15min_process["date"])


def slider_callback(attr, old_range, new_range):
    end_idx = int(slider.value)
    if end_idx >= len(df_5min):
        end_idx = len(df_5min) - 1

    temp = int(end_idx / 3)  # 15"盘中索引号
    df_intraday = pd.DataFrame(columns=df_15min_chart.columns)  # 创建 15" 盘中ohlc df二维的数据结构
    for i in range(temp):
        df_intraday.loc[i] = df_15min_chart.loc[i]
    df_intraday.loc[temp] = df_15min_process.loc[end_idx]
    print(df_intraday)

    inc_15min = df_intraday.close >= df_intraday.open
    dec_15min = df_intraday.open > df_intraday.close
    source_inc_15min.data = df_intraday[inc_15min].to_dict('list')
    source_dec_15min.data = df_intraday[dec_15min].to_dict('list')

    selected_date_5min = df_5min["date"].iloc[end_idx].strftime('%m/%d %H:%M')
    slider.title = f'{selected_date_5min};____________15"盘中index:{temp};____________5“index'
    p_5min.x_range.start = df_5min.index[max(0, end_idx-50)] - 0.4
    p_5min.x_range.end = df_5min.index[end_idx] + 0.4
    p_15min.x_range.start = df_intraday.index[max(0, temp-50)] - 0.4
    p_15min.x_range.end = df_intraday.index[temp] + 0.4
    p_15min.add_tools(hover)

slider.on_change('value', slider_callback)
app_layout = gridplot([[p_5min], [p_15min], [slider]])
curdoc().add_root(app_layout)

Stack traceback or browser console output

No response

Screenshots

No response

@wjquant wjquant added the TRIAGE label Dec 5, 2023
@wjquant
Copy link
Author

wjquant commented Dec 5, 2023

@bryevdv
Copy link
Member

bryevdv commented Dec 5, 2023

@wjquant This seems like a usage/support issue. E.g., re-adding the hover tool on every slider move is almost certainly not a good idea. The GitHub issue tracker is reserved for bug reports and feature requests only. It is not the appropriate place to seek general support or help with usage questions. For that, please consider Discourse site or Stack Overflow. If, after discussion there, an actual issue is identified, we can re-open.

@wjquant
Copy link
Author

wjquant commented Dec 9, 2023

Please allow me to express my heartfelt gratitude to the GitHub community for assisting me in resolving my issue. Your guidance and direction have been invaluable in finding a solution. The problem has now been effectively resolved, and I want to emphasize how truly grateful I am.

Thank you for providing the guidance that led to a successful resolution. Your support is greatly appreciated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants