简体   繁体   English

如何创建Bokeh DataTable DateTime格式化程序?

[英]How to create a Bokeh DataTable DateTime formatter?

Bokeh provides bokeh.models.DateFormatter() to show the date in a column (see class DateFormatter(**kwargs) ). Bokeh提供了bokeh.models.DateFormatter()来显示列中的日期(请参阅类DateFormatter(** kwargs) )。 Is there a way to create a Formatter that also shows the time HH:MM:SS and possible also the milliseconds? 有没有办法创建一个格式化程序,也显示时间HH:MM:SS,也可能是毫秒? When editing in the table(double click on a date) one can see the epoch milliseconds. 在表格中编辑(双击日期)时,可以看到纪元毫秒。 I could create another column with another set of data having seconds of day and use class NumberFormatter(**kwargs) with format "00:00:00" and another column with the milliseconds, but I would not be able to edit those and have it reflected on the graph glyphs, unless somehow I make the seconds of day data continuosly linked to the datetime values (is there a way to do so?). 我可以使用另一组数据创建另一列,其中包含一天中的几秒钟,并使用格式为“00:00:00”的类NumberFormatter(** kwargs)和具有毫秒的另一列,但我无法编辑这些列并且具有它反映在图形字形上,除非我以某种方式将日期数据连续地与日期时间值相关联(有没有办法这样做?)。 So I hope there is a way to work on a Formatter to display time out of the datetime data in a bokeh DataTable. 所以我希望有一种方法可以使用Formatter来显示散景DataTable中日期时间数据的时间。

from datetime import datetime
import bokeh, bokeh.plotting

data = dict(dates=[datetime(2016, 12, 2,15,30,0,123456),
                   datetime(2016, 12, 2,15,30,5,250500),
                   datetime(2016, 12, 2,15,30,10,756050)],
            altitude=[100.,150.,125.])

source = bokeh.models.ColumnDataSource(data)

columns = [bokeh.models.TableColumn(field="dates", title="Date",
           formatter=bokeh.models.DateFormatter()),
           bokeh.models.TableColumn(field="altitude", title="altitude")]

data_table = bokeh.models.DataTable(source=source, columns=columns,
                   row_headers=False,width=500, height=150, editable=True)

p = bokeh.plotting.figure(width=400,height=200,x_axis_type="datetime", 
                          background_fill_color="lightgray")
p.circle(x="dates",y="altitude",source=source, size=10)
bokeh.io.output_notebook()
bokeh.io.show(bokeh.io.gridplot([[p],[data_table]]))

在此输入图像描述

UPDATE UPDATE

A workaround is to create another column with the time HH:MM:SS (no milliseconds though. For that @Julian solution will work best). 解决方法是使用时间HH:MM:SS创建另一个列(不过毫秒。为此, @ Julian解决方案最有效)。 But editing that value will not be reflected in the graph. 但是编辑该值不会反映在图表中。 Editing the date milliseconds does update the graph but will not update the time column. 编辑日期毫秒会更新图表,但不会更新时间列。 Probably there is a way to connect the time column with the date column with some custom CustomJS . 可能有一种方法可以将时间列与日期列连接到一些自定义CustomJS I have to comment that there is a working progress feature in bokeh github: Updating dates in datatable that might help here a lot. 我要评论有背景虚化在GitHub上工作进度功能: 更新日期中的datatable ,这可能有助于在这里了很多。

from datetime import datetime
import bokeh, bokeh.plotting

t1 = datetime(2016, 12, 2,15,30,0,123456)
t2 = datetime(2016, 12, 2,15,30,5,250500)
t3 = datetime(2016, 12, 2,15,30,10,756050)

data = dict(dates = [t1,t2,t3],
            times = [t1.hour*3600 + t1.minute*60 + t1.second,
                    t2.hour*3600 + t2.minute*60 + t2.second,
                    t3.hour*3600 + t3.minute*60 + t3.second],
            altitude=[100.,150.,125.])

source = bokeh.models.ColumnDataSource(data)

tfmt = bokeh.models.NumberFormatter(format="00:00:00")
datefmt = bokeh.models.DateFormatter(format="ddMyy")
hfmt = bokeh.models.NumberFormatter(format="0.00")
columns = [bokeh.models.TableColumn(field="dates", title="Date",formatter=datefmt),
           bokeh.models.TableColumn(field="times", title="time", formatter=tfmt, editor=None),
           bokeh.models.TableColumn(field="altitude", title="altitude (km)", formatter=hfmt)]

data_table = bokeh.models.DataTable(source=source, columns=columns,
                   row_headers=False,width=500, height=150, editable=True)

p = bokeh.plotting.figure(width=400,height=200,x_axis_type="datetime", 
                          background_fill_color="lightgray")
p.circle(x="dates",y="altitude",source=source, size=10)
bokeh.io.output_notebook()
bokeh.io.show(bokeh.io.gridplot([[p],[data_table]]))

在此输入图像描述

UPDATE 04/13/2018 更新04/13/2018

Bokeh version 0.12.16dev allows for a DataTable to have a DateFormatter that supports time information: Bokeh版本0.12.16dev允许DataTable具有支持时间信息的DateFormatter

bokeh.models.DateFormatter(format="%m/%d/%Y %H:%M:%S")

For all the possibilities see class DateFormatter(**kwargs) . 对于所有可能性,请参阅类DateFormatter(** kwargs)

在此输入图像描述

One partial solution is to separate the data sources for table and plot. 一个部分解决方案是分离表和绘图的数据源。 Then you can leave the x-data for the plot in datetime format, and transform the date data for the table into a string. 然后,您可以以日期时间格式保留绘图的x数据,并将表的日期数据转换为字符串。 Of cause, then edits in the table are not reflected in the plot. 原因是,表中的编辑没有反映在情节中。 Maybe with a callback function you can link the two data sources. 也许使用回调函数,您可以链接两个数据源。

from datetime import datetime
import bokeh, bokeh.plotting

datetime_dates=[datetime(2016, 12, 2,15,30,0,123456),
                   datetime(2016, 12, 2,15,30,5,250500),
                   datetime(2016, 12, 2,15,30,10,756050)]
string_dates = []
for i in datetime_dates: string_dates.append(i.strftime("%Y-%m-%d %H:%M:%S.%f"))

data_plt = dict(dates=datetime_dates,
                     altitude=[100.,150.,125.])
data_tbl = dict(dates=string_dates,
                     altitude=[100.,150.,125.])

source_plot  = bokeh.models.ColumnDataSource(data_plt)
source_table = bokeh.models.ColumnDataSource(data_tbl)

columns = [bokeh.models.TableColumn(field="dates", title="Date"),
           bokeh.models.TableColumn(field="altitude", title="altitude")]

data_table = bokeh.models.DataTable(source=source_table, columns=columns,
                   row_headers=False,width=500, height=150, editable=True)

p = bokeh.plotting.figure(width=400,height=200,x_axis_type="datetime", 
                          background_fill_color="lightgray")
p.circle(x="dates",y="altitude",source=source_plot, size=10)
bokeh.io.output_notebook()
bokeh.io.show(bokeh.io.gridplot([[p],[data_table]]))

enter image description here 在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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