Sector.ets 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. // 定义扇形组件
  2. @Component
  3. export struct Sector {
  4. @Prop radius: number; // 扇形的半径
  5. @Prop angle: number; // 扇形的角度
  6. @Prop color: string; // 扇形的颜色
  7. // 创建扇形路径的函数
  8. createSectorPath(radius: number, angle: number): string {
  9. const centerX = radius / 2; // 计算扇形中心的X坐标
  10. const centerY = radius / 2; // 计算扇形中心的Y坐标
  11. const startX = centerX; // 扇形起始点的X坐标
  12. const startY = centerY - radius; // 扇形起始点的Y坐标
  13. const halfAngle = angle / 4; // 计算半个角度
  14. // 计算扇形结束点1的坐标
  15. const endX1 = centerX + radius * Math.cos((halfAngle * Math.PI) / 180);
  16. const endY1 = centerY - radius * Math.sin((halfAngle * Math.PI) / 180);
  17. // 计算扇形结束点2的坐标
  18. const endX2 = centerX + radius * Math.cos((-halfAngle * Math.PI) / 180);
  19. const endY2 = centerY - radius * Math.sin((-halfAngle * Math.PI) / 180);
  20. // 判断是否为大弧
  21. const largeArcFlag = angle / 2 > 180 ? 1 : 0;
  22. const sweepFlag = 1; // 设置弧线方向为顺时针
  23. const pathCommands =
  24. `M${startX} ${startY} A${radius} ${radius} 0 ${largeArcFlag} ${sweepFlag} ${endX1} ${endY1} L${centerX} ${centerY} L${endX2} ${endY2} A${radius} ${radius} 0 ${largeArcFlag} ${1 -
  25. sweepFlag} ${startX} ${startY} Z`;
  26. // 生成SVG路径命令
  27. // const pathCommands =
  28. // `M${startX} ${startY} A${radius} ${radius} 0 ${largeArcFlag} ${sweepFlag} ${startX} ${startY} A${radius} ${radius} 0 ${largeArcFlag} ${1 -
  29. // sweepFlag} ${endX2} ${endY2} Z`;
  30. return pathCommands; // 返回路径命令
  31. }
  32. // 构建扇形组件
  33. build() {
  34. Stack() {
  35. // 创建第一个扇形路径
  36. Path()
  37. .width(`${this.radius}px`) // 设置宽度为半径
  38. .height(`${this.radius}px`) // 设置高度为半径
  39. .commands(this.createSectorPath(this.radius, this.angle)) // 设置路径命令
  40. .fillOpacity(1) // 设置填充透明度
  41. .fill(this.color) // 设置填充颜色
  42. // .stroke(Color.Black)
  43. .strokeWidth(0) // 设置边框宽度为0
  44. .rotate({ angle: this.angle / 4 - 90 }); // 旋转扇形
  45. // 创建第二个扇形路径
  46. Path()
  47. .width(`${this.radius}px`) // 设置宽度为半径
  48. .height(`${this.radius}px`) // 设置高度为半径
  49. .commands(this.createSectorPath(this.radius, this.angle)) // 设置路径命令
  50. .fillOpacity(1) // 设置填充透明度
  51. .fill(this.color) // 设置填充颜色
  52. // .stroke('#f8edfa')
  53. // .stroke(Color.Black)
  54. .strokeWidth(0) // 设置边框宽度为0
  55. .rotate({ angle: 180 - (this.angle / 4 - 90) }); // 旋转扇形
  56. }
  57. }
  58. }
  59. // 定义单元格类
  60. @ObservedV2
  61. export class Cell {
  62. @Trace id:number=0
  63. @Trace angle: number = 0; // 扇形的角度
  64. @Trace title: string; // 当前格子的标题
  65. @Trace color: string; // 背景颜色
  66. @Trace rotate: number = 0; // 在转盘要旋转的角度
  67. angleStart: number = 0; // 轮盘所在区间的起始
  68. angleEnd: number = 0; // 轮盘所在区间的结束
  69. proportion: number = 0; // 所占比例
  70. // 构造函数
  71. constructor(id:number,proportion: number, title: string, color: string) {
  72. this.proportion = proportion; // 设置比例
  73. this.title = title; // 设置标题
  74. this.color = color; // 设置颜色
  75. this.id=id
  76. }
  77. }