Skip to content

Commit

Permalink
refactor: Move entire chart definition to alt_theme_test
Browse files Browse the repository at this point in the history
Resolve part of #3630 (comment)
  • Loading branch information
dangotbanned committed Oct 5, 2024
1 parent 436a65d commit f466d8a
Showing 1 changed file with 117 additions and 114 deletions.
231 changes: 117 additions & 114 deletions tests/altair_theme_test.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# ruff: noqa: E711
from __future__ import annotations

import json
Expand All @@ -8,135 +9,136 @@
import altair as alt
from vega_datasets import data

common_data = alt.InlineData(
[
{"Index": 1, "Value": 28, "Position": 1, "Category": "A"},
{"Index": 2, "Value": 55, "Position": 2, "Category": "A"},
{"Index": 3, "Value": 43, "Position": 3, "Category": "A"},
{"Index": 4, "Value": 91, "Position": 4, "Category": "A"},
{"Index": 5, "Value": 81, "Position": 5, "Category": "A"},
{"Index": 6, "Value": 53, "Position": 6, "Category": "A"},
{"Index": 7, "Value": 19, "Position": 1, "Category": "B"},
{"Index": 8, "Value": 87, "Position": 2, "Category": "B"},
{"Index": 9, "Value": 52, "Position": 3, "Category": "B"},
{"Index": 10, "Value": 48, "Position": 4, "Category": "B"},
{"Index": 11, "Value": 24, "Position": 5, "Category": "B"},
{"Index": 12, "Value": 49, "Position": 6, "Category": "B"},
{"Index": 13, "Value": 87, "Position": 1, "Category": "C"},
{"Index": 14, "Value": 66, "Position": 2, "Category": "C"},
{"Index": 15, "Value": 17, "Position": 3, "Category": "C"},
{"Index": 16, "Value": 27, "Position": 4, "Category": "C"},
{"Index": 17, "Value": 68, "Position": 5, "Category": "C"},
{"Index": 18, "Value": 16, "Position": 6, "Category": "C"},
]
)


bar = (
alt.Chart(common_data, title="Bar", width=480, height=150)
.mark_bar()
.encode(x=alt.X("Index:O").axis(offset=1), y=alt.Y("Value:Q"), tooltip="Value:Q")
)

