2016-03-02 148 views
1

工具提示未在折線圖中以正確的格式顯示日期(下圖)。但是,數據在下表中以正確的格式顯示。工具提示未以正確的格式顯示日期

我的代碼在這裏列出。任何人都可以分享一些有用的信息,將整數轉換爲工具提示中的年/月/日。謝謝,

漢克

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

回答

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