From 390d59935bf1cf81ce4988fdc6099b9d9a50b790 Mon Sep 17 00:00:00 2001 From: dangotbanned <125183946+dangotbanned@users.noreply.github.com> Date: Fri, 6 Sep 2024 10:42:03 +0100 Subject: [PATCH] docs: Use `when` in arguments syntax examples (2/2) --- .../bar_chart_with_highlighted_bar.py | 22 ++++++++++--------- .../bar_chart_with_negatives.py | 19 ++++++++-------- .../candlestick_chart.py | 8 ++++--- .../dot_dash_plot.py | 8 ++++--- .../interactive_brush.py | 2 +- .../interactive_reorder_stacked_bars.py | 2 +- .../scatter_linked_brush.py | 2 +- .../scatter_with_histogram.py | 2 +- .../selection_histogram.py | 2 +- .../selection_layer_bar_month.py | 2 +- .../selection_zorder.py | 7 +++--- .../slider_cutoff.py | 6 ++--- 12 files changed, 44 insertions(+), 38 deletions(-) diff --git a/tests/examples_arguments_syntax/bar_chart_with_highlighted_bar.py b/tests/examples_arguments_syntax/bar_chart_with_highlighted_bar.py index d277bf9f3..511f44f15 100644 --- a/tests/examples_arguments_syntax/bar_chart_with_highlighted_bar.py +++ b/tests/examples_arguments_syntax/bar_chart_with_highlighted_bar.py @@ -9,13 +9,15 @@ source = data.wheat() -alt.Chart(source).mark_bar().encode( - x='year:O', - y="wheat:Q", - # The highlight will be set on the result of a conditional statement - color=alt.condition( - alt.datum.year == 1810, # If the year is 1810 this test returns True, - alt.value('orange'), # which sets the bar orange. - alt.value('steelblue') # And if it's not true it sets the bar steelblue. - ) -).properties(width=600) +# If the `year` column equals `1810` +# then, set the bar color to `"orange"` +# otherwise, use `"steelblue"` +color = alt.when(year=1810).then(alt.value("orange")).otherwise(alt.value("steelblue")) + +chart = ( + alt.Chart(source) + .mark_bar() + .encode(x="year:O", y="wheat:Q", color=color) + .properties(width=600) +) +chart \ No newline at end of file diff --git a/tests/examples_arguments_syntax/bar_chart_with_negatives.py b/tests/examples_arguments_syntax/bar_chart_with_negatives.py index 127a16ba4..647b67c76 100644 --- a/tests/examples_arguments_syntax/bar_chart_with_negatives.py +++ b/tests/examples_arguments_syntax/bar_chart_with_negatives.py @@ -9,12 +9,13 @@ source = data.us_employment() -alt.Chart(source).mark_bar().encode( - x="month:T", - y="nonfarm_change:Q", - color=alt.condition( - alt.datum.nonfarm_change > 0, - alt.value("steelblue"), # The positive color - alt.value("orange") # The negative color - ) -).properties(width=600) +predicate = alt.datum.nonfarm_change > 0 +color = alt.when(predicate).then(alt.value("steelblue")).otherwise(alt.value("orange")) + +chart = ( + alt.Chart(source) + .mark_bar() + .encode(x="month:T", y="nonfarm_change:Q", color=color) + .properties(width=600) +) +chart \ No newline at end of file diff --git a/tests/examples_arguments_syntax/candlestick_chart.py b/tests/examples_arguments_syntax/candlestick_chart.py index 23b338b8e..4dc524782 100644 --- a/tests/examples_arguments_syntax/candlestick_chart.py +++ b/tests/examples_arguments_syntax/candlestick_chart.py @@ -12,9 +12,11 @@ source = data.ohlc() -open_close_color = alt.condition("datum.open <= datum.close", - alt.value("#06982d"), - alt.value("#ae1325")) +open_close_color = ( + alt.when("datum.open <= datum.close") + .then(alt.value("#06982d")) + .otherwise(alt.value("#ae1325")) +) base = alt.Chart(source).encode( alt.X('date:T', diff --git a/tests/examples_arguments_syntax/dot_dash_plot.py b/tests/examples_arguments_syntax/dot_dash_plot.py index 72d73d2e8..64c6ef450 100644 --- a/tests/examples_arguments_syntax/dot_dash_plot.py +++ b/tests/examples_arguments_syntax/dot_dash_plot.py @@ -12,28 +12,30 @@ # Configure the options common to all layers brush = alt.selection_interval() +brush_origin = alt.when(brush).then("Origin") base = alt.Chart(source).add_params(brush) # Configure the points points = base.mark_point().encode( x=alt.X('Miles_per_Gallon', title=''), y=alt.Y('Horsepower', title=''), - color=alt.condition(brush, 'Origin', alt.value('grey')) + color=brush_origin.otherwise(alt.value("grey")), ) # Configure the ticks tick_axis = alt.Axis(labels=False, domain=False, ticks=False) +tick_color = brush_origin.otherwise(alt.value("lightgrey")) x_ticks = base.mark_tick().encode( alt.X('Miles_per_Gallon', axis=tick_axis), alt.Y('Origin', title='', axis=tick_axis), - color=alt.condition(brush, 'Origin', alt.value('lightgrey')) + color=tick_color ) y_ticks = base.mark_tick().encode( alt.X('Origin', title='', axis=tick_axis), alt.Y('Horsepower', axis=tick_axis), - color=alt.condition(brush, 'Origin', alt.value('lightgrey')) + color=tick_color ) # Build the chart diff --git a/tests/examples_arguments_syntax/interactive_brush.py b/tests/examples_arguments_syntax/interactive_brush.py index 2cfd66037..bee087c57 100644 --- a/tests/examples_arguments_syntax/interactive_brush.py +++ b/tests/examples_arguments_syntax/interactive_brush.py @@ -15,5 +15,5 @@ alt.Chart(source).mark_point().encode( x='Horsepower:Q', y='Miles_per_Gallon:Q', - color=alt.condition(brush, 'Cylinders:O', alt.value('grey')), + color=alt.when(brush).then("Cylinders:O").otherwise(alt.value("grey")), ).add_params(brush) diff --git a/tests/examples_arguments_syntax/interactive_reorder_stacked_bars.py b/tests/examples_arguments_syntax/interactive_reorder_stacked_bars.py index 606bc37a1..2fbed1dca 100644 --- a/tests/examples_arguments_syntax/interactive_reorder_stacked_bars.py +++ b/tests/examples_arguments_syntax/interactive_reorder_stacked_bars.py @@ -25,7 +25,7 @@ y='variety:N', color='site:N', order='site_order:N', - opacity=alt.condition(selection, alt.value(0.9), alt.value(0.2)) + opacity=alt.when(selection).then(alt.value(0.9)).otherwise(alt.value(0.2)) ).add_params( selection ) diff --git a/tests/examples_arguments_syntax/scatter_linked_brush.py b/tests/examples_arguments_syntax/scatter_linked_brush.py index adf88d34b..b8548fd02 100644 --- a/tests/examples_arguments_syntax/scatter_linked_brush.py +++ b/tests/examples_arguments_syntax/scatter_linked_brush.py @@ -14,7 +14,7 @@ base = alt.Chart(source).mark_point().encode( y='Miles_per_Gallon', - color=alt.condition(brush, 'Origin', alt.ColorValue('gray')), + color=alt.when(brush).then("Origin").otherwise(alt.ColorValue("gray")), ).add_params( brush ).properties( diff --git a/tests/examples_arguments_syntax/scatter_with_histogram.py b/tests/examples_arguments_syntax/scatter_with_histogram.py index dda40dec0..40cb65c2b 100644 --- a/tests/examples_arguments_syntax/scatter_with_histogram.py +++ b/tests/examples_arguments_syntax/scatter_with_histogram.py @@ -42,7 +42,7 @@ mag = alt.Chart().mark_bar().encode( x='mbin:N', y="count()", - color=alt.condition(pts, alt.value("black"), alt.value("lightgray")) + color=alt.when(pts).then(alt.value("black")).otherwise(alt.value("lightgray")) ).properties( width=300, height=300 diff --git a/tests/examples_arguments_syntax/selection_histogram.py b/tests/examples_arguments_syntax/selection_histogram.py index dc0ccab8b..155b9fc68 100644 --- a/tests/examples_arguments_syntax/selection_histogram.py +++ b/tests/examples_arguments_syntax/selection_histogram.py @@ -16,7 +16,7 @@ points = alt.Chart(source).mark_point().encode( x='Horsepower:Q', y='Miles_per_Gallon:Q', - color=alt.condition(brush, 'Origin:N', alt.value('lightgray')) + color=alt.when(brush).then("Origin:N").otherwise(alt.value("lightgray")) ).add_params( brush ) diff --git a/tests/examples_arguments_syntax/selection_layer_bar_month.py b/tests/examples_arguments_syntax/selection_layer_bar_month.py index 43f4e97d2..e97914765 100644 --- a/tests/examples_arguments_syntax/selection_layer_bar_month.py +++ b/tests/examples_arguments_syntax/selection_layer_bar_month.py @@ -15,7 +15,7 @@ bars = alt.Chart().mark_bar().encode( x='month(date):O', y='mean(precipitation):Q', - opacity=alt.condition(brush, alt.OpacityValue(1), alt.OpacityValue(0.7)), + opacity = alt.when(brush).then(alt.value(1)).otherwise(alt.value(0.7)), ).add_params( brush ) diff --git a/tests/examples_arguments_syntax/selection_zorder.py b/tests/examples_arguments_syntax/selection_zorder.py index c8e3bd84a..6f6d7c93d 100644 --- a/tests/examples_arguments_syntax/selection_zorder.py +++ b/tests/examples_arguments_syntax/selection_zorder.py @@ -16,18 +16,19 @@ cars = data.cars.url hover = alt.selection_point(on='pointerover', nearest=True, empty=False) +when_hover = alt.when(hover) chart = alt.Chart(cars, title='Selection obscured by other points').mark_circle(opacity=1).encode( x='Horsepower:Q', y='Miles_per_Gallon:Q', - color=alt.condition(hover, alt.value('coral'), alt.value('lightgray')), - size=alt.condition(hover, alt.value(300), alt.value(30)) + color=when_hover.then(alt.value("coral")).otherwise(alt.value("lightgray")), + size=when_hover.then(alt.value(300)).otherwise(alt.value(30)) ).add_params( hover ) chart | chart.encode( - order=alt.condition(hover, alt.value(1), alt.value(0)) + order=when_hover.then(alt.value(1)).otherwise(alt.value(0)) ).properties( title='Selection brought to front' ) diff --git a/tests/examples_arguments_syntax/slider_cutoff.py b/tests/examples_arguments_syntax/slider_cutoff.py index 19db77493..786268aeb 100644 --- a/tests/examples_arguments_syntax/slider_cutoff.py +++ b/tests/examples_arguments_syntax/slider_cutoff.py @@ -17,14 +17,12 @@ slider = alt.binding_range(min=0, max=100, step=1) cutoff = alt.param(bind=slider, value=50) +predicate = alt.datum.xval < cutoff alt.Chart(df).mark_point().encode( x='xval', y='yval', - color=alt.condition( - alt.datum.xval < cutoff, - alt.value('red'), alt.value('blue') - ) + color=alt.when(predicate).then(alt.value("red")).otherwise(alt.value("blue")), ).add_params( cutoff ) \ No newline at end of file