G2 Visualization Grammar

repository·v5·Indexed 11 days ago

https://github.com/antvis/g2

A concise and progressive visualization grammar for Javascript based on the 'Grammar of Graphics'. G2 version 5.4.8 provides a declarative API for building dashboards, data exploration, and storytelling, supporting Canvas, SVG, WebGL, and Node.js rendering.

Tokens
451.2K
Snippets
1.1K
Records
1.4K
Agent score
94%

What's inside G2

  1. Introduction to Treemap

    v5
    A Treemap is a hierarchical visualization that uses nested rectangles to represent tree-structured data. The area of each rectangle is proportional to its corresponding value, and colors are used to distinguish categories or levels. It is ideal for displaying large amounts of hierarchical data like file systems, organizational structures, or budget allocations.
  2. Overview of G2 features and capabilities

    v5

    G2 is a concise and progressive visualization grammar designed for building dashboards, data exploration, and data storytelling. Key features include:

    • Progressive Grammar: Combines graphic, animation, and interaction grammars.
    • Rich Marks: Includes 10+ basic marks and 8+ high-level marks.
    • High Extensibility: Provides a unified mechanism to extend all visualization components.
    • Personalized Styles: Supports hand-drawn, rounded corners, and textured styles.
    • Multi-environment Rendering: Supports Canvas, SVG, WebGL, and Node.js server-side rendering.
  3. Identify suitable scenarios for Sunburst Charts

    v5

    Sunburst charts are best used when you need to visualize hierarchical data with clear parent-child relationships and proportional distributions.

    Suitable Data & Scenarios:

    • Data Type: Hierarchical/nested data structures.
    • Use Cases:
      • Organizational structures
      • File systems
      • Budget allocations
      • Business analysis and geographic research

    Comparison with other charts:

    • Similar to Pie charts and Donut charts for proportions, but adds hierarchical depth.
    • Similar to Treemaps for hierarchical structures, but uses a radial layout instead of rectangular tiling.
  4. Explore the G2 ecosystem

    v5

    G2 has several related packages and integrations:

    • g2-react: A lightweight React component for G2.
    • g2-extensions: Official extensions for features like 3D visualization and automated visual analytics.
    • ant-design-charts: A React chart library built on top of G2 and other AntV libraries.
  5. What is the Theta coordinate system?

    v5

    The theta coordinate system is a special polar coordinate system used to transform column charts into pie charts, donut charts, or sector charts. It works by combining a Transpose operation (exchanging x/y axes) with a Polar coordinate transformation, mapping data to angles on a fixed radius.

    To create a pie or donut chart using theta, you typically use an interval type with a stackY transform to aggregate values into sectors.

    chart.options({
      type: 'interval',
      data: data,
      encode: { y: 'value', color: 'category' },
      transform: [{ type: 'stackY' }],
      coordinate: { type: 'theta' },
    });
  6. What is Mark Transform in G2

    v5

    Mark Transform is a mechanism in G2 used to modify data and mark options to support data analysis. It functions as a way to filter, modify, aggregate, and generate new channel values during the rendering process. This optimizes graphical displays and enhances data interpretability without requiring manual manipulation of the raw input data.

    Common use cases include:

    • Preventing overlap: Using transforms like dodgeX, stackY, or jitterX to manage visual channel collisions (e.g., when multiple data points occupy the same position).
    • Data aggregation: Performing operations like bin and group during rendering, allowing you to operate directly on channel values.
    • Drawing graphic annotations: Since annotations are treated as marks in G2, they can leverage the same transform capabilities.
  7. What is the Time scale?

    v5

    The Time scale is a continuous scale specifically designed for time series data. It maps time data (typically Date objects) to a continuous numerical range using the mapping function y = x.getTime() + b, where x is the time value and b is the offset.

    Key capabilities include:

    • Automatic handling of time data parsing and formatting.
    • Flexible time tick generation mechanisms.
    • Support for both Date objects and date strings (when a comparison function is provided).
  8. What is a Bubble Map

    v5

    A Bubble Map is a visualization that uses circular markers (bubbles) placed on a geographic map to represent data.

    • Position: Represents geographic coordinates (longitude and latitude).
    • Size: Represents the magnitude of a numeric value.
    • Color: Can be used to distinguish different categories or represent another dimension of values (e.g., color intensity for value ranges).

    Note on G2 5.0 Support: G2 5.0 does not directly support complete map background functionality. To implement a Bubble Map, you must import additional map components and geographic data alongside G2.

  9. What is a Legend in G2

    v5

    A Legend in G2 is an auxiliary chart element used to distinguish different data types using visual channels like color, size, shape, and opacity. It also serves as a tool for data filtering.

    G2 automatically generates legends when graphic attributes are mapped to data fields. If a single variable is mapped to multiple graphic attributes, G2 merges them into a simplified legend. The following non-spatial channels trigger automatic legend generation:

    • color: Legend based on color variations.
    • opacity: Legend based on opacity variations.
    • size: Legend based on size variations.
    • shape: Legend based on shape variations.
  10. What is a Dot Map and when to use it

    v5

    A Dot Map (or Dot Density Map) is a geographic visualization that represents data distribution density and patterns using densely distributed dots on a map. Each dot typically represents a specific quantity of statistical units (e.g., population, economic activity).

    Key Characteristics

    • Purpose: Intuitively displays spatial distribution patterns and identifies concentration vs. sparse areas.
    • Data Requirements: Requires geographic coordinates (longitude and latitude) and numerical fields.
    • Mapping Logic:
      • Longitude/Latitude $\rightarrow$ Map positions.
      • Numerical fields $\rightarrow$ Number or density of dots.
      • Categorical fields $\rightarrow$ Colors or shapes.
    • Data Volume: Best suited for medium to large datasets (typically 100 to 10,000 dots).

    When to Use

    • Use Dot Maps when: You want to show continuous spatial distribution without being constrained by administrative boundaries (unlike Choropleth maps).
    • Avoid Dot Maps when:
      • You need precise numerical comparison of specific values (use Bubble Maps instead).
      • You are visualizing continuous surface data like temperature or precipitation (use Heatmaps or Contour Maps instead).
  11. What is the quantize scale?

    v5

    The quantize scale is a discretization scale used to divide a continuous data domain into several equal-width intervals and map those intervals to discrete values in a specified range.

    Key Characteristics

    • Equal-width segmentation: It divides the data range [min, max] into $n$ equal intervals, where $n$ is the length of the range array. It does not consider data distribution.
    • Automatic calculation: Unlike the threshold scale which requires manual split points, quantize automatically calculates intervals based on the data domain.
    • Comparison with quantile: While quantize uses equal-width intervals, the quantile scale segments data based on quantiles to ensure each interval contains the same number of data points.

    Mapping Example

    With a data domain of [0, 100] and a range of ['Small', 'Medium', 'Large']:

    • 0 - 33.33 $\rightarrow$ 'Small'
    • 33.33 - 66.67 $\rightarrow$ 'Medium'
    • 66.67 - 100 $\rightarrow$ 'Large'
  12. What is a pow (power) scale?

    v5

    The pow scale is a continuous scale that applies an exponential transformation to input data before mapping it to an output range using the formula y = x ^ k, where k is the exponent parameter.

    Use a pow scale when:

    • Data distribution shows exponential growth or decay.
    • You need to amplify or reduce differences between data points.
    • Data has a large range but you want to display it more evenly.

    Note: When exponent = 1, the scale is equivalent to a linear scale. For better performance, use linear instead of pow with an exponent of 1.

    y = x ^ k