Altair 单击时工具提示的位置记录到文件中

huangapple go评论81阅读模式
英文:

Altair log position of tooltip on click to file

问题

以下是翻译好的部分:

"我打算保留用户点击过的所有国家的日志记录(日志文件或数据框)。我正在尝试在点击时记录工具提示的 id,但不知道如何提取任何值。我会很高兴听取有关如何提取值的任何建议。到目前为止,获取国家名称和突出显示国家已经很好了。

任何帮助都将不胜感激,即使 altair 不支持这个解决方案,指向其他框架如何实现这一点的方式也将很有帮助。"

英文:

I aim to keep a log (log file or dataframe) of all the countries a user has clicked on. I am trying to log the id of the tooltip on click, but do not know how I can extract any values. I'd be happy about any advice on how to extract values. Getting the country name and highlighting the country work well so far.

Any help is appreciated, also if altair does not support this solution, a pointer to if/how other frameworks accomplish this would also be helpful.

import pandas as pd
import altair as alt
import streamlit as st
from vega_datasets import data


@st.cache
def get_iso_names(url: str) -> pd.DataFrame:
    return pd.read_csv(url)


# Data generators for the background
sphere = alt.sphere()
graticule = alt.graticule()

# Source of land data
source = alt.topo_feature(data.world_110m.url, "countries")
iso_name_url = "https://raw.githubusercontent.com/stefangabos/world_countries/master/data/countries/en/world.csv"
country_names = get_iso_names(iso_name_url)

click = alt.selection_multi(empty="none")

# Layering and configuring the components
background = (
    alt.layer(
        alt.Chart(sphere).mark_geoshape(fill="lightblue"),
        alt.Chart(graticule).mark_geoshape(stroke="white", strokeWidth=0.5),
        alt.Chart(source)
        .mark_geoshape(
            stroke="black",
            strokeWidth=0.15,
        )
        .encode(
            tooltip=[
                alt.Tooltip("name:N", title="Country"),
            ],
            # color=alt.condition(hover, alt.value("firebrick"), "none"),
            color=alt.condition(click, alt.value("firebrick"), alt.value("white")),
        )
        .transform_lookup(
            lookup="id",
            from_=alt.LookupData(country_names, "id", ["name"]),
        ),
    )
    .add_selection(click)
    .project("naturalEarth1")
    .properties(width=1200, height=800)
)


st.altair_chart(
    background.interactive(),
    # (background + foreground).interactive(),
    use_container_width=False,
)

答案1

得分: 1

没有办法直接使用Altair来完成这个操作,您需要使用一个支持从所选点提取数据功能的仪表板库,比如Panel(可能会在将来添加到streamlit中)。更多信息请参考https://github.com/altair-viz/altair/issues/1153。

英文:

There is no way to do this directly with Altair, you would need to use a dashboard library that supports the functionality of extracting data from the selected points, such as Panel (might be added to streamlit in the future). More info here https://github.com/altair-viz/altair/issues/1153

huangapple
  • 本文由 发表于 2023年6月29日 00:24:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/76575069.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定