Skip to content

Commit

Permalink
docs: Use when in arguments syntax examples (1/2)
Browse files Browse the repository at this point in the history
Covers all the examples that had a *methods* equivalent.
Now have 16 more to do, across 12 files
  • Loading branch information
dangotbanned committed Sep 6, 2024
1 parent dfab00c commit ff0bc34
Show file tree
Hide file tree
Showing 18 changed files with 69 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
alt.X('level_0', title=None),
alt.Y('level_1', title=None),
alt.Color('correlation', scale=alt.Scale(domain=[-1, 1], scheme='blueorange')),
opacity=alt.condition(select_x & select_y, alt.value(1), alt.value(0.4))
opacity=alt.when(select_x, select_y).then(alt.value(1)).otherwise(alt.value(0.4)),
).add_params(
select_x, select_y
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
bar = alt.Chart(source).mark_bar().encode(
x='Major_Genre:N',
y='count()',
color=alt.condition(pts, alt.ColorValue("steelblue"), alt.ColorValue("grey"))
color=alt.when(pts).then(alt.ColorValue("steelblue")).otherwise(alt.ColorValue("grey"))
).properties(
width=550,
height=200
Expand Down
2 changes: 1 addition & 1 deletion tests/examples_arguments_syntax/interactive_legend.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
alt.X('yearmonth(date):T', axis=alt.Axis(domain=False, format='%Y', tickSize=0)),
alt.Y('sum(count):Q', stack='center', axis=None),
alt.Color('series:N', scale=alt.Scale(scheme='category20b')),
opacity=alt.condition(selection, alt.value(1), alt.value(0.2))
opacity=alt.when(selection).then(alt.value(1)).otherwise(alt.value(0.2))
).add_params(
selection
)
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
).mark_circle(opacity=0.35, tooltip=True).encode(
longitude="lon:Q",
latitude="lat:Q",
color=alt.condition(brush, alt.value("goldenrod"), alt.value("steelblue")),
color=alt.when(brush).then(alt.value("goldenrod")).otherwise(alt.value("steelblue")),
size=alt.Size("mag:Q", scale=alt.Scale(type="pow", range=[1, 1000], domain=[0, 7], exponent=4)),
).add_params(brush)

Expand Down
12 changes: 6 additions & 6 deletions tests/examples_arguments_syntax/lasagna_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@

source = data.stocks()

color_condition = alt.condition(
"month(datum.value) == 1 && date(datum.value) == 1",
alt.value("black"),
alt.value(None),
color_condition = (
alt.when(alt.expr.month("datum.value") == 1, alt.expr.date("datum.value") == 1)
.then(alt.value("black"))
.otherwise(alt.value(None))
)

alt.Chart(source, width=300, height=100).transform_filter(
Expand All @@ -23,8 +23,8 @@
format="%Y",
labelAngle=0,
labelOverlap=False,
labelColor=color_condition,
tickColor=color_condition,
labelColor=color_condition, # type: ignore[arg-type]
tickColor=color_condition, # type: ignore[arg-type]
),
title="Time",
),
Expand Down
12 changes: 6 additions & 6 deletions tests/examples_arguments_syntax/layered_heatmap_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@
)
)

color = (
alt.when(alt.datum.mean_horsepower > 150)
.then(alt.value("black"))
.otherwise(alt.value("white"))
)
# Configure text
text = base.mark_text(baseline='middle').encode(
text=alt.Text('mean_horsepower:Q', format=".0f"),
color=alt.condition(
alt.datum.mean_horsepower > 150,
alt.value('black'),
alt.value('white')
)
text=alt.Text('mean_horsepower:Q', format=".0f"), color=color
)

# Draw the chart
Expand Down
2 changes: 1 addition & 1 deletion tests/examples_arguments_syntax/multiline_highlight.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
)

lines = base.mark_line().encode(
size=alt.condition(~highlight, alt.value(1), alt.value(3))
size=alt.when(~highlight).then(alt.value(1)).otherwise(alt.value(3))
)

points + lines
5 changes: 3 additions & 2 deletions tests/examples_arguments_syntax/multiline_tooltip.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,16 @@
).add_params(
nearest
)
when_near = alt.when(nearest)

# Draw points on the line, and highlight based on selection
points = line.mark_point().encode(
opacity=alt.condition(nearest, alt.value(1), alt.value(0))
opacity=when_near.then(alt.value(1)).otherwise(alt.value(0))
)

# Draw text labels near the points, and highlight based on selection
text = line.mark_text(align="left", dx=5, dy=-5).encode(
text=alt.condition(nearest, "y:Q", alt.value(" "))
text=when_near.then("y:Q").otherwise(alt.value(" "))
)

