简体   繁体   English

Altair mark_text 标签,其中 x 轴中的域被链接

[英]Altair mark_text labels where domain in x-axis is linked

I was trying to give text labels on some altair chart linked to a selected interval from another chart.我试图在一些 altair 图表上给出文本标签,该图表链接到另一个图表中的选定间隔。 I realize that the text given by "mark_text ()" doesn't show completely at the last points of the chart where the domain in the x-axis is specified to be the interval selected, also I didn't know how to specify the format so the dates will be given just as yyyy-mm or month-year (don't want to display the day).我意识到“mark_text ()”给出的文本没有完全显示在图表的最后一点,其中 x 轴中的域被指定为选定的间隔,我也不知道如何指定格式,因此日期将被指定为 yyyy-mm 或月-年(不想显示日期)。

Another thing that I realized, is when one specifies the tooltip doesn't show at all when the domain on the x-axis of the graph is also linked to an interval selected in another chart, that's the reason I used the mark_text()我意识到的另一件事是,当图形的 x 轴上的域也链接到另一个图表中选择的间隔时,当一个人指定工具提示根本不显示时,这就是我使用 mark_text() 的原因
the code I'm using is the following我正在使用的代码如下

import altair as alt
from vega_datasets import data

nearest = alt.selection_single(nearest=True, on='mouseover',
                    encodings=['x','y'], empty='none')
interval = alt.selection_interval(encodings=['x'])
weather = data.seattle_weather()
base = alt.Chart(weather).mark_rule(size=2).encode(
x='date:T')

chart = base.mark_line().encode(
x=alt.X('date:T', scale=alt.Scale(domain=interval.ref())),
    y='temp_max:Q',).properties(
width=800,
height=300)

text=base.mark_text(align='left', dx=5, dy=5).encode(
y='temp_max:Q',
text=alt.condition(nearest, 'label:N', alt.value(' '))
).transform_calculate(label='"Date: " + format(datum.date, "") '
                     ).properties(selection=nearest,width=800,
height=300)

point=base.mark_point().encode(y='temp_max:Q',opacity=alt.condition(nearest, alt.value(1), alt.value(0)))



view = base.mark_line().add_selection(
interval).properties(width=800, height=20)


(point+text+chart) &view

It looks like you're trying to create a tooltip using a layer, and this is the cause of many of the problems you're having.看起来您正在尝试使用图层创建工具提示,这就是您遇到的许多问题的原因。 Have you considered using the tooltip encoding?您是否考虑过使用工具提示编码?

import altair as alt
from vega_datasets import data

nearest = alt.selection_single(nearest=True, on='mouseover',
                    encodings=['x','y'], empty='none')
interval = alt.selection_interval(encodings=['x'])
weather = data.seattle_weather()

line = alt.Chart(weather).mark_line().encode(
    x=alt.X('date:T', scale=alt.Scale(domain=interval)),
    y='temp_max:Q'
).properties(
    width=800,
    height=200
)
point = line.mark_point().encode(
    tooltip='yearmonth(date):N',
    opacity=alt.condition(nearest, alt.value(1), alt.value(0))
).add_selection(nearest)

view = alt.Chart(weather).mark_line().encode(
    x='date:T',
).properties(
    width=800,
    height=20
).add_selection(interval)

(point + line) & view

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

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