tag-word-cloud.tsx


Overview

tag-word-cloud.tsx defines a React functional component, TagWordCloud, which renders an interactive word cloud visualization of tags fetched from a data source. This component leverages the @antv/g2 charting library to create a dynamic and visually engaging word cloud where tag sizes correspond to their associated values (e.g., frequency or importance).

The component fetches a list of tags with their counts, processes and sorts them, and then renders the top tags as a word cloud, allowing users to quickly grasp the prominence of different tags through font size and color encoding.


Detailed Description

Component: TagWordCloud

Purpose

TagWordCloud is designed to visualize tag data as a word cloud, where each tag's font size reflects its relative value within the dataset. It provides a concise and intuitive representation of tag importance or frequency.

Usage

import { TagWordCloud } from './tag-word-cloud';

function App() {
  return (
    <div>
      <h1>Tag Word Cloud</h1>
      <TagWordCloud />
    </div>
  );
}

Simply include the <TagWordCloud /> component in your JSX to display the tag word cloud.


Internal Mechanics and Workflow

  1. Fetching Data

    • Uses a custom hook useFetchTagList() from '@/hooks/knowledge-hooks' to asynchronously fetch tag data as a list of tuples [tagName: string, tagValue: number].

    • Example returned data structure:

      [
        ['React', 120],
        ['JavaScript', 95],
        ['TypeScript', 80],
        // ...
      ]
      
  2. Processing Data

    • The tag list is sorted descendingly by their values.

    • Only the top 256 tags are kept to maintain performance and readability.

    • Each tag is transformed into an object with properties:

      • text: tag name (string)

      • value: tag value (number)

      • name: tag name (string, for tooltips)

    • The sum of tag values and their count are also computed but currently not used for font size scaling.

  3. Rendering the Word Cloud

    • The component uses a ref to attach the G2 chart to a div container.

    • The chart is initialized with:

      • type: 'wordCloud'

      • autoFit: true for responsiveness

      • A font size range between 10 and 50 pixels.

      • Color encoding mapped to the tag text.

      • Tooltip configuration to show tag names and values.

    • The chart data is set inline from the processed tag list.

    • The chart is rendered on mount and re-rendered whenever the tag list changes.

    • On unmount, the chart instance is destroyed to prevent memory leaks.


Functions and Hooks

Name

Type

Description

TagWordCloud

React Component

Main functional component rendering the tag word cloud.

useFetchTagList

Custom Hook

Fetches tag list data (imported from external hook).

useMemo

React Hook

Memoizes processed tag data to avoid unnecessary recalculations.

useRef

React Hook

References to DOM element and chart instance.

useCallback

React Hook

Memoizes the function that renders the word cloud chart.

useEffect

React Hook

Handles side effects: rendering and cleanup of the chart.


Parameters and Return Values


Important Implementation Details


Interaction with Other System Parts


Visual Diagram

componentDiagram
    TagWordCloud --> useFetchTagList : fetches tag data
    TagWordCloud --> Chart : renders word cloud
    TagWordCloud --> DOM <div ref=domRef> : renders container div

Summary

The tag-word-cloud.tsx file provides a reusable React component that visually represents tag data as an interactive word cloud. It efficiently fetches, processes, and renders the data using hooks and the powerful G2 charting library, enabling users to intuitively understand tag prominence at a glance. The component is well-structured with proper lifecycle management and preparation for scaling or customization.


Example

import React from 'react';
import { TagWordCloud } from './tag-word-cloud';

export default function Dashboard() {
  return (
    <div>
      <h2>Tag Popularity</h2>
      <TagWordCloud />
    </div>
  );
}

This will display the tag word cloud inside the Dashboard page, updating automatically when the tag data changes.