Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue 283 nano plot scaling #404

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions great_tables/_formats.py
Original file line number Diff line number Diff line change
Expand Up @@ -3846,7 +3846,10 @@

# Check each cell in the column and get each of them that contains a scalar value
# Why are we grabbing the first element of a tuple? (Note this also happens again below.)
all_single_y_vals = to_list(data_tbl[columns])
if rows is not None:
all_single_y_vals = to_list(data_tbl[columns][rows])
else:
all_single_y_vals = to_list(data_tbl[columns])

autoscale = False

Expand All @@ -3868,7 +3871,10 @@
# TODO: if a column of delimiter separated strings is passed. E.g. "1 2 3 4". Does this mean
# that autoscale does not work? In this case, is col_i_y_vals_raw a string that gets processed?
# downstream?
all_y_vals_raw = to_list(data_tbl[columns])
if rows is not None:
all_y_vals_raw = to_list(data_tbl[columns][rows])
else:
all_y_vals_raw = to_list(data_tbl[columns])

Check warning on line 3877 in great_tables/_formats.py

View check run for this annotation

Codecov / codecov/patch

great_tables/_formats.py#L3877

Added line #L3877 was not covered by tests

all_y_vals = []

Expand Down
70 changes: 70 additions & 0 deletions tests/test_formats.py
Original file line number Diff line number Diff line change
Expand Up @@ -1603,6 +1603,8 @@ def _nanoplot_has_tag_attrs(nanoplot_str: str, tag: str, attrs: list[tuple[str,

df_fmt_nanoplot_single = pl.DataFrame({"vals": [-5.3, 6.3]})

df_fmt_nanoplot_single_different_scales = pl.DataFrame({"vals": [-5.3, 100.0]})

df_fmt_nanoplot_multi = pl.DataFrame(
{
"vals": [
Expand All @@ -1612,6 +1614,14 @@ def _nanoplot_has_tag_attrs(nanoplot_str: str, tag: str, attrs: list[tuple[str,
}
)

df_fmt_nanoplot_multi_different_scales = pl.DataFrame(
{
"vals": [
{"x": [-12.0, -5.0, 6.0, 3.0, 0.0, 8.0, -7.0]},
{"x": [2, 0, 150, 7, 80, 10, 10, 240, 17, 13, 6]},
],
}
)

FMT_NANOPLOT_CASES: list[dict[str, Any]] = [
# 1. default case
Expand Down Expand Up @@ -1988,6 +1998,66 @@ def test_fmt_nanoplot_multi_vals_bar_ref_line_ref_area():
)


# Test category 8: Scale unaffected by unselected rows for single line plot
def test_fmt_nanoplot_single_vals_same_values_in_selected_rows():

# All test cases should have the same scale for each plot when their values are the same in
# in selected rows, ignoring values in unselected rows

for _, params in enumerate(FMT_NANOPLOT_CASES):

gt = GT(df_fmt_nanoplot_single).fmt_nanoplot(
columns="vals",
plot_type="line",
rows=[0],
**params,
)

gt_different_scales = GT(df_fmt_nanoplot_single_different_scales).fmt_nanoplot(
columns="vals",
plot_type="line",
rows=[0],
**params,
)

res = _get_column_of_values(gt, column_name="vals", context="html")[0]
res_different_scales = _get_column_of_values(
gt_different_scales, column_name="vals", context="html"
)[0]
assert res == res_different_scales


# Test category 9: Scale unaffected by unselected rows for line plot with multiple values
def test_fmt_nanoplot_multiple_vals_same_values_in_selected_rows_with_autoscale():

# All test cases should have the same scale for each plot when their values are the same in
# in selected rows and autoscale is used, ignoring values in unselected rows

for _, params in enumerate(FMT_NANOPLOT_CASES):

gt = GT(df_fmt_nanoplot_multi).fmt_nanoplot(
columns="vals",
plot_type="line",
rows=[0],
autoscale=True,
**params,
)

gt_different_scales = GT(df_fmt_nanoplot_multi_different_scales).fmt_nanoplot(
columns="vals",
plot_type="line",
rows=[0],
autoscale=True,
**params,
)

res = _get_column_of_values(gt, column_name="vals", context="html")[0]
res_different_scales = _get_column_of_values(
gt_different_scales, column_name="vals", context="html"
)[0]
assert res == res_different_scales


@pytest.mark.parametrize(
"plot_type",
[
Expand Down