defmodule <%= @module_name %>.Components.UI.Chart do @moduledoc """ Chart integration component with PhiaChart vanilla JS hook. Ejected from PhiaUI — you own this copy. Customise freely. ## Example <.phia_chart id="revenue-chart" type={:area} title="Revenue" description="Last 12 months" series={[%{name: "MRR", data: [10, 20, 30]}]} labels={["Jan", "Feb", "Mar"]} height="350px" /> ## LiveView update push_event(socket, "update-chart-revenue-chart", %{ series: [%{name: "MRR", data: [50, 60, 70]}] }) """ use Phoenix.Component import <%= @module_name %>.ClassMerger, only: [cn: 1] import <%= @module_name %>.Components.UI.ChartShell, only: [chart_shell: 1] attr :id, :string, required: true attr :type, :atom, default: :line, values: [:line, :bar, :pie, :area] attr :series, :list, default: [] attr :labels, :list, default: [] attr :height, :string, default: "300px" attr :title, :string, default: nil attr :description, :string, default: nil attr :class, :string, default: nil attr :rest, :global def phia_chart(assigns) do assigns = assigns |> assign(:config_json, Jason.encode!(build_config(assigns.type, assigns.labels))) |> assign(:series_json, Jason.encode!(assigns.series)) ~H""" <%%= if @title do %> <.chart_shell title={@title} description={@description} min_height={@height} class={@class} {@rest}> <.chart_canvas id={@id} config_json={@config_json} series_json={@series_json} height={@height} /> <%% else %> <.chart_canvas id={@id} config_json={@config_json} series_json={@series_json} height={@height} class={@class} {@rest} /> <%% end %> """ end attr :id, :string, required: true attr :config_json, :string, required: true attr :series_json, :string, required: true attr :height, :string, required: true attr :class, :string, default: nil attr :rest, :global defp chart_canvas(assigns) do ~H"""