Многострочный интерактивный сюжет с выпадающим меню - PullRequest
0 голосов
/ 05 июня 2019

Я пытаюсь создать сюжет, похожий на этот:

https://altair -viz.github.io / gallery / multiline_tooltip.html

Я хочудобавить раскрывающееся меню для выбора различных объектов.

Я изменил свой код, чтобы создать пример для иллюстрации:

import altair as alt
import pandas as pd
import numpy as np

np.random.seed(42)
source = pd.DataFrame(np.cumsum(np.random.randn(100, 3), 0).round(2),
                    columns=['A', 'B', 'C'], index=pd.RangeIndex(100, name='x'))
source = source.reset_index().melt('x', var_name='category', value_name='y')
source['Type'] = 'First'

source_1 = source.copy()
source_1['y'] = source_1['y'] + 5
source_1['Type'] = 'Second'

source_2 = source.copy()
source_2['y'] = source_2['y'] - 5
source_2['Type'] = 'Third'

source = pd.concat([source, source_1, source_2])

input_dropdown = alt.binding_select(options=['First', 'Second', 'Third'])
selection = alt.selection_single(name='Select', fields=['Type'],
                                   bind=input_dropdown)

# color = alt.condition(select_state,
#                       alt.Color('Type:N', legend=None),
#                       alt.value('lightgray'))

# Create a selection that chooses the nearest point & selects based on x-value
nearest = alt.selection(type='single', nearest=True, on='mouseover',
                        fields=['x'], empty='none')

# The basic line
base = alt.Chart(source).encode(
    x='x:Q',
    y='y:Q',
    color='category:N'
)

# add drop-down menu
lines = base.mark_line(interpolate='basis').add_selection(selection
).transform_filter(selection)

# Transparent selectors across the chart. This is what tells us
# the x-value of the cursor
selectors = alt.Chart(source).mark_point().encode(
    x='x:Q',
    opacity=alt.value(0),
).add_selection(
    nearest
)

# Draw points on the line, and highlight based on selection
points = base.mark_point().encode(
    opacity=alt.condition(nearest, alt.value(1), alt.value(0))
)

# Draw text labels near the points, and highlight based on selection
text = base.mark_text(align='left', dx=5, dy=-5).encode(
    text=alt.condition(nearest, 'y:Q', alt.value(' '))
)

# Draw a rule at the location of the selection
rules = alt.Chart(source).mark_rule(color='gray').encode(
    x='x:Q',
).transform_filter(
    nearest
)

#Put the five layers into a chart and bind the data
alt.layer(
    lines, selectors, points, rules, text
).properties(
    width=500, height=300
)

Как видите, каждый раз, когда я выбираю одинтип («Первый», «Второй» или «Третий»), на интерактивном графике по-прежнему отображаются точки всех трех, а не только одной, хотя отображаются только линии одного типа.


Оригинальный вопрос:

Я пытаюсь создать сюжет, похожий на этот:

https://altair -viz.github.io / gallery / multiline_tooltip.html

с экспортом, импортом и дефицитом.Я хочу добавить раскрывающееся меню для выбора различных состояний (поэтому каждый штат будет иметь такой график).

Мои данные выглядят так:

    State           Year    Category        Trade, in Million Dollars
0   Texas           2008     Export         8970.979210
1   California      2008    Export          11697.850116
2   Washington      2008    Import          8851.678608
3   South Carolina  2008     Deficit        841.495319
4   Oregon          2008     Import         2629.939168

Я пробовалНесколько разных методов, но все не удалось.Если я только хочу построить объект «линия», я могу сделать это хорошо.Но я не могу объединить «линию» с «точками».

import altair as alt

states = list(df_trade_china.State.unique())
input_dropdown = alt.binding_select(options=states)
select_state = alt.selection_single(name='Select', fields=['State'],
                                   bind=input_dropdown)

# Create a selection that chooses the nearest point & selects based on x-value
nearest = alt.selection(type='single', nearest=True, on='mouseover',
                        fields=['Year'], empty='none')

# The basic line
line = alt.Chart(df_trade_china).mark_line().encode(
    x='Year:O',
    y='Trade, in Million Dollars:Q',
    color='Category:N'
).add_selection(select_state
).transform_filter(select_state
)

# Transparent selectors across the chart. This is what tells us
# the x-value of the cursor
selectors = alt.Chart(df_trade_china).mark_point().encode(
    x='Year:O',
    opacity=alt.value(0),
).add_selection(
    nearest
)

# Draw points on the line, and highlight based on selection
points = line.mark_point().encode(
    opacity=alt.condition(nearest, alt.value(1), alt.value(0))
)

