如何创建一个工具提示以显示Altair中一个字段的多个值?

问题描述 投票:1回答:1

我已经成功创建了一个交互式图表,作为教程here,以下是我的结果:

<< img src =“ https://image.soinside.com/eyJ1cmwiOiAiaHR0cHM6Ly9pLnN0YWNrLmltZ3VyLmNvbS9RSUJRZi5wbmcifQ==” alt =“在此处输入图像描述”>

我想创建一个工具提示,如下图所示:“在此处输入图像描述”

但是我在Altair教程文档中找不到任何提示。

任何人都可以提供有关如何进行操作的建议吗?

tooltip visualization altair
1个回答
0
投票

您可以使用pivot transform为多行创建一个工具提示。这是一个使用vega数据集的示例。

这个想法是工具提示绑定到垂直规则标记,该标记表示数据的枢轴版本,其中每个相关值在同一行中:

import altair as alt
from vega_datasets import data

source = data.stocks()
base = alt.Chart(source).encode(x='date:T')
columns = sorted(source.symbol.unique())
selection = alt.selection_single(
    fields=['date'], nearest=True, on='mouseover', empty='none', clear='mouseout'
)

lines = base.mark_line().encode(y='price:Q', color='symbol:N')
points = lines.mark_point().transform_filter(selection)

rule = base.transform_pivot(
    'symbol', value='price', groupby=['date']
).mark_rule().encode(
    opacity=alt.condition(selection, alt.value(0.3), alt.value(0)),
    tooltip=[alt.Tooltip(c, type='quantitative') for c in columns]
).add_selection(selection)

lines + points + rule

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.