GnuplotEx.LiveView.Component (gnuplot_ex v0.5.1)
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
endReal-time Updates
def handle_info({:new_data, data}, socket) do
plot = GnuplotEx.new()
|> GnuplotEx.line(data, label: "Real-time")
{:noreply, assign(socket, plot: plot)}
endInteractive 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
Renders a GnuplotEx plot in LiveView.
Attributes
plot(required) - TheGnuplotEx.PlotorGnuplotEx.MultiPlotstruct to renderformat- Output format (:svgor:png), defaults to:svgwidth- Plot width in pixels, defaults to800height- Plot height in pixels, defaults to600cache- Enable plot caching, defaults totruecache_ttl- Cache TTL in milliseconds, defaults to60_000(1 minute)class- CSS class for container div, defaults to""on_error- Custom error handler function, defaults tonilrest- 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 to800.height(:integer) - Plot height in pixels. Defaults to600.cache(:boolean) - Enable plot caching. Defaults totrue.cache_ttl(:integer) - Cache TTL in milliseconds. Defaults to60000.class(:string) - CSS class for container. Defaults to"".on_error(:any) - Custom error handler function. Defaults tonil.- Global attributes are accepted. Additional HTML attributes.
Slots
fallback- Content to show while loading or on error.