# Draw text labels near the points, and highlight based on selection
text = line.mark_text(align='left', dx=5, dy=-5).encode(
    text=alt.condition(nearest, 'Trade, in Million Dollars:Q', alt.value(' '))
)

# Draw a rule at the location of the selection
rules = alt.Chart(df_trade_china).mark_rule(color='gray').encode(
    x='Year:Q',
).transform_filter(
    nearest
)

#Put the five layers into a chart and bind the data
alt.layer(
    line
).properties(
    width=500, height=300
)

Вот сообщение об ошибке, которое я получил.

JavaScript Error: Duplicate signal name: "Select_tuple"

This usually means there's a typo in your chart specification. See the javascript console for the full traceback.

1 Ответ

0 голосов
/ 05 июня 2019

Новый ответ на новый вопрос:

Преобразование фильтра применяется только к строковым данным и поэтому фильтрует только строки.Если вы хотите отфильтровать каждый слой, убедитесь, что у каждого слоя есть преобразование фильтра.

Вот как это будет выглядеть с вашим кодом:

import altair as alt
import pandas as pd
import numpy as np

np.random.seed(42)
source = pd.DataFrame(np.cumsum(np.random.randn(100, 3), 0).round(2),
                    columns=['A', 'B', 'C'], index=pd.RangeIndex(100, name='x'))
source = source.reset_index().melt('x', var_name='category', value_name='y')
source['Type'] = 'First'

source_1 = source.copy()
source_1['y'] = source_1['y'] + 5
source_1['Type'] = 'Second'

source_2 = source.copy()
source_2['y'] = source_2['y'] - 5
source_2['Type'] = 'Third'

source = pd.concat([source, source_1, source_2])

input_dropdown = alt.binding_select(options=['First', 'Second', 'Third'])
selection = alt.selection_single(name='Select', fields=['Type'],
                                   bind=input_dropdown, init={'Type': 'First'})

# color = alt.condition(select_state,
#                       alt.Color('Type:N', legend=None),
#                       alt.value('lightgray'))

# Create a selection that chooses the nearest point & selects based on x-value
nearest = alt.selection(type='single', nearest=True, on='mouseover',
                        fields=['x'], empty='none')

# The basic line
base = alt.Chart(source).encode(
    x='x:Q',
    y='y:Q',
    color='category:N'
).transform_filter(
    selection
)

# add drop-down menu
lines = base.mark_line(interpolate='basis').add_selection(selection
)

# Transparent selectors across the chart. This is what tells us
# the x-value of the cursor
selectors = alt.Chart(source).mark_point().encode(
    x='x:Q',
    opacity=alt.value(0),
).add_selection(
    nearest
)

# Draw points on the line, and highlight based on selection
points = base.mark_point().encode(
    opacity=alt.condition(nearest, alt.value(1), alt.value(0))
)

# Draw text labels near the points, and highlight based on selection
text = base.mark_text(align='left', dx=5, dy=-5).encode(
    text=alt.condition(nearest, 'y:Q', alt.value(' '))
)

# Draw a rule at the location of the selection
rules = alt.Chart(source).mark_rule(color='gray').encode(
    x='x:Q',
).transform_filter(
    nearest
)

#Put the five layers into a chart and bind the data
alt.layer(
    lines, selectors, points, rules, text
).properties(
    width=500, height=300
)

Оригиналответ на оригинальный вопрос:

Отдельный выбор может быть добавлен на график только один раз.Когда вы пишете что-то вроде этого:

line = alt.Chart(...).add_selection(selection)

points = line.mark_point()

, один и тот же выбор добавляется в line и points (потому что points является производным от line).При их наложении каждый слой объявляет идентичный выбор, что приводит к ошибке Duplicate Signal Name.

Чтобы исправить это, не добавляйте один и тот же выбор к нескольким компонентам одной диаграммы.

Например, вы можете сделать что-то вроде этого (переключиться на пример набора данных, потому что вы не предоставили данные в своем вопросе):

import altair as alt
from vega_datasets import data

stocks = data.stocks()
stocks.symbol.unique().tolist()

input_dropdown = alt.binding_select(options=stocks.symbol.unique().tolist())
selection = alt.selection_single(fields=['symbol'], bind=input_dropdown,
                                 name='Company', init={'symbol': 'GOOG'})
color = alt.condition(selection,
                      alt.Color('symbol:N', legend=None),
                      alt.value('lightgray'))


base = alt.Chart(stocks).encode(
    x='date',
    y='price',
    color=color
)

line = base.mark_line().add_selection(
    selection
)

point = base.mark_point()

line + point

enter image description here

Обратите внимание, что объявление выбора с помощью add_selection() может быть вызвано только для одного компонента диаграммы, в то время как эффект выделения (здесь, условие цвета) можно добавить к нескольким компонентам диаграммы.

...