From 2191e7f4f436364b57b91af9c95b813183067a80 Mon Sep 17 00:00:00 2001 From: shibao Date: Fri, 11 Nov 2022 19:34:23 -0500 Subject: [PATCH] fix chart to sum by day --- CHANGELOG.md | 1 + assets/js/shot_log_chart.js | 16 +++--- lib/cannery_web/live/range_live/index.ex | 53 +++++-------------- .../live/range_live/index.html.heex | 4 +- priv/gettext/de/LC_MESSAGES/default.po | 28 +++------- priv/gettext/de/LC_MESSAGES/prompts.po | 2 +- priv/gettext/default.pot | 28 +++------- priv/gettext/en/LC_MESSAGES/default.po | 28 +++------- priv/gettext/en/LC_MESSAGES/prompts.po | 2 +- priv/gettext/es/LC_MESSAGES/default.po | 28 +++------- priv/gettext/es/LC_MESSAGES/prompts.po | 2 +- priv/gettext/fr/LC_MESSAGES/default.po | 28 +++------- priv/gettext/fr/LC_MESSAGES/prompts.po | 2 +- priv/gettext/ga/LC_MESSAGES/default.po | 28 +++------- priv/gettext/ga/LC_MESSAGES/prompts.po | 2 +- priv/gettext/prompts.pot | 2 +- 16 files changed, 74 insertions(+), 180 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 08d7705..33ab6c8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,6 @@ # v0.6.1 - Add shading to table component +- Fix chart to sum by day # v0.6.0 - Update translations diff --git a/assets/js/shot_log_chart.js b/assets/js/shot_log_chart.js index bbf8143..14a3f99 100644 --- a/assets/js/shot_log_chart.js +++ b/assets/js/shot_log_chart.js @@ -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 } } }, diff --git a/lib/cannery_web/live/range_live/index.ex b/lib/cannery_web/live/range_live/index.ex index d876eda..c6dbd61 100644 --- a/lib/cannery_web/live/range_live/index.ex +++ b/lib/cannery_web/live/range_live/index.ex @@ -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() diff --git a/lib/cannery_web/live/range_live/index.html.heex b/lib/cannery_web/live/range_live/index.html.heex index b593f02..718a2de 100644 --- a/lib/cannery_web/live/range_live/index.html.heex +++ b/lib/cannery_web/live/range_live/index.html.heex @@ -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.") %> diff --git a/priv/gettext/de/LC_MESSAGES/default.po b/priv/gettext/de/LC_MESSAGES/default.po index 3038c3b..c09683c 100644 --- a/priv/gettext/de/LC_MESSAGES/default.po +++ b/priv/gettext/de/LC_MESSAGES/default.po @@ -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" diff --git a/priv/gettext/de/LC_MESSAGES/prompts.po b/priv/gettext/de/LC_MESSAGES/prompts.po index 9d923d0..ce36d97 100644 --- a/priv/gettext/de/LC_MESSAGES/prompts.po +++ b/priv/gettext/de/LC_MESSAGES/prompts.po @@ -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?" diff --git a/priv/gettext/default.pot b/priv/gettext/default.pot index 12df682..f217070 100644 --- a/priv/gettext/default.pot +++ b/priv/gettext/default.pot @@ -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 "" diff --git a/priv/gettext/en/LC_MESSAGES/default.po b/priv/gettext/en/LC_MESSAGES/default.po index dd8be40..75292f4 100644 --- a/priv/gettext/en/LC_MESSAGES/default.po +++ b/priv/gettext/en/LC_MESSAGES/default.po @@ -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 "" diff --git a/priv/gettext/en/LC_MESSAGES/prompts.po b/priv/gettext/en/LC_MESSAGES/prompts.po index a35d726..8ebcf74 100644 --- a/priv/gettext/en/LC_MESSAGES/prompts.po +++ b/priv/gettext/en/LC_MESSAGES/prompts.po @@ -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 "" diff --git a/priv/gettext/es/LC_MESSAGES/default.po b/priv/gettext/es/LC_MESSAGES/default.po index c73806d..9da0828 100644 --- a/priv/gettext/es/LC_MESSAGES/default.po +++ b/priv/gettext/es/LC_MESSAGES/default.po @@ -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 "" diff --git a/priv/gettext/es/LC_MESSAGES/prompts.po b/priv/gettext/es/LC_MESSAGES/prompts.po index eb2f225..357543b 100644 --- a/priv/gettext/es/LC_MESSAGES/prompts.po +++ b/priv/gettext/es/LC_MESSAGES/prompts.po @@ -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 "" diff --git a/priv/gettext/fr/LC_MESSAGES/default.po b/priv/gettext/fr/LC_MESSAGES/default.po index 4be1baa..a6ff859 100644 --- a/priv/gettext/fr/LC_MESSAGES/default.po +++ b/priv/gettext/fr/LC_MESSAGES/default.po @@ -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" diff --git a/priv/gettext/fr/LC_MESSAGES/prompts.po b/priv/gettext/fr/LC_MESSAGES/prompts.po index 10a8625..4f2deb5 100644 --- a/priv/gettext/fr/LC_MESSAGES/prompts.po +++ b/priv/gettext/fr/LC_MESSAGES/prompts.po @@ -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 ?" diff --git a/priv/gettext/ga/LC_MESSAGES/default.po b/priv/gettext/ga/LC_MESSAGES/default.po index 0a24529..4f8796f 100644 --- a/priv/gettext/ga/LC_MESSAGES/default.po +++ b/priv/gettext/ga/LC_MESSAGES/default.po @@ -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 "" diff --git a/priv/gettext/ga/LC_MESSAGES/prompts.po b/priv/gettext/ga/LC_MESSAGES/prompts.po index 26e89ec..77c8a64 100644 --- a/priv/gettext/ga/LC_MESSAGES/prompts.po +++ b/priv/gettext/ga/LC_MESSAGES/prompts.po @@ -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 "" diff --git a/priv/gettext/prompts.pot b/priv/gettext/prompts.pot index 7517a5e..d69951a 100644 --- a/priv/gettext/prompts.pot +++ b/priv/gettext/prompts.pot @@ -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 ""