Skip to content

Commit

Permalink
Add Tucan.Axes.set_title_color/3
Browse files Browse the repository at this point in the history
  • Loading branch information
pnezis committed Jun 22, 2024
1 parent a495fb3 commit 54b3c40
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 19 deletions.
10 changes: 1 addition & 9 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ Tucan.layers([
```

- Add `Tucan.Axes.set_color/2`, `Tucan.Axes.set_color/3` helpers.
- Add `Tucan.Axes.set_title_color/3` helpers.
- Add `Tucan.Grid.set_color/2` helper.
- Add `Tucan.Scale.set_clamp/3`.
- Support setting `:container` to `width` and `height`.
Expand Down Expand Up @@ -179,15 +180,6 @@ Tucan.new()
- Add `Tucan.background_image/2` helper function.

- Add `Tucan.circle/4` helper function

```tucan
Tucan.new()
|> Tucan.circle({3, 2}, 5, line_color: "purple")
|> Tucan.circle({-1, 6}, 2, line_color: "red")
|> Tucan.circle({0, 1}, 4, line_color: "green", stroke_width: 3)
|> Tucan.Scale.set_xy_domain(-4, 8)
```

- Add `Tucan.Scale.set_xy_domain/3`
- Support setting multi-line string in `Tucan.set_title/3`

Expand Down
18 changes: 18 additions & 0 deletions lib/tucan/axes.ex
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,24 @@ defmodule Tucan.Axes do
put_options(vl, axis, title: title)
end

@doc """
Sets the title color of the given `axis`
## Examples
```tucan
Tucan.scatter(:iris, "petal_width", "petal_length")
|> Tucan.Axes.set_title_color(:x, "red")
|> Tucan.Axes.set_title_color(:y, "#F3B212")
```
"""
@spec set_title_color(vl :: VegaLite.t(), axis :: axis(), color :: String.t()) :: VegaLite.t()
def set_title_color(vl, axis, color) when is_struct(vl, VegaLite) and is_binary(color) do
validate_axis!(axis, [:x, :y], "set_title_color/3")

put_options(vl, axis, title_color: color)
end

@doc """
Sets the axis offset (in pixels).
Expand Down
40 changes: 30 additions & 10 deletions test/tucan/axes_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ defmodule Tucan.AxesTest do
end
end

describe "set_color" do
describe "colors" do
test "raises if invalid axis" do
assert_raise ArgumentError,
"invalid axis :z set in set_color/3, only one of [:x, :y] is allowed",
Expand All @@ -192,17 +192,37 @@ defmodule Tucan.AxesTest do
"domainColor" => "#fa2323"
}
end
end

test "set_color/2 sets both axes colors" do
vl =
Vl.new()
|> Vl.encode_field(:x, "x", type: :quantitative, axis: [foo: 1])
|> Vl.encode_field(:y, "y", type: :quantitative, axis: [foo: 1])
|> Tucan.Axes.set_color("red")
test "set_color/2 sets both axes colors" do
vl =
Vl.new()
|> Vl.encode_field(:x, "x", type: :quantitative, axis: [foo: 1])
|> Vl.encode_field(:y, "y", type: :quantitative, axis: [foo: 1])
|> Tucan.Axes.set_color("red")

assert get_in(vl.spec, ["encoding", "x", "axis"]) == %{"foo" => 1, "domainColor" => "red"}

assert get_in(vl.spec, ["encoding", "x", "axis"]) == %{"foo" => 1, "domainColor" => "red"}
assert get_in(vl.spec, ["encoding", "y", "axis"]) == %{"foo" => 1, "domainColor" => "red"}
end

assert get_in(vl.spec, ["encoding", "y", "axis"]) == %{"foo" => 1, "domainColor" => "red"}
test "set_title_color/3 raises with invalid axis" do
assert_raise ArgumentError,
"invalid axis :z set in set_title_color/3, only one of [:x, :y] is allowed",
fn ->
Tucan.Axes.set_title_color(Vl.new(), :z, "red")
end
end

test "set_title_color/3 sets the title_color of the given axis" do
vl =
Vl.new()
|> Vl.encode_field(:x, "x")
|> Vl.encode_field(:y, "y")
|> Tucan.Axes.set_title_color(:x, "red")
|> Tucan.Axes.set_title_color(:y, "#FFCCEE")

assert get_in(vl.spec, ["encoding", "x", "axis", "titleColor"]) == "red"
assert get_in(vl.spec, ["encoding", "y", "axis", "titleColor"]) == "#FFCCEE"
end
end
end

0 comments on commit 54b3c40

Please sign in to comment.