line = (
alt.Chart(common_data, width=240, height=150, title="Line")
.mark_line()
.encode(
x=alt.X("Position:O").axis(grid=False),
y=alt.Y("Value:Q").axis(grid=False),
color=alt.Color("Category:N").legend(None),
tooltip=["Index:O", "Value:Q", "Position:O", "Category:N"],
def alt_theme_test() -> alt.VConcatChart:
common_data = alt.InlineData(
[
{"Index": 1, "Value": 28, "Position": 1, "Category": "A"},
{"Index": 2, "Value": 55, "Position": 2, "Category": "A"},
{"Index": 3, "Value": 43, "Position": 3, "Category": "A"},
{"Index": 4, "Value": 91, "Position": 4, "Category": "A"},
{"Index": 5, "Value": 81, "Position": 5, "Category": "A"},
{"Index": 6, "Value": 53, "Position": 6, "Category": "A"},
{"Index": 7, "Value": 19, "Position": 1, "Category": "B"},
{"Index": 8, "Value": 87, "Position": 2, "Category": "B"},
{"Index": 9, "Value": 52, "Position": 3, "Category": "B"},
{"Index": 10, "Value": 48, "Position": 4, "Category": "B"},
{"Index": 11, "Value": 24, "Position": 5, "Category": "B"},
{"Index": 12, "Value": 49, "Position": 6, "Category": "B"},
{"Index": 13, "Value": 87, "Position": 1, "Category": "C"},
{"Index": 14, "Value": 66, "Position": 2, "Category": "C"},
{"Index": 15, "Value": 17, "Position": 3, "Category": "C"},
{"Index": 16, "Value": 27, "Position": 4, "Category": "C"},
{"Index": 17, "Value": 68, "Position": 5, "Category": "C"},
{"Index": 18, "Value": 16, "Position": 6, "Category": "C"},
]
)
)

point_shape = (
alt.Chart(common_data, width=200, height=200, title="Point (Shape)")
.mark_point()
.encode(
x=alt.X("Position:O").axis(grid=False),
y=alt.Y("Value:Q").axis(grid=False),
shape=alt.Shape("Category:N").legend(None),
color=alt.Color("Category:N").legend(None),
tooltip=["Index:O", "Value:Q", "Position:O", "Category:N"],
bar = (
alt.Chart(common_data, title="Bar", width=480, height=150)
.mark_bar()
.encode(
x=alt.X("Index:O").axis(offset=1), y=alt.Y("Value:Q"), tooltip="Value:Q"
)
)
)

bar_facet = (
alt.Chart(data.barley(), width=220, title=alt.Title("Bar (Facet)", anchor="middle"))
.mark_bar(tooltip=True)
.encode(column="year:O", x="yield", y="variety", color="site")
)

rect_heatmap = (
alt.Chart(
data.seattle_weather(),
title=alt.Title(
"Rect (Heatmap)", subtitle="Daily Max Temperatures (C) in Seattle, WA"
),
height=200,
)
.mark_rect()
.encode(
x=alt.X("date(date):O").title("Day").axis(format="%e", labelAngle=0),
y=alt.Y("month(date):O").title("Month"),
color=alt.Color("max(temp_max)").title(None),
tooltip=[
alt.Tooltip("monthdate(date)", title="Date"),
alt.Tooltip("max(temp_max)", title="Max Temp"),
],
line = (
alt.Chart(common_data, width=240, height=150, title="Line")
.mark_line()
.encode(
x=alt.X("Position:O").axis(grid=False),
y=alt.Y("Value:Q").axis(grid=False),
color=alt.Color("Category:N").legend(None),
tooltip=["Index:O", "Value:Q", "Position:O", "Category:N"],
)
)
)

geoshape = (
alt.Chart(
alt.topo_feature(data.us_10m.url, "counties"),
title=alt.Title("Geoshape", subtitle="Unemployment rate per county"),
width=500,
height=300,
)
.mark_geoshape(tooltip=True)
.encode(color="rate:Q")
.transform_lookup(
lookup="id",
from_=alt.LookupData(data.unemployment.url, "id", ["rate"]), # pyright: ignore[reportArgumentType]
point_shape = (
alt.Chart(common_data, width=200, height=200, title="Point (Shape)")
.mark_point()
.encode(
x=alt.X("Position:O").axis(grid=False),
y=alt.Y("Value:Q").axis(grid=False),
shape=alt.Shape("Category:N").legend(None),
color=alt.Color("Category:N").legend(None),
tooltip=["Index:O", "Value:Q", "Position:O", "Category:N"],
)
)
.project(type="albersUsa")
)

point = (
alt.Chart(data.movies.url, height=250, width=250, title="Point")
.mark_point(tooltip=True)
.transform_filter(alt.datum["IMDB_Rating"] != None) # noqa: E711
.transform_filter(
alt.FieldRangePredicate("Release_Date", [None, 2019], timeUnit="year")
)
.transform_joinaggregate(Average_Rating="mean(IMDB_Rating)")
.transform_calculate(
Rating_Delta=alt.datum["IMDB_Rating"] - alt.datum.Average_Rating
bar_facet = (
alt.Chart(
data.barley(), width=220, title=alt.Title("Bar (Facet)", anchor="middle")
)
.mark_bar(tooltip=True)
.encode(column="year:O", x="yield", y="variety", color="site")
)
.encode(
x=alt.X("Release_Date:T").title("Release Date"),
y=alt.Y("Rating_Delta:Q").title("Rating Delta"),
color=alt.Color("Rating_Delta:Q").title("Rating Delta").scale(domainMid=0),

rect_heatmap = (
alt.Chart(
data.seattle_weather(),
title=alt.Title(
"Rect (Heatmap)", subtitle="Daily Max Temperatures (C) in Seattle, WA"
),
height=200,
)
.mark_rect()
.encode(
x=alt.X("date(date):O").title("Day").axis(format="%e", labelAngle=0),
y=alt.Y("month(date):O").title("Month"),
color=alt.Color("max(temp_max)").title(None),
tooltip=[
alt.Tooltip("monthdate(date)", title="Date"),
alt.Tooltip("max(temp_max)", title="Max Temp"),
],
)
)
)

area = (
alt.Chart(data.iowa_electricity(), title="Area", height=250, width=250)
.mark_area(tooltip=True)
.encode(
alt.X("year:T").title("Year"),
alt.Y("net_generation:Q")
.title("Share of net generation")
.stack("normalize")
.axis(format=".0%"),
alt.Color("source:N").title("Electricity source"),
geoshape = (
alt.Chart(
alt.topo_feature(data.us_10m.url, "counties"),
title=alt.Title("Geoshape", subtitle="Unemployment rate per county"),
width=500,
height=300,
)
.mark_geoshape(tooltip=True)
.encode(color="rate:Q")
.transform_lookup(
"id", alt.LookupData(alt.UrlData(data.unemployment.url), "id", ["rate"])
)
.project(type="albersUsa")
)
)

point = (
alt.Chart(data.movies.url, height=250, width=250, title="Point")
.mark_point(tooltip=True)
.transform_filter(alt.datum["IMDB_Rating"] != None)
.transform_filter(
alt.FieldRangePredicate("Release_Date", [None, 2019], timeUnit="year")
)
.transform_joinaggregate(Average_Rating="mean(IMDB_Rating)")
.transform_calculate(
Rating_Delta=alt.datum["IMDB_Rating"] - alt.datum.Average_Rating
)
.encode(
x=alt.X("Release_Date:T").title("Release Date"),
y=alt.Y("Rating_Delta:Q").title("Rating Delta"),
color=alt.Color("Rating_Delta:Q").title("Rating Delta").scale(domainMid=0),
)
)

def alt_theme_test() -> alt.VConcatChart:
return (
area = (
alt.Chart(data.iowa_electricity(), title="Area", height=250, width=250)
.mark_area(tooltip=True)
.encode(
alt.X("year:T").title("Year"),
alt.Y("net_generation:Q")
.title("Share of net generation")
.stack("normalize")
.axis(format=".0%"),
alt.Color("source:N").title("Electricity source"),
)
)
compound_chart = (
(bar | line)
& (point_shape | bar_facet).resolve_scale(color="independent")
& (point | area)
Expand All @@ -150,6 +152,7 @@ def alt_theme_test() -> alt.VConcatChart:
offset=16,
)
)
return compound_chart


TEMPLATE = jinja2.Template(
Expand Down

0 comments on commit f466d8a

Please sign in to comment.