/// import { View, Chart } from '@antv/g2/lib/chart'; import { ErrorBoundaryProps } from 'react-error-boundary'; import { ViewPadding, ScaleOption, Datum, SizeAttrCallback, AdjustOption, ColorAttrCallback, ShapeAttrCallback, StyleOption, LooseObject, StyleCallback, GeometryTooltipOption, TooltipCallback, AnimateOption, LabelOption, GeometryLabelCfg, LabelCallback, StateOption } from '@antv/g2/lib/interface'; export * from '@antv/g2/lib/interface'; export { View, Chart }; export interface IEvent { target?: any; view?: View; gEvent?: any; event?: any; x?: number; y?: number; clientX?: number; clientY?: number; type?: string; [key: string]: any; } export type FieldString = string; export type ColorString = string; export type ShapeString = string; /** [min: number, max: number] */ export type SizeRange = [number, number]; export interface IChartProps extends IViewProps { forceUpdate?: boolean; /** 指定 chart 绘制的 DOM,可以传入 DOM id,也可以直接传入 dom 实例。 */ container?: HTMLElement; /** 图表宽度。 */ width?: number; /** 当数据量过大的时候,可以手动关闭此选项,避免对比数据带来的性能开销 */ notCompareData?: boolean; /** 图表高度。 */ height?: number; /** * 图表是否自适应容器宽高,默认为 false,用户需要手动设置 width 和 height。 * 当 `autoFit: true` 时,会自动取图表容器的宽高,如果用户设置了 height,那么会以用户设置的 height 为准。 */ autoFit?: boolean; /** 设置设备像素比,默认取浏览器的值 `window.devicePixelRatio`。 */ pixelRatio?: number; /** * 设置图表的内边距,使用方式参考 CSS 盒模型。 * 下图黄色区域即为 padding 的范围。 * ![](https://gw.alipayobjects.com/mdn/rms_2274c3/afts/img/A*pYwiQrdXGJ8AAAAAAAAAAABkARQnAQ) * * @example * 1. padding: 20 * 2. padding: [ 10, 30, 30 ] */ padding?: ViewPadding; /** * 是否开启局部刷新,默认开启。 */ localRefresh?: boolean; /** * chart 是否可见,默认为 true,设置为 false 则会隐藏。 */ visible?: boolean; /** * 配置图表默认交互,仅支持字符串形式。 */ defaultInteractions?: string[]; /** 是否对超出坐标系范围的 Geometry 进行剪切 */ limitInPlot?: boolean; /** 主题 */ theme?: object | string; pure?: boolean; onMousedown?: EventFunc; onMouseup?: EventFunc; onDblclick?: EventFunc; onMouseenter?: EventFunc; onMouseout?: EventFunc; onMouseover?: EventFunc; onMousemove?: EventFunc; onMouseleave?: EventFunc; onContextmenu?: EventFunc; onClick?: EventFunc; onDragstart?: EventFunc; onDrag?: EventFunc; onDragend?: EventFunc; onDragover?: EventFunc; onDragenter?: EventFunc; onDragleave?: EventFunc; onDrop?: EventFunc; onBeforerender?: EventFunc; onAfterrender?: EventFunc; onBeforepaint?: EventFunc; onAfterpaint?: EventFunc; onBeforechangedata?: EventFunc; onAfterchangedata?: EventFunc; onBeforeclear?: EventFunc; onAfterclear?: EventFunc; onBeforedestroy?: EventFunc; onTouchstart?: EventFunc; onTouchmove?: EventFunc; onTouchend?: EventFunc; events?: { [EventName: string]: EventFunc; }; /** * 发生错误的时候显示的内容, errorContent api已调整, * 请使用 ErrorBoundaryProps * @example ErrorBoundaryProps={{FallbackComponent: error =>
{error}
}} */ errorContent?: React.ReactElement>; [key: string]: any; /** * ErrorBoundary 使用的是 react-error-boundary, 可通过ErrorBoundaryProps透传其属性 * @example ErrorBoundaryProps={{FallbackComponent: error =>
{error}
}} */ ErrorBoundaryProps?: ErrorBoundaryProps; } /** * @interface IViewProps * @extends {React.ComponentPropsWithRef} */ export interface IViewProps extends React.ComponentPropsWithRef { /** 数据源配置。 */ data?: any[]; /** 列定义配置,用于配置数值的类型等,以 data 中的数据属性为 key。 */ scale?: { [field: string]: ScaleOption; }; /** * 设置图表的内边距,使用方式参考 CSS 盒模型。 * @example * 1. padding: 20 * 2. padding: [ 10, 30, 30 ] */ padding?: ViewPadding; /** view 的绘制范围,起始点为左上角。 */ region?: { start?: { x: number | string; y: number | string; }; end?: { x: number | string; y: number | string; }; }; /** * @memberof IView * 设置数据筛选规则。 * * ```ts * 3.x 写法 * value !== '杭州'], * ['value', (value: any, datum: Datum) => value > 100] * ]} /> * * value !== '杭州'], * 'value': (value: any, datum: Datum) => value > 100], * }} /> * * // 删除 'city' 字段对应的筛选规则。 * * ``` * * @param filter [string, null | ((value: any, datum: Datum) => boolean)][] 或者 Record boolean> */ filter?: [string, null | ((value: any, datum: Datum) => boolean)][] | Record boolean>; /** 其他 */ [key: string]: any; } export interface IBaseGemoProps extends React.PropsWithRef { /** 图形数据坐标 */ position: string; /** * @example * ```typescript * // data: [{ x: 'A', y: 10, color: 'red' }, { x: 'B', y: 30, color: 'yellow' }] * ; * * // 使用 '#1890ff' 颜色渲染图形 * * * // 根据 x 字段的数据值进行颜色的映射,这时候会在内部调用默认的回调函数,读取默认提供的颜色进行数据值到颜色值的映射。 * * * // 将 'x' 字段的数据值映射至指定的颜色值 colors(可以是字符串也可以是数组),此时用于通常映射分类数据 * * * // 使用回调函数进行颜色值的自定义;可以使用多个字段使用、*号连接 * { * if (xVal === 'a') { * return 'red'; * } * return 'blue'; * }]} /> * * // 指定颜色的渐变路径,用于映射连续的数据 * * ``` * @type {(ColorString | [ FieldString, ColorString | ColorString[] | ColorAttrCallback ])} * @memberof IBaseGemo */ color?: ColorString | FieldString | [FieldString, ColorString | ColorString[] | ColorAttrCallback]; /** * * @example * ```typescript * // data: [{ x: 'A', y: 10, color: 'red' }, { x: 'B', y: 30, color: 'yellow' }] * * // 指定常量,将所有数据值映射到固定的 shape * * * // 将指定的字段映射到内置的 shapes 数组中 * * * // 将指定的字段映射到指定的 shapes 数组中 * * * // 使用回调函数获取 shape,用于个性化的 shape 定制,可以根据单个或者多个字段确定 * { * if (xVal === 'a') { * return 'circle'; * } * return 'diamond'; * } * ]} /> * ``` * @type {(ShapeString | FieldString | [ShapeString, ShapeString[] | ShapeAttrCallback])} * @memberof IBaseGemo */ shape?: ShapeString | FieldString | [ShapeString, ShapeString[] | ShapeAttrCallback]; /** * * @example * ```typescript * // data: [{ x: 'A', y: 10, color: 'red' }, { x: 'B', y: 30, color: 'yellow' }] * * // 直接指定像素大小 * * * // 指定映射到 size 的字段,使用内置的默认大小范围为 [1, 10] * * * // 指定映射到 size 字段外,还提供了 size 的最大值和最小值范围 * * * // 使用回调函数映射 size,用于个性化的 size 定制,可以使用多个字段进行映射 * { * if (xVal === 'a') { * return 10; * } * return 5; * }]} /> * ``` * * @type {( number | FieldString | [FieldString, SizeAttrCallback | SizeRange])} * @memberof IBaseGemo */ size?: number | FieldString | [FieldString, SizeAttrCallback | SizeRange]; /** * 设置数据调整方式。G2 目前内置了四种类型: * 1. dodge * 2. stack * 3. symmetric * 4. jitter * * **Tip** * + 对于 'dodge' 类型,可以额外进行如下属性的配置: * ```typescript * * ``` * * @example * ```typescript * * * * * // 组合使用 adjust * * ``` * * @type {('dodge' | 'stack' | 'symmetric' | 'jitter' | string | string[] | AdjustOption | AdjustOption[])} * @memberof IBaseGemo */ adjust?: 'dodge' | 'stack' | 'symmetric' | 'jitter' | string | AdjustOption | AdjustOption[]; /** * * 图形样式配置。 * * @example * ```typescript * // 配置图形样式 * * * // 根据具体的数据进行详细配置 * { * const style = { lineWidth: 2, stroke: '#1890ff' }; * if (xVal === 'a') { * style.lineDash = [ 2, 2 ]; * } * return style; * }, * }} /> * ``` * ```typescript * // 根据具体的数据进行详细配置 [FieldString, StyleCallback] * { * const style = { lineWidth: 2, stroke: '#1890ff' }; * if (xVal === 'a') { * style.lineDash = [ 2, 2 ]; * } * return style; * }]} /> * ``` * * @type {(StyleOption | LooseObject | [FieldString, StyleCallback])} * @memberof IBaseGemo */ style?: StyleOption | LooseObject | [FieldString, StyleCallback]; /** * 配置 Geometry 显示的 tooltip 内容。 * * `tooltip={false}` 代表关闭 tooltip。 * `tooltip={true}` 代表开启 tooltip。 * * Geometry 默认允许 tooltip 展示,我们可以使用以下方法对 tooltip 的展示内容进行配置: * * @example * ```typescript * // data: [{x: 'a', y: 10}] * * ``` * * ```typescript * * ``` * 方法同样支持数据映射及回调用法: * * @example * ```typescript * * * { * return { * x, * y, * }; * }, * }} /> * // 等同于 * { * return { * x, * y, * }; * }]} /> * * 其返回的值必须为对象,该值中的属性同 Tooltip 的 itemTpl 模板相对应,返回的变量可用于 itemTpl 的字符串模板。 * * * @type {(GeometryTooltipOption | boolean | FieldString | [FieldString, TooltipCallback])} * @memberof IBaseGemo */ tooltip?: GeometryTooltipOption | boolean | FieldString | [FieldString, TooltipCallback]; /** * Geometry 动画配置。 * * + `` 关闭动画 * + `` 开启动画,默认开启。 * * 我们将动画分为四个场景: * 1. appear: 图表第一次加载时的入场动画; * 2. enter: 图表绘制完成,发生更新后,产生的新图形的进场动画; * 3. update: 图表绘制完成,数据发生变更后,有状态变更的图形的更新动画; * 4. leave: 图表绘制完成,数据发生变更后,被销毁图形的销毁动画。 * * @example * ```typescript * ; * ``` * * * @type {(AnimateOption | boolean)} * @memberof IBaseGemo */ animate?: AnimateOption | boolean; /** * * Geometry label 配置。 * * @example * ```ts * // data: [ {x: 1, y: 2, z: 'a'}, {x: 2, y: 2, z: 'b'} ] * * * * * @type {(LabelOption | false | FieldString | [FieldString, GeometryLabelCfg | LabelCallback] | [FieldString, LabelCallback, GeometryLabelCfg])} * @memberof IBaseGemo */ label?: LabelOption | false | FieldString | [FieldString, GeometryLabelCfg | LabelCallback] | [FieldString, LabelCallback, GeometryLabelCfg]; /** *设置状态对应的样式。 * * @example * ```ts * * ``` * * 如果图形 shape 是由多个 shape 组成,即为一个 G.Group 对象,那么针对 group 中的每个 shape,我们需要使用下列方式进行状态样式设置: * 如果我们为 group 中的每个 shape 设置了 'name' 属性(shape.set('name', 'xx')),则以 'name' 作为 key,否则默认以索引值(即 shape 的 添加顺序)为 key。 * * ```ts * * ``` * * @type {StateOption} * @memberof IBaseGemo */ state?: StateOption; /** * 用来设置默认选中的图形元素,或者做条件样式。 * 在图表绘制后执行。 * { * elements.forEach(ele => { * const data = elements.data; * if (data.id = 'xx') { * ele.setState('selected', true); // 默认选中 * } * }) * }} /> * @type (elements:Element[]) => {} * @memberof IBaseGemoProps */ setElements?: (elements: Element[]) => {}; [key: string]: any; } export type EventFunc = (IEvent: any) => any;