【发布时间】:2020-10-03 11:35:57
【问题描述】:
我正在尝试复制以下堆叠图表,可以通过与底部图表交互来选择顶部图表的域,但使用烛台图(这是一个分层图表):
(下面的示例代码在这里:https://altair-viz.github.io/gallery/interval_selection.html)
我无法让它与分层图表一起使用 (https://altair-viz.github.io/gallery/candlestick_chart.html)。是否可以使用分层图表以这种方式按日期擦洗?谢谢。
这是我尝试过的:
import altair as alt
from vega_datasets import data
source = data.ohlc()
brush = alt.selection(type='interval', encodings=['x'])
open_close_color = alt.condition("datum.open <= datum.close", alt.value("green"), alt.value("firebrick"))
base = alt.Chart(source).encode(
alt.X('date:T', axis=alt.Axis(format='%m/%d', title='Date in 2009')),
color=open_close_color
).properties(width=600, height=400)
rule = base.mark_rule().encode(
alt.Y('low:Q', title='Price', scale=alt.Scale(domain=brush, zero=False)),
alt.Y2('high:Q')
)
bar = base.mark_bar().encode(
alt.Y('open:Q', scale=alt.Scale(domain=brush, zero=False)),
alt.Y2('close:Q')
)
candles = rule + bar
lower = base.properties(
height=60
).add_selection(brush)
candles & lower
【问题讨论】: