mirror of
https://github.com/philomena-dev/philomena.git
synced 2025-02-01 03:46:44 +01:00
fix graph height calculation
This commit is contained in:
parent
75b15a59b7
commit
af97bd1717
1 changed files with 6 additions and 5 deletions
|
@ -5,18 +5,19 @@ defmodule PhilomenaWeb.StatView do
|
||||||
data = Enum.sort_by(data, & &1["key"])
|
data = Enum.sort_by(data, & &1["key"])
|
||||||
n_buckets = length(data)
|
n_buckets = length(data)
|
||||||
|
|
||||||
{%{"key" => min_time}, %{"key" => max_time}} = Enum.min_max_by(data, & &1["key"], fn -> %{"key" => 0} end)
|
{
|
||||||
{%{"doc_count" => min_docs}, %{"doc_count" => max_docs}} = Enum.min_max_by(data, & &1["doc_count"], fn -> %{"doc_count" => 0} end)
|
%{"doc_count" => min_docs},
|
||||||
|
%{"doc_count" => max_docs}
|
||||||
|
} = Enum.min_max_by(data, & &1["doc_count"], fn -> %{"doc_count" => 0} end)
|
||||||
|
|
||||||
graph_width = 950
|
graph_width = 950
|
||||||
graph_height = 475
|
graph_height = 475
|
||||||
|
|
||||||
bar_width = safe_div(graph_width, n_buckets)
|
bar_width = safe_div(graph_width, n_buckets)
|
||||||
max_bar_height = safe_div(graph_height, max_docs - min_docs)
|
|
||||||
|
|
||||||
content_tag :svg, class: "upload-stats", viewBox: "0 0 #{graph_width} #{graph_height}" do
|
content_tag :svg, class: "upload-stats", viewBox: "0 0 #{graph_width} #{graph_height}" do
|
||||||
for {datum, i} <- Enum.with_index(data) do
|
for {datum, i} <- Enum.with_index(data) do
|
||||||
bar_height = safe_div(datum["doc_count"], max_docs) * max_bar_height
|
bar_height = safe_div(datum["doc_count"], max_docs) * graph_height
|
||||||
|
|
||||||
x = i * bar_width
|
x = i * bar_width
|
||||||
y = graph_height-bar_height
|
y = graph_height-bar_height
|
||||||
|
@ -29,6 +30,6 @@ defmodule PhilomenaWeb.StatView do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
defp safe_div(n, 0), do: 0
|
defp safe_div(_n, 0), do: 0
|
||||||
defp safe_div(n, d), do: n / d
|
defp safe_div(n, d), do: n / d
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue