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

[FE] feat#254 그래프 hover시에 툴팁으로 커밋 정보 안내, UX 개선 #294

Merged
merged 7 commits into from
Dec 13, 2023
Merged
Show file tree
Hide file tree
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
37 changes: 17 additions & 20 deletions packages/frontend/src/components/graph/Graph.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { QuizGitGraphCommit } from "../../types/quiz";

import fillColor from "./fillColor";
import { InitialDataProps, parsingMultipleParents } from "./parsing";
import renderTooltip from "./renderTooltip";

const { grey500 } = color.$scale;

Expand All @@ -24,7 +25,6 @@ function renderD3(svgRef: RefObject<SVGGElement>, data: InitialDataProps[]) {
const svg = d3.select(svgRef.current);

if (!parsedData.length) {
svg.select("#text").selectAll("*").remove();
svg.select("#link").selectAll("*").remove();
svg.select("#node").selectAll("*").remove();
return;
Expand All @@ -45,24 +45,6 @@ function renderD3(svgRef: RefObject<SVGGElement>, data: InitialDataProps[]) {
const treeData = treeLayout(rootNode);
fillColor(treeData);

// Add text next to each node
svg
.select("#text")
.selectAll("text")
.data(treeData.descendants())
.join(
(enter) => enter.append("text").style("opacity", 0),
(update) => update,
(exit) =>
exit.transition().duration(DURATION).style("opacity", 0).remove(),
)
.text((d) => d.data.message)
.attr("x", (d) => d.x + 20)
.attr("y", (d) => d.y + 5)
.transition()
.duration(DURATION)
.style("opacity", 1);

additionalLinks.forEach(({ id, parentId }) => {
const sourceNode = treeData.descendants().filter((d) => d.id === id)[0];
const targetNode = treeData
Expand Down Expand Up @@ -106,6 +88,12 @@ function renderD3(svgRef: RefObject<SVGGElement>, data: InitialDataProps[]) {
(exit) =>
exit.transition().duration(DURATION).style("opacity", 0).remove(),
)
.on("mouseover", (event, d) => {
const existingTooltip = svg.select("#tooltip");
if (existingTooltip.empty()) {
renderTooltip(svg, d);
}
})
.attr("cx", (d) => d.x)
.attr("cy", (d) => d.y)
.attr("r", 13)
Expand All @@ -114,10 +102,20 @@ function renderD3(svgRef: RefObject<SVGGElement>, data: InitialDataProps[]) {
.transition()
.duration(DURATION)
.style("opacity", 1)
.style("cursor", "pointer")
.attr(
"fill",
(d: d3.HierarchyPointNode<InitialDataProps>) => d.data?.color ?? "",
);

svg.select("#node").on("mouseout", (event) => {
const hoverTooltip =
event.relatedTarget.nodeName === "rect" ||
event.relatedTarget.nodeName === "tspan";
if (!hoverTooltip) {
svg.select("#tooltip").remove();
}
});
}

interface GraphProps {
Expand All @@ -138,7 +136,6 @@ export function Graph({ data, className = "" }: GraphProps) {
<g ref={gRef} transform="translate(100,70)">
<g id="link" />
<g id="node" />
<g id="text" />
</g>
</svg>
</div>
Expand Down
116 changes: 116 additions & 0 deletions packages/frontend/src/components/graph/renderTooltip.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
import * as d3 from "d3";

import color from "../../design-system/tokens/color";

import { InitialDataProps } from "./parsing";

interface TooltipProps {
text: string;
value: string;
id: string;
}

export default function renderTooltip(
svg: d3.Selection<SVGGElement | null, unknown, null, undefined>,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

renderTooltip을 호출하는 쪽에서 타입 가드하면 여기 타입을 좁힐 수 있을 거 같지만..아직 D3 타입이 익숙하지 않아서 모르겠네요. 나중에 개선하면 좋을 것 같아요!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

맞아요ㅠㅠ 저도 이걸로 고민했는데 당장은 떠오르지 않아서 우선 IDE에 뜨는 타입대로 작성하였습니다! 추후에 수정해볼게요!

d: d3.HierarchyPointNode<InitialDataProps>,
) {
const tooltipStyle = {
width: 220,
height: 40,
borderRadius: 5,
position: `translate(${d.x - 110},${d.y - 60})`,
};

const transparentRectPosition = "translate(95, 40)";

const tooltipDataFormat = [
{
text: "Commit Hash: ",
value: d.data.id.slice(0, 7),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

해시 7자리만 보여주는 거 너무 좋아요!

id: "text",
},
{
text: "Commit Message: ",
value: d.data.message,
id: "value",
},
];

// hover 시 보이는 툴팁
const tooltip = svg
.select("#node")
.append("g")
.attr("id", "tooltip")
.attr("tabindex", 0)
.attr("transform", tooltipStyle.position);

tooltip
.append("rect")
.attr("width", tooltipStyle.width)
.attr("height", tooltipStyle.height)
.attr("rx", tooltipStyle.borderRadius)
.attr("ry", tooltipStyle.borderRadius)
.attr("fill", color.$semantic.bgDefault)
.attr("stroke", color.$scale.grey300);

// 투명한 네모를 위에 위치시키기
tooltip
.append("rect")
.attr("width", 30)
.attr("height", 30)
// 요기 바꿔보심 돼여
.attr("fill", "transparent")
.style("cursor", "pointer")
.attr("transform", transparentRectPosition);
Comment on lines +56 to +64
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이런 방법도 있네요 굿굿!👍👍👍👍


tooltip
.append("text")
.attr("x", 0)
.attr("y", 16.5)
.selectAll("tspan")
.data(tooltipDataFormat)
.enter()
.append("tspan")
.attr("x", 6)
.attr("dy", (_: TooltipProps, index: number) => index * 15) // Adjust the line height as needed
.text((tooltipData: TooltipProps) => tooltipData.text)
.attr("fill", color.$scale.grey600)
.append("tspan")
.attr("fill", color.$scale.grey700)
.text((tooltipData: TooltipProps) => tooltipData.value)
.attr("id", (tooltipData: TooltipProps) => tooltipData.id)
.each(() => {
const currentValueNode: d3.Selection<
SVGTSpanElement | null,
unknown,
HTMLElement,
undefined
> = d3.select("#value");

if (!currentValueNode.empty() && currentValueNode.node()) {
const tspanMaxWidth = 110;
const originalText = currentValueNode.text();
if (currentValueNode.node()!.getComputedTextLength() < tspanMaxWidth) {
return;
}

let start = 0;
let end = tspanMaxWidth;
let mid;
while (start < end) {
mid = Math.floor((start + end) / 2);
const truncatedText = `${originalText.slice(0, mid)}...`;
currentValueNode.text(truncatedText); // Set text here for width calculation
const textWidth =
currentValueNode?.node()?.getComputedTextLength() || 0;
if (textWidth > tspanMaxWidth) {
end = mid;
} else {
start = mid + 1;
}
}
const truncatedText = `${originalText.slice(0, start - 1)}...`;
currentValueNode.text(truncatedText);
Comment on lines +96 to +113
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

말줄임에 이분 탐색이 사용되다니 신기해요!!
tspan 너비를 안 넘어갈 때까지 반복하면서 텍스트를 자르는 건가요?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

네 맞습니다!!🤩
tspan의 너비를 잴 수 있는 방법이나 메서드가 뚜렷하지 않아 추가하게 되었습니다!

}
});
}
1 change: 0 additions & 1 deletion packages/frontend/src/pages/_app.page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ export default function App({ Component, pageProps }: AppProps) {
<UserQuizStatusProvider initialUserQuizStatus={userQuizStatus}>
<ThemeWrapper>
<Layout>
{/* eslint-disable-next-line react/jsx-props-no-spreading */}
<Component {...pageProps} />
</Layout>
</ThemeWrapper>
Expand Down
2 changes: 1 addition & 1 deletion packages/frontend/src/pages/_document.page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Head, Html, Main, NextScript } from "next/document";

export default function Document() {
return (
<Html data-theme="light" lang="en">
<Html data-theme="light" lang="ko">
<Head>
<link
rel="apple-touch-icon"
Expand Down