Skip to content

Commit

Permalink
Allow setting width, height to "container" (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
nallwhy authored May 20, 2024
1 parent f482699 commit 3de20a8
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 12 deletions.
19 changes: 13 additions & 6 deletions lib/tucan.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4265,10 +4265,15 @@ defmodule Tucan do
This sets both width and height at once.
"""
@doc section: :styling
@spec set_size(vl :: VegaLite.t(), width :: pos_integer(), height :: pos_integer()) ::
@spec set_size(
vl :: VegaLite.t(),
width :: pos_integer() | :container,
height :: pos_integer() | :container
) ::
VegaLite.t()
def set_size(vl, width, height)
when is_struct(vl, VegaLite) and is_pos_integer(width) and is_pos_integer(height) do
when is_struct(vl, VegaLite) and (is_pos_integer(width) or width == :container) and
(is_pos_integer(height) or height == :container) do
vl
|> set_width(width)
|> set_height(height)
Expand All @@ -4278,17 +4283,19 @@ defmodule Tucan do
Sets the width of the plot (in pixels).
"""
@doc section: :styling
@spec set_width(vl :: VegaLite.t(), width :: pos_integer()) :: VegaLite.t()
def set_width(vl, width) when is_struct(vl, VegaLite) and is_pos_integer(width) do
@spec set_width(vl :: VegaLite.t(), width :: pos_integer() | :container) :: VegaLite.t()
def set_width(vl, width)
when is_struct(vl, VegaLite) and (is_pos_integer(width) or width == :container) do
update_in(vl.spec, fn spec -> Map.merge(spec, %{"width" => width}) end)
end

@doc """
Sets the height of the plot (in pixels).
"""
@doc section: :styling
@spec set_height(vl :: VegaLite.t(), height :: pos_integer()) :: VegaLite.t()
def set_height(vl, height) when is_struct(vl, VegaLite) and is_pos_integer(height) do
@spec set_height(vl :: VegaLite.t(), height :: pos_integer() | :container) :: VegaLite.t()
def set_height(vl, height)
when is_struct(vl, VegaLite) and (is_pos_integer(height) or height == :container) do
update_in(vl.spec, fn spec -> Map.merge(spec, %{"height" => height}) end)
end

Expand Down
10 changes: 6 additions & 4 deletions lib/tucan/options.ex
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,16 @@ defmodule Tucan.Options do
options = [
# Global opts
width: [
type: :integer,
doc: "Width of the image",
type: {:or, [:pos_integer, {:in, [:container]}]},
doc:
"Width of the plot. Can either be the width in pixels or `:container` to indicate that the width of the plot should be the same as its surrounding container. ",
section: :style,
dest: :spec
],
height: [
type: :integer,
doc: "Height of the image",
type: {:or, [:pos_integer, {:in, [:container]}]},
doc:
"Height of the plot. Can either be the height in pixels or `:container` to indicate that the height of the plot should be the same as its surrounding container. ",
section: :style,
dest: :spec
],
Expand Down
23 changes: 21 additions & 2 deletions test/tucan_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2497,25 +2497,44 @@ defmodule TucanTest do
end

describe "plot size" do
test "set_size/3" do
test "set_size/3 with integers" do
vl = Tucan.set_size(Vl.new(), 100, 120)

assert vl.spec["width"] == 100
assert vl.spec["height"] == 120
end

test "sets_width/2" do
test "set_size/3 with :container" do
vl = Tucan.set_size(Vl.new(), :container, :container)

assert vl.spec["width"] == :container
assert vl.spec["height"] == :container
end

test "sets_width/2 with integer" do
vl = Tucan.set_width(Vl.new(), 100)

assert vl.spec["width"] == 100
end

test "sets_width/2 with :container" do
vl = Tucan.set_width(Vl.new(), :container)

assert vl.spec["width"] == :container
end

test "set_height/2" do
vl = Tucan.set_height(Vl.new(), 100)

assert vl.spec["height"] == 100
end

test "sets_height/2 with :container" do
vl = Tucan.set_height(Vl.new(), :container)

assert vl.spec["height"] == :container
end

test "can be called multiple times" do
vl =
Vl.new()
Expand Down

0 comments on commit 3de20a8

Please sign in to comment.