Visualize.Chart.Text (Visualize v0.2.25)

Copy Markdown View Source

A text as one string with interpolation (spec/14 §7.1): "Uptime for #{var(:host)}".

A :text — a label's text, a legend's title, meta.name, meta.description — is a string in which #{var(:name)} stands for a variable and #{field(:name)} for a datum's field (a mark's label reads one per element, §5.5). A literal #{ is written \#{. parse/1 reads the string into its parts and unparse/1 writes parts back; the two are inverse, and a list of parts — the form an earlier version wrote — is what unparse/1 takes, which is the migration (§9).

iex> Visualize.Chart.Text.parse("Uptime for \#{var(:host)} over 24h")
{:ok, ["Uptime for ", %Visualize.Chart.Var{name: :host}, " over 24h"]}

iex> Visualize.Chart.Text.parse("cost \\\#{literal}")
{:ok, ["cost \#{literal}"]}

iex> Visualize.Chart.Text.unparse(["u: ", %Visualize.Chart.Var{name: :unit}])
"u: \#{var(:unit)}"

iex> Visualize.Chart.Text.parse("open \#{var(:x")
{:error, :unterminated}

Summary

Types

One part of a text: a string, a variable, or a datum's field.

Functions

Parts as lines: the parts split at every newline in a string part; [[]] for an empty text (spec/14 §7.1).

The parts of a text. A list is already parts and is returned as it is; a string is read, and an unterminated #{ or a hole that names neither a variable nor a field is an error.

The parts of a text, raising on one that does not read — for a text already validated.

Whether a string carries a hole, so a plain string can be told from a template at a glance.

The string form of parts: a variable as #{var(:name)}, a field as #{field(:name)}, a literal #{ escaped. A string is returned as it is.

The estimated width of a text at a font size: 0.6 × the font size per character (spec/14 §5.5). It is the one estimate the library makes of a text's width — the legend's layout and a mark label's fit read it — and it is public so a design can size itself by it, as a ring leaves room for the labels outside it (#489). A design cannot measure a glyph, so it is an estimate: a wide glyph runs over it.

Types

part()

@type part() :: String.t() | Visualize.Chart.Var.t() | {:field, atom()}

One part of a text: a string, a variable, or a datum's field.

Functions

lines(parts)

@spec lines([part()]) :: [[part()]]

Parts as lines: the parts split at every newline in a string part; [[]] for an empty text (spec/14 §7.1).

iex> Visualize.Chart.Text.lines(["Uptime\n24 h ", %Visualize.Chart.Var{name: :unit}])
[["Uptime"], ["24 h ", %Visualize.Chart.Var{name: :unit}]]

iex> Visualize.Chart.Text.lines(["one line"])
[["one line"]]

iex> Visualize.Chart.Text.lines([])
[[]]

parse(parts)

@spec parse(String.t() | [part()]) ::
  {:ok, [part()]} | {:error, :unterminated | {:hole, String.t()}}

The parts of a text. A list is already parts and is returned as it is; a string is read, and an unterminated #{ or a hole that names neither a variable nor a field is an error.

parse!(text)

@spec parse!(String.t() | [part()]) :: [part()]

The parts of a text, raising on one that does not read — for a text already validated.

template?(string)

@spec template?(term()) :: boolean()

Whether a string carries a hole, so a plain string can be told from a template at a glance.

unparse(string)

@spec unparse(String.t() | [part()]) :: String.t()

The string form of parts: a variable as #{var(:name)}, a field as #{field(:name)}, a literal #{ escaped. A string is returned as it is.

width(text, font_size)

@spec width(String.t(), number()) :: float()

The estimated width of a text at a font size: 0.6 × the font size per character (spec/14 §5.5). It is the one estimate the library makes of a text's width — the legend's layout and a mark label's fit read it — and it is public so a design can size itself by it, as a ring leaves room for the labels outside it (#489). A design cannot measure a glyph, so it is an estimate: a wide glyph runs over it.

iex> Visualize.Chart.Text.width("Wednesday", 11)
59.4

iex> Visualize.Chart.Text.width("", 11)
0.0