
My objective: I have to monitor a machine performance by a figure. The performance data is a time series. I wish the width of the x_range is two-hours wide.
My tools: I used a periodical callback function to change the x_range in the beginning of every single hour.
Testing: The following program is a testing code.
Executing command: bokeh serve --show allMachineEffiencyTest.py --address 120.xxx.xxx.xxx --allow-websocket-origin=120.xxx.xxx.xx:xxxx --port xxxx
My problem: Why the x_range was changed just for a split second at every updating moment, and then quickly restore to the original range?
from bokeh.layouts import gridplot, column
from bokeh.models import ColumnDataSource, Div, HoverTool, BoxAnnotation
from bokeh.plotting import curdoc, figure
import numpy as np
from datetime import datetime, timedelta
from numpy import arange, linspace, pi, sin
print(allMachineEffiencyToday())
x = 0.0
def allMachineEffiencyToday():
ratioPlot = list()
timeAxisStr = []
dataSource = []
itemInfos = "Machine 001"
y = sin(x)
plot = figure(title="Machine 001", toolbar_location=None, tools="hover", tooltips="@y")
plot.background_fill_color = "#efefef"
plot.xgrid.grid_line_color=None
plot.xaxis.axis_label = 'x'
plot.yaxis.axis_label = 'sin(x)'
line_data = {"x":[],"y":[]}
line_data['x'].append(x)
line_data['y'].append(y)
dataSource = ColumnDataSource(data=line_data)
plot.line(x='x', y= 'y', source=dataSource, color='blue')
plot.x_range.start = 0.0
plot.x_range.end = 5.0
plot.y_range.start = -1.0
plot.y_range.end = 1.0
ratioPlot.append(plot)
def update_data():
global x
x = x + 0.1
y = sin(x)
dataSource.data['y'].append(y)
dataSource.data['x'].append(x)
dataSource.trigger('data', dataSource.data, dataSource.data)
plot.x_range.start = int(x*5)/5 - 1.0
plot.x_range.end = int(x*5)/5 + 1.0
grid = gridplot(ratioPlot, ncols=1)
curdoc().add_root(grid)
curdoc().add_periodic_callback(update_data, 1000)
return 'end of allMachineEffiencyToday()'

