Skip to content

G2

🕒 Published at:

说明

适合画各种花里胡哨的饼状图之类的

例如:

options,就是配置里的options

dataOptions,就是配置里的dataOptions

依次类推

下载

npm install @antv/g2

https://g2.antv.antgroup.com/examples

初始化

接收配置并渲染

javascript
import { Chart } from '@antv/g2';
const chart = new Chart({container: 'container'});
chart
    .options(options)
    .data(dataOptions)
    //坐标系配置
    .coordinate(coordinateOptions)
    //气泡框展示的字段
    .tooltip(['value', 'gdp'])
    .render();

常用配置

initOptions

API描述默认值类型
container指定 chart 绘制的 DOM,可以传入 DOM id,也可以直接传入 dom 实例string
width图表宽度640number
height图表高度480number
depth图表深度,在 3D 图表中使用0number
renderer指定渲染引擎,默认使用 canvas。
plugins指定渲染时使用的插件 ,具体见pluginany[]
autoFit图表是否自适应容器宽高,默认为false,falseboolean
padding图表内边距30number

options

API描述示例对应方法
type指定渲染类型interval 填充的,绘制柱形图、条形图、饼图等

line 线性的,绘制折线图

text 该类型用于在绘制的图形上显示文字
.interval()

.line()

.text()
width/height设置
title设置
tooltip设置tooltip
theme主题
data设置传递给组件的自定义数据data:[
{name: 1,age: 2}
]
coordinate设置坐标系coordinate:{.coordinate(obj)
axis设置坐标系
encode设置每个坐标轴对应的字段名称encode: {.encode(obj)

.encode(key,value)
scale设置坐标轴的比例尺.scale(obj)
transform转换坐标系transform: [
{ type: 'stackY' },
{ type: 'sortX' }
],
.transform(obj)
layout布局layout: {
nodeAlign: 'center',
nodePadding: 0.03,
},
.layout(obj)
labellabels: [{
text: 'genre', // 指定绑定的字段
dy: -15, // 指定样式
}]
style样式.style(obj)
.style(key,value)
state状态state: {
active: {},
inactive: {}
},
.state(obj)
.state(key,value)
animate动画animate: {
enter: {
type: 'scaleInX',
duration: 100,
delay: 10,
},
update: {
type: 'morphing',
},
},
.animate(obj)
.animate(key,value)
legend
javascript
const options={
    //interval常用来绘制柱形图、条形图、饼图等,需要指定x,y,饼状图需要指定x/y,color
    //line 绘制折线图 需要x,y
    //text 该类型用于在绘制的图形上显示文字
    type: 'interval',
     //根据容器大小自适应宽高
    autoFit: true,
    //坐标轴对应的属性,有x,y,color三种,color用于饼状图
    encode: {x: 'genre',y: 'sold'},
    //轴上对应刻度显示的配置
    axis:{x:{labelFormatter:'~s'}}, //x轴格式化为千分位123,222的格式
    style:{}
}

dataOptions

javascript
//仅写数组时,为默认type为inline的语法糖
const data=[{ genre: 'Sports', sold: 275 }]
const dataOptions={
    type: 'fetch', //默认为inline
    value: 'https://gw.alipayobjects.com/os/antvdemo/assets/data/scatter.json',
    transform: [
       {type: 'filter',callback: (d) => d.a < 3},
       {type: 'map',callback: (d,index) => { ...d, index }},
       {type: 'sort',callback: (a, b) => b.a - a.a},
       {type: 'sortBy',callback: (a, b) => b.a - a.a},
       {type: 'slice',fields: ['sold']},// 根据 sold 字段排序
    ],
    callback: (d) => d.a < 3
}

coordinateOptions

javascript
//坐标系配置
const coordinateOptions={
  //transpose 将 (x, y) 变换成 (y, x),常见于条形图
  // transform: [{ type: 'transpose' }],
  //theta 一种特殊的极坐标系,半径长度固定,常用于饼状图,
      //如果使用该类型,encode的x需要换成color,还有.transform({ type: 'stackY' })
   type: 'theta'
}

图形绘制关键点

//interval常用来绘制柱形图、条形图、饼图等,需要指定x,y,饼状图需要指定x/y,color
    //line 绘制折线图 需要x,y
    //text 该类型用于在绘制的图形上显示文字
    //area面积图,可以理解为平滑的柱形图,需要指定x,y
    //wordCloud 词云图,也就是全是文字的图
    //heatmap 热力图,需要同时指定x,y,color
    //gauge 仪表盘 需要将data的value改为: {target: 120,total: 400,name: 'score'},
        //target是当前进度,total是总进度
    type: 'interval',

叠加的柱/条状图

//关键点在于,x/y属性需要有相同的属性值

一个刻度有多个柱的分组柱状图

//关键点
.transform({ type: 'dodgeX' })

饼图

//关键点
.coordinate({ type: 'theta' })
.transform({ type: 'stackY' })
  .encode('y', 'percent')
  .encode('color', 'item')

环形图

//关键点
通过 outerRadius: 0.8, innerRadius: 0.5指定内外圈基于原始圈的半径
.coordinate({ type: 'theta', outerRadius: 0.8, innerRadius: 0.5 })
.transform({ type: 'stackY' })
  .encode('y', 'percent')
  .encode('color', 'item')