// 定义扇形组件 @Component export struct Sector { @Prop radius: number; // 扇形的半径 @Prop angle: number; // 扇形的角度 @Prop color: string; // 扇形的颜色 // 创建扇形路径的函数 createSectorPath(radius: number, angle: number): string { const centerX = radius / 2; // 计算扇形中心的X坐标 const centerY = radius / 2; // 计算扇形中心的Y坐标 const startX = centerX; // 扇形起始点的X坐标 const startY = centerY - radius; // 扇形起始点的Y坐标 const halfAngle = angle / 4; // 计算半个角度 // 计算扇形结束点1的坐标 const endX1 = centerX + radius * Math.cos((halfAngle * Math.PI) / 180); const endY1 = centerY - radius * Math.sin((halfAngle * Math.PI) / 180); // 计算扇形结束点2的坐标 const endX2 = centerX + radius * Math.cos((-halfAngle * Math.PI) / 180); const endY2 = centerY - radius * Math.sin((-halfAngle * Math.PI) / 180); // 判断是否为大弧 const largeArcFlag = angle / 2 > 180 ? 1 : 0; const sweepFlag = 1; // 设置弧线方向为顺时针 const pathCommands = `M${startX} ${startY} A${radius} ${radius} 0 ${largeArcFlag} ${sweepFlag} ${endX1} ${endY1} L${centerX} ${centerY} L${endX2} ${endY2} A${radius} ${radius} 0 ${largeArcFlag} ${1 - sweepFlag} ${startX} ${startY} Z`; // 生成SVG路径命令 // const pathCommands = // `M${startX} ${startY} A${radius} ${radius} 0 ${largeArcFlag} ${sweepFlag} ${startX} ${startY} A${radius} ${radius} 0 ${largeArcFlag} ${1 - // sweepFlag} ${endX2} ${endY2} Z`; return pathCommands; // 返回路径命令 } // 构建扇形组件 build() { Stack() { // 创建第一个扇形路径 Path() .width(`${this.radius}px`) // 设置宽度为半径 .height(`${this.radius}px`) // 设置高度为半径 .commands(this.createSectorPath(this.radius, this.angle)) // 设置路径命令 .fillOpacity(1) // 设置填充透明度 .fill(this.color) // 设置填充颜色 // .stroke(Color.Black) .strokeWidth(0) // 设置边框宽度为0 .rotate({ angle: this.angle / 4 - 90 }); // 旋转扇形 // 创建第二个扇形路径 Path() .width(`${this.radius}px`) // 设置宽度为半径 .height(`${this.radius}px`) // 设置高度为半径 .commands(this.createSectorPath(this.radius, this.angle)) // 设置路径命令 .fillOpacity(1) // 设置填充透明度 .fill(this.color) // 设置填充颜色 // .stroke('#f8edfa') // .stroke(Color.Black) .strokeWidth(0) // 设置边框宽度为0 .rotate({ angle: 180 - (this.angle / 4 - 90) }); // 旋转扇形 } } } // 定义单元格类 @ObservedV2 export class Cell { @Trace id:number=0 @Trace angle: number = 0; // 扇形的角度 @Trace title: string; // 当前格子的标题 @Trace color: string; // 背景颜色 @Trace rotate: number = 0; // 在转盘要旋转的角度 angleStart: number = 0; // 轮盘所在区间的起始 angleEnd: number = 0; // 轮盘所在区间的结束 proportion: number = 0; // 所占比例,方便后续迭代,初始每份给1就行 // 构造函数 constructor(id:number,proportion: number, title: string, color: string) { this.proportion = proportion; // 设置比例 this.title = title; // 设置标题 this.color = color; // 设置颜色 this.id=id } }