fix chart to sum by day

This commit is contained in:
shibao 2022-11-11 19:34:23 -05:00
parent 8231683958
commit 2191e7f4f4
16 changed files with 74 additions and 180 deletions

View File

@ -1,5 +1,6 @@
# v0.6.1
- Add shading to table component
- Fix chart to sum by day
# v0.6.0
- Update translations

View File

@ -11,22 +11,23 @@ export default {
data: {
datasets: [{
label: el.dataset.label,
data: data.map(({ date, count, labels }) => ({
labels,
data: data.map(({ date, count, label }) => ({
label,
x: date,
y: count
})),
backgroundColor: el.dataset.color,
backgroundColor: `${el.dataset.color}77`,
borderColor: el.dataset.color,
fill: true,
borderWidth: 4
borderWidth: 3,
pointBorderWidth: 1
}]
},
options: {
elements: {
point: {
radius: 7,
hoverRadius: 10
radius: 9,
hoverRadius: 12
}
},
plugins: {
@ -39,7 +40,8 @@ export default {
tooltip: {
displayColors: false,
callbacks: {
label: ({ raw: { labels } }) => labels
title: (contexts) => contexts.map(({ raw: { x } }) => Intl.DateTimeFormat([], { timeZone: 'Etc/UTC', dateStyle: 'short' }).format(new Date(x))),
label: ({ raw: { label } }) => label
}
}
},

View File

@ -88,13 +88,7 @@ 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)
chart_data = shot_groups |> get_chart_data_for_shot_group()
socket
|> assign(
@ -106,40 +100,21 @@ defmodule CanneryWeb.RangeLive.Index do
)
end
@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)
@spec get_chart_data_for_shot_group([ShotGroup.t()]) :: [map()]
defp get_chart_data_for_shot_group(shot_groups) do
shot_groups
|> Repo.preload(ammo_group: :ammo_type)
|> Enum.group_by(fn %{date: date} -> date end, fn %{count: count} -> count end)
|> Enum.map(fn {date, rounds} ->
sum = Enum.sum(rounds)
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}
%{
date: date,
count: sum,
label: gettext("Rounds shot: %{count}", count: sum)
}
end)
|> Map.put(:labels, labels)
|> Enum.sort_by(fn %{date: date} -> date end)
end
@spec get_row_data_for_shot_group(ShotGroup.t(), [map()]) :: map()

View File

@ -59,9 +59,9 @@
phx-update="ignore"
class="max-h-72"
data-chart-data={Jason.encode!(@chart_data)}
data-label={gettext("Rounds fired")}
data-label={gettext("Rounds shot")}
data-color={random_color()}
aria-label={gettext("Rounds fired chart")}
aria-label={gettext("Rounds shot chart")}
role="img"
>
<%= dgettext("errors", "Your browser does not support the canvas element.") %>

View File

@ -567,6 +567,7 @@ msgstr "Patronen verbleibend"
#: lib/cannery_web/live/ammo_group_live/show.ex:87
#: lib/cannery_web/live/range_live/index.ex:81
#: lib/cannery_web/live/range_live/index.html.heex:62
#, elixir-autogen, elixir-format
msgid "Rounds shot"
msgstr "Patronen abgefeuert"
@ -932,27 +933,7 @@ msgstr ""
msgid "%{percentage}%"
msgstr ""
#: lib/cannery_web/live/range_live/index.ex:121
#, elixir-autogen, elixir-format
msgid "Name: %{name}"
msgstr ""
#: lib/cannery_web/live/range_live/index.ex:115
#, elixir-autogen, elixir-format
msgid "Notes: %{notes}"
msgstr ""
#: lib/cannery_web/live/range_live/index.html.heex:62
#, elixir-autogen, elixir-format, fuzzy
msgid "Rounds fired"
msgstr "Patronen verbraucht"
#: lib/cannery_web/live/range_live/index.html.heex:64
#, elixir-autogen, elixir-format
msgid "Rounds fired chart"
msgstr ""
#: lib/cannery_web/live/range_live/index.ex:125
#: lib/cannery_web/live/range_live/index.ex:114
#, elixir-autogen, elixir-format, fuzzy
msgid "Rounds shot: %{count}"
msgstr "Patronen abgefeuert"
@ -1018,3 +999,8 @@ msgstr ""
#, elixir-autogen, elixir-format, fuzzy
msgid "Used up!"
msgstr ""
#: lib/cannery_web/live/range_live/index.html.heex:64
#, elixir-autogen, elixir-format, fuzzy
msgid "Rounds shot chart"
msgstr "Patronen abgefeuert"

View File

@ -214,7 +214,7 @@ msgid "Are you sure you want to unstage this ammo?"
msgstr "Sind sie sicher, dass Sie diese Munition demarkieren möchten?"
#: lib/cannery_web/live/ammo_group_live/show.ex:132
#: lib/cannery_web/live/range_live/index.ex:184
#: lib/cannery_web/live/range_live/index.ex:159
#, elixir-autogen, elixir-format
msgid "Are you sure you want to delete this shot record?"
msgstr "Sind sie sicher, dass sie die Schießkladde löschen möchten?"

View File

@ -550,6 +550,7 @@ msgstr ""
#: lib/cannery_web/live/ammo_group_live/show.ex:87
#: lib/cannery_web/live/range_live/index.ex:81
#: lib/cannery_web/live/range_live/index.html.heex:62
#, elixir-autogen, elixir-format
msgid "Rounds shot"
msgstr ""
@ -915,27 +916,7 @@ msgstr ""
msgid "%{percentage}%"
msgstr ""
#: lib/cannery_web/live/range_live/index.ex:121
#, elixir-autogen, elixir-format
msgid "Name: %{name}"
msgstr ""
#: lib/cannery_web/live/range_live/index.ex:115
#, elixir-autogen, elixir-format
msgid "Notes: %{notes}"
msgstr ""
#: lib/cannery_web/live/range_live/index.html.heex:62
#, elixir-autogen, elixir-format
msgid "Rounds fired"
msgstr ""
#: lib/cannery_web/live/range_live/index.html.heex:64
#, elixir-autogen, elixir-format
msgid "Rounds fired chart"
msgstr ""
#: lib/cannery_web/live/range_live/index.ex:125
#: lib/cannery_web/live/range_live/index.ex:114
#, elixir-autogen, elixir-format
msgid "Rounds shot: %{count}"
msgstr ""
@ -1001,3 +982,8 @@ msgstr ""
#, elixir-autogen, elixir-format
msgid "Used up!"
msgstr ""
#: lib/cannery_web/live/range_live/index.html.heex:64
#, elixir-autogen, elixir-format
msgid "Rounds shot chart"
msgstr ""

View File

@ -551,6 +551,7 @@ msgstr ""
#: lib/cannery_web/live/ammo_group_live/show.ex:87
#: lib/cannery_web/live/range_live/index.ex:81
#: lib/cannery_web/live/range_live/index.html.heex:62
#, elixir-autogen, elixir-format
msgid "Rounds shot"
msgstr ""
@ -916,27 +917,7 @@ msgstr ""
msgid "%{percentage}%"
msgstr ""
#: lib/cannery_web/live/range_live/index.ex:121
#, elixir-autogen, elixir-format
msgid "Name: %{name}"
msgstr ""
#: lib/cannery_web/live/range_live/index.ex:115
#, elixir-autogen, elixir-format
msgid "Notes: %{notes}"
msgstr ""
#: lib/cannery_web/live/range_live/index.html.heex:62
#, elixir-autogen, elixir-format, fuzzy
msgid "Rounds fired"
msgstr ""
#: lib/cannery_web/live/range_live/index.html.heex:64
#, elixir-autogen, elixir-format
msgid "Rounds fired chart"
msgstr ""
#: lib/cannery_web/live/range_live/index.ex:125
#: lib/cannery_web/live/range_live/index.ex:114
#, elixir-autogen, elixir-format, fuzzy
msgid "Rounds shot: %{count}"
msgstr ""
@ -1002,3 +983,8 @@ msgstr ""
#, elixir-autogen, elixir-format, fuzzy
msgid "Used up!"
msgstr ""
#: lib/cannery_web/live/range_live/index.html.heex:64
#, elixir-autogen, elixir-format, fuzzy
msgid "Rounds shot chart"
msgstr ""

View File

@ -194,7 +194,7 @@ msgid "Are you sure you want to unstage this ammo?"
msgstr ""
#: lib/cannery_web/live/ammo_group_live/show.ex:132
#: lib/cannery_web/live/range_live/index.ex:184
#: lib/cannery_web/live/range_live/index.ex:159
#, elixir-autogen, elixir-format
msgid "Are you sure you want to delete this shot record?"
msgstr ""

View File

@ -565,6 +565,7 @@ msgstr ""
#: lib/cannery_web/live/ammo_group_live/show.ex:87
#: lib/cannery_web/live/range_live/index.ex:81
#: lib/cannery_web/live/range_live/index.html.heex:62
#, elixir-autogen, elixir-format
msgid "Rounds shot"
msgstr ""
@ -930,27 +931,7 @@ msgstr ""
msgid "%{percentage}%"
msgstr ""
#: lib/cannery_web/live/range_live/index.ex:121
#, elixir-autogen, elixir-format
msgid "Name: %{name}"
msgstr ""
#: lib/cannery_web/live/range_live/index.ex:115
#, elixir-autogen, elixir-format
msgid "Notes: %{notes}"
msgstr ""
#: lib/cannery_web/live/range_live/index.html.heex:62
#, elixir-autogen, elixir-format, fuzzy
msgid "Rounds fired"
msgstr ""
#: lib/cannery_web/live/range_live/index.html.heex:64
#, elixir-autogen, elixir-format
msgid "Rounds fired chart"
msgstr ""
#: lib/cannery_web/live/range_live/index.ex:125
#: lib/cannery_web/live/range_live/index.ex:114
#, elixir-autogen, elixir-format, fuzzy
msgid "Rounds shot: %{count}"
msgstr ""
@ -1016,3 +997,8 @@ msgstr ""
#, elixir-autogen, elixir-format, fuzzy
msgid "Used up!"
msgstr ""
#: lib/cannery_web/live/range_live/index.html.heex:64
#, elixir-autogen, elixir-format, fuzzy
msgid "Rounds shot chart"
msgstr ""

View File

@ -213,7 +213,7 @@ msgid "Are you sure you want to unstage this ammo?"
msgstr "Está seguro que desea desmontar esta munición?"
#: lib/cannery_web/live/ammo_group_live/show.ex:132
#: lib/cannery_web/live/range_live/index.ex:184
#: lib/cannery_web/live/range_live/index.ex:159
#, elixir-autogen, elixir-format
msgid "Are you sure you want to delete this shot record?"
msgstr ""

View File

@ -569,6 +569,7 @@ msgstr "Cartouches restantes"
#: lib/cannery_web/live/ammo_group_live/show.ex:87
#: lib/cannery_web/live/range_live/index.ex:81
#: lib/cannery_web/live/range_live/index.html.heex:62
#, elixir-autogen, elixir-format
msgid "Rounds shot"
msgstr "Cartouches tirées"
@ -935,27 +936,7 @@ msgstr ""
msgid "%{percentage}%"
msgstr ""
#: lib/cannery_web/live/range_live/index.ex:121
#, elixir-autogen, elixir-format
msgid "Name: %{name}"
msgstr ""
#: lib/cannery_web/live/range_live/index.ex:115
#, elixir-autogen, elixir-format
msgid "Notes: %{notes}"
msgstr ""
#: lib/cannery_web/live/range_live/index.html.heex:62
#, elixir-autogen, elixir-format, fuzzy
msgid "Rounds fired"
msgstr "Cartouches utilisées"
#: lib/cannery_web/live/range_live/index.html.heex:64
#, elixir-autogen, elixir-format
msgid "Rounds fired chart"
msgstr ""
#: lib/cannery_web/live/range_live/index.ex:125
#: lib/cannery_web/live/range_live/index.ex:114
#, elixir-autogen, elixir-format, fuzzy
msgid "Rounds shot: %{count}"
msgstr "Cartouches tirées"
@ -1021,3 +1002,8 @@ msgstr ""
#, elixir-autogen, elixir-format, fuzzy
msgid "Used up!"
msgstr ""
#: lib/cannery_web/live/range_live/index.html.heex:64
#, elixir-autogen, elixir-format, fuzzy
msgid "Rounds shot chart"
msgstr "Cartouches tirées"

View File

@ -215,7 +215,7 @@ msgid "Are you sure you want to unstage this ammo?"
msgstr "Êtes-vous certain·e de vouloir désélectionner cette munition?"
#: lib/cannery_web/live/ammo_group_live/show.ex:132
#: lib/cannery_web/live/range_live/index.ex:184
#: lib/cannery_web/live/range_live/index.ex:159
#, elixir-autogen, elixir-format
msgid "Are you sure you want to delete this shot record?"
msgstr "Êtes-vous certain·e de vouloir supprimer cet enregistrement de tir?"

View File

@ -561,6 +561,7 @@ msgstr ""
#: lib/cannery_web/live/ammo_group_live/show.ex:87
#: lib/cannery_web/live/range_live/index.ex:81
#: lib/cannery_web/live/range_live/index.html.heex:62
#, elixir-autogen, elixir-format
msgid "Rounds shot"
msgstr ""
@ -926,27 +927,7 @@ msgstr ""
msgid "%{percentage}%"
msgstr ""
#: lib/cannery_web/live/range_live/index.ex:121
#, elixir-autogen, elixir-format
msgid "Name: %{name}"
msgstr ""
#: lib/cannery_web/live/range_live/index.ex:115
#, elixir-autogen, elixir-format
msgid "Notes: %{notes}"
msgstr ""
#: lib/cannery_web/live/range_live/index.html.heex:62
#, elixir-autogen, elixir-format, fuzzy
msgid "Rounds fired"
msgstr ""
#: lib/cannery_web/live/range_live/index.html.heex:64
#, elixir-autogen, elixir-format
msgid "Rounds fired chart"
msgstr ""
#: lib/cannery_web/live/range_live/index.ex:125
#: lib/cannery_web/live/range_live/index.ex:114
#, elixir-autogen, elixir-format, fuzzy
msgid "Rounds shot: %{count}"
msgstr ""
@ -1012,3 +993,8 @@ msgstr ""
#, elixir-autogen, elixir-format, fuzzy
msgid "Used up!"
msgstr ""
#: lib/cannery_web/live/range_live/index.html.heex:64
#, elixir-autogen, elixir-format, fuzzy
msgid "Rounds shot chart"
msgstr ""

View File

@ -204,7 +204,7 @@ msgid "Are you sure you want to unstage this ammo?"
msgstr ""
#: lib/cannery_web/live/ammo_group_live/show.ex:132
#: lib/cannery_web/live/range_live/index.ex:184
#: lib/cannery_web/live/range_live/index.ex:159
#, elixir-autogen, elixir-format
msgid "Are you sure you want to delete this shot record?"
msgstr ""

View File

@ -193,7 +193,7 @@ msgid "Are you sure you want to unstage this ammo?"
msgstr ""
#: lib/cannery_web/live/ammo_group_live/show.ex:132
#: lib/cannery_web/live/range_live/index.ex:184
#: lib/cannery_web/live/range_live/index.ex:159
#, elixir-autogen, elixir-format
msgid "Are you sure you want to delete this shot record?"
msgstr ""