Tapir.Helpers (Tapir v0.1.0)
View SourceHelper functions for working with charts and data.
This module provides utilities for creating charts from Ash resources and handling common chart operations.
Summary
Functions
Creates chart configuration for different chart types.
Creates a chart component with real Ash data.
Generates a color palette for charts.
Returns empty chart data structure for error states.
Converts a field name to a human-readable label.
Transforms Ash resource data into Chart.js format.
Functions
Creates chart configuration for different chart types.
This helper can be used to generate chart.js configuration objects with sensible defaults for different chart types.
Creates a chart component with real Ash data.
This is an example of how to use the chart library with actual data instead of static demo data.
Example
# In your LiveView
def mount(_params, _session, socket) do
chart_data = AshCharts.Helpers.fetch_chart_data(MyApp.User, %{
x_field: :role,
y_field: :count,
aggregate_function: :count
})
socket = assign(socket, :user_chart_data, chart_data)
{:ok, socket}
end
# In your template
<.chart_with_data
data={@user_chart_data}
chart_type={:bar}
title="User Statistics"
/>
Generates a color palette for charts.
Examples
iex> AshCharts.Helpers.generate_colors(3, 0.2)
["rgba(255, 99, 132, 0.2)", "rgba(54, 162, 235, 0.2)", "rgba(255, 205, 86, 0.2)"]
Returns empty chart data structure for error states.
Converts a field name to a human-readable label.
Examples
iex> AshCharts.Helpers.humanize_field(:total_amount)
"Total Amount"
iex> AshCharts.Helpers.humanize_field("user_count")
"User Count"
Transforms Ash resource data into Chart.js format.
This is a utility function that can be used to convert query results from Ash into the format expected by Chart.js.