vega / altair

Declarative statistical visualization library for Python
https://altair-viz.github.io/
BSD 3-Clause "New" or "Revised" License
9.32k stars 794 forks source link

strokeDash or opacity not working when using with alt.condition() in mark_line() #2459

Closed rahulsingh7 closed 3 years ago

rahulsingh7 commented 3 years ago

I want to make the line after December 7th, 2020 as dashed or at least reduce the opacity of it after 7 Dec 2020 but neither opacity nor strokeDash is having any effect on the line...

.................Code Snippet ..................

cases_df = alt.UrlData("https://raw.githubusercontent.com/keto08/covid-19/master/COVID19-UK/UK_Cases_timeline_data.csv")

base = alt.Chart(cases_df,title="TimeSeries showing Daily Cases For United Kingdome").encode(x=alt.X('date:T',axis = alt.Axis(title = 'Date'.upper(), format = ("%d %b %Y"))))

bar = base.mark_bar(color='#F5B041',size=3).encode(y=alt.Y('sum(daily_cases):Q',title='Cases'),\
                                                          tooltip = [alt.Tooltip('sum(daily_cases):Q',title='Total Cases'),\
      alt.Tooltip('date:T',title='Date')],opacity=alt.condition(alt.datum.date <= alt.expr.toDate('2020-12-07T00:00:00'),alt.value(1),
            alt.value(0.4))).transform_filter(
    alt.FieldOneOfPredicate(field='region_name', oneOf=['East Midlands', 'East of England', 'London', 'North East',
       'North West', 'South East', 'South West', 'Wales', 'West Midlands',
       'Yorkshire and The Humber'])).interactive()

line =  base.mark_line(color='black').encode(
    y=alt.Y('sum(cases_7D):Q'),tooltip = [alt.Tooltip('sum(cases_7D):Q',title='7 Day Rolling Average',format='.0f'),\
      alt.Tooltip('date:T',title='Date')], strokeDash=alt.condition(
        alt.datum.date <= alt.expr.toDate('2020-12-07T00:00:00'),
        alt.value([0]),
        alt.value([5, 5]),opacity=alt.condition(alt.datum.date <= alt.expr.toDate('2020-12-07T00:00:00'),alt.value(1),
            alt.value(0.4)) )).transform_filter(
    alt.FieldOneOfPredicate(field='region_name', oneOf=['East Midlands', 'East of England', 'London', 'North East',
       'North West', 'South East', 'South West', 'Wales', 'West Midlands',
       'Yorkshire and The Humber'])).interactive()

(bar + line ).properties(width=1000)

This code Gives me

visualization (34)

I basically want to make the line after 7-dec-2020 to be dashed or reduce it's opacity just like I did for the bars in the chart.

jakevdp commented 3 years ago

Lines cannot have different strokes, opaticites, colors, etc. along their length. If you want to change these properties for only a portion of the line, you'll have to use an encoding to draw two separate lines.

joelostblom commented 3 years ago

If you are OK with changing the size of the line instead of opacity, stroke, or color, you can use mark_trail instead of mark_line:

line =  base.mark_trail(color='black').encode(
    y=alt.Y('sum(cases_7D):Q'),
    tooltip = [alt.Tooltip('sum(cases_7D):Q',title='7 Day Rolling Average',format='.0f'), alt.Tooltip('date:T',title='Date')], 
    size=alt.condition(alt.datum.date <= alt.expr.toDate('2020-12-07T00:00:00'), alt.value(3), alt.value(1)))

image

rahulsingh7 commented 3 years ago

Thanks again @joelostblom, This workaround works for me... :)