1 | import { debounce, each, isString, get } from '@antv/util';
|
2 | import { ChartCfg } from '../interface';
|
3 | import { GROUP_Z_INDEX, VIEW_LIFE_CIRCLE } from '../constant';
|
4 | import { getEngine } from '../engine';
|
5 | import { createDom, getChartSize, removeDom, modifyCSS } from '../util/dom';
|
6 | import View from './view';
|
7 |
|
8 |
|
9 |
|
10 |
|
11 | export default class Chart extends View {
|
12 |
|
13 | public ele: HTMLElement;
|
14 |
|
15 |
|
16 | public width: number;
|
17 |
|
18 | public height: number;
|
19 |
|
20 | public localRefresh: boolean;
|
21 |
|
22 | public autoFit: boolean;
|
23 |
|
24 | public renderer: 'canvas' | 'svg';
|
25 |
|
26 | private wrapperElement: HTMLElement;
|
27 |
|
28 |
|
29 | constructor(props: ChartCfg) {
|
30 | const {
|
31 | container,
|
32 | width,
|
33 | height,
|
34 | autoFit = false,
|
35 | padding,
|
36 | appendPadding,
|
37 | renderer = 'canvas',
|
38 | pixelRatio,
|
39 | localRefresh = true,
|
40 | visible = true,
|
41 | supportCSSTransform = false,
|
42 | defaultInteractions = ['tooltip', 'legend-filter', 'legend-active', 'continuous-filter', 'ellipsis-text'],
|
43 | options,
|
44 | limitInPlot,
|
45 | theme,
|
46 | syncViewPadding,
|
47 | } = props;
|
48 |
|
49 | const ele: HTMLElement = isString(container) ? document.getElementById(container) : container;
|
50 |
|
51 |
|
52 | const wrapperElement = createDom('<div style="position:relative;"></div>');
|
53 | ele.appendChild(wrapperElement);
|
54 |
|
55 |
|
56 | const size = getChartSize(ele, autoFit, width, height);
|
57 |
|
58 | const G = getEngine(renderer);
|
59 |
|
60 | const canvas = new G.Canvas({
|
61 | container: wrapperElement,
|
62 | pixelRatio,
|
63 | localRefresh,
|
64 | supportCSSTransform,
|
65 | ...size,
|
66 | });
|
67 |
|
68 |
|
69 | super({
|
70 | parent: null,
|
71 | canvas,
|
72 |
|
73 | backgroundGroup: canvas.addGroup({ zIndex: GROUP_Z_INDEX.BG }),
|
74 | middleGroup: canvas.addGroup({ zIndex: GROUP_Z_INDEX.MID }),
|
75 | foregroundGroup: canvas.addGroup({ zIndex: GROUP_Z_INDEX.FORE }),
|
76 | padding,
|
77 | appendPadding,
|
78 | visible,
|
79 | options,
|
80 | limitInPlot,
|
81 | theme,
|
82 | syncViewPadding,
|
83 | });
|
84 |
|
85 | this.ele = ele;
|
86 | this.canvas = canvas;
|
87 | this.width = size.width;
|
88 | this.height = size.height;
|
89 | this.autoFit = autoFit;
|
90 | this.localRefresh = localRefresh;
|
91 | this.renderer = renderer;
|
92 | this.wrapperElement = wrapperElement;
|
93 |
|
94 |
|
95 | this.updateCanvasStyle();
|
96 | this.bindAutoFit();
|
97 | this.initDefaultInteractions(defaultInteractions);
|
98 | }
|
99 |
|
100 | private initDefaultInteractions(interactions) {
|
101 | each(interactions, (interaction) => {
|
102 | this.interaction(interaction);
|
103 | });
|
104 | }
|
105 |
|
106 | |
107 |
|
108 |
|
109 |
|
110 |
|
111 |
|
112 | public changeSize(width: number, height: number) {
|
113 |
|
114 | if (this.width === width && this.height === height) {
|
115 | return this;
|
116 | }
|
117 |
|
118 | this.emit(VIEW_LIFE_CIRCLE.BEFORE_CHANGE_SIZE);
|
119 |
|
120 | this.width = width;
|
121 | this.height = height;
|
122 | this.canvas.changeSize(width, height);
|
123 |
|
124 |
|
125 | this.render(true);
|
126 |
|
127 | this.emit(VIEW_LIFE_CIRCLE.AFTER_CHANGE_SIZE);
|
128 |
|
129 | return this;
|
130 | }
|
131 |
|
132 | |
133 |
|
134 |
|
135 |
|
136 | public destroy() {
|
137 | super.destroy();
|
138 |
|
139 | this.unbindAutoFit();
|
140 | this.canvas.destroy();
|
141 |
|
142 | removeDom(this.wrapperElement);
|
143 | this.wrapperElement = null;
|
144 | }
|
145 |
|
146 | |
147 |
|
148 |
|
149 |
|
150 |
|
151 | public changeVisible(visible: boolean) {
|
152 | super.changeVisible(visible);
|
153 | this.wrapperElement.style.display = visible ? '' : 'none';
|
154 |
|
155 | return this;
|
156 | }
|
157 |
|
158 | |
159 |
|
160 |
|
161 | public forceFit() {
|
162 |
|
163 | if (!this.destroyed) {
|
164 |
|
165 | const { width, height } = getChartSize(this.ele, true, this.width, this.height);
|
166 | this.changeSize(width, height);
|
167 | }
|
168 | }
|
169 |
|
170 | private updateCanvasStyle() {
|
171 | modifyCSS(this.canvas.get('el'), {
|
172 | display: 'inline-block',
|
173 | verticalAlign: 'middle',
|
174 | });
|
175 | }
|
176 |
|
177 | private bindAutoFit() {
|
178 | if (this.autoFit) {
|
179 | window.addEventListener('resize', this.onResize);
|
180 | }
|
181 | }
|
182 |
|
183 | private unbindAutoFit() {
|
184 | if (this.autoFit) {
|
185 | window.removeEventListener('resize', this.onResize);
|
186 | }
|
187 | }
|
188 |
|
189 | |
190 |
|
191 |
|
192 | private onResize = debounce(() => {
|
193 | this.forceFit();
|
194 | }, 300);
|
195 | }
|