初始化流程
Chart -> View -> Base -> EventEmitter
1️⃣Base#constructor(cfg)
Chart#getDefaultCfg
chart拥有自己的config + view#getDefaultCfg() + visible
2️⃣View#constructor(cfg)
view#_setTheme()
主题相关的配置
geom属性创建
初始化所有内置的geom,并且赋值给view的同名属性 类似如下:view#point = new Point({cfg});
chart#init() => view#init()
chart#init()
chart._initCanvas();//new Canvaschart._initPlot();chart._initEvents();//into view#init()view._initViewPlot(); // 先创建容器if (view.get('data')) {//有数据,就初始化 -- 初始化直接带数据那种view._initData(this.get('data'));}view._initOptions();view._initControllers();view._bindEvents();//back chart#init()//初始化各种Controller
3️⃣Controller
Controller#Scale,Coord,Axis,Guide,Legend,Tooltip,Event
| 属性 | Controller |
|---|---|
| scaleController (for view) | Scale |
| coordController (for view) | Coord |
| axisController (for view) | Axis |
| guideController (for view) | Guide |
| legendController (for chart) | Legend |
| tooltipController (for chart) | Tooltip |
| eventController (for view) | Event |
Chart中属性&对应的Controller
基础配置
chart#cfg
{id: null, //view层id,这个和container属性设置一个即可forceFit: false, // plot根据resize事件来同步拉伸缩放container: null, //见id,这个是view的实例wrapperEl: null, // 类似于一个container 如:<div style="position:relative;"></div>canvas: null, // 绘制的canvas,详见 @antv/g/lib/canvas.jswidth: 500, //plot宽度height: 500, //plot高度pixelRatio: null,//像素比? -- 像素的缩放比例(TODO 具体待查)backPlot: null, // plot在最后面的一层frontPlot: null,// plot在最前面的一层/**const backgroundStyle: G2.Styles.background = {fill: "#aaa", // 图表背景色fillOpacity: .5, // 图表背景透明度stroke: "#aaa", // 图表边框颜色strokeOpacity: .5, // 图表边框透明度opacity: .5, // 图表整体透明度lineWidth: 1, // 图表边框粗度radius: 5 // 图表圆角大小}*/plotBackground: null, // plot 背景颜色padding: [ 20, 20, 95, 80 ],//left top right bottombackground: null, //参见 plotBackgroundautoPaddingAppend: 5, //自动padding增加的像素limitInPlot: false, // TODO 姑且认为作用是限制绘图的区域在plot里面吧renderer: 'canvas', // 默认是canvas,还有svg.. 具体见@antv/g/lib/renders/index.jsviews: [] //G2 的图表可以由多个视图 view 构成,同时各个视图可以拥有各自的数据,即支持异构数据}
view#cfg
{viewContainer: null,//绘制view的视图实例coord: null, //几何坐标系(目前全是二维坐标 直角,极坐标,地理, 螺旋坐标)start: { x: 0, y: 0 }, //开始坐标end: { x: 1, y: 1 }, //结束坐标geoms: [],//几何标记-点、线、面这些几何图形-都是配置此来决定绘制什么类型图表scales: {},//度量-主要是归一化的操作,主要针对 连续数据类型(将数据缩放到[0,1]的范围)和分类数据类型(采用index形式[0,1....])options: {},//视觉通道选项(position,color,size,shape,opacity) => 这些都是属于一个geoms的scaleController: null, //度量Controllerpadding: 0,theme: null, //主题parent: null,tooltipEnable: true, // 是否展示 tooltipanimate: Global.animate,//动画配置默认开启visible: true //是否可见}
控制器(Controller)
Scale: 度量
Coord: 坐标系
Axis: 坐标轴
Guide: 辅助标记
Legend: 图例
Tooptip: 提示信息
Event: 鼠标操作事件
canvas.on('mousedown', Util.wrapBehavior(this, 'onDown'));canvas.on('mousemove', Util.wrapBehavior(this, 'onMove'));canvas.on('mouseleave', Util.wrapBehavior(this, 'onOut'));canvas.on('mouseup', Util.wrapBehavior(this, 'onUp'));canvas.on('click', Util.wrapBehavior(this, 'onClick'));canvas.on('dblclick', Util.wrapBehavior(this, 'onClick'));canvas.on('touchstart', Util.wrapBehavior(this, 'onTouchstart'));canvas.on('touchmove', Util.wrapBehavior(this, 'onTouchmove'));canvas.on('touchend', Util.wrapBehavior(this, 'onTouchend'));
