### Install VegaLite in Livebook Source: https://github.com/livebook-dev/vega_lite/blob/main/README.md Use Mix.install/2 to add the VegaLite and kino_vega_lite dependencies for Livebook integration. Kino_vega_lite ensures proper rendering within Livebook. ```elixir Mix.install([ {:vega_lite, "~> 0.1.11"}, {:kino_vega_lite, "~> 0.1.8"} ]) ``` -------------------------------- ### Load VegaLite Specification from JSON Source: https://context7.com/livebook-dev/vega_lite/llms.txt Parses an existing Vega-Lite JSON specification. This allows working with external examples or pre-defined specifications. Further customizations can be chained after loading. ```elixir alias VegaLite, as: Vl vl = Vl.from_json(""" { "data": { "url": "https://vega.github.io/editor/data/cars.json" }, "mark": "point", "encoding": { "x": { "field": "Horsepower", "type": "quantitative" }, "y": { "field": "Miles_per_Gallon", "type": "quantitative" } } } """) # You can further customize the parsed specification vl |> Vl.encode_field(:color, "Origin", type: :nominal) ``` -------------------------------- ### Shorthand Chart Creation with VegaLite.Data Source: https://context7.com/livebook-dev/vega_lite/llms.txt The `VegaLite.Data` module offers a shorthand API for creating charts with automatic type inference. It simplifies chart creation, especially when starting with raw data. ```elixir alias VegaLite.Data data = [ %{"category" => "A", "score" => 28}, %{"category" => "B", "score" => 55}, %{"category" => "C", "score" => 43} ] # Quick chart creation with automatic type inference Data.chart(data, :bar, x: "category", y: "score") ``` ```elixir alias VegaLite, as: Vl alias VegaLite.Data Vl.new(title: "Sales by Category", width: 400) |> Data.chart(data, :bar, x: "category", y: "score") ``` ```elixir alias VegaLite.Data # Using extra_fields for additional data Data.chart(data, :bar, x: "category", extra_fields: ["score"]) |> Vl.encode_field(:y, "score", type: :quantitative, title: "Total Score") ``` ```elixir alias VegaLite.Data # With mark options Data.chart(data, [type: :bar, color: "steelblue"], x: "category", y: "score") ``` -------------------------------- ### Define Multiple Named Datasets Source: https://context7.com/livebook-dev/vega_lite/llms.txt Defines multiple named datasets using `datasets_from_values/2`, which can then be referenced within the specification. This example defines 'results' and 'points' datasets. ```elixir alias VegaLite, as: Vl results = [ %{"category" => "A", "score" => 28}, %{"category" => "B", "score" => 55} ] points = [ %{"x" => 1, "y" => 10}, %{"x" => 2, "y" => 100} ] Vl.new() |> Vl.datasets_from_values(results: results, points: points) |> Vl.data(name: "results") |> Vl.mark(:bar) |> Vl.encode_field(:x, "category", type: :nominal) |> Vl.encode_field(:y, "score", type: :quantitative) ``` -------------------------------- ### Set Global Configuration Source: https://context7.com/livebook-dev/vega_lite/llms.txt Use `VegaLite.config/2` to set global configuration properties for the entire visualization. This example sets view padding, background color, axis grid properties, and point mark options. ```elixir alias VegaLite, as: Vl Vl.new() |> Vl.data_from_values(x: 1..10, y: 1..10) |> Vl.mark(:point) |> Vl.encode_field(:x, "x", type: :quantitative) |> Vl.encode_field(:y, "y", type: :quantitative) |> Vl.config( view: [stroke: :transparent], padding: 20, background: "#f5f5f5", axis: [grid: true, grid_color: "#dedede"], point: [filled: true, size: 100] ) ``` -------------------------------- ### Load CSV Data from URL with Format Source: https://context7.com/livebook-dev/vega_lite/llms.txt Loads CSV data from a URL, specifying the format as `:csv`. This example creates a line chart using stock data, encoding temporal and quantitative fields. ```elixir alias VegaLite, as: Vl # Load CSV data with format specification Vl.new() |> Vl.data_from_url("https://vega.github.io/editor/data/stocks.csv", format: :csv) |> Vl.mark(:line) |> Vl.encode_field(:x, "date", type: :temporal) |> Vl.encode_field(:y, "price", type: :quantitative) ``` -------------------------------- ### Configure Map Projections Source: https://context7.com/livebook-dev/vega_lite/llms.txt Use `VegaLite.projection/2` to configure geographic projections for mapping data. This example shows a basic geospatial chart and then how to use latitude and longitude with point marks. ```elixir alias VegaLite, as: Vl Vl.new(width: 500, height: 300) |> Vl.data_from_url("https://vega.github.io/editor/data/us-10m.json", format: [type: :topojson, feature: "counties"]) |> Vl.projection(type: :albers_usa) |> Vl.mark(:geoshape) |> Vl.encode(:color, field: "id", type: :quantitative) ``` ```elixir alias VegaLite, as: Vl Vl.new() |> Vl.data_from_values([ %{"city" => "NYC", "lat" => 40.7128, "lon" => -74.0060}, %{"city" => "LA", "lat" => 34.0522, "lon" => -118.2437}, %{"city" => "Chicago", "lat" => 41.8781, "lon" => -87.6298} ]) |> Vl.projection(type: :albers_usa) |> Vl.mark(:circle) |> Vl.encode_field(:longitude, "lon", type: :quantitative) |> Vl.encode_field(:latitude, "lat", type: :quantitative) ``` -------------------------------- ### Load Data from URL for Point Chart Source: https://context7.com/livebook-dev/vega_lite/llms.txt Loads data from a remote URL to create a point chart. The client rendering the specification will fetch the data. This example uses the 'penguins.json' dataset. ```elixir alias VegaLite, as: Vl # Load JSON data Vl.new() |> Vl.data_from_url("https://vega.github.io/editor/data/penguins.json") |> Vl.mark(:point) |> Vl.encode_field(:x, "Flipper Length (mm)", type: :quantitative) |> Vl.encode_field(:y, "Body Mass (g)", type: :quantitative) ``` -------------------------------- ### Set Simple Point Mark Source: https://context7.com/livebook-dev/vega_lite/llms.txt Specifies the visual mark type as a point. This is a basic example demonstrating how to set a mark for a simple scatter plot. ```elixir alias VegaLite, as: Vl # Simple mark Vl.new() |> Vl.data_from_values(x: 1..10, y: 1..10) |> Vl.mark(:point) |> Vl.encode_field(:x, "x", type: :quantitative) |> Vl.encode_field(:y, "y", type: :quantitative) ``` -------------------------------- ### Set Inline Data from Keyword Lists Source: https://context7.com/livebook-dev/vega_lite/llms.txt Sets inline data using keyword lists, suitable for series data where keys represent field names. This example demonstrates creating a line chart with quantitative x and y axes. ```elixir alias VegaLite, as: Vl # Using keyword lists with series data xs = 1..100 ys = Enum.map(1..100, &(&1 * &1)) Vl.new() |> Vl.data_from_values(x: xs, y: ys) |> Vl.mark(:line) |> Vl.encode_field(:x, "x", type: :quantitative) |> Vl.encode_field(:y, "y", type: :quantitative) ``` -------------------------------- ### Extract Vega-Lite Specification Source: https://context7.com/livebook-dev/vega_lite/llms.txt Use `VegaLite.to_spec/1` to get the underlying Vega-Lite specification as an Elixir map. This map can then be converted to JSON using a library like Jason. ```elixir alias VegaLite, as: Vl vl = Vl.new(width: 400) |> Vl.data_from_values(x: 1..5, y: [10, 20, 15, 25, 30]) |> Vl.mark(:bar) |> Vl.encode_field(:x, "x", type: :ordinal) |> Vl.encode_field(:y, "y", type: :quantitative) # Get the specification map spec = Vl.to_spec(vl) # => %{ # "$schema" => "https://vega.github.io/schema/vega-lite/v5.json", # "data" => %{"values" => [...]}, # "encoding" => %{...}, # "mark" => "bar", # "width" => 400 # } # Convert to JSON (requires Jason) json = Jason.encode!(spec, pretty: true) ``` -------------------------------- ### Configure Scale Resolution for Multi-View Graphics Source: https://context7.com/livebook-dev/vega_lite/llms.txt Use `VegaLite.resolve/3` to control how scales, axes, and legends are shared or kept independent across multiple views. This example sets independent Y-scales for layered views. ```elixir alias VegaLite, as: Vl Vl.new() |> Vl.data_from_url("https://vega.github.io/editor/data/cars.json") |> Vl.layers([ Vl.new() |> Vl.mark(:bar) |> Vl.encode_field(:x, "Origin", type: :nominal) |> Vl.encode(:y, aggregate: :count, type: :quantitative), Vl.new() |> Vl.mark(:line) |> Vl.encode_field(:x, "Origin", type: :nominal) |> Vl.encode_field(:y, "Horsepower", aggregate: :mean, type: :quantitative) ]) |> Vl.resolve(:scale, y: :independent) ``` -------------------------------- ### Create New VegaLite Specification Source: https://context7.com/livebook-dev/vega_lite/llms.txt Initializes a new Vega-Lite specification with optional top-level properties like title, width, and height. Use `Vl.new/0` for a minimal specification. ```elixir alias VegaLite, as: Vl # Create a basic specification with dimensions vl = Vl.new( title: "Sales Over Time", width: 400, height: 300 ) # Create a minimal specification vl = Vl.new() ``` -------------------------------- ### Interactive Parameters: Selections and Bindings Source: https://context7.com/livebook-dev/vega_lite/llms.txt Adds interactive parameters for selections (e.g., interval brushing) and UI bindings. Parameters can be defined with values, expressions, or bound to input elements. ```elixir alias VegaLite, as: Vl # Interval selection for brushing Vl.new() |> Vl.data_from_url("https://vega.github.io/editor/data/cars.json") |> Vl.param("brush", select: [type: :interval, encodings: [:x]]) |> Vl.mark(:point) |> Vl.encode_field(:x, "Horsepower", type: :quantitative) |> Vl.encode_field(:y, "Miles_per_Gallon", type: :quantitative) |> Vl.encode(:color, condition: [param: "brush", field: "Origin", type: :nominal], value: "lightgray") ``` ```elixir alias VegaLite, as: Vl # UI input binding Vl.new() |> Vl.param("point_size", value: 50, bind: [input: :range, min: 10, max: 200, step: 10]) |> Vl.data_from_values(x: 1..10, y: 1..10) |> Vl.mark(:point, size: [expr: "point_size"]) |> Vl.encode_field(:x, "x", type: :quantitative) |> Vl.encode_field(:y, "y", type: :quantitative) ``` ```elixir alias VegaLite, as: Vl # Computed parameter Vl.new() |> Vl.param("height", value: 20, bind: [input: :range, min: 1, max: 100, step: 1]) |> Vl.param("halfHeight", expr: "height / 2") ``` -------------------------------- ### Create Layered Views with Multiple Marks Source: https://context7.com/livebook-dev/vega_lite/llms.txt Use `VegaLite.layers/2` to overlay different marks (lines, points, rules) on the same axes. This is useful for showing multiple data aspects simultaneously. ```elixir alias VegaLite, as: Vl data = [ %{"x" => 1, "y" => 10}, %{"x" => 2, "y" => 25}, %{"x" => 3, "y" => 15}, %{"x" => 4, "y" => 30} ] Vl.new(width: 400, height: 300) |> Vl.data_from_values(data) |> Vl.layers([ # Line layer Vl.new() |> Vl.mark(:line) |> Vl.encode_field(:x, "x", type: :quantitative) |> Vl.encode_field(:y, "y", type: :quantitative), # Points layer Vl.new() |> Vl.mark(:point, size: 100) |> Vl.encode_field(:x, "x", type: :quantitative) |> Vl.encode_field(:y, "y", type: :quantitative), # Horizontal rule layer Vl.new() |> Vl.mark(:rule, color: "red", stroke_dash: [4, 4]) |> Vl.encode(:y, datum: 20) ]) ``` -------------------------------- ### Set Point Mark with Options Source: https://context7.com/livebook-dev/vega_lite/llms.txt Sets a point mark with additional options for tooltip, size, and color. This allows for more detailed and visually customized point marks. ```elixir alias VegaLite, as: Vl # Mark with options Vl.new() |> Vl.data_from_values(x: 1..10, y: 1..10) |> Vl.mark(:point, tooltip: true, size: 100, color: "red") |> Vl.encode_field(:x, "x", type: :quantitative) |> Vl.encode_field(:y, "y", type: :quantitative) ``` -------------------------------- ### Create Faceted Views by Data Fields Source: https://context7.com/livebook-dev/vega_lite/llms.txt Use `VegaLite.facet/3` to create small multiples, partitioning data into separate views based on specified fields. Supports simple faceting or row/column faceting. ```elixir alias VegaLite, as: Vl # Simple facet by field Vl.new() |> Vl.data_from_url("https://vega.github.io/editor/data/cars.json") |> Vl.facet( [field: "Origin", type: :nominal], Vl.new() |> Vl.mark(:bar) |> Vl.encode_field(:x, "Cylinders", type: :ordinal) |> Vl.encode(:y, aggregate: :count, type: :quantitative) ) ``` ```elixir alias VegaLite, as: Vl # Row and column faceting Vl.new() |> Vl.data_from_url("https://vega.github.io/editor/data/cars.json") |> Vl.facet( [ row: [field: "Origin", title: "Country of Origin"], column: [field: "Cylinders", title: "Number of Cylinders"] ], Vl.new() |> Vl.mark(:point) |> Vl.encode_field(:x, "Horsepower", type: :quantitative) |> Vl.encode_field(:y, "Miles_per_Gallon", type: :quantitative) ) ``` -------------------------------- ### Add VegaLite to Mix Project Dependencies Source: https://github.com/livebook-dev/vega_lite/blob/main/README.md Include the :vega_lite dependency in your mix.exs file for use in regular Elixir projects. This allows saving graphics to various formats or rendering them via a webviewer. ```elixir def deps do [ {:vega_lite, "~> 0.1.11"} ] end ``` -------------------------------- ### Create Repeated Views for Scatter Plot Matrices Source: https://context7.com/livebook-dev/vega_lite/llms.txt Use `VegaLite.repeat/3` to generate a view for each field in a list, ideal for scatter plot matrices or comparing multiple variables. Can be configured for row/column repeats. ```elixir alias VegaLite, as: Vl # Simple repeat Vl.new() |> Vl.data_from_url("https://vega.github.io/editor/data/weather.csv") |> Vl.repeat( ["temp_max", "precipitation", "wind"], Vl.new() |> Vl.mark(:line) |> Vl.encode_field(:x, "date", time_unit: :month) |> Vl.encode_repeat(:y, :repeat, aggregate: :mean, type: :quantitative) ) ``` ```elixir alias VegaLite, as: Vl # Grid repeat (scatter plot matrix) Vl.new() |> Vl.data_from_url("https://vega.github.io/editor/data/penguins.json") |> Vl.repeat( [ row: ["Beak Length (mm)", "Beak Depth (mm)", "Flipper Length (mm)"], column: ["Flipper Length (mm)", "Beak Depth (mm)", "Beak Length (mm)"] ], Vl.new() |> Vl.mark(:point) |> Vl.encode_repeat(:x, :column, type: :quantitative) |> Vl.encode_repeat(:y, :row, type: :quantitative) |> Vl.encode_field(:color, "Species", type: :nominal) ) ``` -------------------------------- ### Set Inline Data with Field Selection Source: https://context7.com/livebook-dev/vega_lite/llms.txt Uses the `:only` option with `data_from_values/3` to select specific fields from the input data. This is useful for reducing the dataset size or focusing on relevant columns. ```elixir alias VegaLite, as: Vl # Using the :only option to select specific fields Vl.new() |> Vl.data_from_values(data, only: ["category", "score"]) ``` -------------------------------- ### Data Transformations: Filtering and Calculation Source: https://context7.com/livebook-dev/vega_lite/llms.txt Applies data transformations such as filtering, calculating new fields, and performing regression analysis. Use `datum` to reference data points within transformations. ```elixir alias VegaLite, as: Vl # Filter transformation Vl.new() |> Vl.data_from_url("https://vega.github.io/editor/data/weather.csv") |> Vl.transform(filter: "datum.location == 'Seattle'") |> Vl.mark(:line) |> Vl.encode_field(:x, "date", type: :temporal) |> Vl.encode_field(:y, "temp_max", type: :quantitative) ``` ```elixir alias VegaLite, as: Vl # Calculate new field Vl.new() |> Vl.data_from_values(x: Enum.map(0..100, &(&1 * 0.1))) |> Vl.transform(calculate: "sin(datum.x)", as: "sin_x") |> Vl.mark(:line) |> Vl.encode_field(:x, "x", type: :quantitative) |> Vl.encode_field(:y, "sin_x", type: :quantitative) ``` ```elixir alias VegaLite, as: Vl # Regression transformation Vl.new() |> Vl.data_from_url("https://vega.github.io/editor/data/movies.json") |> Vl.transform(filter: "datum.IMDB_Rating != null && datum.Rotten_Tomatoes_Rating != null") |> Vl.transform(regression: "IMDB_Rating", on: "Rotten_Tomatoes_Rating") |> Vl.mark(:line) |> Vl.encode_field(:x, "Rotten_Tomatoes_Rating", type: :quantitative) |> Vl.encode_field(:y, "IMDB_Rating", type: :quantitative) ``` -------------------------------- ### Concatenate Views Horizontally and Vertically Source: https://context7.com/livebook-dev/vega_lite/llms.txt Use `VegaLite.concat/3` to arrange multiple views side-by-side or in a grid. Specify `:horizontal` or `:vertical` for layout direction. ```elixir alias VegaLite, as: Vl Vl.new() |> Vl.data_from_url("https://vega.github.io/editor/data/weather.csv") |> Vl.transform(filter: "datum.location == 'Seattle'") |> Vl.concat([ # First chart: bar chart of precipitation Vl.new() |> Vl.mark(:bar) |> Vl.encode_field(:x, "date", time_unit: :month, type: :ordinal) |> Vl.encode_field(:y, "precipitation", aggregate: :mean), # Second chart: scatter plot Vl.new() |> Vl.mark(:point) |> Vl.encode_field(:x, "temp_min", bin: true) |> Vl.encode_field(:y, "temp_max", bin: true) |> Vl.encode(:size, aggregate: :count) ]) ``` ```elixir Vl.new() |> Vl.data_from_values(x: 1..10, y: 1..10) |> Vl.concat([ Vl.new() |> Vl.mark(:bar) |> Vl.encode_field(:x, "x") |> Vl.encode_field(:y, "y"), Vl.new() |> Vl.mark(:line) |> Vl.encode_field(:x, "x") |> Vl.encode_field(:y, "y") ], :horizontal) ``` ```elixir Vl.new() |> Vl.data_from_values(x: 1..10, y: 1..10) |> Vl.concat([ Vl.new() |> Vl.mark(:bar) |> Vl.encode_field(:x, "x") |> Vl.encode_field(:y, "y"), Vl.new() |> Vl.mark(:line) |> Vl.encode_field(:x, "x") |> Vl.encode_field(:y, "y") ], :vertical) ``` -------------------------------- ### Encode Fields to Visual Channels Source: https://context7.com/livebook-dev/vega_lite/llms.txt Maps data fields to visual properties like position, color, and size. Supports various field types and aggregation with time units. ```elixir alias VegaLite, as: Vl data = [ %{"time" => "2024-01-01", "price" => 100, "country" => "US", "volume" => 500}, %{"time" => "2024-01-02", "price" => 120, "country" => "US", "volume" => 600}, %{"time" => "2024-01-01", "price" => 90, "country" => "UK", "volume" => 400}, %{"time" => "2024-01-02", "price" => 110, "country" => "UK", "volume" => 450} ] Vl.new() |> Vl.data_from_values(data) |> Vl.mark(:point) |> Vl.encode_field(:x, "time", type: :temporal) |> Vl.encode_field(:y, "price", type: :quantitative) |> Vl.encode_field(:color, "country", type: :nominal) |> Vl.encode_field(:size, "volume", type: :quantitative) ``` ```elixir alias VegaLite, as: Vl # With aggregation and time units Vl.new() |> Vl.data_from_url("https://vega.github.io/editor/data/weather.csv") |> Vl.mark(:bar) |> Vl.encode_field(:x, "date", time_unit: :month, title: "Month") |> Vl.encode_field(:y, "precipitation", aggregate: :mean, title: "Mean Precipitation") ``` -------------------------------- ### Generic Encoding for Constants and Aggregations Source: https://context7.com/livebook-dev/vega_lite/llms.txt Adds encoding without a specific field, useful for aggregate values or constants. Supports encoding constant values and aggregations like count. ```elixir alias VegaLite, as: Vl # Encode a constant value Vl.new() |> Vl.data_from_values(x: 1..10) |> Vl.mark(:point) |> Vl.encode_field(:x, "x", type: :quantitative) |> Vl.encode(:y, value: 5) ``` ```elixir alias VegaLite, as: Vl # Encode with aggregation (count) Vl.new() |> Vl.data_from_url("https://vega.github.io/editor/data/cars.json") |> Vl.mark(:bar) |> Vl.encode_field(:x, "Origin", type: :nominal) |> Vl.encode(:y, aggregate: :count, type: :quantitative) ``` ```elixir alias VegaLite, as: Vl # Multiple tooltips Vl.new() |> Vl.data_from_values(height: [160, 170, 180], width: [50, 60, 70]) |> Vl.mark(:point, tooltip: true) |> Vl.encode_field(:x, "height", type: :quantitative) |> Vl.encode_field(:y, "width", type: :quantitative) |> Vl.encode(:tooltip, [ [field: "height", type: :quantitative], [field: "width", type: :quantitative] ]) ``` -------------------------------- ### Set Inline Data from List of Maps Source: https://context7.com/livebook-dev/vega_lite/llms.txt Sets inline data for a specification using a list of maps. This is useful for small, static datasets. Ensure the data structure implements the `Table.Reader` protocol. ```elixir alias VegaLite, as: Vl # Using a list of maps data = [ %{"category" => "A", "score" => 28}, %{"category" => "B", "score" => 55}, %{"category" => "C", "score" => 43} ] Vl.new() |> Vl.data_from_values(data) |> Vl.mark(:bar) |> Vl.encode_field(:x, "category", type: :nominal) |> Vl.encode_field(:y, "score", type: :quantitative) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.