# Draw a rule at the location of the selection
Expand Down
5 changes: 3 additions & 2 deletions tests/examples_arguments_syntax/multiline_tooltip_standard.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,11 @@
y="y:Q",
color="category:N"
)
when_near = alt.when(nearest)

# Draw points on the line, and highlight based on selection
points = line.mark_point().encode(
opacity=alt.condition(nearest, alt.value(1), alt.value(0))
opacity=when_near.then(alt.value(1)).otherwise(alt.value(0))
)

# Draw a rule at the location of the selection
Expand All @@ -41,7 +42,7 @@
groupby=["x"]
).mark_rule(color="gray").encode(
x="x:Q",
opacity=alt.condition(nearest, alt.value(0.3), alt.value(0)),
opacity=when_near.then(alt.value(0.3)).otherwise(alt.value(0)),
tooltip=[alt.Tooltip(c, type="quantitative") for c in columns],
).add_params(nearest)

Expand Down
22 changes: 11 additions & 11 deletions tests/examples_arguments_syntax/multiple_interactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,32 +61,32 @@
rating_radio = alt.binding_radio(options=ratings, name="Rating")
rating_select = alt.selection_point(fields=['MPAA_Rating'], bind=rating_radio)

rating_color_condition = alt.condition(
rating_select,
alt.Color('MPAA_Rating:N', legend=None),
alt.value('lightgray')
rating_color = (
alt.when(rating_select)
.then(alt.Color('MPAA_Rating:N', legend=None))
.otherwise(alt.value("lightgray"))
)

highlight_ratings = base.add_params(
rating_select
).encode(
color=rating_color_condition
color=rating_color
).properties(title="Radio Button Highlighting")

# Boolean selection for format changes
input_checkbox = alt.binding_checkbox(name="Big Budget Films ")
checkbox_selection = alt.param(bind=input_checkbox)

size_checkbox_condition = alt.condition(
checkbox_selection,
alt.Size('Hundred_Million_Production:Q'),
alt.SizeValue(25)
size_checkbox = (
alt.when(checkbox_selection)
.then(alt.Size('Big_Budget_Film:N', scale=alt.Scale(range=[25, 150])))
.otherwise(alt.value(25))
)

budget_sizing = base.add_params(
checkbox_selection
).encode(
size=size_checkbox_condition
size=size_checkbox
).properties(title="Checkbox Formatting")

(filter_year | filter_genres) & (highlight_ratings | budget_sizing)
(filter_year | budget_sizing) & (highlight_ratings | filter_genres)
2 changes: 1 addition & 1 deletion tests/examples_arguments_syntax/scatter_linked_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
points = alt.Chart(source).mark_point().encode(
x='Horsepower:Q',
y='Miles_per_Gallon:Q',
color=alt.condition(brush, alt.value('steelblue'), alt.value('grey'))
color=alt.when(brush).then(alt.value("steelblue")).otherwise(alt.value("grey"))
).add_params(brush)

# Base chart for data tables
Expand Down
31 changes: 15 additions & 16 deletions tests/examples_arguments_syntax/scatter_point_paths_hover.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,22 +53,21 @@
"datum.country !== 'North Korea' && datum.country !== 'South Korea'"
)

search_matches = alt.expr.test(alt.expr.regexp(search_box, "i"), alt.datum.country)
opacity = (
alt.when(hover_point_opacity, search_matches)
.then(alt.value(0.8))
.otherwise(alt.value(0.1))
)
# Points that are always visible (filtered by slider and search)
visible_points = base.mark_circle(size=100).encode(
opacity=alt.condition(
hover_point_opacity
& alt.expr.test(alt.expr.regexp(search_box, 'i'), alt.datum.country),
alt.value(0.8),
alt.value(0.1)
)
).transform_filter(
x_select
).add_params(
hover,
hover_point_opacity,
x_select
visible_points = (
base.mark_circle(size=100)
.encode(opacity=opacity)
.transform_filter(x_select)
.add_params(hover, hover_point_opacity, x_select)
)

when_hover = alt.when(hover)
hover_line = alt.layer(
# Line layer
base.mark_trail().encode(
Expand All @@ -81,12 +80,12 @@
scale=alt.Scale(domain=[1955, 2005], range=[1, 12]),
legend=None
),
opacity=alt.condition(hover, alt.value(0.3), alt.value(0)),
opacity=when_hover.then(alt.value(0.3)).otherwise(alt.value(0)),
color=alt.value('#222222')
),
# Point layer
base.mark_point(size=50).encode(
opacity=alt.condition(hover, alt.value(0.8), alt.value(0)),
opacity=when_hover.then(alt.value(0.8)).otherwise(alt.value(0)),
)
)

Expand All @@ -108,7 +107,7 @@
y='life_expect:Q',
text='country:N',
color=alt.value('black'),
opacity=alt.condition(hover, alt.value(1), alt.value(0))
opacity=when_hover.then(alt.value(1)).otherwise(alt.value(0))
).transform_window(
rank='rank(life_expect)',
sort=[alt.SortField('life_expect', order='descending')],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@

color_scale = alt.Scale(domain=['M', 'F'],
range=['#1FC3AA', '#8624F5'])

color = (
alt.when(selector)
.then(alt.Color("gender:N", scale=color_scale))
.otherwise(alt.value("lightgray"))
)
base = alt.Chart(source).properties(
width=250,
height=250
Expand All @@ -39,11 +43,7 @@
points = base.mark_point(filled=True, size=200).encode(
x=alt.X('mean(height):Q', scale=alt.Scale(domain=[0,84])),
y=alt.Y('mean(weight):Q', scale=alt.Scale(domain=[0,250])),
color=alt.condition(
selector,
'gender:N',
alt.value('lightgray'),
scale=color_scale),
color=color,
)

hists = base.mark_bar(opacity=0.5, thickness=100).encode(
Expand Down
2 changes: 1 addition & 1 deletion tests/examples_arguments_syntax/scatter_with_minimap.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
.encode(
x="date:T",
y="temp_max:Q",
color=alt.condition(zoom, "weather", alt.value("lightgray")),
color=alt.when(zoom).then("weather").otherwise(alt.value("lightgray")),
)
.properties(
width=200,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
title='Maximum Daily Temperature (C)',
scale=alt.Scale(domain=[-5, 40])
),
color=alt.condition(brush, color, alt.value('lightgray')),
color=alt.when(brush).then(color).otherwise(alt.value("lightgray")),
size=alt.Size('precipitation:Q', scale=alt.Scale(range=[5, 200]))
).properties(
width=550,
Expand All @@ -43,7 +43,7 @@
bars = alt.Chart().mark_bar().encode(
x='count()',
y='weather:N',
color=alt.condition(click, color, alt.value('lightgray')),
color=alt.when(click).then(color).otherwise(alt.value("lightgray")),
).transform_filter(
brush
).properties(
Expand Down
7 changes: 6 additions & 1 deletion tests/examples_arguments_syntax/select_detail.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@
# Data is prepared, now make a chart

selector = alt.selection_point(fields=['id'])
color = (
alt.when(selector)
.then(alt.Color("id:O", legend=None))
.otherwise(alt.value("lightgray"))
)

base = alt.Chart(data).properties(
width=250,
Expand All @@ -51,7 +56,7 @@
points = base.mark_point(filled=True, size=200).encode(
x='mean(x)',
y='mean(y)',
color=alt.condition(selector, 'id:O', alt.value('lightgray'), legend=None),
color=color,
)

line = base.mark_line().encode(
Expand Down
7 changes: 2 additions & 5 deletions tests/examples_arguments_syntax/us_employment.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,15 @@
"president": "Obama"
}
])
predicate = alt.datum.nonfarm_change > 0

bars = alt.Chart(
source,
title="The U.S. employment crash during the Great Recession"
).mark_bar().encode(
x=alt.X("month:T", title=""),
y=alt.Y("nonfarm_change:Q", title="Change in non-farm employment (in thousands)"),
color=alt.condition(
alt.datum.nonfarm_change > 0,
alt.value("steelblue"),
alt.value("orange")
)
color=alt.when(predicate).then(alt.value("steelblue")).otherwise(alt.value("orange")),
)

rule = alt.Chart(presidents).mark_rule(
Expand Down
4 changes: 2 additions & 2 deletions tests/examples_arguments_syntax/us_state_capitals.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@

text = base.mark_text(dy=-5, align='right').encode(
alt.Text('city', type='nominal'),
opacity=alt.condition(~hover, alt.value(0), alt.value(1))
opacity=alt.when(~hover).then(alt.value(0)).otherwise(alt.value(1))
)

points = base.mark_point().encode(
color=alt.value('black'),
size=alt.condition(~hover, alt.value(30), alt.value(100))
size=alt.when(~hover).then(alt.value(30)).otherwise(alt.value(100))
).add_params(hover)

background + points + text

0 comments on commit ff0bc34

Please sign in to comment.