图形标签 - Shape
上一篇
坐标系
下一篇
绘图属性 - Style
Loading...
F2 底层使用了 G 绘图引擎。本篇列出了常见的图形标签。
详见:图形使用
所有图形标签支持的通用属性:
| 属性 | 类型 | 描述 |
|---|---|---|
className | string | 对象标记,由用户指定 |
visible | boolean | 显示或隐藏图形 |
zIndex | number | z-index 值,用于调整绘制顺序 |
style | Style | 图形样式 |
animation | Animation | 图形动画 |
onPan 等 | Event | 图形事件 |
更多详情:绘图属性
更多详情:图形动画属性
更多详情:图形事件属性
包含一组图形,用于图形分组管理。
<group className="my-group"><rect style={{ x: 10, y: 10, width: 50, height: 50, fill: 'red' }} /><rect style={{ x: 70, y: 10, width: 50, height: 50, fill: 'blue' }} /></group>
矩形,用于绘制矩形区域。
| 属性 | 类型 | 描述 | 默认值 |
|---|---|---|---|
x | number | 左上角 x 坐标 | 0 |
y | number | 左上角 y 坐标 | 0 |
width | number | 宽度 | 0 |
height | number | 高度 | 0 |
radius | number | number[] | 圆角半径 | 0 |
// 基础矩形<rectstyle={{x: 100,y: 100,width: 50,height: 50,lineWidth: '2px',stroke: '#000',fill: 'red',}}/>
// 统一圆角<rectstyle={{x: 100,y: 100,width: 100,height: 50,radius: 10,fill: 'blue',}}/>// 分别设置每个角<rectstyle={{x: 100,y: 100,width: 100,height: 50,radius: [10, 20, 30, 40], // [top-left, top-right, bottom-right, bottom-left]fill: 'green',}}/>
圆形,用于绘制圆形区域。
| 属性 | 类型 | 描述 | 默认值 |
|---|---|---|---|
cx | number | 圆心 x 坐标 | 0 |
cy | number | 圆心 y 坐标 | 0 |
r | number | 圆的半径 | 0 |
<circlestyle={{cx: 100,cy: 100,r: 50,lineWidth: '2px',stroke: '#000',fill: 'red',}}/>
扇形,用于绘制饼图、环形图等。
| 属性 | 类型 | 描述 | 默认值 |
|---|---|---|---|
cx | number | 圆心 x 坐标 | 0 |
cy | number | 圆心 y 坐标 | 0 |
r | number | 外半径 | 0 |
r0 | number | 内半径 | 0 |
startAngle | number | string | 起始角度/弧度 | 0 |
endAngle | number | string | 结束角度/弧度 | 0 |
anticlockwise | boolean | 是否逆时针方向 | false |
// 使用弧度<sectorstyle={{cx: 100,cy: 100,r: 50,startAngle: 0,endAngle: Math.PI / 2,fill: 'red',}}/>// 使用角度<sectorstyle={{cx: 100,cy: 100,r: 50,startAngle: '0 deg',endAngle: '90 deg',fill: 'blue',}}/>
<sectorstyle={{cx: 100,cy: 100,r: 50,r0: 30,startAngle: 0,endAngle: Math.PI,fill: 'green',}}/>
多边形,用于绘制任意多边形。
| 属性 | 类型 | 描述 | 默认值 |
|---|---|---|---|
points | [number, number][] | 多边形的顶点坐标数组 | [] |
// 三角形<polygonstyle={{points: [[50, 10],[90, 90],[10, 90],],lineWidth: '2px',stroke: '#000',fill: 'red',}}/>
// 五边形<polygonstyle={{points: [[50, 10],[90, 40],[75, 90],[25, 90],[10, 40],],lineWidth: '2px',stroke: '#000',fill: 'blue',}}/>
直线,用于绘制两点之间的线段。
| 属性 | 类型 | 描述 | 默认值 |
|---|---|---|---|
x1 | number | 起始点 x 坐标 | 0 |
y1 | number | 起始点 y 坐标 | 0 |
x2 | number | 结束点 x 坐标 | 0 |
y2 | number | 结束点 y 坐标 | 0 |
<linestyle={{x1: 10,y1: 10,x2: 100,y2: 100,lineWidth: '2px',stroke: '#000',}}/>
<linestyle={{x1: 10,y1: 10,x2: 100,y2: 100,lineWidth: '2px',stroke: '#000',lineDash: [5, 5],}}/>
圆弧,用于绘制圆弧形曲线。
| 属性 | 类型 | 描述 | 默认值 |
|---|---|---|---|
cx | number | 圆心 x 坐标 | 0 |
cy | number | 圆心 y 坐标 | 0 |
r | number | 半径 | 0 |
startAngle | number | string | 起始角度/弧度 | 0 |
endAngle | number | string | 结束角度/弧度 | 0 |
anticlockwise | boolean | 是否逆时针方向 | false |
<arcstyle={{cx: 100,cy: 100,r: 50,startAngle: 0,endAngle: Math.PI,lineWidth: '2px',stroke: '#000',}}/>
多点线段,用于绘制连续的折线。
| 属性 | 类型 | 描述 | 默认值 |
|---|---|---|---|
points | [number, number][] | 线段的点坐标数组 | [] |
smooth | boolean | 是否平滑曲线 | false |
// 折线<polylinestyle={{points: [[10, 10],[50, 50],[80, 70],],lineWidth: '2px',stroke: '#000',}}/>
<polylinestyle={{points: [[10, 10],[50, 50],[80, 70],[100, 30],],lineWidth: '2px',stroke: '#000',smooth: true,}}/>
文本,用于显示文字。
| 属性 | 类型 | 描述 | 默认值 |
|---|---|---|---|
x | number | 文本 x 坐标 | 0 |
y | number | 文本 y 坐标 | 0 |
text | string | 文本内容 | '' |
textAlign | string | 文本水平对齐方式 | 'start' |
textBaseline | string | 文本垂直基线 | 'alphabetic' |
fontStyle | string | 字体样式 | 'normal' |
fontSize | number | 字号(像素) | 12 |
fontFamily | string | 字体系列 | 'sans-serif' |
fontWeight | string | 字体粗细 | 'normal' |
fontVariant | string | 字体变体 | 'normal' |
lineHeight | number | 行高(像素) | - |
'start' - 默认,文本从指定位置开始'center' - 文本居中对齐'end' - 文本从指定位置结束'left' - 文本左对齐'right' - 文本右对齐'top' - 文本顶部对齐'hanging' - 悬挂基线'middle' - 文本垂直居中'alphabetic' - 默认,字母基线'ideographic' - 表意基线'bottom' - 文本底部对齐// 简单文本<textstyle={{x: 100,y: 100,text: 'Hello F2',fontSize: 20,fill: '#000',}}/>
// 居中文本<textstyle={{x: 150,y: 100,text: '居中文本',fontSize: 16,textAlign: 'center',textBaseline: 'middle',fill: '#000',}}/>
// 粗体斜体<textstyle={{x: 100,y: 100,text: '粗体斜体',fontSize: 18,fontWeight: 'bold',fontStyle: 'italic',fill: '#000',}}/>
图片,用于显示图像。
| 属性 | 类型 | 描述 | 默认值 |
|---|---|---|---|
x | number | 左上角 x 坐标 | 0 |
y | number | 左上角 y 坐标 | 0 |
width | number | 宽度 | 0 |
height | number | 高度 | 0 |
src | string | 图片 URL | '' |
cacheImage | boolean | 是否缓存图片(解决闪动问题) | false |
<imagestyle={{src: 'https://f2.antv.vision/favicon-32x32.png',x: 10,y: 10,width: 32,height: 32,}}/>
// 如果图片有闪动,可以开启缓存<imagestyle={{src: 'https://example.com/image.png',x: 10,y: 10,width: 100,height: 100,cacheImage: true,}}/>
interface ShapeProps {className?: stringvisible?: booleanzIndex?: numberstyle?: ShapeStyleanimation?: Animation// 事件处理器onPan?: (event: Event) => voidonTap?: (event: Event) => voidonPress?: (event: Event) => void// ... 其他事件}interface RectStyle {x?: numbery?: numberwidth?: numberheight?: numberradius?: number | number[]fill?: stringstroke?: stringlineWidth?: number | stringlineDash?: number[]// ... 通用样式属性}interface CircleStyle {cx?: numbercy?: numberr?: numberfill?: stringstroke?: stringlineWidth?: number | string// ... 通用样式属性}interface SectorStyle {cx?: numbercy?: numberr?: numberr0?: numberstartAngle?: number | stringendAngle?: number | stringanticlockwise?: booleanfill?: stringstroke?: string// ... 通用样式属性}interface PolygonStyle {points?: [number, number][]fill?: stringstroke?: stringlineWidth?: number | string// ... 通用样式属性}interface LineStyle {x1?: numbery1?: numberx2?: numbery2?: numberstroke?: stringlineWidth?: number | stringlineDash?: number[]// ... 通用样式属性}interface ArcStyle {cx?: numbercy?: numberr?: numberstartAngle?: number | stringendAngle?: number | stringanticlockwise?: booleanstroke?: stringlineWidth?: number | string// ... 通用样式属性}interface PolylineStyle {points?: [number, number][]stroke?: stringlineWidth?: number | stringsmooth?: booleanlineDash?: number[]// ... 通用样式属性}interface TextStyle {x?: numbery?: numbertext?: stringtextAlign?: 'start' | 'center' | 'end' | 'left' | 'right'textBaseline?: 'top' | 'hanging' | 'middle' | 'alphabetic' | 'ideographic' | 'bottom'fontStyle?: 'normal' | 'italic' | 'oblique'fontSize?: numberfontFamily?: stringfontWeight?: 'normal' | 'bold' | 'bolder' | 'lighter' | '100' | '200' | '300' | '400' | '500' | '600' | '700' | '800' | '900'fontVariant?: 'normal' | 'small-caps'lineHeight?: numberfill?: string// ... 通用样式属性}interface ImageStyle {x?: numbery?: numberwidth?: numberheight?: numbersrc?: stringcacheImage?: boolean}
使用 stroke 和 lineWidth 属性:
<rectstyle={{x: 10,y: 10,width: 100,height: 50,fill: 'blue',stroke: 'red',lineWidth: 2,}}/>
使用 lineDash 属性:
<linestyle={{x1: 10,y1: 10,x2: 100,y2: 10,stroke: '#000',lineWidth: 2,lineDash: [5, 5], // 5px 实线,5px 空白}}/>
使用 fillOpacity 或 strokeOpacity 属性:
<circlestyle={{cx: 100,cy: 100,r: 50,fill: 'red',fillOpacity: 0.5,}}/>
支持两种方式:
// 方式 1: 弧度值(推荐)<sectorstyle={{startAngle: 0,endAngle: Math.PI / 2, // 90 度}}/>// 方式 2: 角度字符串<sectorstyle={{startAngle: '0 deg',endAngle: '90 deg',}}/>