GnuplotEx.LiveView.Component (gnuplot_ex v0.5.0)

Phoenix LiveView components for GnuplotEx.

Provides the live_gnuplot/1 function component for rendering plots in LiveView.

Installation

Add {:phoenix_live_view, "~> 1.0"} to your dependencies in mix.exs.

Basic Usage

defmodule MyAppWeb.ChartLive do
  use Phoenix.LiveView
  import GnuplotEx.LiveView.Component

  def render(assigns) do
    ~H"""
    <.live_gnuplot plot={@plot} />
    """
  end

  def mount(_params, _session, socket) do
    plot = GnuplotEx.new()
      |> GnuplotEx.scatter([[1, 2], [3, 4], [5, 6]])
      |> GnuplotEx.title("My Plot")

    {:ok, assign(socket, plot: plot)}
  end
end

Real-time Updates

def handle_info({:new_data, data}, socket) do
  plot = GnuplotEx.new()
    |> GnuplotEx.line(data, label: "Real-time")

  {:noreply, assign(socket, plot: plot)}
end

Interactive 3D

For interactive 3D plots with mouse controls, use the GnuplotInteractive hook:

<.live_gnuplot
  plot={@plot}
  id="my-plot"
  phx-hook="GnuplotInteractive"
/>

See the LiveView guide for more examples.

Summary

Functions

Renders a GnuplotEx plot in LiveView.

Functions

live_gnuplot(assigns)

Renders a GnuplotEx plot in LiveView.

Attributes

  • plot (required) - The GnuplotEx.Plot or GnuplotEx.MultiPlot struct to render
  • format - Output format (:svg or :png), defaults to :svg
  • width - Plot width in pixels, defaults to 800
  • height - Plot height in pixels, defaults to 600
  • cache - Enable plot caching, defaults to true
  • cache_ttl - Cache TTL in milliseconds, defaults to 60_000 (1 minute)
  • class - CSS class for container div, defaults to ""
  • on_error - Custom error handler function, defaults to nil
  • rest - Additional HTML attributes passed to the container

Slots

  • fallback - Content to show while loading or on error

Examples

# Basic usage
<.live_gnuplot plot={@plot} />

# Custom size
<.live_gnuplot plot={@plot} width={1200} height={600} />

# PNG format with caching disabled
<.live_gnuplot plot={@plot} format={:png} cache={false} />

# With fallback content
<.live_gnuplot plot={@plot}>
  <:fallback>
    <div class="loading">Rendering plot...</div>
  </:fallback>
</.live_gnuplot>

# With custom error handler
<.live_gnuplot
  plot={@plot}
  on_error={fn reason -> content_tag(:p, "Error: #{reason}") end}
/>

Attributes

  • plot (:any) (required) - The GnuplotEx.Plot or GnuplotEx.MultiPlot to render.
  • format (:atom) - Output format. Defaults to :svg. Must be one of :svg, or :png.
  • width (:integer) - Plot width in pixels. Defaults to 800.
  • height (:integer) - Plot height in pixels. Defaults to 600.
  • cache (:boolean) - Enable plot caching. Defaults to true.
  • cache_ttl (:integer) - Cache TTL in milliseconds. Defaults to 60000.
  • class (:string) - CSS class for container. Defaults to "".
  • on_error (:any) - Custom error handler function. Defaults to nil.
  • Global attributes are accepted. Additional HTML attributes.

Slots

  • fallback - Content to show while loading or on error.