Skip to content

Commit

Permalink
docs: Use when in arguments syntax examples (2/2)
Browse files Browse the repository at this point in the history
  • Loading branch information
dangotbanned committed Sep 6, 2024
1 parent ff0bc34 commit 390d599
Show file tree
Hide file tree
Showing 12 changed files with 44 additions and 38 deletions.
22 changes: 12 additions & 10 deletions tests/examples_arguments_syntax/bar_chart_with_highlighted_bar.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
19 changes: 10 additions & 9 deletions tests/examples_arguments_syntax/bar_chart_with_negatives.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
8 changes: 5 additions & 3 deletions tests/examples_arguments_syntax/candlestick_chart.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
8 changes: 5 additions & 3 deletions tests/examples_arguments_syntax/dot_dash_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/examples_arguments_syntax/interactive_brush.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
2 changes: 1 addition & 1 deletion tests/examples_arguments_syntax/scatter_linked_brush.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion tests/examples_arguments_syntax/scatter_with_histogram.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/examples_arguments_syntax/selection_histogram.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down
7 changes: 4 additions & 3 deletions tests/examples_arguments_syntax/selection_zorder.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
)
6 changes: 2 additions & 4 deletions tests/examples_arguments_syntax/slider_cutoff.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)

0 comments on commit 390d599

Please sign in to comment.