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 # v0.6.1
- Add shading to table component - Add shading to table component
- Fix chart to sum by day
# v0.6.0 # v0.6.0
- Update translations - Update translations

View File

@ -11,22 +11,23 @@ export default {
data: { data: {
datasets: [{ datasets: [{
label: el.dataset.label, label: el.dataset.label,
data: data.map(({ date, count, labels }) => ({ data: data.map(({ date, count, label }) => ({
labels, label,
x: date, x: date,
y: count y: count
})), })),
backgroundColor: el.dataset.color, backgroundColor: `${el.dataset.color}77`,
borderColor: el.dataset.color, borderColor: el.dataset.color,
fill: true, fill: true,
borderWidth: 4 borderWidth: 3,
pointBorderWidth: 1
}] }]
}, },
options: { options: {
elements: { elements: {
point: { point: {
radius: 7, radius: 9,
hoverRadius: 10 hoverRadius: 12
} }
}, },
plugins: { plugins: {
@ -39,7 +40,8 @@ export default {
tooltip: { tooltip: {
displayColors: false, displayColors: false,
callbacks: { 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 shot_groups
|> Enum.map(fn shot_group -> shot_group |> get_row_data_for_shot_group(columns) end) |> Enum.map(fn shot_group -> shot_group |> get_row_data_for_shot_group(columns) end)
chart_data = chart_data = shot_groups |> get_chart_data_for_shot_group()
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 socket
|> assign( |> assign(
@ -106,40 +100,21 @@ defmodule CanneryWeb.RangeLive.Index do
) )
end end
@spec get_chart_data_for_shot_group(ShotGroup.t(), keys :: [atom()]) :: map() @spec get_chart_data_for_shot_group([ShotGroup.t()]) :: [map()]
defp get_chart_data_for_shot_group(shot_group, keys) do defp get_chart_data_for_shot_group(shot_groups) do
shot_group = shot_group |> Repo.preload(ammo_group: :ammo_type) 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 date: date,
[gettext("Notes: %{notes}", notes: shot_group.notes)] count: sum,
else label: gettext("Rounds shot: %{count}", count: sum)
[] }
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) end)
|> Map.put(:labels, labels) |> Enum.sort_by(fn %{date: date} -> date end)
end end
@spec get_row_data_for_shot_group(ShotGroup.t(), [map()]) :: map() @spec get_row_data_for_shot_group(ShotGroup.t(), [map()]) :: map()

View File

@ -59,9 +59,9 @@
phx-update="ignore" phx-update="ignore"
class="max-h-72" class="max-h-72"
data-chart-data={Jason.encode!(@chart_data)} data-chart-data={Jason.encode!(@chart_data)}
data-label={gettext("Rounds fired")} data-label={gettext("Rounds shot")}
data-color={random_color()} data-color={random_color()}
aria-label={gettext("Rounds fired chart")} aria-label={gettext("Rounds shot chart")}
role="img" role="img"
> >
<%= dgettext("errors", "Your browser does not support the canvas element.") %> <%= 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/ammo_group_live/show.ex:87
#: lib/cannery_web/live/range_live/index.ex:81 #: lib/cannery_web/live/range_live/index.ex:81
#: lib/cannery_web/live/range_live/index.html.heex:62
#, elixir-autogen, elixir-format #, elixir-autogen, elixir-format
msgid "Rounds shot" msgid "Rounds shot"
msgstr "Patronen abgefeuert" msgstr "Patronen abgefeuert"
@ -932,27 +933,7 @@ msgstr ""
msgid "%{percentage}%" msgid "%{percentage}%"
msgstr "" msgstr ""
#: lib/cannery_web/live/range_live/index.ex:121 #: lib/cannery_web/live/range_live/index.ex:114
#, 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
#, elixir-autogen, elixir-format, fuzzy #, elixir-autogen, elixir-format, fuzzy
msgid "Rounds shot: %{count}" msgid "Rounds shot: %{count}"
msgstr "Patronen abgefeuert" msgstr "Patronen abgefeuert"
@ -1018,3 +999,8 @@ msgstr ""
#, elixir-autogen, elixir-format, fuzzy #, elixir-autogen, elixir-format, fuzzy
msgid "Used up!" msgid "Used up!"
msgstr "" 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?" 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/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 #, elixir-autogen, elixir-format
msgid "Are you sure you want to delete this shot record?" msgid "Are you sure you want to delete this shot record?"
msgstr "Sind sie sicher, dass sie die Schießkladde löschen möchten?" 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/ammo_group_live/show.ex:87
#: lib/cannery_web/live/range_live/index.ex:81 #: lib/cannery_web/live/range_live/index.ex:81
#: lib/cannery_web/live/range_live/index.html.heex:62
#, elixir-autogen, elixir-format #, elixir-autogen, elixir-format
msgid "Rounds shot" msgid "Rounds shot"
msgstr "" msgstr ""
@ -915,27 +916,7 @@ msgstr ""
msgid "%{percentage}%" msgid "%{percentage}%"
msgstr "" msgstr ""
#: lib/cannery_web/live/range_live/index.ex:121 #: lib/cannery_web/live/range_live/index.ex:114
#, 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
#, elixir-autogen, elixir-format #, elixir-autogen, elixir-format
msgid "Rounds shot: %{count}" msgid "Rounds shot: %{count}"
msgstr "" msgstr ""
@ -1001,3 +982,8 @@ msgstr ""
#, elixir-autogen, elixir-format #, elixir-autogen, elixir-format
msgid "Used up!" msgid "Used up!"
msgstr "" 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/ammo_group_live/show.ex:87
#: lib/cannery_web/live/range_live/index.ex:81 #: lib/cannery_web/live/range_live/index.ex:81
#: lib/cannery_web/live/range_live/index.html.heex:62
#, elixir-autogen, elixir-format #, elixir-autogen, elixir-format
msgid "Rounds shot" msgid "Rounds shot"
msgstr "" msgstr ""
@ -916,27 +917,7 @@ msgstr ""
msgid "%{percentage}%" msgid "%{percentage}%"
msgstr "" msgstr ""
#: lib/cannery_web/live/range_live/index.ex:121 #: lib/cannery_web/live/range_live/index.ex:114
#, 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
#, elixir-autogen, elixir-format, fuzzy #, elixir-autogen, elixir-format, fuzzy
msgid "Rounds shot: %{count}" msgid "Rounds shot: %{count}"
msgstr "" msgstr ""
@ -1002,3 +983,8 @@ msgstr ""
#, elixir-autogen, elixir-format, fuzzy #, elixir-autogen, elixir-format, fuzzy
msgid "Used up!" msgid "Used up!"
msgstr "" 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 "" msgstr ""
#: lib/cannery_web/live/ammo_group_live/show.ex:132 #: 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 #, elixir-autogen, elixir-format
msgid "Are you sure you want to delete this shot record?" msgid "Are you sure you want to delete this shot record?"
msgstr "" msgstr ""

View File

@ -565,6 +565,7 @@ msgstr ""
#: lib/cannery_web/live/ammo_group_live/show.ex:87 #: 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.ex:81
#: lib/cannery_web/live/range_live/index.html.heex:62
#, elixir-autogen, elixir-format #, elixir-autogen, elixir-format
msgid "Rounds shot" msgid "Rounds shot"
msgstr "" msgstr ""
@ -930,27 +931,7 @@ msgstr ""
msgid "%{percentage}%" msgid "%{percentage}%"
msgstr "" msgstr ""
#: lib/cannery_web/live/range_live/index.ex:121 #: lib/cannery_web/live/range_live/index.ex:114
#, 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
#, elixir-autogen, elixir-format, fuzzy #, elixir-autogen, elixir-format, fuzzy
msgid "Rounds shot: %{count}" msgid "Rounds shot: %{count}"
msgstr "" msgstr ""
@ -1016,3 +997,8 @@ msgstr ""
#, elixir-autogen, elixir-format, fuzzy #, elixir-autogen, elixir-format, fuzzy
msgid "Used up!" msgid "Used up!"
msgstr "" 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?" msgstr "Está seguro que desea desmontar esta munición?"
#: lib/cannery_web/live/ammo_group_live/show.ex:132 #: 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 #, elixir-autogen, elixir-format
msgid "Are you sure you want to delete this shot record?" msgid "Are you sure you want to delete this shot record?"
msgstr "" msgstr ""

View File

@ -569,6 +569,7 @@ msgstr "Cartouches restantes"
#: lib/cannery_web/live/ammo_group_live/show.ex:87 #: 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.ex:81
#: lib/cannery_web/live/range_live/index.html.heex:62
#, elixir-autogen, elixir-format #, elixir-autogen, elixir-format
msgid "Rounds shot" msgid "Rounds shot"
msgstr "Cartouches tirées" msgstr "Cartouches tirées"
@ -935,27 +936,7 @@ msgstr ""
msgid "%{percentage}%" msgid "%{percentage}%"
msgstr "" msgstr ""
#: lib/cannery_web/live/range_live/index.ex:121 #: lib/cannery_web/live/range_live/index.ex:114
#, 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
#, elixir-autogen, elixir-format, fuzzy #, elixir-autogen, elixir-format, fuzzy
msgid "Rounds shot: %{count}" msgid "Rounds shot: %{count}"
msgstr "Cartouches tirées" msgstr "Cartouches tirées"
@ -1021,3 +1002,8 @@ msgstr ""
#, elixir-autogen, elixir-format, fuzzy #, elixir-autogen, elixir-format, fuzzy
msgid "Used up!" msgid "Used up!"
msgstr "" 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?" 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/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 #, elixir-autogen, elixir-format
msgid "Are you sure you want to delete this shot record?" msgid "Are you sure you want to delete this shot record?"
msgstr "Êtes-vous certain·e de vouloir supprimer cet enregistrement de tir?" 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/ammo_group_live/show.ex:87
#: lib/cannery_web/live/range_live/index.ex:81 #: lib/cannery_web/live/range_live/index.ex:81
#: lib/cannery_web/live/range_live/index.html.heex:62
#, elixir-autogen, elixir-format #, elixir-autogen, elixir-format
msgid "Rounds shot" msgid "Rounds shot"
msgstr "" msgstr ""
@ -926,27 +927,7 @@ msgstr ""
msgid "%{percentage}%" msgid "%{percentage}%"
msgstr "" msgstr ""
#: lib/cannery_web/live/range_live/index.ex:121 #: lib/cannery_web/live/range_live/index.ex:114
#, 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
#, elixir-autogen, elixir-format, fuzzy #, elixir-autogen, elixir-format, fuzzy
msgid "Rounds shot: %{count}" msgid "Rounds shot: %{count}"
msgstr "" msgstr ""
@ -1012,3 +993,8 @@ msgstr ""
#, elixir-autogen, elixir-format, fuzzy #, elixir-autogen, elixir-format, fuzzy
msgid "Used up!" msgid "Used up!"
msgstr "" 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 "" msgstr ""
#: lib/cannery_web/live/ammo_group_live/show.ex:132 #: 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 #, elixir-autogen, elixir-format
msgid "Are you sure you want to delete this shot record?" msgid "Are you sure you want to delete this shot record?"
msgstr "" msgstr ""

View File

@ -193,7 +193,7 @@ msgid "Are you sure you want to unstage this ammo?"
msgstr "" msgstr ""
#: lib/cannery_web/live/ammo_group_live/show.ex:132 #: 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 #, elixir-autogen, elixir-format
msgid "Are you sure you want to delete this shot record?" msgid "Are you sure you want to delete this shot record?"
msgstr "" msgstr ""