从散景中的 x 轴获取所需的日期时间值
Getting the desired datetime value from x axis in bokeh
我已阅读文档并在 Google 和 Whosebug 中搜索答案,但 none 更明智。
我有一个散景图,带有两个变量 'score' 和 'stress' 的圆形字形,第三个变量 'date' 作为日期时间 x 轴(图片 here) .我希望用户能够单击圆圈并转到 URL,显示由相应日期标识的特定数据点的详细视图。
我启用了带有 openURL 回调的点击工具,它在 URL 的末尾附加了日期时间值。问题在于,一旦单击数据点,传递的日期时间值就不是所需的格式:'2016-07-20'
。我得到的是以下值:“1468969200000”。因此,用户被重定向到 'url/1468969200000/' 而不是 'url/2016-07-20/'。
有没有办法更改单击数据点后传递的日期值的格式?
这是我的代码(运行 in jupyter notebook):
import datetime
from bokeh.plotting import figure, output_notebook, show
from bokeh.models import Range1d, OpenURL, TapTool, HoverTool, ColumnDataSource, DatetimeTickFormatter
data = {'score': [4.33, 2.66, 4.66, 2.66, 2.66, 1.66, 1.0, 4.33],
'stress': [3.66, 3.0, 3.0, 1.33, 3.66, 3.33, 1.0, 4.33],
'date': [
datetime.date(2016, 7, 17),
datetime.date(2016, 7, 18),
datetime.date(2016, 7, 19),
datetime.date(2016, 7, 20),
datetime.date(2016, 7, 21),
datetime.date(2016, 7, 22),
datetime.date(2016, 7, 23),
datetime.date(2016, 7, 24)
]
}
source = ColumnDataSource(data=data)
TOOLS = ['hover', 'pan', 'tap']
plot = figure(x_axis_type='datetime', plot_height=250, tools=TOOLS)
plot.circle('date', 'score', legend='score', size=15, color='red', source=source)
plot.circle('date', 'stress', legend='stress', size=10, color='orange', source=source)
plot.y_range = Range1d(1, 5, bounds=(1,5))
plot.x_range = Range1d(datetime.date(2016, 7, 17), datetime.date(2016, 7, 23))
hover = plot.select(type=HoverTool)
hover.tooltips = [
("score", "@score"),
("stress", "@stress"),
("date", "@date")
]
url = 'url/@date/'
taptool = plot.select(type=TapTool)
taptool.callback = OpenURL(url=url)
show(plot)
一个简单的解决方法是另外提供字符串格式的日期。
dateStr= {'dateStr': [x.isoformat() for x in data['date']]}
data.update(dateStr)
然后,您可以在 hover.tooltips 中使用 dateStr 并生成 url
hover.tooltips = [
("score", "@score"),
("stress", "@stress"),
("date", "@dateStr")
]
url = 'url/@dateStr/'
我已阅读文档并在 Google 和 Whosebug 中搜索答案,但 none 更明智。
我有一个散景图,带有两个变量 'score' 和 'stress' 的圆形字形,第三个变量 'date' 作为日期时间 x 轴(图片 here) .我希望用户能够单击圆圈并转到 URL,显示由相应日期标识的特定数据点的详细视图。
我启用了带有 openURL 回调的点击工具,它在 URL 的末尾附加了日期时间值。问题在于,一旦单击数据点,传递的日期时间值就不是所需的格式:'2016-07-20'
。我得到的是以下值:“1468969200000”。因此,用户被重定向到 'url/1468969200000/' 而不是 'url/2016-07-20/'。
有没有办法更改单击数据点后传递的日期值的格式?
这是我的代码(运行 in jupyter notebook):
import datetime
from bokeh.plotting import figure, output_notebook, show
from bokeh.models import Range1d, OpenURL, TapTool, HoverTool, ColumnDataSource, DatetimeTickFormatter
data = {'score': [4.33, 2.66, 4.66, 2.66, 2.66, 1.66, 1.0, 4.33],
'stress': [3.66, 3.0, 3.0, 1.33, 3.66, 3.33, 1.0, 4.33],
'date': [
datetime.date(2016, 7, 17),
datetime.date(2016, 7, 18),
datetime.date(2016, 7, 19),
datetime.date(2016, 7, 20),
datetime.date(2016, 7, 21),
datetime.date(2016, 7, 22),
datetime.date(2016, 7, 23),
datetime.date(2016, 7, 24)
]
}
source = ColumnDataSource(data=data)
TOOLS = ['hover', 'pan', 'tap']
plot = figure(x_axis_type='datetime', plot_height=250, tools=TOOLS)
plot.circle('date', 'score', legend='score', size=15, color='red', source=source)
plot.circle('date', 'stress', legend='stress', size=10, color='orange', source=source)
plot.y_range = Range1d(1, 5, bounds=(1,5))
plot.x_range = Range1d(datetime.date(2016, 7, 17), datetime.date(2016, 7, 23))
hover = plot.select(type=HoverTool)
hover.tooltips = [
("score", "@score"),
("stress", "@stress"),
("date", "@date")
]
url = 'url/@date/'
taptool = plot.select(type=TapTool)
taptool.callback = OpenURL(url=url)
show(plot)
一个简单的解决方法是另外提供字符串格式的日期。
dateStr= {'dateStr': [x.isoformat() for x in data['date']]}
data.update(dateStr)
然后,您可以在 hover.tooltips 中使用 dateStr 并生成 url
hover.tooltips = [
("score", "@score"),
("stress", "@stress"),
("date", "@dateStr")
]
url = 'url/@dateStr/'