Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Render tooltips pointing up or down relative to the datapoint clicked #6

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 29 additions & 13 deletions lib/plox.ex
Original file line number Diff line number Diff line change
Expand Up @@ -314,23 +314,39 @@ defmodule Plox do
slot :inner_block, required: true

def tooltip(assigns) do
height = assigns.dataset.dimensions.height
point = GraphDataset.to_graph_point(assigns.dataset, assigns.x, assigns.y, assigns.point_id)

assigns =
assign(assigns, point: point)
{container_classes, pointer_classes} =
if point.y < height / 2 do
# top half of the graph: draw tooltip BELOW the clicked point
{"top: #{point.y + 12}px;", "top: -0.5rem;"}
else
# bottom half of the graph: draw tooltip ABOVE the clicked point
{"bottom: #{height - point.y + 12}px;", "bottom: -0.5rem;"}
end

assigns = assign(assigns, point: point, container_classes: container_classes, pointer_classes: pointer_classes)

~H"""
<div
style={[
"position: absolute; padding: 1rem; font-size: 0.75rem; background: #4B4C4D; color: #CACBCC; z-index: 10; border-radius: 0.75rem; transform: translate(-50%);",
"left: #{@point.x}px; bottom: #{@dataset.dimensions.height - @point.y + 12}px;",
"box-shadow: 0 4px 6px -1px rgb(0 0 0 / 0.1);"
]}
phx-click-away={assigns[:"phx-click-away"]}
phx-target={assigns[:"phx-target"]}
>
<%= render_slot(@inner_block, @point.data_point.original) %>
<div style="transform: translate(-50%) rotate(45deg); position: absolute; left: 50%; bottom: -0.5rem; width: 1rem; height: 1rem; z-index: -10; background: #4B4C4D" />
<div style={[
"position: absolute; left: #{@point.x}px; transform: translate(-50%);",
@container_classes
]}>
<div
style={[
"position: relative; padding: 1rem; font-size: 0.75rem; background: #4B4C4D; color: #CACBCC; z-index: 10; border-radius: 0.75rem;",
"box-shadow: 0 4px 6px -1px rgb(0 0 0 / 0.1);"
]}
phx-click-away={assigns[:"phx-click-away"]}
phx-target={assigns[:"phx-target"]}
>
<%= render_slot(@inner_block, @point.data_point.original) %>
</div>
<div style={[
"position: absolute; left: 50%; transform: translate(-50%) rotate(45deg); width: 1rem; height: 1rem; z-index: -10; background: #4B4C4D;",
@pointer_classes
]} />
</div>
"""
end
Expand Down