VegaLite Elixir Bindings

repository·main·Indexed 42 days ago

https://github.com/livebook-dev/vega_lite

Elixir bindings for the Vega-Lite visualization grammar. It allows developers to create complex graphics in Livebook notebooks (via :kino_vega_lite) or export them as PNG, SVG, and PDF files in standard Elixir applications. Features include a shorthand API via VegaLite.Data.chart/3 and automatic data type inference for quantitative, temporal, and nominal fields.

Tokens
2K
Snippets
7
Records
12
Agent score
53%

What's inside VegaLite

  1. Overview of VegaLite Elixir bindings

    main

    VegaLite provides Elixir bindings to the Vega-Lite visualization grammar.

    Key capabilities include:

    • Livebook Integration: Plot graphics directly in notebooks (requires kino_vega_lite).
    • File Export: Save graphics to PNG, SVG, or PDF formats in regular Elixir projects.
    • Web Rendering: Render graphics using a webviewer.

    For a higher-level plotting experience built on top of these bindings, consider using Tucan.

  2. Install VegaLite in Livebook

    main

    To use VegaLite within a Livebook notebook, use Mix.install/2 to include both :vega_lite and :kino_vega_lite. The :kino_vega_lite dependency is required to ensure that graphics are rendered correctly within the Livebook interface.

    Mix.install([
      {:vega_lite, "~> 0.1.11"},
      {:kino_vega_lite, "~> 0.1.8"}
    ])
  3. How VegaLite.Data infers data types

    main

    The VegaLite.Data module uses internal type inference to automatically assign Vega-Lite types to columns based on the content of the data. This reduces the need for manual type declarations in many cases.

    Inferred Types Mapping

    • :quantitative: Numbers (integers, floats) and Decimal types.
    • :temporal: Date, NaiveDateTime, DateTime objects, or ISO8601 formatted strings.
    • :nominal: Atoms and strings that do not match date/time formats.

    When using Data.chart/3 or Data.chart/4, the module inspects the data provided via the Table.Reader protocol to determine these types for the encoded fields.

  4. Migrate from VegaLite.Export to VegaLite.Convert

    main

    The VegaLite.Export module is deprecated and scheduled for removal in v1.0. For all export operations, you should use the VegaLite.Convert module from the :vega_lite_convert package instead.

    Deprecated FunctionReplacementPackage
    VegaLite.Export.save!/3VegaLite.Convert.save!/3:vega_lite_convert
    VegaLite.Export.to_json/1VegaLite.Convert.to_json/1:vega_lite_convert
    VegaLite.Export.to_html/1VegaLite.Convert.to_html/1:vega_lite_convert
    VegaLite.Export.to_png/2VegaLite.Convert.to_png/1:vega_lite_convert
    VegaLite.Export.to_svg/2VegaLite.Convert.to_svg/1:vega_lite_convert
    VegaLite.Export.to_pdf/2VegaLite.Convert.to_pdf/1:vega_lite_convert
  5. Migrate from VegaLite.Viewer to VegaLite.Convert

    main

    The VegaLite.Viewer module is deprecated and will be removed in version 1.0. To display Vega-Lite specifications, you should migrate to the :vega_lite_convert package and use its corresponding functions.

    • Instead of VegaLite.Viewer.show/1, use VegaLite.Convert.open_viewer/1.
    • Instead of VegaLite.Viewer.show_and_wait/1, use VegaLite.Convert.open_viewer_and_wait/1.
  6. Install required npm dependencies for image exports

    main

    To use image export formats (:png, :svg, or :pdf), the VegaLite.Export module requires Node.js and specific npm packages. If the command fails, ensure you have installed the necessary dependencies globally or in your current directory:

    npm install -g vega vega-lite canvas
    # or in the current directory
    npm install vega vega-lite canvas
    npm install -g vega vega-lite canvas
  7. Troubleshoot VegaLite.WxViewer startup failures

    main

    If VegaLite.WxViewer.start/1 fails, it is likely due to missing Erlang dependencies. The module raises specific RuntimeError messages to help identify the cause:

    1. Missing :wx module: If the error states VegaLite.Viewer requires Erlang compilation to include the :wx module, but it's not available, your Erlang installation lacks the :wx library.
    2. Missing :wxWebView module: If the error states VegaLite.Viewer requires Erlang compilation to include the :wxWebView module, but it's not available. This module is available as of Erlang/OTP 24, you need to upgrade to Erlang/OTP 24 or higher or ensure :wxWebView is enabled in your build.
  8. Extend an existing specification with VegaLite.Data.chart/4

    main

    If you already have a VegaLite specification (e.g., one with a title or existing configuration), you can use VegaLite.Data.chart/4 to add data, a mark, and encodings to it.

    Arguments

    • vl: An existing VegaLite specification instance.
    • data: Data implementing the Table.Reader protocol.
    • mark: An atom or keyword list defining the mark type.
    • fields: A keyword list defining the encodings.

    Example

    # Start with a spec that has a title
    VegaLite.new(title: "My Chart")
      |> VegaLite.Data.chart(data, :bar, x: "category", y: "score")
    Vl.new(title: "With title")
          |> Data.chart(data, :bar, x: "category", y: "score")
  9. Export VegaLite specifications to files using save!/3

    main

    The VegaLite.Export.save!/3 function saves a VegaLite specification to a file path. The output format is determined by the file extension of the path or explicitly via the :format option.

    Supported formats:

    • :json
    • :html
    • :png
    • :svg
    • :pdf

    Note: This function is deprecated. Use VegaLite.Convert.save!/3 from the :vega_lite_convert package instead.

    # Example: Saving as PNG (uses file extension to infer format)
    VegaLite.Export.save!(vl, "chart.png")
    
    # Example: Saving with explicit format option
    VegaLite.Export.save!(vl, "chart.output", format: :pdf)
  10. Display Vega-Lite graphics in a desktop window using VegaLite.WxViewer

    main

    The VegaLite.WxViewer module provides a way to render Vega-Lite specifications in a standalone desktop window using Erlang's :wx and :wxWebView modules. This is useful for viewing graphics outside of a Livebook or web environment.

    Requirements

    • Erlang/OTP 24 or later: Required for the :wxWebView module.
    • :wx module: Must be included in your Erlang compilation.

    Usage

    Call VegaLite.WxViewer.start(html) with the HTML string containing your Vega-Lite specification. It returns {:ok, pid} on success, where pid is the process managing the window.

    # Assuming 'html_content' is a binary containing the Vega-Lite HTML
    case VegaLite.WxViewer.start(html_content) do
      {:ok, pid} -> IO.puts("Viewer started with PID: #{inspect(pid)}")
      :error -> IO.puts("Failed to start viewer")
    end
  11. Create a chart from data using VegaLite.Data.chart/3

    main

    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")