Skip to content

Commit

Permalink
change concat (and friends) signature
Browse files Browse the repository at this point in the history
In most use cases you only need an empty vega lite struct so the first
argument is removed. Instead an optional `opts` is added for setting the
parent plots settings if needed.
  • Loading branch information
pnezis committed Dec 22, 2023
1 parent eb20a18 commit eb10e4d
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 44 deletions.
23 changes: 11 additions & 12 deletions lib/tucan.ex
Original file line number Diff line number Diff line change
Expand Up @@ -917,7 +917,7 @@ defmodule Tucan do
|> Tucan.set_size(160, 160)
end
Tucan.hconcat(Tucan.new(), plots)
Tucan.hconcat(plots)
```
You can use `:line_color` and `:stroke_width` to modify the look of the error bars:
Expand Down Expand Up @@ -2165,8 +2165,7 @@ defmodule Tucan do
|> Tucan.set_title(interpolation)
end
VegaLite.new(columns: 2)
|> Tucan.concat(plots)
Tucan.concat(plots, columns: 2)
```
"""
@doc section: :plots
Expand Down Expand Up @@ -3307,18 +3306,18 @@ defmodule Tucan do
Concatenates horizontally the given plots.
"""
@doc section: :layout
@spec hconcat(vl :: VegaLite.t(), plots :: [VegaLite.t()]) :: VegaLite.t()
def hconcat(vl \\ Vl.new(), plots) when is_list(plots) do
VegaLite.concat(vl, plots, :horizontal)
@spec hconcat(plots :: [VegaLite.t()], opts :: keyword()) :: VegaLite.t()
def hconcat(plots, opts \\ []) when is_list(plots) do
VegaLite.concat(Vl.new(opts), plots, :horizontal)
end

@doc """
Concatenates vertically the given plots.
"""
@doc section: :layout
@spec vconcat(vl :: VegaLite.t(), plots :: [VegaLite.t()]) :: VegaLite.t()
def vconcat(vl \\ Vl.new(), plots) when is_list(plots) do
VegaLite.concat(vl, plots, :vertical)
@spec vconcat(plots :: [VegaLite.t()], opts :: keyword()) :: VegaLite.t()
def vconcat(plots, opts \\ []) when is_list(plots) do
VegaLite.concat(Vl.new(opts), plots, :vertical)
end

@doc """
Expand All @@ -3327,9 +3326,9 @@ defmodule Tucan do
This corresponds to the general concatenation of vega-lite (wrappable).
"""
@doc section: :layout
@spec concat(vl :: VegaLite.t(), plots :: [VegaLite.t()]) :: VegaLite.t()
def concat(vl \\ Vl.new(), plots) when is_list(plots) do
VegaLite.concat(vl, plots, :wrappable)
@spec concat(plots :: [VegaLite.t()], opts :: keyword()) :: VegaLite.t()
def concat(plots, opts \\ []) when is_list(plots) do
VegaLite.concat(Vl.new(opts), plots, :wrappable)
end

