UNPKG

16.1 kBTypeScriptView Raw
1/// <reference types="react" />
2import { View, Chart } from '@antv/g2/lib/chart';
3import { ErrorBoundaryProps } from 'react-error-boundary';
4import { ViewPadding, ScaleOption, Datum, SizeAttrCallback, AdjustOption, ColorAttrCallback, ShapeAttrCallback, StyleOption, LooseObject, StyleCallback, GeometryTooltipOption, TooltipCallback, AnimateOption, LabelOption, GeometryLabelCfg, LabelCallback, StateOption } from '@antv/g2/lib/interface';
5export * from '@antv/g2/lib/interface';
6export { View, Chart };
7export interface IEvent {
8 target?: any;
9 view?: View;
10 gEvent?: any;
11 event?: any;
12 x?: number;
13 y?: number;
14 clientX?: number;
15 clientY?: number;
16 type?: string;
17 [key: string]: any;
18}
19export declare type FieldString = string;
20export declare type ColorString = string;
21export declare type ShapeString = string;
22/** [min: number, max: number] */
23export declare type SizeRange = [number, number];
24export interface IChartProps extends IViewProps {
25 forceUpdate?: boolean;
26 /** 指定 chart 绘制的 DOM,可以传入 DOM id,也可以直接传入 dom 实例。 */
27 container?: HTMLElement;
28 /** 图表宽度。 */
29 width?: number;
30 /** 当数据量过大的时候,可以手动关闭此选项,避免对比数据带来的性能开销 */
31 notCompareData?: boolean;
32 /** 图表高度。 */
33 height?: number;
34 /**
35 * 图表是否自适应容器宽高,默认为 false,用户需要手动设置 width 和 height。
36 * 当 `autoFit: true` 时,会自动取图表容器的宽高,如果用户设置了 height,那么会以用户设置的 height 为准。
37 */
38 autoFit?: boolean;
39 /** 设置设备像素比,默认取浏览器的值 `window.devicePixelRatio`。 */
40 pixelRatio?: number;
41 /**
42 * 设置图表的内边距,使用方式参考 CSS 盒模型。
43 * 下图黄色区域即为 padding 的范围。
44 * ![](https://gw.alipayobjects.com/mdn/rms_2274c3/afts/img/A*pYwiQrdXGJ8AAAAAAAAAAABkARQnAQ)
45 *
46 * @example
47 * 1. padding: 20
48 * 2. padding: [ 10, 30, 30 ]
49 */
50 padding?: ViewPadding;
51 /**
52 * 是否开启局部刷新,默认开启。
53 */
54 localRefresh?: boolean;
55 /**
56 * chart 是否可见,默认为 true,设置为 false 则会隐藏。
57 */
58 visible?: boolean;
59 /**
60 * 配置图表默认交互,仅支持字符串形式。
61 */
62 defaultInteractions?: string[];
63 /** 是否对超出坐标系范围的 Geometry 进行剪切 */
64 limitInPlot?: boolean;
65 /** 主题 */
66 theme?: object | string;
67 pure?: boolean;
68 onMousedown?: EventFunc;
69 onMouseup?: EventFunc;
70 onDblclick?: EventFunc;
71 onMouseenter?: EventFunc;
72 onMouseout?: EventFunc;
73 onMouseover?: EventFunc;
74 onMousemove?: EventFunc;
75 onMouseleave?: EventFunc;
76 onContextmenu?: EventFunc;
77 onClick?: EventFunc;
78 onDragstart?: EventFunc;
79 onDrag?: EventFunc;
80 onDragend?: EventFunc;
81 onDragover?: EventFunc;
82 onDragenter?: EventFunc;
83 onDragleave?: EventFunc;
84 onDrop?: EventFunc;
85 onBeforerender?: EventFunc;
86 onAfterrender?: EventFunc;
87 onBeforepaint?: EventFunc;
88 onAfterpaint?: EventFunc;
89 onBeforechangedata?: EventFunc;
90 onAfterchangedata?: EventFunc;
91 onBeforeclear?: EventFunc;
92 onAfterclear?: EventFunc;
93 onBeforedestroy?: EventFunc;
94 onTouchstart?: EventFunc;
95 onTouchmove?: EventFunc;
96 onTouchend?: EventFunc;
97 events?: {
98 [EventName: string]: EventFunc;
99 };
100 /**
101 * 发生错误的时候显示的内容, errorContent api已调整,
102 * 请使用 ErrorBoundaryProps
103 * @example ErrorBoundaryProps={{FallbackComponent: error => <div>{error}</div>}}
104 */
105 errorContent?: React.ReactElement<unknown, string | React.FunctionComponent<{}>>;
106 [key: string]: any;
107 /**
108 * ErrorBoundary 使用的是 react-error-boundary, 可通过ErrorBoundaryProps透传其属性
109 * @example ErrorBoundaryProps={{FallbackComponent: error => <div>{error}</div>}}
110 */
111 ErrorBoundaryProps?: ErrorBoundaryProps;
112}
113/**
114 * @interface IViewProps
115 * @extends {React.ComponentPropsWithRef<any>}
116 */
117export interface IViewProps extends React.ComponentPropsWithRef<any> {
118 /** 数据源配置。 */
119 data?: any[];
120 /** 列定义配置,用于配置数值的类型等,以 data 中的数据属性为 key。 */
121 scale?: {
122 [field: string]: ScaleOption;
123 };
124 /**
125 * 设置图表的内边距,使用方式参考 CSS 盒模型。
126 * @example
127 * 1. padding: 20
128 * 2. padding: [ 10, 30, 30 ]
129 */
130 padding?: ViewPadding;
131 /** view 的绘制范围,起始点为左上角。 */
132 region?: {
133 start?: {
134 x: number | string;
135 y: number | string;
136 };
137 end?: {
138 x: number | string;
139 y: number | string;
140 };
141 };
142 /**
143 * @memberof IView
144 * 设置数据筛选规则。
145 *
146 * ```ts
147 * 3.x 写法
148 * <View filter={[
149 * ['city', (value: any, datum: Datum) => value !== '杭州'],
150 * ['value', (value: any, datum: Datum) => value > 100]
151 * ]} />
152 *
153 * <View filter={{
154 * 'city': (value: any, datum: Datum) => value !== '杭州'],
155 * 'value': (value: any, datum: Datum) => value > 100],
156 * }} />
157 *
158 * // 删除 'city' 字段对应的筛选规则。
159 * <View filter={{
160 * 'city': null
161 * }} />
162 * ```
163 *
164 * @param filter [string, null | ((value: any, datum: Datum) => boolean)][] 或者 Record<string, (value: any, datum: Datum) => boolean>
165 */
166 filter?: [string, null | ((value: any, datum: Datum) => boolean)][] | Record<string, (value: any, datum: Datum) => boolean>;
167 /** 其他 */
168 [key: string]: any;
169}
170export interface IBaseGemoProps extends React.PropsWithRef<any> {
171 /** 图形数据坐标 */
172 position: string;
173 /**
174 * @example
175 * ```typescript
176 * // data: [{ x: 'A', y: 10, color: 'red' }, { x: 'B', y: 30, color: 'yellow' }]
177 * <Geomy color={{
178 * fields: [ 'x' ],
179 * values: [ '#1890ff', '#5AD8A6' ],
180 * }} />;
181 *
182 * // 使用 '#1890ff' 颜色渲染图形
183 * <Geom color="#1890ff" />
184 *
185 * // 根据 x 字段的数据值进行颜色的映射,这时候会在内部调用默认的回调函数,读取默认提供的颜色进行数据值到颜色值的映射。
186 * <Geom color="x" />
187 *
188 * // 将 'x' 字段的数据值映射至指定的颜色值 colors(可以是字符串也可以是数组),此时用于通常映射分类数据
189 * <Geom color={['x', [ '#1890ff', '#5AD8A6' ]]} />
190 *
191 * // 使用回调函数进行颜色值的自定义;可以使用多个字段使用、*号连接
192 * <Geom color={['x', (xVal) => {
193 * if (xVal === 'a') {
194 * return 'red';
195 * }
196 * return 'blue';
197 * }]} />
198 *
199 * // 指定颜色的渐变路径,用于映射连续的数据
200 * <Geom color={['x', '#BAE7FF-#1890FF-#0050B3']} />
201 * ```
202 * @type {(ColorString | [ FieldString, ColorString | ColorString[] | ColorAttrCallback ])}
203 * @memberof IBaseGemo
204 */
205 color?: ColorString | FieldString | [FieldString, ColorString | ColorString[] | ColorAttrCallback];
206 /**
207 *
208 * @example
209 * ```typescript
210 * // data: [{ x: 'A', y: 10, color: 'red' }, { x: 'B', y: 30, color: 'yellow' }]
211 *
212 * // 指定常量,将所有数据值映射到固定的 shape
213 * <Geom shape="circle" />
214 *
215 * // 将指定的字段映射到内置的 shapes 数组中
216 * <Geom shape="x" />
217 *
218 * // 将指定的字段映射到指定的 shapes 数组中
219 * <Geom shape={['x', [ 'circle', 'diamond', 'square' ]]} />
220 *
221 * // 使用回调函数获取 shape,用于个性化的 shape 定制,可以根据单个或者多个字段确定
222 * <Geom shape={[
223 * 'x',
224 * (xVal) => {
225 * if (xVal === 'a') {
226 * return 'circle';
227 * }
228 * return 'diamond';
229 * }
230 * ]} />
231 * ```
232 * @type {(ShapeString | FieldString | [ShapeString, ShapeString[] | ShapeAttrCallback])}
233 * @memberof IBaseGemo
234 */
235 shape?: ShapeString | FieldString | [ShapeString, ShapeString[] | ShapeAttrCallback];
236 /**
237 *
238 * @example
239 * ```typescript
240 * // data: [{ x: 'A', y: 10, color: 'red' }, { x: 'B', y: 30, color: 'yellow' }]
241 *
242 * // 直接指定像素大小
243 * <Geom size={10} />
244 *
245 * // 指定映射到 size 的字段,使用内置的默认大小范围为 [1, 10]
246 * <Geom size="x" />
247 *
248 * // 指定映射到 size 字段外,还提供了 size 的最大值和最小值范围
249 * <Geom size={['x', [ 5, 30 ]]} />
250 *
251 * // 使用回调函数映射 size,用于个性化的 size 定制,可以使用多个字段进行映射
252 * <Geom size={['x', (xVal) => {
253 * if (xVal === 'a') {
254 * return 10;
255 * }
256 * return 5;
257 * }]} />
258 * ```
259 *
260 * @type {( number | FieldString | [FieldString, SizeAttrCallback | SizeRange])}
261 * @memberof IBaseGemo
262 */
263 size?: number | FieldString | [FieldString, SizeAttrCallback | SizeRange];
264 /**
265 * 设置数据调整方式。G2 目前内置了四种类型:
266 * 1. dodge
267 * 2. stack
268 * 3. symmetric
269 * 4. jitter
270 *
271 * **Tip**
272 * + 对于 'dodge' 类型,可以额外进行如下属性的配置:
273 * ```typescript
274 * <Geom adjust={['dodge', {
275 * marginRatio: 0, // 取 0 到 1 范围的值(相对于每个柱子宽度),用于控制一个分组中柱子之间的间距
276 * dodgeBy: 'x', // 该属性只对 'dodge' 类型生效,声明以哪个数据字段为分组依据
277 * }]}
278 * ```
279 *
280 * + 对于 'stack' 类型,可以额外进行如下属性的配置:
281 * ```typescript
282 * <Geom adjust={{
283 * type: 'stack',
284 * reverseOrder: false, // 用于控制是否对数据进行反序操作
285 * }} />
286 * ```
287 *
288 * @example
289 * ```typescript
290 * <Geom adjust="stack" />
291 *
292 * <Geom adjust={{
293 * type: 'stack',
294 * reverseOrder: false,
295 * }} />
296 *
297 * // 组合使用 adjust
298 * <Geom adjust={[
299 * { type: 'stack' },
300 * { type: 'dodge', dodgeBy: 'x' },
301 * ]} />
302 * ```
303 *
304 * @type {('dodge' | 'stack' | 'symmetric' | 'jitter' | string | string[] | AdjustOption | AdjustOption[])}
305 * @memberof IBaseGemo
306 */
307 adjust?: 'dodge' | 'stack' | 'symmetric' | 'jitter' | string | AdjustOption | AdjustOption[];
308 /**
309 *
310 * 图形样式配置。
311 *
312 * @example
313 * ```typescript
314 * // 配置图形样式
315 * <Geom style={{
316 * lineWidth: 2,
317 * stroke: '#1890ff',
318 * }} />
319 *
320 * // 根据具体的数据进行详细配置
321 * <Geom style={{
322 * fields: [ 'x', 'y' ], // 数据字段
323 * callback: (xVal, yVal) => {
324 * const style = { lineWidth: 2, stroke: '#1890ff' };
325 * if (xVal === 'a') {
326 * style.lineDash = [ 2, 2 ];
327 * }
328 * return style;
329 * },
330 * }} />
331 * ```
332 * ```typescript
333 * // 根据具体的数据进行详细配置 [FieldString, StyleCallback]
334 * <Geom style={['x*y', (xVal, yVal) => {
335 * const style = { lineWidth: 2, stroke: '#1890ff' };
336 * if (xVal === 'a') {
337 * style.lineDash = [ 2, 2 ];
338 * }
339 * return style;
340 * }]} />
341 * ```
342 *
343 * @type {(StyleOption | LooseObject | [FieldString, StyleCallback])}
344 * @memberof IBaseGemo
345 */
346 style?: StyleOption | LooseObject | [FieldString, StyleCallback];
347 /**
348 * 配置 Geometry 显示的 tooltip 内容。
349 *
350 * `tooltip={false}` 代表关闭 tooltip。
351 * `tooltip={true}` 代表开启 tooltip。
352 *
353 * Geometry 默认允许 tooltip 展示,我们可以使用以下方法对 tooltip 的展示内容进行配置:
354 *
355 * @example
356 * ```typescript
357 * // data: [{x: 'a', y: 10}]
358 * <Geom tooltip="x" />
359 * ```
360 *
361 * ```typescript
362 * <Geom tooltip={{
363 * fields: [ 'x', 'y' ],
364 * }} />
365 * ```
366 * 方法同样支持数据映射及回调用法:
367 *
368 * @example
369 * ```typescript
370 * <Tooltip itemTpl="<li>{x}: {y}</li>" />
371 *
372 * <Line tooltip={{
373 * fields: [ 'x', 'y' ],
374 * callback: (x, y) => {
375 * return {
376 * x,
377 * y,
378 * };
379 * },
380 * }} />
381 * // 等同于
382 * <Line tooltip={['x*y', (x, y) => {
383 * return {
384 * x,
385 * y,
386 * };
387 * }]} />
388 *
389 * 其返回的值必须为对象,该值中的属性同 Tooltip 的 itemTpl 模板相对应,返回的变量可用于 itemTpl 的字符串模板。
390 *
391 *
392 * @type {(GeometryTooltipOption | boolean | FieldString | [FieldString, TooltipCallback])}
393 * @memberof IBaseGemo
394 */
395 tooltip?: GeometryTooltipOption | boolean | FieldString | [FieldString, TooltipCallback];
396 /**
397 * Geometry 动画配置。
398 *
399 * + `<Geom animate={false} />` 关闭动画
400 * + `<Geom animate />` 开启动画,默认开启。
401 *
402 * 我们将动画分为四个场景:
403 * 1. appear: 图表第一次加载时的入场动画;
404 * 2. enter: 图表绘制完成,发生更新后,产生的新图形的进场动画;
405 * 3. update: 图表绘制完成,数据发生变更后,有状态变更的图形的更新动画;
406 * 4. leave: 图表绘制完成,数据发生变更后,被销毁图形的销毁动画。
407 *
408 * @example
409 * ```typescript
410 * <Geom animate={{
411 * enter: {
412 * duration: 1000, // enter 动画执行时间
413 * },
414 * leave: false, // 关闭 leave 销毁动画
415 * }} />;
416 * ```
417 *
418 *
419 * @type {(AnimateOption | boolean)}
420 * @memberof IBaseGemo
421 */
422 animate?: AnimateOption | boolean;
423 /**
424 *
425 * Geometry label 配置。
426 *
427 * @example
428 * ```ts
429 * // data: [ {x: 1, y: 2, z: 'a'}, {x: 2, y: 2, z: 'b'} ]
430 *
431 * <Interval label="y" />
432 *
433
434 *
435 * @type {(LabelOption | false | FieldString | [FieldString, GeometryLabelCfg | LabelCallback] | [FieldString, LabelCallback, GeometryLabelCfg])}
436 * @memberof IBaseGemo
437 */
438 label?: LabelOption | false | FieldString | [FieldString, GeometryLabelCfg | LabelCallback] | [FieldString, LabelCallback, GeometryLabelCfg];
439 /**
440 *设置状态对应的样式。
441 *
442 * @example
443 * ```ts
444 * <Geom state={{
445 * selected: {
446 * animate: { duration: 100, easing: 'easeLinear' },
447 * style: {
448 * lineWidth: 2,
449 * stroke: '#000',
450 * },
451 * },
452 * }} />
453 * ```
454 *
455 * 如果图形 shape 是由多个 shape 组成,即为一个 G.Group 对象,那么针对 group 中的每个 shape,我们需要使用下列方式进行状态样式设置:
456 * 如果我们为 group 中的每个 shape 设置了 'name' 属性(shape.set('name', 'xx')),则以 'name' 作为 key,否则默认以索引值(即 shape 的 添加顺序)为 key。
457 *
458 * ```ts
459 * <Geom state={{
460 * selected: {
461 * style: {
462 * 0: { lineWidth: 2 },
463 * 1: { fillOpacity: 1 },
464 * }
465 * }
466 * }} />
467 * ```
468 *
469 * @type {StateOption}
470 * @memberof IBaseGemo
471 */
472 state?: StateOption;
473 /**
474 * 用来设置默认选中的图形元素,或者做条件样式。
475 * 在图表绘制后执行。
476 * <Geom setElements={(elements) => {
477 * elements.forEach(ele => {
478 * const data = elements.data;
479 * if (data.id = 'xx') {
480 * ele.setState('selected', true); // 默认选中
481 * }
482 * })
483 * }} />
484 * @type (elements:Element[]) => {}
485 * @memberof IBaseGemoProps
486 */
487 setElements?: (elements: Element[]) => {};
488 [key: string]: any;
489}
490export declare type EventFunc = (IEvent: any) => any;