Fix clipping gradient, define once

pull/3463/head
Adam Rutkowski 3 years ago committed by Adrian Gruntkowski
parent b3dc6a7ba8
commit cb6727ef69

@ -8,6 +8,7 @@ defmodule PlausibleWeb.Live.Components.Visitors do
attr :site, Plausible.Site, required: true
attr :height, :integer, default: 50
attr :tick, :integer, default: 20
def chart(assigns) do
site = assigns.site
@ -17,14 +18,16 @@ defmodule PlausibleWeb.Live.Components.Visitors do
site
|> Plausible.Stats.timeseries(q, [:visitors])
|> scale(assigns.height)
|> Enum.with_index(fn scaled_value, index -> "#{(index - 1) * 20},#{scaled_value}" end)
|> Enum.with_index(fn scaled_value, index ->
"#{(index - 1) * assigns.tick},#{scaled_value}"
end)
clip_points =
List.flatten([
"-20,#{assigns.height + 1}",
"-#{assigns.tick},#{assigns.height + 1}",
points,
"#{(length(points) - 2) * 20},#{assigns.height + 1}",
"-20,#{assigns.height + 1}"
"#{(length(points) - 2) * assigns.tick},#{assigns.height + 1}",
"-#{assigns.tick},#{assigns.height + 1}"
])
assigns =
@ -36,10 +39,6 @@ defmodule PlausibleWeb.Live.Components.Visitors do
~H"""
<svg viewBox={"0 0 #{24 * 20} #{@height + 1}"} class="chart w-full mb-2">
<defs>
<linearGradient id={"chart-gradient-#{@id}"} x1="0" x2="0" y1="0" , y2="1">
<stop offset="0%" stop-color="lightblue" />
<stop offset="100%" stop-color="white" />
</linearGradient>
<clipPath id={"gradient-cut-off-#{@id}"}>
<polyline points={@clip_points} />
</clipPath>
@ -49,7 +48,7 @@ defmodule PlausibleWeb.Live.Components.Visitors do
y="0"
width={24 * 20}
height={@height + 1}
fill={"url(#chart-gradient-#{@id})"}
fill="url(#chart-gradient-cut-off)"
clip-path={"url(#gradient-cut-off-#{@id})"}
/>
<polyline fill="none" stroke="#6366f1" stroke-width="3" points={@points} />
@ -57,6 +56,19 @@ defmodule PlausibleWeb.Live.Components.Visitors do
"""
end
def gradient_defs(assigns) do
~H"""
<svg class="inline-block">
<defs>
<linearGradient id="chart-gradient-cut-off" x1="0" x2="0" y1="0" y2="1">
<stop offset="0%" stop-color="#C5CAE9" />
<stop offset="100%" stop-color="white" />
</linearGradient>
</defs>
</svg>
"""
end
defp scale(data, target_range) do
max_value = Enum.max_by(data, fn %{visitors: visitors} -> visitors end)

@ -36,6 +36,8 @@ defmodule PlausibleWeb.Live.Sites do
@keydown.escape.window="invitationOpen = false"
class="container pt-6"
>
<PlausibleWeb.Live.Components.Visitors.gradient_defs />
<%= if @needs_to_upgrade == {:needs_to_upgrade, :no_active_subscription} do %>
<div class="rounded-md bg-yellow-100 p-4">
<div class="flex">
@ -126,12 +128,12 @@ defmodule PlausibleWeb.Live.Sites do
</div>
<% end %>
<%= for site <- @sites.entries do %>
<div class="relative">
<%= link(to: "/" <> URI.encode_www_form(site.domain)) do %>
<li class="col-span-1 bg-white dark:bg-gray-800 rounded-lg shadow p-4 group-hover:shadow-lg cursor-pointer">
<div class="w-full flex items-center justify-between space-x-4">
<.favicon domain={site.domain} />
<%= for site <- @sites.entries do %>
<div class="relative">
<%= link(to: "/" <> URI.encode_www_form(site.domain)) do %>
<li class="col-span-1 bg-white dark:bg-gray-800 rounded-lg shadow p-4 group-hover:shadow-lg cursor-pointer">
<div class="w-full flex items-center justify-between space-x-4">
<.favicon domain={site.domain} />
<div class="flex-1 -mt-px w-full">
<h3
class="text-gray-900 font-medium text-lg truncate dark:text-gray-100"

Loading…
Cancel
Save