【问题标题】:Is it possible to create an Altair binding to a datalist element instead of select?是否可以创建一个 Altair 绑定到 datalist 元素而不是 select?
【发布时间】:2020-04-23 17:44:47
【问题描述】:

我正在尝试在时间序列图上设置基因表达的交互式过滤器。创建此类过滤器的文档化方法是将select_single 绑定到输入表单。对于较少数量的选项,binding_select 会起作用。例如

import altair as alt
group_dropdown = alt.binding_select(options=gene_names)
group_select = alt.selection_single(fields=['gene'], bind=group_dropdown, name='Feature', init={'gene': gene_names[0]})
filter_group = chart.add_selection(group_select).transform_filter(group_select)

但是,我有大约 50K 基因可供选择,因此下拉列表 (binding_select) 并不是一个真正的选择。 <datalist> 元素将是完美的。 Input Binding 上的 vega-lite 文档暗示我应该能够使用任何 HTML 表单输入元素,但我无法弄清楚将映射到该元素的 Altair 类。

【问题讨论】:

    标签: python plot interactive vega-lite altair


    【解决方案1】:

    这是可能的,但有点困难,原因有两个:

    • 虽然 Vega 支持形成输入的任意参数,但 vega-lite 的架构禁止此类参数。这意味着您需要绕过 Altair 的正常验证机制才能使用它。
    • <datalist> 必须注入到图表的 HTML 输出中,并且没有很好的机制来执行此操作。

    以下示例说明了如何解决这些限制并在 Altair 选择输入绑定中使用数据列表:

    from IPython.display import HTML, display
    
    import altair as alt
    from vega_datasets import data
    
    from altair.utils.display import HTMLRenderer
    from altair.utils import schemapi
    
    datalist = """
    <datalist id="origin">
      <option value="USA">
      <option value="Europe">
      <option value="Japan">
    </datalist>
    """
    
    # Allow specifications that are invalid according to the schema.
    # This prevents a validation error for the `list` argument below.
    schemapi.DEBUG_MODE = False
    # `list` here should match the ID of the <datalist> specification.
    widget = alt.binding(input='text', name='Country', list='origin')
    
    # now create the chart as normal:
    selection = alt.selection_single(fields=['Origin'], bind=widget)
    color = alt.condition(selection,
                        alt.Color('Origin:N', legend=None),
                        alt.value('lightgray'))
    chart = alt.Chart(data.cars.url).mark_point().encode(
        x='Horsepower:Q',
        y='Miles_per_Gallon:Q',
        color=color,
        tooltip='Name:N'
    ).add_selection(
        selection
    )
    
    # Note the following assumes the default renderer.
    alt.renderers.enable('default')
    
    # Render the chart to HTML without validating it against the schema:
    renderer = alt.renderers.get()
    html = renderer(chart.to_dict(validate=False))['text/html']
    
    # Now display the datalist and chart rendering:
    display(HTML(datalist + html))
    

    【讨论】:

      猜你喜欢
      • 2017-06-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-14
      • 2016-05-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-19
      相关资源
      最近更新 更多