0

I am trying to plot an interactive financial chart, but it doesn’t turn dynamic when I use st.altair_chart (as is the default for altair charts). Here’s the code:

base = alt.Chart(df).encode(
    alt.X('Date:T', axis=alt.Axis(labelAngle=-45)),
    color=alt.condition("datum.Open <= datum.Close",
                        alt.value("#06982d"), alt.value("#ae1325"))
)

rule = base.mark_rule().encode(alt.Y('Low:Q', title='Price',
                                        scale=alt.Scale(zero=False)), alt.Y2('High:Q'))
bar = base.mark_bar().encode(alt.Y('Open:Q'), alt.Y2('Close:Q'))
st.altair_chart(rule + bar, use_container_width=True)

This code results in the plot as follows:

candle-plot

(For reference,the original question)

Pratik K.
  • 147
  • 2
  • 13

1 Answers1

3

Basically, you have to explicitly layer the markings and call on .interactive() to make the axis interactive:

base = alt.Chart(df).encode(
    alt.X('Date:T', axis=alt.Axis(labelAngle=-45)),
    color=alt.condition("datum.Open <= datum.Close",
                        alt.value("#06982d"), alt.value("#ae1325"))
)

chart = alt.layer(
    base.mark_rule().encode(alt.Y('Low:Q', title='Price',
                                    scale=alt.Scale(zero=False)), alt.Y2('High:Q')),
    base.mark_bar().encode(alt.Y('Open:Q'), alt.Y2('Close:Q')),
).interactive()
st.altair_chart(chart, use_container_width=True)
Pratik K.
  • 147
  • 2
  • 13