工具提示不会以正确的格式显示日期

时间:2016-03-02 16:25:47

标签: python flask bokeh

工具提示不会在折线图中以正确的格式显示日期。(下图)。但是,数据在下表中以正确的格式显示。

我的代码列在这里。任何人都可以共享一些有用的信息,以便在工具提示中将整数转换为年/月/日。谢谢,

汉克

def makeplotTDS(samplesTDS):
years=[ri["CollectionDate"] for ri in samplesTDS]
values=[ri["TdsValue"] for ri in samplesTDS]


sourcetds = ColumnDataSource(
    data=dict(
        year=years,
        value=values,

    )
)

hover = HoverTool(
    tooltips="""
         <div style="background: #FFFFFF;">
            <span style="font-size: 20px;">Year, TDS Value</span><br />
            <span style="font-size: 16px; color: black;">(@year, @value)</span>
        </div>
    """

enter image description here enter image description here

1 个答案:

答案 0 :(得分:0)

您可以在数据源中创建另一个包含字符串格式日期的变量。

from bokeh.plotting import figure,show
from bokeh.models import ColumnDataSource,HoverTool
from time import gmtime,strftime
from calendar import timegm

dates = [(2011,3,15),(2011,8,22),(2012, 7,17),(2012,11,13),(2013, 7,16),
        (2013,11,18),(2014,12, 1),(2015, 6, 8),(2015,12, 7)]
values = [1050,1250,1100,1700,1200,2650,1400,1050,1100]
years = [timegm(x+(0,0,0))*1000 for x in dates]

p = figure(plot_width = 600, plot_height=300, x_axis_type="datetime")
sourcetds = ColumnDataSource(
    data=dict(
        year  = years,
        value = values,
        syear = [strftime("%Y-%m-%d",gmtime(x/1000)) for x in years]
        # syear is the date in string format
    )
)

p.line(x='year', y='value', source = sourcetds)
p.circle(x='year', y='value', source = sourcetds)

hover = HoverTool(
    tooltips="""
         <div style="background: #FFFFFF;">
            <span style="font-size: 20px;">Year, TDS Value</span><br />
            <span style="font-size: 16px; color: black;">(@syear, @value)</span>
        </div>
    """)
p.add_tools(hover)
show(p)

结果是: enter image description here