简体   繁体   中英

Bokeh Line from AjaxDataSource disappearing on 'replace' update

I am using Bokeh to render a stock price chart that updates by polling for json updates. Strangely, the line graph disappears when using the 'replace' mode on the AjaxDataSource. I can tell it's still there, because there is a HoverTool that still shows the data when it is hovered over. As soon as the first poll is executed and the data is fed back, the line becomes invisible.

# price ajax endpoint
def Data(request, ticker):
    prices = RealtimeStockPrice.objects.filter(ticker=ticker).order_by('last_time')
    x = []
    y = []
    for p in prices:
        x.append(p.last_time.astimezone(timezone('US/Eastern')))
        y.append(float(p.last_price))

    return JsonResponse({'time': x, 'price': y})


# bokeh plotting code
p1 = figure(x_axis_type="datetime", title="Realtime {} Prices".format(signal.ticker))
source = AjaxDataSource(data_url=data_url, polling_interval=10000, mode='replace')
source.data = dict(time=times, price=prices)

# missing line:
p1.line(x='time', y='price', source=source, line_width=3, line_color='#22d18f')

# visible lines:
p1.line(static_times, trigger_price, line_width=2, line_dash='dotted', color='#e14646')
p1.line(static_times, open, line_width=2, line_dash='dotted', color='grey')

plot with the line invisible using 'replace'

when I change the AjaxDataSource's mode='replace' to mode='append', the line is visible once again.

plot with the line visible using 'append'

Is it possible the ajax updates are overwriting the color or visibility/alpha? I would also appreciate any suggestions for debugging this problem, because I do not know how to get visibility into the html canvas goings-on. Thanks!

If anyone sees this, I believe it was because Bokeh requires ajax updates for timeseries to be in microseconds since epoch:(

https://groups.google.com/a/continuum.io/forum/#!topic/bokeh/cK4MihXn7JM https://github.com/hhprogram/PyramidSite/blob/master/webgraphing/views/ajaxView.py

I was able to slap.timestamp() on all my datetime values on my x axis and it worked. eg for ajax endpoint

def Data(request, ticker):
    prices = RealtimeStockPrice.objects.filter(ticker=ticker).order_by('last_time')
    x = []
    y = []
    for p in prices:
        x.append(p.last_time.astimezone(timezone('US/Eastern')).timestamp())
        y.append(float(p.last_price))

    return JsonResponse({'time': x, 'price': y})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM