tag-word-cloud.tsx
Overview
tag-word-cloud.tsx is a React functional component that renders a dynamic word cloud visualization based on a list of tags fetched from a custom hook. It leverages the AntV G2 charting library to create an interactive word cloud where tag size correlates with tag frequency or value, providing a visually intuitive representation of tag importance or popularity.
The component fetches tag data, processes it to obtain the top tags, and renders them as a word cloud inside a resizable container. It also manages chart lifecycle including initialization, rendering, and cleanup to ensure efficient resource usage.
Detailed Explanation
Component: TagWordCloud
A React functional component that displays a word cloud of tags.
Functionality
Fetches a list of tags with associated numeric values using the
useFetchTagListhook.Sorts and limits the tag list to the top 256 entries.
Maps the sorted tags into an appropriate format for the G2 chart.
Uses AntV G2's
ChartAPI to render a word cloud where font size corresponds to tag value.Cleans up the chart instance on component unmount.
Internal Variables / Hooks
domRef: React.RefObject
Reference to the container DOM element where the chart will be rendered.chartRef: React.MutableRefObject<Chart | undefined>
Mutable reference to store the chart instance for interaction and cleanup.list: [string, number][]
Tag list fetched fromuseFetchTagListhook, expected to be an array of tuples[tagName, tagValue].tagList: { text: string; value: number; name: string }[]
Memoized list of processed tag objects used by the chart. Contains top 256 tags sorted by value descending.
React Hooks Used
useFetchTagList()
Custom hook to retrieve tag data.useMemo()
Processes the raw tag list to sort, slice top 256, and map into the chart-friendly format. Runs whenlistchanges.useCallback()
DefinesrenderWordCloudmethod to initialize and render the word cloud chart. Depends ontagList.useEffect()
CallsrenderWordCloudon component mount and whenever the tag list changes. Also handles chart destruction on unmount.
Method: renderWordCloud
A memoized callback function responsible for creating and rendering the word cloud chart.
Parameters
None.
Returns
Void.
Behavior
Checks if the container DOM element is available.
Creates a new
Chartinstance from AntV G2 with the container element.Configures the chart with:
Type:
'wordCloud'Layout options specifying font size range
[10, 50].Data passed as inline values from
tagList.Encoding color by the tag text.
Tooltip showing tag name and value.
Legend disabled.
Calls render() on the chart instance to display the word cloud.
Usage Example
// Inside the TagWordCloud component, this method is called automatically by useEffect.
// If called manually, ensure that domRef.current exists.
renderWordCloud();
Implementation Details
Data Processing:
The tag list is sorted descending by the numeric tag value and truncated to 256 tags to optimize rendering performance and readability.Data Mapping Format:
Each tag object includes:{ text: string; value: number; name: string }
This format aligns with AntV G2's word cloud requirements.Font Size Configuration:
The font size is statically set between 10 and 50 pixels. There is commented-out code indicating a previous or potential approach for dynamic font sizing based on relative tag value proportions.Chart Lifecycle Management:
The chart instance is stored in a mutable ref to persist across renders and destroyed on component unmount to prevent memory leaks.
Interaction with Other Parts of the System
useFetchTagListHook
This component depends on theuseFetchTagListcustom hook (imported from@/hooks/knowledge-hooks) to asynchronously retrieve the tag data. The shape and source of this data are external to this file.AntV G2 Chart Library
Utilizes theChartclass from@antv/g2to render the word cloud visualization.lodashsumBy Utility
Used to calculate the total sum of tag values for potential dynamic font sizing (currently unused in rendering).Styling
The container div uses Tailwind CSS classes for width and height (w-full h-[38vh]), ensuring the chart adapts responsively.
Visual Diagram
componentDiagram
component TagWordCloud {
+domRef: Ref<HTMLDivElement>
+chartRef: Ref<Chart | undefined>
+useFetchTagList(): { list: [string, number][] }
+tagList: { text, value, name }[]
+renderWordCloud(): void
}
TagWordCloud --> useFetchTagList : fetches tags
TagWordCloud --> Chart : initializes & renders word cloud
TagWordCloud --> lodash.sumBy : calculates sum of tag values
TagWordCloud --> DOM div : renders chart here
Summary
The TagWordCloud component is a specialized visualization tool within the application that transforms tag frequency data into an engaging and informative word cloud. By efficiently processing data and leveraging the AntV G2 charting capabilities, it provides users with an immediate visual understanding of tag prominence. This component is modular, reactive to data changes, and integrates cleanly with the application's data fetching architecture.