Skip to content

Commit

Permalink
add Tucan.View.set_view_background/2
Browse files Browse the repository at this point in the history
  • Loading branch information
pnezis committed Nov 18, 2023
1 parent c23f4e9 commit 72a5936
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 3 deletions.
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@

### Added

- Support for setting plot's background color through `Tucan.View.set_background/2`
- Support setting plot's background color through `Tucan.View.set_background/2`
- Support setting view's background color through `Tucan.View.set_view_background/2`
- Add `Tucan.Axes.set_offset/3`
- Support setting axes orientation with `Tucan.Axes.set_orientation/3`
- Add `Tucan.Legend.set_offset/3`
- Support setting axes orientation with `Tucan.Axes.set_orientation/3`

```tucan
Tucan.scatter(:iris, "petal_width", "petal_length")
Expand Down
32 changes: 31 additions & 1 deletion lib/tucan/view.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule Tucan.View do
"""

@doc """
Sets the background color of the plot view.
Sets the background color of the visualization canvas.
## Examples
Expand All @@ -17,4 +17,34 @@ defmodule Tucan.View do
def set_background(vl, color) when is_struct(vl, VegaLite) and is_binary(color) do
update_in(vl.spec, fn spec -> Map.merge(spec, %{"background" => color}) end)
end

@doc """
Sets the background color of the view.
`set_background/2` defines the background of the whole visualization canvas. Meanwhile,
the view property of a single-view or layer specification can define the background of
the view.
## Examples
Two concatenated plots with different view backgrounds and an overall canvas
background.
```tucan
Tucan.hconcat(
Tucan.new(),
[
Tucan.scatter(:iris, "sepal_width", "sepal_length")
|> Tucan.View.set_view_background("#e6fae1"),
Tucan.scatter(:iris, "petal_width", "petal_length")
|> Tucan.View.set_view_background("#facbc5"),
]
)
|> Tucan.View.set_background("#fcffde")
```
"""
@spec set_view_background(vl :: VegaLite.t(), color :: String.t()) :: VegaLite.t()
def set_view_background(vl, color) do
update_in(vl.spec, fn spec -> Map.merge(spec, %{"view" => %{"fill" => color}}) end)
end
end
6 changes: 6 additions & 0 deletions test/tucan/view_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,10 @@ defmodule Tucan.ViewTest do

assert Tucan.View.set_background(Vl.new(), "green") == expected
end

test "set_view_background/2" do
vl = Tucan.View.set_view_background(Vl.new(), "red")

assert get_in(vl.spec, ["view"]) == %{"fill" => "red"}
end
end

0 comments on commit 72a5936

Please sign in to comment.