Tapir.DataHelper (Tapir v0.1.0)

View Source

Helper module for processing Ash resource data into chart-ready format.

This module handles the transformation of data from Ash resources into the format expected by Chart.js for rendering charts.

Summary

Functions

Returns empty chart data structure for error states.

Processes data from an Ash resource for chart rendering.

Transforms a list of data into Chart.js format.

Functions

get_empty_chart_data()

Returns empty chart data structure for error states.

process_data(params)

Processes data from an Ash resource for chart rendering.

Parameters

  • resource - The Ash resource to query
  • x_field - Field to use for X-axis/labels
  • y_field - Field to use for Y-axis/values
  • group_by - Optional field to group data by
  • query_params - Additional filters for the query
  • aggregate_function - How to aggregate data (:count, :sum, :avg, etc.)
  • date_grouping - How to group date fields (:day, :week, :month, :year)
  • chart_type - Type of chart being created

Returns

A map with the structure expected by Chart.js:

%{
  labels: ["Label 1", "Label 2", "Label 3"],
  datasets: [
    %{
      label: "Dataset Name",
      data: [10, 20, 30],
      backgroundColor: ["color1", "color2", "color3"],
      borderColor: ["color1", "color2", "color3"],
      borderWidth: 1
    }
  ]
}

transform_for_chart(data, params)

Transforms a list of data into Chart.js format.

Examples

iex> data = [%{name: "A", value: 10}, %{name: "B", value: 20}]
iex> params = %{x_field: :name, y_field: :value}
iex> Tapir.DataHelper.transform_for_chart(data, params)
%{
  labels: ["A", "B"],
  datasets: [%{label: "Value", data: [10, 20], ...}]
}