All files / src/components/DonutChart/Pie Pie.tsx

100% Statements 45/45
100% Branches 7/7
100% Functions 12/12
100% Lines 40/40

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 11122x 22x   22x   22x 22x 22x   22x 22x   22x 22x       207x                 30x 30x 30x     207x       30x 207x 207x                                                   69x 69x 69x 69x 69x 69x       69x   69x   207x                   22x 39x       39x     30x 1x     22x 7x     30x 69x 69x 207x   69x   22x  
import * as React from 'react';
import { pie as d3Pie } from 'd3-shape';
import { IPieProps, IPieStyleProps, IPieStyles } from './index';
import { Arc, IArcData } from '../Arc/index';
import { IChartDataPoint } from '../index';
import { classNamesFunction } from '@fluentui/react/lib/Utilities';
import { getStyles } from './Pie.styles';
import { wrapTextInsideDonut } from '../../../utilities/index';
 
const getClassNames = classNamesFunction<IPieStyleProps, IPieStyles>();
const TEXT_PADDING: number = 5;
 
export class Pie extends React.Component<IPieProps, {}> {
  public static defaultProps: Partial<IPieProps> = {
    pie: d3Pie()
      .sort(null)
      // eslint-disable-next-line @typescript-eslint/no-explicit-any
      .value((d: any) => d.data)
      .padAngle(0.02),
  };
 
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  private _pieForFocusRing: any;
  private _totalValue: number;
 
  constructor(props: IPieProps) {
    super(props);
    this._hoverCallback = this._hoverCallback.bind(this);
    this._pieForFocusRing = d3Pie()
      .sort(null)
      // eslint-disable-next-line @typescript-eslint/no-explicit-any
      .value((d: any) => d.data)
      .padAngle(0);
  }
 
  public arcGenerator = (d: IArcData, i: number, focusData: IArcData, href?: string): JSX.Element => {
    const color = d && d.data && d.data.color;
    return (
      <Arc
        key={i}
        data={d}
        focusData={focusData}
        innerRadius={this.props.innerRadius}
        outerRadius={this.props.outerRadius}
        color={color!}
        onFocusCallback={this._focusCallback}
        hoverOnCallback={this._hoverCallback}
        onBlurCallback={this.props.onBlurCallback}
        hoverLeaveCallback={this.props.hoverLeaveCallback}
        uniqText={this.props.uniqText}
        activeArc={this.props.activeArc}
        href={href}
        calloutId={this.props.calloutId}
        valueInsideDonut={this.props.valueInsideDonut}
        theme={this.props.theme!}
        focusedArcId={this.props.focusedArcId}
        showLabelsInPercent={this.props.showLabelsInPercent}
        totalValue={this._totalValue}
        hideLabels={this.props.hideLabels}
      />
    );
  };
 
  public render(): JSX.Element {
    const { pie, data } = this.props;
    const focusData = this._pieForFocusRing(data);
    const piechart = pie(data);
    const translate = `translate(${this.props.width / 2}, ${this.props.height / 2})`;
    const classNames = getClassNames(getStyles, {
      theme: this.props.theme!,
    });
 
    this._totalValue = this._computeTotalValue();
 
    return (
      <g transform={translate}>
        {piechart.map((d: IArcData, i: number) => this.arcGenerator(d, i, focusData[i], this.props.href))}
        {this.props.valueInsideDonut && (
          <text y={5} textAnchor="middle" dominantBaseline="middle" className={classNames.insideDonutString}>
            {this.props.valueInsideDonut}
          </text>
        )}
      </g>
    );
  }
 
  public componentDidUpdate(): void {
    const classNames = getClassNames(getStyles, {
      theme: this.props.theme!,
    });
 
    wrapTextInsideDonut(classNames.insideDonutString, this.props.innerRadius! * 2 - TEXT_PADDING);
  }
 
  private _focusCallback = (data: IChartDataPoint, id: string, e: SVGPathElement): void => {
    this.props.onFocusCallback!(data, id, e);
  };
 
  private _hoverCallback(data: IChartDataPoint, e: React.MouseEvent<SVGPathElement>): void {
    this.props.hoverOnCallback!(data, e);
  }
 
  private _computeTotalValue = () => {
    let totalValue = 0;
    this.props.data.forEach((arc: IChartDataPoint) => {
      totalValue += arc.data!;
    });
    return totalValue;
  };
}