label-word-cloud.tsx
Overview
label-word-cloud.tsx is a React functional component that visualizes a set of labeled data as a word cloud using the @antv/g2 charting library. It retrieves label data from a custom hook, processes it into a suitable format, and renders an interactive word cloud chart inside a DOM container. The main purpose of this file is to provide an intuitive visual summary of label frequencies or weights, enhancing the user's understanding of testing results or similar datasets.
Detailed Explanation
LabelWordCloud Component
Purpose:
Renders a dynamic word cloud visualization based on label data fetched from the application's state or data store.
Dependencies:
React hooks:
useRef,useMemo,useCallback,useEffectfor managing component lifecycle, memoization, and references.Custom Hook:
useSelectTestingResultto retrieve label data.Charting Library:
@antv/g2to generate the word cloud visualization.
Implementation Details
1. References
domRef(type:React.RefObject<HTMLDivElement>): Refers to the container DOM element where the chart will be rendered.chartRef(type:React.MutableRefObject<Chart | undefined>): Stores the chart instance for lifecycle management (rendering and cleanup).
2. Data Preparation (list)
The component uses useMemo to transform the raw labels object into an array suitable for the chart:
Input:
labels— An object where keys are label strings and values are numeric weights/frequencies.Output: An array of objects with the shape:
{ text: string; // label text for display and color encoding name: string; // label identifier used in the tooltip value: number; // numeric weight determining word size }If no labels exist, returns an empty array.
3. Chart Rendering (renderWordCloud)
Wrapped in useCallback to memoize and avoid unnecessary re-renders:
Checks if the container DOM is ready and if the data list is non-empty.
Instantiates a new
Chartfrom@antv/g2inside thedomRefcontainer.Configures the chart with the following options:
type: 'wordCloud'specifies the visualization type.autoFit: trueenables responsive sizing.layout.fontSize: [6, 15]sets minimum and maximum font sizes for words.datais provided inline with the processed list.encode.coloruses thetextfield for coloring words.legendis disabled.tooltipdisplays the labelnameas title and thevalueas detail on hover.
Calls
.render()to draw the chart.
4. Lifecycle Management (useEffect)
Invokes
renderWordCloudwhenever the data changes.Returns a cleanup function to destroy the chart instance when the component unmounts or before re-rendering to prevent memory leaks.
5. Render Output
Returns a single
<div>element with a referencedomRef.The div has CSS classes setting full width and a fixed height (
13vh), providing the bounding box for the chart.
Usage Example
import React from 'react';
import { LabelWordCloud } from './label-word-cloud';
function Dashboard() {
return (
<div>
<h2>Testing Result Labels</h2>
<LabelWordCloud />
</div>
);
}
The component assumes
useSelectTestingResultis properly set up in the context or global state.It requires CSS styling for height and width on the container to display properly.
Interactions with Other Parts of the System
useSelectTestingResultHook:
This custom hook is responsible for providing thelabelsdata, presumably from a global state, context, or API response related to testing results or knowledge base management. The component relies entirely on this hook for input data.@antv/g2Library:
Provides the charting engine and API to render the word cloud visualization. The component creates and destroys chart instances dynamically.Styling and Layout:
The component expects the parent container or global styles to correctly size the div (w-full h-[13vh]), which influences the chart's visual appearance.
Important Implementation Notes
The chart is recreated from scratch every time
labelschange because therenderWordCloudcallback recreates theChartinstance and calls.render(). This approach ensures the word cloud always reflects the latest data but may have performance implications if label data updates frequently.The component handles cleanup by destroying the chart instance on unmount or before re-rendering to avoid resource leaks.
The font size range
[6, 15]is fixed but can be adjusted for different visual emphases.Tooltip configuration provides immediate data insight on hover: label name and its associated value.
Visual Diagram
componentDiagram
component LabelWordCloud {
+domRef: Ref<HTMLDivElement>
+chartRef: Ref<Chart>
+labels: Record<string, number>
+list: Array<{text, name, value}>
+renderWordCloud(): void
+useEffect(): void
}
component useSelectTestingResult
component Chart
LabelWordCloud --> useSelectTestingResult: fetch labels
LabelWordCloud --> Chart: create / render / destroy chart
LabelWordCloud --> domRef: render container div
Summary
label-word-cloud.tsx is a focused React component designed to visualize label frequency data as an interactive word cloud. It demonstrates effective use of React hooks for data processing, rendering, and cleanup, and integrates with a powerful charting library to deliver rich visual feedback. This file fits within a larger application context where labeled testing results or similar data need clear, immediate representation.