@doc """
Expand Down
5 changes: 2 additions & 3 deletions lib/tucan/scale.ex
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ defmodule Tucan.Scale.Utils do
)
end

Vl.new(columns: 2)
|> Tucan.concat(plots)
Tucan.concat(plots, columns: 2)
|> Vl.resolve(:scale, color: :independent)
|> Vl.to_spec()
|> Jason.encode!()
Expand Down Expand Up @@ -405,7 +404,7 @@ defmodule Tucan.Scale do
defp validate_scale_options!(_type, opts), do: Keyword.validate!(opts, [])

@doc """
Sets the same `[x, y]` domain for _x-axis_ and _y-axis_ at once.
Sets the same `[x, y]` domain for _x-axis_ and _y-axis_ at once.
"""
@spec set_xy_domain(vl :: VegaLite.t(), min :: number(), max :: number()) :: VegaLite.t()
def set_xy_domain(vl, min, max) do
Expand Down
1 change: 0 additions & 1 deletion lib/tucan/view.ex
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ defmodule Tucan.View do
```tucan
Tucan.hconcat(
Tucan.new(),
[
Tucan.scatter(:iris, "sepal_width", "sepal_length")
|> Tucan.View.set_view_background("#e6fae1"),
Expand Down
57 changes: 29 additions & 28 deletions test/tucan_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -1532,10 +1532,13 @@ defmodule TucanTest do
expected =
Vl.new(bounds: :flush, spacing: 15)
|> Vl.data_from_url(@iris_dataset)
|> Tucan.vconcat([
marginal_x,
Tucan.hconcat(Vl.new(bounds: :flush, spacing: 15), [joint, marginal_y])
])
|> Vl.concat(
[
marginal_x,
Vl.concat(Vl.new(bounds: :flush, spacing: 15), [joint, marginal_y], :horizontal)
],
:vertical
)

assert(Tucan.jointplot(:iris, "petal_width", "petal_length") == expected)
end
Expand All @@ -1559,10 +1562,13 @@ defmodule TucanTest do
expected =
Vl.new(bounds: :flush, spacing: 10)
|> Vl.data_from_url(@iris_dataset)
|> Tucan.vconcat([
marginal_x,
Tucan.hconcat(Vl.new(bounds: :flush, spacing: 10), [joint, marginal_y])
])
|> Vl.concat(
[
marginal_x,
Vl.concat(Vl.new(bounds: :flush, spacing: 10), [joint, marginal_y], :horizontal)
],
:vertical
)

assert(
Tucan.jointplot(:iris, "petal_width", "petal_length",
Expand Down Expand Up @@ -1594,10 +1600,13 @@ defmodule TucanTest do
expected =
Vl.new(bounds: :flush, spacing: 15)
|> Vl.data_from_url(@iris_dataset)
|> Tucan.vconcat([
marginal_x,
Tucan.hconcat(Vl.new(bounds: :flush, spacing: 15), [joint, marginal_y])
])
|> Vl.concat(
[
marginal_x,
Vl.concat(Vl.new(bounds: :flush, spacing: 15), [joint, marginal_y], :horizontal)
],
:vertical
)

assert(
Tucan.jointplot(:iris, "petal_width", "petal_length",
Expand Down Expand Up @@ -1627,10 +1636,13 @@ defmodule TucanTest do
expected =
Vl.new(bounds: :flush, spacing: 15)
|> Vl.data_from_url(@iris_dataset)
|> Tucan.vconcat([
marginal_x,
Tucan.hconcat(Vl.new(bounds: :flush, spacing: 15), [joint, marginal_y])
])
|> Vl.concat(
[
marginal_x,
Vl.concat(Vl.new(bounds: :flush, spacing: 15), [joint, marginal_y], :horizontal)
],
:vertical
)

assert(
Tucan.jointplot(:iris, "petal_width", "petal_length",
Expand Down Expand Up @@ -1935,25 +1947,14 @@ defmodule TucanTest do
end

describe "concat and friends" do
test "with no input plot" do
test "with default options" do
plot1 = Tucan.scatter(@dataset, "x", "y")
plot2 = Tucan.scatter(@dataset, "x", "y")

assert Tucan.concat([plot1, plot2]) == Vl.concat(Vl.new(), [plot1, plot2], :wrappable)
assert Tucan.hconcat([plot1, plot2]) == Vl.concat(Vl.new(), [plot1, plot2], :horizontal)
assert Tucan.vconcat([plot1, plot2]) == Vl.concat(Vl.new(), [plot1, plot2], :vertical)
end

test "with input plot" do
vl = VegaLite.new(width: 400, height: 300)

plot1 = Tucan.scatter(@dataset, "x", "y")
plot2 = Tucan.scatter(@dataset, "x", "y")

assert Tucan.concat(vl, [plot1, plot2]) == Vl.concat(vl, [plot1, plot2], :wrappable)
assert Tucan.hconcat(vl, [plot1, plot2]) == Vl.concat(vl, [plot1, plot2], :horizontal)
assert Tucan.vconcat(vl, [plot1, plot2]) == Vl.concat(vl, [plot1, plot2], :vertical)
end
end

describe "layers" do
Expand Down

0 comments on commit eb10e4d

Please sign in to comment.