All files / src/components/graph/SVG/PieSegment PieSegment.tsx

0% Statements 0/20
0% Branches 0/4
0% Functions 0/3
0% Lines 0/20

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123                                                                                                                                                                                                                                                     
import React from "react";
import { guidGenerator } from "@/util/id";
import { Point } from "@/objects/interfaces/graph/Point";
 
export interface PieSegmentProps {
  fillColor: string;
  strokeColor: string;
  start: number;
  delta: number;
  rIn: number;
  rOut: number;
  centre: Point;
  label: string;
  highlight: boolean;
}
 
export class PieSegment extends React.Component {
  props: PieSegmentProps;
 
  constructor(props: PieSegmentProps) {
    super(props);
    this.props = props;
  }
 
  generatePathDef(
    centre: Point,
    rIn: number,
    rOut: number,
    start: number,
    delta: number
  ) {
    const endRad = start + delta;
 
    const startOut = {
      x: centre.x + rOut * Math.cos(start),
      y: centre.y + rOut * Math.sin(start),
    };
 
    const endOut = {
      x: centre.x + rOut * Math.cos(endRad),
      y: centre.y + rOut * Math.sin(endRad),
    };
 
    const startIn = {
      x: centre.x + rIn * Math.cos(endRad),
      y: centre.y + rIn * Math.sin(endRad),
    };
 
    const endIn = {
      x: centre.x + rIn * Math.cos(start),
      y: centre.y + rIn * Math.sin(start),
    };
 
    const largeArc = delta > Math.PI ? 1 : 0;
 
    return (
      `M${startOut.x},${startOut.y}` +
      ` A${rOut},${rOut} 0 ` +
      `${largeArc},1 ${endOut.x},${endOut.y}` +
      ` L${startIn.x},${startIn.y}` +
      ` A${rIn},${rIn} 0 ` +
      `${largeArc},0 ${endIn.x},${endIn.y}` +
      ` L${startOut.x},${startOut.y} Z`
    );
  }
 
  render() {
    const { fillColor, strokeColor, start, delta, rIn, rOut, centre } =
      this.props;
 
    const pathDef = this.generatePathDef(centre, rIn, rOut, start, delta);
 
    const labelDist = rIn + 1.8 * (rOut - rIn);
 
    const labelRad = start + delta;
 
    const labelPos = {
      x: centre.x + labelDist * Math.cos(labelRad) - 10,
      y: centre.y + labelDist * Math.sin(labelRad),
    };
 
    const labelStyle = {
      transform: `translate(${labelPos.x}px, ${labelPos.y}px)`,
      zIndex: 8,
      fill: this.props.fillColor,
      stroke: this.props.strokeColor,
    };
    const text_els: React.ReactNode[] = [];
    //   <text key={guidGenerator()} style={labelStyle}>
    //     {this.props.label}
    //   </text>,
    // ];
    if (this.props.highlight) {
      text_els.push(
        <rect
          key={guidGenerator()}
          width={this.props.label.length * 10}
          height={30}
          style={{
            transform: `translate(${labelPos.x}px, ${labelPos.y - 20}px)`,
            zIndex: 4,
            fill: "black",
            strokeWidth: 2,
          }}
        ></rect>
      );
    }
    text_els.push(
      <text key={guidGenerator()} style={labelStyle}>
        {this.props.label}
      </text>
    );
 
    return (
      <g className="react-pie-segment">
        <path stroke={strokeColor} fill={fillColor} d={pathDef} />
        {text_els}
        {/* <text style={labelStyle}>{this.props.label}</text> */}
      </g>
    );
  }
}