UNPKG

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