# antvis/g2 architecture

> Architecture diagram of the GitHub repository antvis/g2, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-06-12). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/antvis/g2
- Repository: https://github.com/antvis/g2

## Overview

##### Project type and purpose
- This is an open-source, modular JavaScript/TypeScript library (G2) providing a declarative “grammar of graphics” for building rich data visualizations.
- It is not a traditional frontend/backend web app; instead it is a standalone visualization engine, supplemented by a documentation site, playground, and various build & test scripts.

##### File structure
- **src/** – the heart of G2. Contains domain-driven subfolders:
- **bundle/** – prebuilt bundles (std & lite).
- **__tests__/** – comprehensive unit, integration, snapshot tests using Vitest.
- **scripts/** – tooling for benchmarks, tree-shaking experiments, site generation, etc.
- **playground/** – simple demo loader (index.html + index.js).
- **site/** – Dumi-powered documentation & examples site.
- **rollup.config.js**, **vite.config.ts**, **vitest.config.ts**, **tsconfig.json** – build, test and type configurations.
- **.github/workflows/** – CI/CD pipelines for build, test, documentation deployment, issue triaging.

##### README for additional insights
- Core purpose: “progressive, declarative API” for charting.
- Emphasis on extensibility (scales, transforms, marks) and pluggable rendering via G (Canvas/SVG/WebGL).
- Ecosystem: React wrappers, extensions, etc.
- CI integrates GitHub Actions to build, test, publish to npm, and deploy docs.

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "Core Library (src)"
        subgraph "API Layer"
            API_Index["index.ts"]:::module
            API_Chart["chart.ts"]:::module
            API_Mark["mark.ts"]:::module
            API_Compose["compose.ts"]:::module
        end
        subgraph "Runtime Layer"
            Runtime_Index["index.ts"]:::module
            Runtime_Render["render.ts"]:::module
            Runtime_Plot["plot.ts"]:::module
            Runtime_Style["style.ts"]:::module
        end
        subgraph "Mark Engine"
            Mark_Index["index.ts"]:::module
            Mark_Area["area.ts"]:::module
            Mark_Interval["interval.ts"]:::module
            Mark_Line["line.ts"]:::module
        end
        subgraph "Composition Engine"
            Comp_Index["index.ts"]:::module
            Comp_Facet["facetCircle.ts"]:::module
            Comp_Space["spaceFlex.ts"]:::module
        end
        subgraph "Data Pipeline"
            Data_Index["index.ts"]:::module
            Data_Filter["filter.ts"]:::module
            Data_Rename["rename.ts"]:::module
            Data_Transform["transform/index.ts"]:::module
            Data_Scale["scale/index.ts"]:::module
        end
        subgraph "Encode Module"
            Encode_Index["index.ts"]:::module
            Encode_Field["field.ts"]:::module
            Encode_Trans["transform.ts"]:::module
        end
        subgraph "Interaction Module"
            Inter_Index["index.ts"]:::module
            Inter_Brush["brushFilter.ts"]:::module
            Inter_Tooltip["tooltip.ts"]:::module
        end
        subgraph "Label Transformations"
            Label_Index["index.ts"]:::module
            Label_Overlap["overlapHide.ts"]:::module
        end
        subgraph "Palette & Theme"
            Pal_Index["palette/index.ts"]:::module
            The_Index["theme/index.ts"]:::module
            The_Classic["theme/classic.ts"]:::module
        end
        subgraph "Spec Parser"
            Spec_Index["index.ts"]:::module
            Spec_Mark["mark.ts"]:::module
            Spec_Encode["encode.ts"]:::module
        end
        subgraph "Core Exports"
            Exports_TS["exports.ts"]:::module
            Exports_Index["index.ts"]:::module
        end
    end

    Renderer["Rendering Engine (G)"]:::external

    subgraph "Build & Bundling"
        Rollup["rollup.config.js"]:::config
        ViteConf["vite.config.ts"]:::config
        Bundle_STD["g2.std.ts"]:::artifact
        Bundle_Lite["g2.lite.ts"]:::artifact
    end

    subgraph "Tests"
        UnitTests["__tests__/unit/..."]:::test
        Integration["__tests__/integration/..."]:::test
        VitestConf["vitest.config.ts"]:::config
    end

    subgraph "Documentation Site"
        DumiConfig[".dumirc.ts"]:::config
        DumiPages[".dumi/pages"]:::module
        DocsManual["site/docs/manual"]:::module
        DocsCharts["site/docs/charts"]:::module
    end

    subgraph "Playground Demo"
        Playground_HTML["index.html"]:::demo
        Playground_JS["index.js"]:::demo
    end

    subgraph "Scripts & Tooling"
        Bench_Chart["bench/chart.ts"]:::script
        Bench_Server["bench/server.ts"]:::script
        Tree_HTML["treeshaking/index.html"]:::script
        Tree_Size["treeshaking/size.js"]:::script
    end

    subgraph "CI/CD"
        GitHubActions["GitHub Actions"]:::cloud
        CI_Build[".github/workflows/build.yml"]:::cloud
        CI_Deploy[".github/workflows/deploy.yml"]:::cloud
        NPMReg["npm Registry"]:::cloud
        GitHubPages["GitHub Pages"]:::cloud
    end

    Developer["Developer"]:::external
    Consumers["External Consumers"]:::external

    %% Core flows
    API_Index -->|"calls"| Runtime_Index
    Runtime_Index -->|"processes"| Data_Index
    Data_Index -->|"transforms"| Mark_Index
    Mark_Index -->|"draws"| Renderer

    Exports_Index --> Rollup
    Exports_Index --> ViteConf
    Rollup --> Bundle_STD
    Rollup --> Bundle_Lite
    ViteConf --> Bundle_STD
    ViteConf --> Bundle_Lite

    Bundle_STD --> Playground_JS
    Bundle_STD --> Consumers
    Bundle_Lite --> Consumers

    %% Test flows
    API_Index --> UnitTests
    Data_Index --> UnitTests
    Mark_Index --> Integration
    Runtime_Index --> Integration

    UnitTests --> VitestConf
    Integration --> VitestConf

    %% Docs & Playground
    Bundle_STD --> DumiPages
    DumiPages --> DocsManual
    DumiPages --> DocsCharts

    Playground_HTML --> Playground_JS

    %% Scripts
    Bench_Chart --> Data_Index
    Bench_Server --> Bench_Chart
    Tree_HTML --> Bundle_STD
    Tree_Size --> Bundle_Lite

    %% CI/CD
    Developer --> GitHubActions
    GitHubActions --> CI_Build
    CI_Build --> UnitTests
    CI_Build --> Integration
    CI_Build --> Rollup
    CI_Build --> ViteConf
    CI_Build --> DumiPages
    CI_Build --> Playground_JS
    GitHubActions --> CI_Deploy
    CI_Deploy --> NPMReg
    CI_Deploy --> GitHubPages

    %% Click events
    click API_Index "https://github.com/antvis/g2/blob/v5/src/api/index.ts"
    click API_Chart "https://github.com/antvis/g2/blob/v5/src/api/chart.ts"
    click API_Mark "https://github.com/antvis/g2/blob/v5/src/api/mark.ts"
    click API_Compose "https://github.com/antvis/g2/blob/v5/src/api/compose.ts"
    click Runtime_Index "https://github.com/antvis/g2/blob/v5/src/runtime/index.ts"
    click Runtime_Render "https://github.com/antvis/g2/blob/v5/src/runtime/render.ts"
    click Runtime_Plot "https://github.com/antvis/g2/blob/v5/src/runtime/plot.ts"
    click Runtime_Style "https://github.com/antvis/g2/blob/v5/src/runtime/option-preprocess/style.ts"
    click Mark_Index "https://github.com/antvis/g2/blob/v5/src/mark/index.ts"
    click Mark_Area "https://github.com/antvis/g2/blob/v5/src/mark/area.ts"
    click Mark_Interval "https://github.com/antvis/g2/blob/v5/src/mark/interval.ts"
    click Mark_Line "https://github.com/antvis/g2/blob/v5/src/mark/line.ts"
    click Comp_Index "https://github.com/antvis/g2/blob/v5/src/composition/index.ts"
    click Comp_Facet "https://github.com/antvis/g2/blob/v5/src/composition/facetCircle.ts"
    click Comp_Space "https://github.com/antvis/g2/blob/v5/src/composition/spaceFlex.ts"
    click Data_Index "https://github.com/antvis/g2/blob/v5/src/data/index.ts"
    click Data_Filter "https://github.com/antvis/g2/blob/v5/src/data/filter.ts"
    click Data_Rename "https://github.com/antvis/g2/blob/v5/src/data/rename.ts"
    click Data_Transform "https://github.com/antvis/g2/blob/v5/src/transform/index.ts"
    click Data_Scale "https://github.com/antvis/g2/blob/v5/src/scale/index.ts"
    click Encode_Index "https://github.com/antvis/g2/blob/v5/src/encode/index.ts"
    click Encode_Field "https://github.com/antvis/g2/blob/v5/src/encode/field.ts"
    click Encode_Trans "https://github.com/antvis/g2/blob/v5/src/encode/transform.ts"
    click Inter_Index "https://github.com/antvis/g2/blob/v5/src/interaction/index.ts"
    click Inter_Brush "https://github.com/antvis/g2/blob/v5/src/interaction/brushFilter.ts"
    click Inter_Tooltip "https://github.com/antvis/g2/blob/v5/src/interaction/tooltip.ts"
    click Label_Index "https://github.com/antvis/g2/blob/v5/src/label-transform/index.ts"
    click Label_Overlap "https://github.com/antvis/g2/blob/v5/src/label-transform/overlapHide.ts"
    click Pal_Index "https://github.com/antvis/g2/blob/v5/src/palette/index.ts"
    click The_Index "https://github.com/antvis/g2/blob/v5/src/theme/index.ts"
    click The_Classic "https://github.com/antvis/g2/blob/v5/src/theme/classic.ts"
    click Spec_Index "https://github.com/antvis/g2/blob/v5/src/spec/index.ts"
    click Spec_Mark "https://github.com/antvis/g2/blob/v5/src/spec/mark.ts"
    click Spec_Encode "https://github.com/antvis/g2/blob/v5/src/spec/encode.ts"
    click Exports_TS "https://github.com/antvis/g2/blob/v5/src/exports.ts"
    click Exports_Index "https://github.com/antvis/g2/blob/v5/src/index.ts"
    click Rollup "https://github.com/antvis/g2/blob/v5/rollup.config.js"
    click ViteConf "https://github.com/antvis/g2/blob/v5/vite.config.ts"
    click Bundle_STD "https://github.com/antvis/g2/blob/v5/bundle/g2.std.ts"
    click Bundle_Lite "https://github.com/antvis/g2/blob/v5/bundle/g2.lite.ts"
    click UnitTests "https://github.com/antvis/g2/blob/v5/__tests__/unit/api/chart.spec.ts"
    click Integration "https://github.com/antvis/g2/blob/v5/__tests__/integration/api-chart-render.spec.ts"
    click VitestConf "https://github.com/antvis/g2/blob/v5/vitest.config.ts"
    click DumiConfig "https://github.com/antvis/g2/blob/v5/site/.dumirc.ts"
    click DumiPages "https://github.com/antvis/g2/tree/v5/site/.dumi/pages"
    click DocsManual "https://github.com/antvis/g2/tree/v5/site/docs/manual"
    click DocsCharts "https://github.com/antvis/g2/tree/v5/site/docs/charts"
    click Playground_HTML "https://github.com/antvis/g2/blob/v5/playground/index.html"
    click Playground_JS "https://github.com/antvis/g2/blob/v5/playground/index.js"
    click Bench_Chart "https://github.com/antvis/g2/blob/v5/scripts/bench/chart.ts"
    click Bench_Server "https://github.com/antvis/g2/blob/v5/scripts/bench/server.ts"
    click Tree_HTML "https://github.com/antvis/g2/blob/v5/scripts/treeshaking/index.html"
    click Tree_Size "https://github.com/antvis/g2/blob/v5/scripts/treeshaking/size.js"
    click CI_Build "https://github.com/antvis/g2/blob/v5/.github/workflows/build.yml"
    click CI_Deploy "https://github.com/antvis/g2/blob/v5/.github/workflows/deploy.yml"

    classDef module fill:#D6EAF8,stroke:#21618C
    classDef config fill:#FCF3CF,stroke:#B7950B
    classDef script fill:#EBDEF0,stroke:#8E44AD
    classDef test fill:#FADBD8,stroke:#C0392B
    classDef artifact fill:#D5F5E3,stroke:#27AE60
    classDef cloud fill:#F0F3F4,stroke:#7B7D7D
    classDef external fill:#FCF0E0,stroke:#CD6155
    classDef demo fill:#FEF9E7,stroke:#B7950B
```
