UNPKG

6.09 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3var tslib_1 = require("tslib");
4var util_1 = require("@antv/util");
5var constant_1 = require("../constant");
6var engine_1 = require("../engine");
7var dom_1 = require("../util/dom");
8var view_1 = tslib_1.__importDefault(require("./view"));
9/**
10 * Chart 类,是使用 G2 进行绘图的入口。
11 */
12var Chart = /** @class */ (function (_super) {
13 tslib_1.__extends(Chart, _super);
14 // @ts-ignore
15 function Chart(props) {
16 var _this = this;
17 var container = props.container, width = props.width, height = props.height, _a = props.autoFit, autoFit = _a === void 0 ? false : _a, padding = props.padding, appendPadding = props.appendPadding, _b = props.renderer, renderer = _b === void 0 ? 'canvas' : _b, pixelRatio = props.pixelRatio, _c = props.localRefresh, localRefresh = _c === void 0 ? true : _c, _d = props.visible, visible = _d === void 0 ? true : _d, _e = props.supportCSSTransform, supportCSSTransform = _e === void 0 ? false : _e, _f = props.defaultInteractions, defaultInteractions = _f === void 0 ? ['tooltip', 'legend-filter', 'legend-active', 'continuous-filter', 'ellipsis-text'] : _f, options = props.options, limitInPlot = props.limitInPlot, theme = props.theme, syncViewPadding = props.syncViewPadding;
18 var ele = util_1.isString(container) ? document.getElementById(container) : container;
19 // 生成内部正式绘制的 div 元素
20 var wrapperElement = dom_1.createDom('<div style="position:relative;"></div>');
21 ele.appendChild(wrapperElement);
22 // if autoFit, use the container size, to avoid the graph render twice.
23 var size = dom_1.getChartSize(ele, autoFit, width, height);
24 var G = engine_1.getEngine(renderer);
25 var canvas = new G.Canvas(tslib_1.__assign({ container: wrapperElement, pixelRatio: pixelRatio,
26 localRefresh: localRefresh,
27 supportCSSTransform: supportCSSTransform }, size));
28 // 调用 view 的创建
29 _this = _super.call(this, {
30 parent: null,
31 canvas: canvas,
32 // create 3 group layers for views.
33 backgroundGroup: canvas.addGroup({ zIndex: constant_1.GROUP_Z_INDEX.BG }),
34 middleGroup: canvas.addGroup({ zIndex: constant_1.GROUP_Z_INDEX.MID }),
35 foregroundGroup: canvas.addGroup({ zIndex: constant_1.GROUP_Z_INDEX.FORE }),
36 padding: padding,
37 appendPadding: appendPadding,
38 visible: visible,
39 options: options,
40 limitInPlot: limitInPlot,
41 theme: theme,
42 syncViewPadding: syncViewPadding,
43 }) || this;
44 /**
45 * when container size changed, change chart size props, and re-render.
46 */
47 _this.onResize = util_1.debounce(function () {
48 _this.forceFit();
49 }, 300);
50 _this.ele = ele;
51 _this.canvas = canvas;
52 _this.width = size.width;
53 _this.height = size.height;
54 _this.autoFit = autoFit;
55 _this.localRefresh = localRefresh;
56 _this.renderer = renderer;
57 _this.wrapperElement = wrapperElement;
58 // 自适应大小
59 _this.updateCanvasStyle();
60 _this.bindAutoFit();
61 _this.initDefaultInteractions(defaultInteractions);
62 return _this;
63 }
64 Chart.prototype.initDefaultInteractions = function (interactions) {
65 var _this = this;
66 util_1.each(interactions, function (interaction) {
67 _this.interaction(interaction);
68 });
69 };
70 /**
71 * 改变图表大小,同时重新渲染。
72 * @param width 图表宽度
73 * @param height 图表高度
74 * @returns
75 */
76 Chart.prototype.changeSize = function (width, height) {
77 // 如果宽高一致,那么 changeSize 不执行任何操作
78 if (this.width === width && this.height === height) {
79 return this;
80 }
81 this.emit(constant_1.VIEW_LIFE_CIRCLE.BEFORE_CHANGE_SIZE);
82 this.width = width;
83 this.height = height;
84 this.canvas.changeSize(width, height);
85 // 重新渲染
86 this.render(true);
87 this.emit(constant_1.VIEW_LIFE_CIRCLE.AFTER_CHANGE_SIZE);
88 return this;
89 };
90 /**
91 * 销毁图表,同时解绑事件,销毁创建的 G.Canvas 实例。
92 * @returns void
93 */
94 Chart.prototype.destroy = function () {
95 _super.prototype.destroy.call(this);
96 this.unbindAutoFit();
97 this.canvas.destroy();
98 dom_1.removeDom(this.wrapperElement);
99 this.wrapperElement = null;
100 };
101 /**
102 * 显示或隐藏图表
103 * @param visible 是否可见,true 表示显示,false 表示隐藏
104 * @returns
105 */
106 Chart.prototype.changeVisible = function (visible) {
107 _super.prototype.changeVisible.call(this, visible); // 需要更新 visible 变量
108 this.wrapperElement.style.display = visible ? '' : 'none';
109 return this;
110 };
111 /**
112 * 自动根据容器大小 resize 画布
113 */
114 Chart.prototype.forceFit = function () {
115 // skip if already destroyed
116 if (!this.destroyed) {
117 // 注意第二参数用 true,意思是即时 autoFit = false,forceFit() 调用之后一样是适配容器
118 var _a = dom_1.getChartSize(this.ele, true, this.width, this.height), width = _a.width, height = _a.height;
119 this.changeSize(width, height);
120 }
121 };
122 Chart.prototype.updateCanvasStyle = function () {
123 dom_1.modifyCSS(this.canvas.get('el'), {
124 display: 'inline-block',
125 verticalAlign: 'middle',
126 });
127 };
128 Chart.prototype.bindAutoFit = function () {
129 if (this.autoFit) {
130 window.addEventListener('resize', this.onResize);
131 }
132 };
133 Chart.prototype.unbindAutoFit = function () {
134 if (this.autoFit) {
135 window.removeEventListener('resize', this.onResize);
136 }
137 };
138 return Chart;
139}(view_1.default));
140exports.default = Chart;
141//# sourceMappingURL=chart.js.map
\No newline at end of file