Get Started
There are currently two ways to use G2:
- Package manager
- CDN
Package manager
If you use Node-based packaging tools such as Webpack and Rollup, you can install G2 through package managers such as NPM or Yarn.
# Install via NPM
npm install @antv/g2
# Install via Yarn
yarn add @antv/g2
After successful installation, provide a container for G2:
<div id="container"></div>
Then enter the following code:
import { Chart } from '@antv/g2';
// Prepare data
const data = [
{ genre: 'Sports', sold: 275 },
{ genre: 'Strategy', sold: 115 },
{ genre: 'Action', sold: 120 },
{ genre: 'Shooter', sold: 350 },
{ genre: 'Other', sold: 150 },
];
// Initialize chart instance and declare visualization
const chart = new Chart({
container: 'container',
});
chart.options({
type: 'interval',
data,
encode: { x: 'genre', y: 'sold' },
});
// Render visualization
chart.render();
CDN
G2 also provides a UMD version, which can be loaded directly through CDN and used directly. At this time, the Chart object can be accessed through the namespace G2.
<script src="https://unpkg.com/@antv/g2/dist/g2.min.js"></script>
<script>
// Prepare data
const data = [
{ genre: 'Sports', sold: 275 },
{ genre: 'Strategy', sold: 115 },
{ genre: 'Action', sold: 120 },
{ genre: 'Shooter', sold: 350 },
{ genre: 'Other', sold: 150 },
];
// Initialize chart instance and declare visualization
const chart = new G2.Chart({
container: 'container',
});
chart.options({
type: 'interval',
data,
encode: { x: 'genre', y: 'sold' },
});
// Render visualization
chart.render();
</script>
The journey begins
No matter which method you use, if you draw a bar chart like the following, it means that everything is going well and the journey of exploring visualization and G2 has officially begun.
import { Chart } from '@antv/g2';
// Initialize chart instance and declare visualization
const chart = new Chart({
container: 'container',
});
chart.options({
type: 'interval',
data: [
{ genre: 'Sports', sold: 275 },
{ genre: 'Strategy', sold: 115 },
{ genre: 'Action', sold: 120 },
{ genre: 'Shooter', sold: 350 },
{ genre: 'Other', sold: 150 },
],
encode: { x: 'genre', y: 'sold' },
});
chart.render();
Using G2 in Frontend Frameworks
Using G2 in ReactG2 provides a complete guide for using in React, including component encapsulation, data binding, state management best practices and example codeView details →Using G2 in VueG2 provides method guides for using in Vue2 and Vue3, supporting both Options API and Composition API, with complete example code referenceView details →
Online Experience with G2
FAQ
Getting Started with Development
Basic Knowledge
G2 Configuration OverviewDetailed display of G2's complete configuration system, covering chart initialization configuration, API chained calls, options method configuration and all available configuration options and hierarchical relationshipsView details →G2 Chart CompositionTo better use G2 for data visualization, we need to understand the composition of G2 charts and related concepts, as well as chart layout knowledgeView details →Chart LifecycleBasic usage and lifecycle management of Chart objects, including creating chart instances, configuring global options, mounting charts, rendering charts, updating charts, and chart cleanup and destructionView details →Chart APIComplete API reference documentation to easily create visualizations, render charts, get instances, trigger events and more. You can also configure charts through chained API callsView details →
Drawing Graphics
MarksThe basic units of G2 drawing, including 30+ mark types such as points, lines, areas, text, etc. Any chart can be composed of one or more marksView details →Data Configuration and TransformSupports convenient data source configuration with inline arrays, remote URLs, JSON/CSV formats, and multiple data transforms like filter, join, kde for easy data preprocessing before drawingView details →EncodeMaps data properties to visual channels (position, color, size, shape, etc.), serving as the key bridge connecting data and graphicsView details →ViewThe core building unit of charts, used to carry and organize multiple marks, unified management of data, coordinate systems, interactions, styles and other configurationsView details →ScaleThe bridge that maps abstract data to visual data, providing linear, logarithmic, time and other types, determining how data is visualizedView details →TransformUnlike data preprocessing, mark transforms provide drawing-time transformations, including stacking, filtering, aggregation, sorting, grouping, jittering and other transformation methods, supporting complex data analysis and graphic optimizationView details →
Configuring Chart Components
Layout and Interaction
CoordinateManages spatial position mapping, supporting multiple types like Cartesian coordinates, polar coordinates, 3D coordinates, etc., to achieve different visualization layoutsView details →InteractionProvides 20+ interaction methods, including hover tooltips, brush highlighting, zoom and pan, etc., enhancing data exploration experienceView details →CompositionSupports multiple combination methods like faceting, repetition, layering, etc., easily building complex layouts like dashboards and small multiplesView details →
Styling and Beautification
StyleFine control over visual styles of marks and views, supporting rich style properties like fill color, stroke, opacity, font, etc.View details →ColorProvides multiple color encoding methods and built-in color palettes, supporting categorical colors, continuous colors, custom palettes and other color schemesView details →ThemeBuilt-in multiple theme styles like Classic, Academy, etc., supporting theme customization and extension to quickly unify chart visual stylesView details →
Advanced Features
StateConfigure styles for chart elements in different states like hover, select, etc., achieving rich interactive feedback effectsView details →EventListen to chart lifecycle and user interaction events, get click data, rendering status and other information to implement custom interaction logicView details →AnimateBuilt-in multiple animation types like fadeIn, scaleIn, morphing, etc., supporting fine control of enter, update, exit animationsView details →