Provides expert knowledge on G2 legend rendering, architecture, and interaction.
Install
mkdir -p .claude/skills/g2-legend-expert && curl -L -o skill.zip "https://agentskills.codes/api/skills/download/7704" && unzip -o skill.zip -d .claude/skills/g2-legend-expert && rm skill.zipInstalls to .claude/skills/g2-legend-expert
Activation
This is the description your AI agent reads to decide when to run this skill — the better it matches your request, the more reliably it fires.
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.Key capabilities
- →Infer legend components from chart scales
- →Support categorical and continuous legend types
- →Calculate layout and sizing for legend components
- →Handle legend filtering and interaction states
- →Integrate with G2 theming system
How it works
The skill follows a multi-stage flow: inferring components from scales, creating specific legend types, calculating layout dimensions, and rendering the final display object.
Inputs & outputs
When to use g2-legend-expert
- →Customizing G2 chart legends
- →Debugging legend rendering flow
- →Implementing interaction handling in charts
About this skill
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:
- Scale Analysis: The system analyzes all scales in the chart, looking for channels like
shape,size,color, andopacity - Legend Type Detection: Based on the scale types, the system determines whether to create
legendCategory(for discrete scales) orlegendContinuous(for continuous scales) - Position Inference: Default positions and orientations are inferred based on the chart type and coordinate system
2. Legend Component Creation
Two main legend component types are implemented:
LegendCategory
Located in src/component/legendCategory.ts, this component handles categorical legends:
- Data Processing: Processes domain values from scales to create legend items
- Marker Inference: Automatically infers appropriate markers based on the chart's shapes and scales
- Layout Wrapper: Uses
LegendCategoryLayoutto handle layout positioning - Rendering Options: Supports both standard GUI rendering and custom HTML rendering via the
renderoption
LegendContinuous
Located in src/component/legendContinuous.ts, this handles continuous legends:
- Scale Type Handling: Supports various continuous scale types including linear, log, time, quantize, quantile, and threshold
- Configuration Inference: Generates appropriate configuration based on scale properties
- Ribbon Generation: Creates color ribbons for continuous scales
- Label Formatting: Handles proper formatting of continuous scale labels
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:
- Auto vs Manual Padding: When padding is set to 'auto', the system dynamically calculates component size based on content. When manually set, it uses default sizes.
- Size Calculation: Different calculation strategies for different component types (axis, legend, title, etc.)
- Cross-size Considerations: Takes into account cross-size dimensions for proper layout
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);
}
};
- Auto Padding ('auto'): Calls
computeComponentSizeto measure actual content dimensions - Manual Padding: Uses
defaultSizedirectly, bypassing content measurement
4. Component Rendering
The renderComponent function in src/runtime/component.ts handles the actual rendering:
- Component Instantiation: Creates the appropriate component based on type
- Context Injection: Provides scale instances, theme, and other context information
- Display Object Creation: Returns a DisplayObject that can be added to the chart
5. Legend Positioning and BBox Assignment
After size calculation, the placeComponents function assigns bounding boxes:
- Position-based Grouping: Components are grouped by position (top, bottom, left, right, center)
- Area Calculation: Each position gets a specific area in the chart layout
- Dimension Assignment: Components receive their final x, y, width, and height values
Legend Types and Configuration
Categorical Legends
Categorical legends (LegendCategory) are used for discrete scales and support:
- Item Markers: Automatic marker generation based on chart types
- Flexible Layouts: Supports both flex and grid layouts
- Custom Rendering: HTML-based custom rendering via the
renderoption - Interaction Features: Support for filtering and focus modes
Continuous Legends
Continuous legends (LegendContinuous) handle continuous scales:
- Color Ranges: Support for various color interpolation methods
- Scale Types: Handles linear, log, time, and quantile scales
- Threshold Handling: Special support for threshold scales with custom boundaries
- Size Control: Configurable ribbon sizes and label spacing
Interaction Handling
Legend Filtering
The legend filtering interaction is implemented in src/interaction/legendFilter.ts:
- Event Delegation: Handles click, pointerenter, and pointerout events
- State Management: Maintains selection states and visual feedback
- Filter Propagation: Updates chart data based on legend selections
- Cross-chart Coordination: Supports filtering across multiple chart views
Visual States
Legends support multiple visual states:
- Selected/Unselected: Visual distinction between selected and unselected items
- Hover States: Pointer interaction feedback
- Focus Mode: Special highlighting for focused items
Customization and Theming
Theme Integration
Legend components integrate with G2's theming system:
- Theme Properties: Respects theme settings for colors, fonts, and spacing
- Component-specific Themes: Separate theme settings for different legend types
- Style Override: Allows specific style overrides via component options
Layout Customization
- Position Control: Configurable positions (top, bottom, left, right, center)
- Size Control: Manual or automatic size calculation
- Padding Control: Configurable padding and spacing
- Orientation Control: Horizontal or vertical orientation options
Best Practices
Performance Considerations
- Efficient Data Processing: Minimize legend data processing for large datasets
- Optimized Rendering: Use appropriate rendering strategies for different legend types
- Event Handling: Properly manage and clean up event listeners
Accessibility
- Label Clarity: Ensure legend labels are clear and descriptive
- Color Contrast: Maintain sufficient contrast for color accessibility
- Interaction Feedback: Provide clear visual feedback for interactions
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:
- When padding is manually set, the layout engine uses the component's
defaultSizeinstead of measuring actual content - For
LegendCategory, the default size is 40 (defined insrc/component/legendCategory.ts) - To fix this, explicitly set the
sizeproperty in the legend configuration to override the default
Interaction Issues
- Event Bubbling: Ensure proper event handling to prevent unwanted interactions
- State Synchronization: Keep legend states synchronized across different chart views
- Performance: Use throttling for filter events to prevent excessive updates
References
For more detailed information on legend layout mechanisms and related topics, see the knowledge directory:
- Legend Layout Mechanisms:
./knowledge/legendLayout.md- Detailed analysis of padding behavior and layout calculations- Manual Padding Effects: How manually set padding values affect legend sizing
- Default Size Fallback: Behavior when padding is set manually vs. 'auto'
- Layout Calculation Logic: The core algorithm in
computePaddingfunction
When not to use it
- →When using non-G2 visualization libraries
- →When manual padding overrides are not properly configured with size properties
Prerequisites
Limitations
- →Manual padding settings require explicit size property configuration to avoid default size fallbacks
- →Performance may degrade with excessive legend data processing in large datasets
How it compares
Unlike manual legend implementation, this skill automates component inference and layout calculation based on the chart's internal scale properties.
Compared to similar skills
g2-legend-expert side by side with the closest alternatives in the catalog.
| Skill | Installs | Updated | Safety | Difficulty |
|---|---|---|---|---|
| g2-legend-expert (this skill) | 1 | 7mo | No flags | Intermediate |
| streamlit | 86 | 9mo | No flags | Intermediate |
| d3-visualization | 7 | 4mo | No flags | Advanced |
| threejs | 35 | 6mo | No flags | Advanced |
Try saying
Example prompts that trigger this skill in your AI assistant.
More by antvis
View all by antvis →You might also like
streamlit
sverzijl
When working with Streamlit web apps, data dashboards, ML/AI app UIs, interactive Python visualizations, or building data science applications with Python
d3-visualization
lyndonkl
Use when creating custom, interactive data visualizations with D3.js—building bar/line/scatter charts from scratch, creating network diagrams or geographic maps, binding changing data to visual elements, adding zoom/pan/brush interactions, animating chart transitions, or when chart libraries (Highcharts, Chart.js) don't support your specific visualization design and you need low-level control over data-driven DOM manipulation, scales, shapes, and layouts.
threejs
mrgoonie
Build 3D web apps with Three.js (WebGL/WebGPU). Use for 3D scenes, animations, custom shaders, PBR materials, VR/XR experiences, games, data visualizations, product configurators.
streaming-mindmap-rendering
SSShooter
Implement real-time streaming mindmap rendering using Mind Elixir in web applications. Supports streaming text parsing and incremental updates.
d3js-visualization
benchflow-ai
Build deterministic, verifiable data visualizations with D3.js (v6). Generate standalone HTML/SVG (and optional PNG) from local data files without external network dependencies. Use when tasks require charts, plots, axes/scales, legends, tooltips, or data-driven SVG output.
dashboard-build
mckinsey
A skill that should be invoked whenever a user wants to build a Dashboard or simple app. This skill is Phase 2 of an e2e process that covers the actual build and testing. For Phase 1 (requirements, layout design, visualization selection), use the dashboard-design skill.