UNPKG

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