使用Altair提供的DatetimeUTC函数和d3-format库中的格式化字符串来格式化工具提示中的日期时间值。
需要注意的是,Altair默认使用美国日期格式(mm/dd/yyyy)。如果需要使用其他日期格式,则应该在日期字符串中指定日期格式。
下面是一个示例代码,它使用DatetimeUTC函数和d3-format字符串来格式化工具提示中的日期时间值:
import altair as alt
from vega_datasets import data
source = data.seattle_weather()
chart = alt.Chart(source).mark_line().encode(
x='date:T',
y='precipitation:Q'
).properties(
width=600,
height=300
)
# Format datetime for tooltips
date_format = '%B %d, %Y'
alt.Tooltip(
'date:T',
format=alt.Format(
type='time',
format=date_format,
)
)
chart
在此示例中,我们将日期格式设置为“月份 日,年份”(例如,“五月 23, 2019”)。此格式可以使用“%B %d, %Y”字符串表示。