UNPKG

20.5 kBTypeScriptView Raw
1import { Attribute, Coordinate, ICanvas, IGroup, Scale } from '../dependents';
2import { AxisOption, ComponentOption, CoordinateCfg, CoordinateOption, Data, Datum, FacetCfgMap, FilterCondition, LegendOption, LooseObject, Options, Point, Region, ScaleOption, TooltipOption, ViewCfg, ViewPadding, ViewAppendPadding, EventPayload } from '../interface';
3import { LAYER } from '../constant';
4import Base from '../base';
5import { Facet } from '../facet';
6import Geometry from '../geometry/base';
7import { Interaction } from '../interaction';
8import { BBox } from '../util/bbox';
9import Annotation from './controller/annotation';
10import { Controller } from './controller/base';
11import CoordinateController from './controller/coordinate';
12import Tooltip from './controller/tooltip';
13import Slider from './controller/slider';
14import Scrollbar from './controller/scrollbar';
15import Axis from './controller/axis';
16import Gesture from './controller/gesture';
17import Legend from './controller/legend';
18import { Layout } from './layout';
19import { PaddingCal } from './layout/padding-cal';
20/**
21 * G2 视图 View 类
22 */
23export declare class View extends Base {
24 /** view id,全局唯一。 */
25 id: string;
26 /** 父级 view,如果没有父级,则为空。 */
27 parent: View;
28 /** 所有的子 view。 */
29 views: View[];
30 /** 所有的 geometry 实例。 */
31 geometries: Geometry[];
32 /** 所有的组件 controllers。 */
33 controllers: Controller[];
34 /** 所有的 Interaction 实例。 */
35 interactions: Record<string, Interaction>;
36 /** view 区域空间。 */
37 viewBBox: BBox;
38 /** 坐标系的位置大小,ViewBBox - padding = coordinateBBox。 */
39 coordinateBBox: BBox;
40 /** view 的 padding 大小,传入的配置(不是解析之后的值)。 */
41 padding: ViewPadding;
42 /** padding的基础上增加的调整值 */
43 appendPadding: ViewAppendPadding;
44 /** G.Canvas 实例。 */
45 canvas: ICanvas;
46 /** 存储最终计算的 padding 结果 */
47 autoPadding: PaddingCal;
48 /** 三层 Group 图形中的背景层。 */
49 backgroundGroup: IGroup;
50 /** 三层 Group 图形中的中间层。 */
51 middleGroup: IGroup;
52 /** 三层 Group 图形中的前景层。 */
53 foregroundGroup: IGroup;
54 /** 是否对超出坐标系范围的 Geometry 进行剪切 */
55 limitInPlot: boolean;
56 /**
57 * 标记 view 的大小位置范围,均是 0 ~ 1 范围,便于开发者使用,起始点为左上角。
58 */
59 protected region: Region;
60 /** 主题配置,存储当前主题配置。 */
61 protected themeObject: LooseObject;
62 protected options: Options;
63 /** 过滤之后的数据 */
64 protected filteredData: Data;
65 /** 配置开启的组件插件,默认为全局配置的组件。 */
66 private usedControllers;
67 /** 所有的 scales */
68 private scalePool;
69 /** 布局函数 */
70 protected layoutFunc: Layout;
71 /** 生成的坐标系实例,{@link https://github.com/antvis/coord/blob/master/src/coord/base.ts|Coordinate} */
72 protected coordinateInstance: Coordinate;
73 /** Coordinate 相关的控制器类,负责坐标系实例的创建、更新、变换等 */
74 protected coordinateController: CoordinateController;
75 /** 分面类实例 */
76 protected facetInstance: Facet;
77 /** 当前鼠标是否在 plot 内(CoordinateBBox) */
78 private isPreMouseInPlot;
79 /** 默认标识位,用于判定数据是否更新 */
80 private isDataChanged;
81 /** 用于判断坐标系范围是否发生变化的标志位 */
82 private isCoordinateChanged;
83 /** 从当前这个 view 创建的 scale key */
84 private createdScaleKeys;
85 /** 背景色样式的 shape */
86 private backgroundStyleRectShape;
87 /** 是否同步子 view 的 padding */
88 private syncViewPadding;
89 constructor(props: ViewCfg);
90 /**
91 * 设置 layout 布局函数
92 * @param layout 布局函数
93 * @returns void
94 */
95 setLayout(layout: Layout): void;
96 /**
97 * 生命周期:初始化
98 * @returns voids
99 */
100 init(): void;
101 /**
102 * 生命周期:渲染流程,渲染过程需要处理数据更新的情况。
103 * render 函数仅仅会处理 view 和子 view。
104 * @param isUpdate 是否触发更新流程。
105 * @param params render 事件参数
106 */
107 render(isUpdate?: boolean, payload?: EventPayload): void;
108 /**
109 * 生命周期:清空图表上所有的绘制内容,但是不销毁图表,chart 仍可使用。
110 * @returns void
111 */
112 clear(): void;
113 /**
114 * 生命周期:销毁,完全无法使用。
115 * @returns void
116 */
117 destroy(): void;
118 /**
119 * 显示或者隐藏整个 view。
120 * @param visible 是否可见
121 * @returns View
122 */
123 changeVisible(visible: boolean): View;
124 /**
125 * 装载数据源。
126 *
127 * ```ts
128 * view.data([{ city: '杭州', sale: 100 }, { city: '上海', sale: 110 } ]);
129 * ```
130 *
131 * @param data 数据源,json 数组。
132 * @returns View
133 */
134 data(data: Data): View;
135 /**
136 * @deprecated
137 * This method will be removed at G2 V4.1. Replaced by {@link #data(data)}
138 */
139 source(data: Data): View;
140 /**
141 * 设置数据筛选规则。
142 *
143 * ```ts
144 * view.filter('city', (value: any, datum: Datum) => value !== '杭州');
145 *
146 * // 删除 'city' 字段对应的筛选规则。
147 * view.filter('city', null);
148 * ```
149 *
150 * @param field 数据字段
151 * @param condition 筛选规则
152 * @returns View
153 */
154 filter(field: string, condition: FilterCondition | null): View;
155 /**
156 * 开启或者关闭坐标轴。
157 *
158 * ```ts
159 * view.axis(false); // 不展示坐标轴
160 * ```
161 * @param field 坐标轴开关
162 */
163 axis(field: boolean): View;
164 /**
165 * 对特定的某条坐标轴进行配置。
166 *
167 * @example
168 * ```ts
169 * view.axis('city', false); // 不展示 'city' 字段对应的坐标轴
170 *
171 * // 将 'city' 字段对应的坐标轴的标题隐藏
172 * view.axis('city', {
173 * title: null,
174 * });
175 * ```
176 *
177 * @param field 要配置的坐标轴对应的字段名称
178 * @param axisOption 坐标轴具体配置,更详细的配置项可以参考:https://github.com/antvis/component#axis
179 */
180 axis(field: string, axisOption: AxisOption): View;
181 /**
182 * 对图例进行整体配置。
183 *
184 * ```ts
185 * view.legend(false); // 关闭图例
186 *
187 * view.legend({
188 * position: 'right',
189 * }); // 图例进行整体配置
190 * ```
191 * @param field
192 * @returns View
193 */
194 legend(field: LegendOption): View;
195 /**
196 * 对特定的图例进行配置。
197 *
198 * @example
199 * ```ts
200 * view.legend('city', false); // 关闭某个图例,通过数据字段名进行关联
201 *
202 * // 对特定的图例进行配置
203 * view.legend('city', {
204 * position: 'right',
205 * });
206 * ```
207 *
208 * @param field 图例对应的数据字段名称
209 * @param legendOption 图例配置,更详细的配置项可以参考:https://github.com/antvis/component#axis
210 * @returns View
211 */
212 legend(field: string, legendOption: LegendOption): View;
213 /**
214 * 批量设置 scale 配置。
215 *
216 * ```ts
217 * view.scale({
218 * sale: {
219 * min: 0,
220 * max: 100,
221 * }
222 * });
223 * ```
224 * Scale 的详细配置项可以参考:https://github.com/antvis/scale#api
225 * @returns View
226 */
227 scale(field: Record<string, ScaleOption>): View;
228 /**
229 * 为特性的数据字段进行 scale 配置。
230 *
231 * ```ts
232 * view.scale('sale', {
233 * min: 0,
234 * max: 100,
235 * });
236 * ```
237 *
238 * @returns View
239 */
240 scale(field: string, scaleOption: ScaleOption): View;
241 /**
242 * tooltip 提示信息配置。
243 *
244 * ```ts
245 * view.tooltip(false); // 关闭 tooltip
246 *
247 * view.tooltip({
248 * shared: true
249 * });
250 * ```
251 *
252 * @param cfg Tooltip 配置,更详细的配置项参考:https://github.com/antvis/component#tooltip
253 * @returns View
254 */
255 tooltip(cfg: boolean | TooltipOption): View;
256 /**
257 * 辅助标记配置。
258 *
259 * ```ts
260 * view.annotation().line({
261 * start: ['min', 85],
262 * end: ['max', 85],
263 * style: {
264 * stroke: '#595959',
265 * lineWidth: 1,
266 * lineDash: [3, 3],
267 * },
268 * });
269 * ```
270 * 更详细的配置项:https://github.com/antvis/component#annotation
271 * @returns [[Annotation]]
272 */
273 annotation(): Annotation;
274 /**
275 * @deprecated
276 * This method will be removed at G2 V4.1. Replaced by {@link #guide()}
277 */
278 guide(): Annotation;
279 /**
280 * 坐标系配置。
281 *
282 * @example
283 * ```ts
284 * view.coordinate({
285 * type: 'polar',
286 * cfg: {
287 * radius: 0.85,
288 * },
289 * actions: [
290 * [ 'transpose' ],
291 * ],
292 * });
293 * ```
294 *
295 * @param option
296 * @returns
297 */
298 coordinate(option?: CoordinateOption): CoordinateController;
299 /**
300 * 声明坐标系类型,并进行配置。
301 *
302 * ```ts
303 * // 直角坐标系,并进行转置变换
304 * view.coordinate('rect').transpose();
305 *
306 * // 默认创建直角坐标系
307 * view.coordinate();
308 * ```
309 *
310 * @param type 坐标系类型
311 * @param [coordinateCfg] 坐标系配置
312 * @returns
313 */
314 coordinate(type: string, coordinateCfg?: CoordinateCfg): CoordinateController;
315 /**
316 * @deprecated
317 * This method will be removed at G2 V4.1. Replaced by {@link #coordinate()}
318 */
319 coord(type: string | CoordinateOption, coordinateCfg?: CoordinateCfg): CoordinateController;
320 /**
321 * view 分面绘制。
322 *
323 * ```ts
324 * view.facet('rect', {
325 * rowField: 'province',
326 * columnField: 'category',
327 * eachView: (innerView: View, facet?: FacetData) => {
328 * innerView.line().position('city*sale');
329 * },
330 * });
331 * ```
332 *
333 * @param type 分面类型
334 * @param cfg 分面配置, [[FacetCfgMap]]
335 * @returns View
336 */
337 facet<T extends keyof FacetCfgMap>(type: T, cfg: FacetCfgMap[T]): View;
338 animate(status: boolean): View;
339 /**
340 * 更新配置项,用于配置项式声明。
341 * @param options 配置项
342 */
343 updateOptions(options: Options): this;
344 /**
345 * 往 `view.options` 属性中存储配置项。
346 * @param name 属性名称
347 * @param opt 属性值
348 * @returns view
349 */
350 option(name: string, opt: any): View;
351 /**
352 * 设置主题。
353 *
354 * ```ts
355 * view.theme('dark'); // 'dark' 需要事先通过 `registerTheme()` 接口注册完成
356 *
357 * view.theme({ defaultColor: 'red' });
358 * ```
359 *
360 * @param theme 主题名或者主题配置
361 * @returns View
362 */
363 theme(theme: string | LooseObject): View;
364 /**
365 * Call the interaction based on the interaction name
366 *
367 * ```ts
368 * view.interaction('my-interaction', { extra: 'hello world' });
369 * ```
370 * 详细文档可以参考:https://g2.antv.vision/zh/docs/api/general/interaction
371 * @param name interaction name
372 * @param cfg interaction config
373 * @returns
374 */
375 interaction(name: string, cfg?: LooseObject): View;
376 /**
377 * 移除当前 View 的 interaction
378 * ```ts
379 * view.removeInteraction('my-interaction');
380 * ```
381 * @param name interaction name
382 */
383 removeInteraction(name: string): void;
384 /**
385 * 修改数据,数据更新逻辑,数据更新仅仅影响当前这一层的 view
386 *
387 * ```ts
388 * view.changeData([{ city: '北京', sale: '200' }]);
389 * ```
390 *
391 * @param data
392 * @returns void
393 */
394 changeData(data: Data): void;
395 /**
396 * 创建子 view
397 *
398 * ```ts
399 * const innerView = view.createView({
400 * start: { x: 0, y: 0 },
401 * end: { x: 0.5, y: 0.5 },
402 * padding: 8,
403 * });
404 * ```
405 *
406 * @param cfg
407 * @returns View
408 */
409 createView(cfg?: Partial<ViewCfg>): View;
410 /**
411 * @deprecated
412 * This method will be removed at G2 V4.1. Replaced by {@link #createView()}
413 */
414 view(cfg?: Partial<ViewCfg>): View;
415 /**
416 * 删除一个子 view
417 * @param view
418 * @return removedView
419 */
420 removeView(view: View): View;
421 /**
422 * 获取当前坐标系实例。
423 * @returns [[Coordinate]]
424 */
425 getCoordinate(): Coordinate;
426 /**
427 * 获取当前 view 的主题配置。
428 * @returns themeObject
429 */
430 getTheme(): LooseObject;
431 /**
432 * 获得 x 轴字段的 scale 实例。
433 * @returns view 中 Geometry 对于的 x scale
434 */
435 getXScale(): Scale;
436 /**
437 * 获取 y 轴字段的 scales 实例。
438 * @returns view 中 Geometry 对于的 y scale 数组
439 */
440 getYScales(): Scale[];
441 /**
442 * 获取 x 轴或者 y 轴对应的所有 scale 实例。
443 * @param dimType x | y
444 * @returns x 轴或者 y 轴对应的所有 scale 实例。
445 */
446 getScalesByDim(dimType: 'x' | 'y'): Record<string, Scale>;
447 /**
448 * 根据字段名去获取 scale 实例。
449 * @param field 数据字段名称
450 * @param key id
451 */
452 getScaleByField(field: string, key?: string): Scale;
453 /**
454 * 返回所有配置信息。
455 * @returns 所有的 view API 配置。
456 */
457 getOptions(): Options;
458 /**
459 * 获取 view 的数据(过滤后的数据)。
460 * @returns 处理过滤器之后的数据。
461 */
462 getData(): Data;
463 /**
464 * 获得绘制的层级 group。
465 * @param layer 层级名称。
466 * @returns 对应层级的 Group。
467 */
468 getLayer(layer: LAYER): IGroup;
469 /**
470 * 对外暴露方法,判断一个点是否在绘图区域(即坐标系范围)内部。
471 * @param point 坐标点
472 */
473 isPointInPlot(point: Point): boolean;
474 /**
475 * 获得所有的 legend 对应的 attribute 实例。
476 * @returns 维度字段的 Attribute 数组
477 */
478 getLegendAttributes(): Attribute[];
479 /**
480 * 获取所有的分组字段的 scale 实例。
481 * @returns 获得分组字段的 scale 实例数组。
482 */
483 getGroupScales(): Scale[];
484 /**
485 * 获取 G.Canvas 实例。
486 * @returns G.Canvas 画布实例。
487 */
488 getCanvas(): ICanvas;
489 /**
490 * 获得根节点 view。
491 */
492 getRootView(): View;
493 /**
494 * 获取该数据在可视化后,对应的画布坐标点。
495 * @param data 原始数据记录
496 * @returns 对应的画布坐标点
497 */
498 getXY(data: Datum): Point;
499 getController(name: 'tooltip'): Tooltip;
500 getController(name: 'axis'): Axis;
501 getController(name: 'legend'): Legend;
502 getController(name: 'scrollbar'): Scrollbar;
503 getController(name: 'slider'): Slider;
504 getController(name: 'annotation'): Annotation;
505 getController(name: 'gestucre'): Gesture;
506 getController(name: string): Controller;
507 /**
508 * 显示 point 坐标点对应的 tooltip。
509 * @param point 画布坐标点
510 * @returns View
511 */
512 showTooltip(point: Point): View;
513 /**
514 * 隐藏 tooltip。
515 * @returns View
516 */
517 hideTooltip(): View;
518 /**
519 * 将 tooltip 锁定到当前位置不能移动。
520 * @returns View
521 */
522 lockTooltip(): View;
523 /**
524 * 将 tooltip 锁定解除。
525 * @returns View
526 */
527 unlockTooltip(): View;
528 /**
529 * 是否锁定 tooltip。
530 * @returns 是否锁定
531 */
532 isTooltipLocked(): boolean;
533 /**
534 * 获取当前 point 对应的 tooltip 数据项。
535 * @param point 坐标点
536 * @returns tooltip 数据项
537 */
538 getTooltipItems(point: Point): any[];
539 /**
540 * 获取逼近的点的数据集合
541 * @param point 当前坐标点
542 * @returns 数据
543 */
544 getSnapRecords(point: Point): any[];
545 /**
546 * 获取所有的 pure component 组件,用于布局。
547 */
548 getComponents(): ComponentOption[];
549 /**
550 * 将 data 数据进行过滤。
551 * @param data
552 * @returns 过滤之后的数据
553 */
554 filterData(data: Data): Data;
555 /**
556 * 对某一个字段进行过滤
557 * @param field
558 * @param data
559 */
560 filterFieldData(field: string, data: Data): Data;
561 /**
562 * 调整 coordinate 的坐标范围。
563 */
564 adjustCoordinate(): void;
565 protected paint(isUpdate: boolean): void;
566 /**
567 * 渲染背景样式的 shape。
568 * 放到 view 中创建的原因是让使用 view 绘制图形的时候,也能够处理背景色
569 */
570 private renderBackgroundStyleShape;
571 /**
572 * 递归计算每个 view 的 padding 值,coordinateBBox 和 coordinateInstance
573 * @param isUpdate
574 */
575 protected renderPaddingRecursive(isUpdate: boolean): void;
576 /**
577 * 递归处理 view 的布局,最终是计算各个 view 的 coordinateBBox 和 coordinateInstance
578 * @param isUpdate
579 */
580 protected renderLayoutRecursive(isUpdate: boolean): void;
581 /**
582 * 最终递归绘制组件和图形
583 * @param isUpdate
584 */
585 protected renderPaintRecursive(isUpdate: boolean): void;
586 /**
587 * 创建 scale,递归到顶层 view 去创建和缓存 scale
588 * @param field
589 * @param data
590 * @param scaleDef
591 * @param key
592 */
593 protected createScale(field: string, data: Data, scaleDef: ScaleOption, key: string): Scale;
594 /**
595 * 递归渲染中的数据处理
596 * @param isUpdate
597 */
598 private renderDataRecursive;
599 /**
600 * 计算 region,计算实际的像素范围坐标
601 * @private
602 */
603 private calculateViewBBox;
604 /**
605 * 初始化事件机制:G 4.0 底层内置支持 name:event 的机制,那么只要所有组件都有自己的 name 即可。
606 *
607 * G2 的事件只是获取事件委托,然后在 view 嵌套结构中,形成事件冒泡机制。
608 * 当前 view 只委托自己 view 中的 Component 和 Geometry 事件,并向上冒泡
609 * @private
610 */
611 private initEvents;
612 private onCanvasEvent;
613 /**
614 * 初始化插件
615 */
616 private initComponentController;
617 private createViewEvent;
618 /**
619 * 触发事件之后
620 * @param evt
621 */
622 private onDelegateEvents;
623 /**
624 * 处理 PLOT_EVENTS
625 * plot event 需要处理所有的基础事件,并判断是否在画布中,然后再决定是否要 emit。
626 * 对于 mouseenter、mouseleave 比较特殊,需要做一下数学比较。
627 * @param e
628 */
629 private doPlotEvent;
630 /**
631 * 处理筛选器,筛选数据
632 * @private
633 */
634 private doFilterData;
635 /**
636 * 初始化 Geometries
637 * @private
638 */
639 private initGeometries;
640 /**
641 * 根据 Geometry 的所有字段创建 scales
642 * 如果存在,则更新,不存在则创建
643 */
644 private createOrUpdateScales;
645 /**
646 * 处理 scale 同步逻辑
647 */
648 private syncScale;
649 /**
650 * 获得 Geometry 中的 scale 对象
651 */
652 private getGeometryScales;
653 private getScaleFields;
654 private getGroupedFields;
655 /**
656 * 调整 scale 配置
657 * @private
658 */
659 private adjustScales;
660 /**
661 * 调整分类 scale 的 range,防止超出坐标系外面
662 * @private
663 */
664 private adjustCategoryScaleRange;
665 /**
666 * 根据 options 配置、Geometry 字段配置,自动生成 components
667 * @param isUpdate 是否是更新
668 * @private
669 */
670 private initComponents;
671 private doLayout;
672 /**
673 * 创建坐标系
674 * @private
675 */
676 private createCoordinate;
677 /**
678 * 根据 options 配置自动渲染 geometry
679 * @private
680 */
681 private paintGeometries;
682 /**
683 * 最后的绘制组件
684 * @param isUpdate
685 */
686 private renderComponents;
687 /**
688 * 渲染分面,会在其中进行数据分面,然后进行子 view 创建
689 * @param isUpdate
690 */
691 private renderFacet;
692 private initOptions;
693 private createGeometry;
694 /**
695 * scale key 的创建方式
696 * @param field
697 */
698 private getScaleKey;
699}
700/**
701 * 注册 geometry 组件
702 * @param name
703 * @param Ctor
704 * @returns Geometry
705 */
706export declare function registerGeometry(name: string, Ctor: any): void;
707export default View;