View Source Tucan.Legend (tucan v0.4.1)

Helper utilities for configuring the plot legend.

Summary

Functions

Set arbitrary options to the legend of the given channel.

Enables or disables the legend of the given encoding channel.

Sets the offset in pixels by which to displace the legend from the data rectangle and axes.

Sets the legend orientation with respect to the scene.

Sets the title of the given legend.

Functions

Link to this function

put_options(vl, channel, opts)

View Source
@spec put_options(vl :: VegaLite.t(), channel :: atom(), opts :: keyword()) ::
  VegaLite.t()

Set arbitrary options to the legend of the given channel.

The options are deep merged with existing options.

Examples

Tucan.scatter(:iris, "petal_width", "petal_length")
|> Tucan.color_by("species")
|> Tucan.Legend.put_options(:color,
  fill_color: "yellow",
  offset: 5,
  padding: 2,
  label_font_size: 14
)
{"$schema":"https://vega.github.io/schema/vega-lite/v5.json","data":{"url":"https://gist.githubusercontent.com/curran/a08a1080b88344b0c8a7/raw/0e7a9b0a5d22642a06d3d5b9bcbad9890c8ee534/iris.csv"},"encoding":{"color":{"field":"species","legend":{"fillColor":"yellow","labelFontSize":14,"offset":5,"padding":2}},"x":{"field":"petal_width","scale":{"zero":false},"type":"quantitative"},"y":{"field":"petal_length","scale":{"zero":false},"type":"quantitative"}},"mark":{"fillOpacity":1,"type":"point"}}
Link to this function

set_enabled(vl, channel, enabled)

View Source
@spec set_enabled(vl :: VegaLite.t(), channel :: atom(), enabled :: boolean()) ::
  VegaLite.t()

Enables or disables the legend of the given encoding channel.

Link to this function

set_offset(vl, channel, offset)

View Source
@spec set_offset(vl :: VegaLite.t(), channel :: atom(), offset :: integer()) ::
  VegaLite.t()

Sets the offset in pixels by which to displace the legend from the data rectangle and axes.

If not set defaults to 18 pixels.

Examples

Tucan.scatter(:iris, "petal_width", "petal_length")
|> Tucan.color_by("species")
|> Tucan.Legend.set_offset(:color, 5)
{"$schema":"https://vega.github.io/schema/vega-lite/v5.json","data":{"url":"https://gist.githubusercontent.com/curran/a08a1080b88344b0c8a7/raw/0e7a9b0a5d22642a06d3d5b9bcbad9890c8ee534/iris.csv"},"encoding":{"color":{"field":"species","legend":{"offset":5}},"x":{"field":"petal_width","scale":{"zero":false},"type":"quantitative"},"y":{"field":"petal_length","scale":{"zero":false},"type":"quantitative"}},"mark":{"fillOpacity":1,"type":"point"}}
Link to this function

set_orientation(vl, channel, orientation)

View Source
@spec set_orientation(
  vl :: VegaLite.t(),
  channel :: atom(),
  orientation :: String.t()
) :: VegaLite.t()

Sets the legend orientation with respect to the scene.

You need to define the channel for which the legend will be configured. Orientation can be one of the following: ["left", "right", "top", "bottom", "top-left", "top-right", "bottom-left", "bottom-right", "none"].

Applicable only on plots with a legend.

Examples

Tucan.scatter(:iris, "petal_width", "petal_length")
|> Tucan.color_by("species")
|> Tucan.shape_by("species")
|> Tucan.Legend.set_orientation(:color, "bottom")
|> Tucan.Legend.set_orientation(:shape, "top")
{"$schema":"https://vega.github.io/schema/vega-lite/v5.json","data":{"url":"https://gist.githubusercontent.com/curran/a08a1080b88344b0c8a7/raw/0e7a9b0a5d22642a06d3d5b9bcbad9890c8ee534/iris.csv"},"encoding":{"color":{"field":"species","legend":{"orient":"bottom"}},"shape":{"field":"species","legend":{"orient":"top"}},"x":{"field":"petal_width","scale":{"zero":false},"type":"quantitative"},"y":{"field":"petal_length","scale":{"zero":false},"type":"quantitative"}},"mark":{"fillOpacity":1,"type":"point"}}
Link to this function

set_title(vl, channel, title, opts \\ [])

View Source
@spec set_title(
  vl :: VegaLite.t(),
  channel :: atom(),
  title :: String.t() | nil,
  opts :: keyword()
) :: VegaLite.t()

Sets the title of the given legend.

You can optionally pass any title option supported by vega-lite to customize the style of it.

Applicable only on plots with a legend.

Examples

Tucan.scatter(:iris, "petal_width", "petal_length", color_by: "species")
|> Tucan.Legend.set_title(:color, "Iris Species", title_color: "red", title_font_weight: 300)
{"$schema":"https://vega.github.io/schema/vega-lite/v5.json","data":{"url":"https://gist.githubusercontent.com/curran/a08a1080b88344b0c8a7/raw/0e7a9b0a5d22642a06d3d5b9bcbad9890c8ee534/iris.csv"},"encoding":{"color":{"field":"species","legend":{"title":"Iris Species","titleColor":"red","titleFontWeight":300},"type":"nominal"},"x":{"field":"petal_width","scale":{"zero":false},"type":"quantitative"},"y":{"field":"petal_length","scale":{"zero":false},"type":"quantitative"}},"mark":{"fillOpacity":1,"type":"point"}}