add graph to range page

This commit is contained in:
2022-11-09 21:04:57 -05:00
parent 1f017ced4a
commit ba8d7988b3
11 changed files with 221 additions and 23 deletions

View File

@ -88,17 +88,67 @@ defmodule CanneryWeb.RangeLive.Index do
shot_groups
|> Enum.map(fn shot_group -> shot_group |> get_row_data_for_shot_group(columns) end)
chart_data =
shot_groups
|> Enum.map(fn shot_group ->
shot_group
|> get_chart_data_for_shot_group([:name, :count, :notes, :date])
end)
|> Enum.sort_by(fn %{date: date} -> date end, Date)
socket
|> assign(ammo_groups: ammo_groups, columns: columns, rows: rows, shot_groups: shot_groups)
|> assign(
ammo_groups: ammo_groups,
columns: columns,
rows: rows,
chart_data: chart_data,
shot_groups: shot_groups
)
end
@spec get_row_data_for_shot_group(ShotGroup.t(), [map()]) :: [map()]
@spec get_chart_data_for_shot_group(ShotGroup.t(), keys :: [atom()]) :: map()
defp get_chart_data_for_shot_group(shot_group, keys) do
shot_group = shot_group |> Repo.preload(ammo_group: :ammo_type)
labels =
if shot_group.notes do
[gettext("Notes: %{notes}", notes: shot_group.notes)]
else
[]
end
labels = [
gettext(
"Name: %{name}",
name: shot_group.ammo_group.ammo_type.name
),
gettext(
"Rounds shot: %{count}",
count: shot_group.count
)
| labels
]
keys
|> Map.new(fn key ->
value =
case key do
:name -> shot_group.ammo_group.ammo_type.name
key -> shot_group |> Map.get(key)
end
{key, value}
end)
|> Map.put(:labels, labels)
end
@spec get_row_data_for_shot_group(ShotGroup.t(), [map()]) :: map()
defp get_row_data_for_shot_group(%{date: date} = shot_group, columns) do
shot_group = shot_group |> Repo.preload(ammo_group: :ammo_type)
assigns = %{shot_group: shot_group}
columns
|> Enum.into(%{}, fn %{key: key} ->
|> Map.new(fn %{key: key} ->
value =
case key do
:name ->

View File

@ -53,11 +53,27 @@
<%= gettext("Shot log") %>
</h1>
<canvas
id="shot-log-chart"
phx-hook="ShotLogChart"
phx-update="ignore"
class="max-h-72"
data-chart-data={Jason.encode!(@chart_data)}
data-label={gettext("Rounds fired")}
data-color={random_color()}
aria-label={gettext("Rounds fired chart")}
role="img"
>
<%= dgettext("errors", "Your browser does not support the canvas element.") %>
</canvas>
<.live_component
module={CanneryWeb.Components.TableComponent}
id="shot_groups_index_table"
columns={@columns}
rows={@rows}
initial_key={:date}
initial_sort_mode={:desc}
/>
<% end %>
</div>