The FlameGraph component is a React-based visualization tool that displays hierarchical data in a two-dimensional view. It features a custom tooltip system that provides additional information when hovering over graph elements.
The main component that renders the flame graph and handles tooltip interactions.
data
(Object): Hierarchical data to be displayed in the flame graphenableTooltips
(Boolean): Controls whether tooltips are enabledwidth
(Number): Width of the flame graphheight
(Number): Height of the flame graphtooltipHeight
(Number): Height of the tooltiponChange
(Function): Callback function triggered when graph data changesThe tooltip uses the following CSS classes:
.Tooltip
: Base tooltip styling.tt-name
: Styling for name labels (red background).tt-value
: Styling for value labels (blue background)import { FlameGraphViewer } from './FlameGraphViewer';
function MyComponent() {
return (
<FlameGraphViewer
tooltipHeight={90}
height={500}
width={800}
enableTooltips={true}
onChange={(node) => console.log(node.name)}
data={hierarchicalData}
/>
);
}
The component uses a helper function getMousePos
to calculate the tooltip position relative to the container:
function getMousePos(relativeContainer, mouseEvent, tooltipHeight) {
if (relativeContainer !== null) {
const rect = relativeContainer.getBoundingClientRect();
const mouseX = mouseEvent.clientX - rect.left;
const mouseY = mouseEvent.clientY - (rect.top - tooltipHeight);
return { mouseX, mouseY };
}
return { mouseX: 0, mouseY: 0 };
}
The component implements three main event handlers:
onMouseOver
: Shows tooltip with node informationonMouseMove
: Updates tooltip positiononMouseOut
: Hides tooltipThe component uses a custom hook useSmartTooltip
to handle tooltip positioning and visibility.