G2 legend expert

From foryourhealth111-pixel/Vibe-Skills · View interactive version

G2 legend expert is an AI agent skill from the GitHub repository foryourhealth111-pixel/Vibe-Skills. Expert skill for G2 legend development - provides comprehensive knowledge about legend rendering implementation, component architecture, layout algorithms, and interaction handling. Use when implementing, customizing, or debugging legend functionality in G2 visualizations.

How to use this skill

Skill file

G2 Legend Expert Skill

Overview

This skill provides comprehensive knowledge about legend rendering in G2, covering the complete legend rendering flow from component creation to layout calculation and interaction handling.

Legend Rendering Flow in G2

The legend rendering flow in G2 follows a multi-stage process that transforms legend configuration into visual components. This flow involves several key stages:

1. Legend Component Inference

Legend components are inferred from the chart's scales and configuration during the initial setup phase. The inference process is handled by the inferComponent function in src/runtime/component.ts:

2. Legend Component Creation

Two main legend component types are implemented:

LegendCategory

Located in src/component/legendCategory.ts, this component handles categorical legends:

LegendContinuous

Located in src/component/legendContinuous.ts, this handles continuous legends:

3. Layout and Sizing Calculation

The layout process is managed in src/runtime/layout.ts and involves several critical steps:

Component Size Computation

The computeComponentSize function handles sizing based on component type:

Padding Calculation Logic

The core layout logic is in the computePadding function:

const autoSizeOf = (d) => {
  if (d.size) return;
  if (value !== 'auto') sizeOf(d);
  else {
    // Compute component size dynamically
    computeComponentSize(
      d,
      crossSize,
      crossPadding,
      position,
      theme,
      library,
    );
    defaultSizeOf(d);
  }
};

4. Component Rendering

The renderComponent function in src/runtime/component.ts handles the actual rendering:

5. Legend Positioning and BBox Assignment

After size calculation, the placeComponents function assigns bounding boxes:

Legend Types and Configuration

Categorical Legends

Categorical legends (LegendCategory) are used for discrete scales and support:

Continuous Legends

Continuous legends (LegendContinuous) handle continuous scales:

Interaction Handling

Legend Filtering

The legend filtering interaction is implemented in src/interaction/legendFilter.ts:

Visual States

Legends support multiple visual states:

Customization and Theming

Theme Integration

Legend components integrate with G2's theming system:

Layout Customization

Best Practices

Performance Considerations

Accessibility

Troubleshooting Common Issues

Layout Issues

When paddingTop is manually set (e.g., paddingTop: 72), the height of legendCategory changes unexpectedly (e.g., from 60px to 40px). This occurs because:

  1. When padding is manually set, the layout engine uses the component's defaultSize instead of measuring actual content
  2. For LegendCategory, the default size is 40 (defined in src/component/legendCategory.ts)
  3. To fix this, explicitly set the size property in the legend configuration to override the default

Interaction Issues

References

For more detailed information on legend layout mechanisms and related topics, see the knowledge directory:

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.