The VegaLite.Data.chart/3 function provides a shorthand API to generate a Vega-Lite specification by combining data, a mark, and field encodings in a single call. It automatically handles data attachment and field encoding.
Arguments
data: Any data implementing the Table.Reader protocol.mark: An atom (e.g., :bar, :line) or a keyword list (e.g., [type: :point, line: true]).fields: A keyword list defining the encodings. Each entry maps a channel (like x or y) to a field name and optional configuration.
Options
:extra_fields: A list of additional field names to include in the data subset for later use in the specification.
Example
data = [
%{"category" => "A", "score" => 28},
%{"category" => "B", "score" => 55}
]
# Basic bar chart
VegaLite.Data.chart(data, :bar, x: "category", y: "score")
# Bar chart with extra fields for further encoding
VegaLite.Data.chart(data, :bar, x: "category", extra_fields: ["score"])
|> VegaLite.encode_field(:y, "score", type: :quantitative)
data = [
%{"category" => "A", "score" => 28},
%{"category" => "B", "score" => 55}
]
VegaLite.Data.chart(data, :bar, x: "category", y: "score")