All files / src/components/HorizontalBarChart HorizontalBarChart.base.tsx

89.85% Statements 124/138
81.73% Branches 94/115
83.33% Functions 20/24
89.31% Lines 117/131

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 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 43925x 25x   25x                   25x 25x 25x 25x 25x   25x                               25x             51x         51x 51x                       51x 51x 51x 51x 51x 51x     25x 32x 32x 32x           40x 40x 40x 40x 40x 40x     97x 97x       97x                     97x 97x 97x 97x             97x                                           184x                                                                                                             25x 184x     25x 6x 5x   6x   5x 5x                           25x       51x                         51x 51x 51x                     51x 92x             25x 92x 92x 92x 92x 92x   92x 92x   84x           4x                 4x 4x               25x 5x 5x 5x   5x       5x                           100x   200x   100x 100x   100x 100x   200x     100x 100x   100x 100x 200x 200x 200x   200x     200x   200x                     100x   100x 200x 200x 200x 100x   200x 200x   200x     200x   200x   200x 200x     200x 7x 2x     5x   5x                             193x                                             100x     51x           51x 201x 201x   201x   201x           25x 40x   25x  
import * as React from 'react';
import { classNamesFunction, find, getId, getRTL } from '@fluentui/react/lib/Utilities';
import { IProcessedStyleSet, IPalette } from '@fluentui/react/lib/Styling';
import {
  IAccessibilityProps,
  IChartProps,
  IHorizontalBarChartProps,
  IHorizontalBarChartStyleProps,
  IHorizontalBarChartStyles,
  IChartDataPoint,
  IRefArrayData,
  HorizontalBarChartVariant,
} from './index';
import { Callout, DirectionalHint } from '@fluentui/react/lib/Callout';
import { convertToLocaleString } from '../../utilities/locale-util';
import { ChartHoverCard, formatValueWithSIPrefix, getAccessibleDataObject } from '../../utilities/index';
import { FocusZone, FocusZoneDirection } from '@fluentui/react-focus';
import { FocusableTooltipText } from '../../utilities/FocusableTooltipText';
 
const getClassNames = classNamesFunction<IHorizontalBarChartStyleProps, IHorizontalBarChartStyles>();
 
export interface IHorizontalBarChartState {
  isCalloutVisible: boolean;
  refSelected: SVGGElement | null | undefined;
  color: string;
  hoverValue: string | number | Date | null;
  lineColor: string;
  legend: string | null;
  xCalloutValue?: string;
  yCalloutValue?: string;
  barCalloutProps?: IChartDataPoint;
  callOutAccessibilityData?: IAccessibilityProps;
  barSpacingInPercent: number;
}
 
export class HorizontalBarChartBase extends React.Component<IHorizontalBarChartProps, IHorizontalBarChartState> {
  private _barHeight: number;
  private _classNames: IProcessedStyleSet<IHorizontalBarChartStyles>;
  private _uniqLineText: string;
  private _calloutId: string;
  private _refArray: IRefArrayData[];
  private _calloutAnchorPoint: IChartDataPoint | null;
  private _isRTL: boolean = getRTL();
  private barChartSvgRef: React.RefObject<SVGSVGElement>;
  private _emptyChartId: string;
 
  constructor(props: IHorizontalBarChartProps) {
    super(props);
    this.state = {
      isCalloutVisible: false,
      hoverValue: '',
      lineColor: '',
      legend: '',
      refSelected: null,
      // eslint-disable-next-line react/no-unused-state
      color: '',
      xCalloutValue: '',
      yCalloutValue: '',
      barSpacingInPercent: 0,
    };
    this._refArray = [];
    this._uniqLineText = '_HorizontalLine_' + Math.random().toString(36).substring(7);
    this._hoverOff = this._hoverOff.bind(this);
    this._calloutId = getId('callout');
    this._emptyChartId = getId('_HBC_empty');
    this.barChartSvgRef = React.createRef<SVGSVGElement>();
  }
 
  public componentDidMount(): void {
    const svgWidth = this.barChartSvgRef.current?.getBoundingClientRect().width || 0;
    const MARGIN_WIDTH_IN_PX = 3;
    Iif (svgWidth) {
      const currentBarSpacing = (MARGIN_WIDTH_IN_PX / svgWidth) * 100;
      this.setState({ barSpacingInPercent: currentBarSpacing });
    }
  }
 
  public render(): JSX.Element {
    const { data, theme } = this.props;
    this._adjustProps();
    const { palette } = theme!;
    let datapoint: number | undefined = 0;
    return !this._isChartEmpty() ? (
      <div className={this._classNames.root} onMouseLeave={this._handleChartMouseLeave}>
        {data!.map((points: IChartProps, index: number) => {
          if (points.chartData && points.chartData![0] && points.chartData![0].horizontalBarChartdata!.x) {
            datapoint = points.chartData![0].horizontalBarChartdata!.x;
          } else E{
            datapoint = 0;
          }
          points.chartData![1] = {
            legend: '',
            horizontalBarChartdata: {
              x: points.chartData![0].horizontalBarChartdata!.y - datapoint!,
              y: points.chartData![0].horizontalBarChartdata!.y,
            },
            color: palette.neutralLight,
          };
 
          // Hide right side text of chart title for absolute-scale variant
          const chartDataText =
            this.props.variant === HorizontalBarChartVariant.AbsoluteScale ? null : this._getChartDataText(points!);
          const bars = this._createBars(points!, palette);
          const keyVal = this._uniqLineText + '_' + index;
          const classNames = getClassNames(this.props.styles!, {
            theme: this.props.theme!,
            width: this.props.width,
            showTriangle: !!points!.chartData![0].data,
            variant: this.props.variant,
          });
 
          return (
            <div key={index}>
              <div className={classNames.items}>
                <FocusZone direction={FocusZoneDirection.horizontal}>
                  <div className={this._classNames.chartTitle}>
                    {points!.chartTitle && (
                      <FocusableTooltipText
                        className={this._classNames.chartTitleLeft}
                        content={points!.chartTitle}
                        accessibilityData={points!.chartTitleAccessibilityData}
                      />
                    )}
                    {chartDataText}
                  </div>
                </FocusZone>
                {points!.chartData![0].data && this._createBenchmark(points!)}
                <FocusZone direction={FocusZoneDirection.horizontal} className={this._classNames.chartWrapper}>
                  <svg ref={this.barChartSvgRef} className={this._classNames.chart} aria-label={points!.chartTitle}>
                    <g
                      id={keyVal}
                      key={keyVal}
                      ref={(e: SVGGElement) => {
                        this._refCallback(e, points!.chartData![0].legend);
                      }}
                      // NOTE: points.chartData![0] contains current data value
                      onClick={() => {
                        const p = points!.chartData![0];
                        Iif (p && p.onClick) {
                          p.onClick();
                        }
                      }}
                    >
                      {bars}
                    </g>
                  </svg>
                </FocusZone>
              </div>
            </div>
          );
        })}
        <Callout
          target={this.state.refSelected}
          coverTarget={true}
          isBeakVisible={false}
          gapSpace={30}
          hidden={!(!this.props.hideTooltip && this.state.isCalloutVisible)}
          directionalHint={DirectionalHint.topAutoEdge}
          id={this._calloutId}
          onDismiss={this._closeCallout}
          preventDismissOnLostFocus={true}
          {...this.props.calloutProps!}
          {...getAccessibleDataObject(this.state.callOutAccessibilityData)}
        >
          <>
            {this.props.onRenderCalloutPerHorizontalBar ? (
              this.props.onRenderCalloutPerHorizontalBar(this.state.barCalloutProps)
            ) : (
              <ChartHoverCard
                Legend={this.state.xCalloutValue ? this.state.xCalloutValue : this.state.legend!}
                YValue={this.state.yCalloutValue ? this.state.yCalloutValue : this.state.hoverValue!}
                color={this.state.lineColor}
                culture={this.props.culture}
              />
            )}
          </>
        </Callout>
      </div>
    ) : (
      <div
        id={this._emptyChartId}
        role={'alert'}
        style={{ opacity: '0' }}
        aria-label={'Graph has no data to display'}
      />
    );
  }
 
  private _refCallback(element: SVGGElement, legendTitle: string | undefined): void {
    this._refArray.push({ index: legendTitle, refElement: element });
  }
 
  private _hoverOn(hoverValue: string | number | Date | null, point: IChartDataPoint): void {
    if ((!this.state.isCalloutVisible || this.state.legend !== point.legend!) && this._calloutAnchorPoint !== point) {
      const currentHoveredElement = find(
        this._refArray,
        (currentElement: IRefArrayData) => currentElement.index === point.legend,
      );
      this._calloutAnchorPoint = point;
      this.setState({
        isCalloutVisible: true,
        hoverValue: hoverValue,
        lineColor: point.color!,
        legend: point.legend!,
        refSelected: currentHoveredElement!.refElement,
        xCalloutValue: point.xAxisCalloutData!,
        yCalloutValue: point.yAxisCalloutData!,
        barCalloutProps: point,
        callOutAccessibilityData: point.callOutAccessibilityData,
      });
    }
  }
 
  private _hoverOff(): void {
    /**/
  }
 
  private _handleChartMouseLeave = () => {
    this._calloutAnchorPoint = null;
    Iif (this.state.isCalloutVisible) {
      this.setState({
        isCalloutVisible: false,
        hoverValue: '',
        refSelected: null,
        lineColor: '',
        legend: '',
      });
    }
  };
 
  private _adjustProps = (): void => {
    this._barHeight = this.props.barHeight || 12;
    this._classNames = getClassNames(this.props.styles!, {
      theme: this.props.theme!,
      width: this.props.width,
      className: this.props.className,
      barHeight: this._barHeight,
      color: this.state.lineColor,
      variant: this.props.variant,
      hideLabels: this.props.hideLabels,
    });
  };
 
  private _getChartDataText = (data: IChartProps) => {
    return this.props.barChartCustomData ? (
      <div role="text">{this.props.barChartCustomData(data)}</div>
    ) : (
      this._getDefaultTextData(data)
    );
  };
 
  private _getDefaultTextData(data: IChartProps): JSX.Element {
    const { culture } = this.props;
    const chartDataMode = this.props.chartDataMode || 'default';
    const chartData: IChartDataPoint = data!.chartData![0];
    const x = chartData.horizontalBarChartdata!.x;
    const y = chartData.horizontalBarChartdata!.y;
 
    const accessibilityData = getAccessibleDataObject(data.chartDataAccessibilityData!, 'text', false);
    switch (chartDataMode) {
      case 'default':
        return (
          <div className={this._classNames.chartTitleRight} {...accessibilityData}>
            {convertToLocaleString(x, culture)}
          </div>
        );
      case 'fraction':
        return (
          <div {...accessibilityData}>
            <span className={this._classNames.chartTitleRight}>{convertToLocaleString(x, culture)}</span>
            <span className={this._classNames.chartDataTextDenominator}>
              {' / ' + convertToLocaleString(y, culture)}
            </span>
          </div>
        );
      case 'percentage':
        const dataRatioPercentage = `${convertToLocaleString(Math.round((x / y) * 100), culture)}%`;
        return (
          <div className={this._classNames.chartTitleRight} {...accessibilityData}>
            {dataRatioPercentage}
          </div>
        );
    }
  }
 
  private _createBenchmark(data: IChartProps): JSX.Element {
    const totalData = data.chartData![0].horizontalBarChartdata!.y;
    const benchmarkData = data.chartData![0].data;
    const benchmarkRatio = Math.round(((benchmarkData ? benchmarkData : 0) / totalData) * 100);
 
    const benchmarkStyles = {
      left: 'calc(' + benchmarkRatio + '% - 4px)',
    };
 
    return (
      <div className={this._classNames.benchmarkContainer}>
        <div className={this._classNames.triangle} style={benchmarkStyles} />
      </div>
    );
  }
 
  /**
   * This functions returns an array of <rect> elements, which form the bars
   * For each bar an x value, and a width needs to be specified
   * The computations are done based on percentages
   * Extra margin is also provided, in the x value to provide some spacing in between the bars
   */
 
  private _createBars(data: IChartProps, palette: IPalette): JSX.Element[] {
    const noOfBars =
      data.chartData?.reduce((count: number, point: IChartDataPoint) => (count += (point.data || 0) > 0 ? 1 : 0), 0) ||
      1;
    const totalMarginPercent = this.state.barSpacingInPercent * (noOfBars - 1);
    const defaultPalette: string[] = [palette.blueLight, palette.blue, palette.blueMid, palette.red, palette.black];
    // calculating starting point of each bar and it's range
    const startingPoint: number[] = [];
    const total = data.chartData!.reduce(
      (acc: number, point: IChartDataPoint) =>
        acc + (point.horizontalBarChartdata!.x ? point.horizontalBarChartdata!.x : 0),
      0,
    );
    let prevPosition = 0;
    let value = 0;
 
    let sumOfPercent = 0;
    data.chartData!.map((point: IChartDataPoint, index: number) => {
      const pointData = point.horizontalBarChartdata!.x ? point.horizontalBarChartdata!.x : 0;
      value = (pointData / total) * 100;
      Iif (value < 0) {
        value = 0;
      } else Iif (value < 1 && value !== 0) {
        value = 1;
      }
      sumOfPercent += value;
 
      return sumOfPercent;
    });
 
    /**
     * The %age of the space occupied by the margin needs to subtracted
     * while computing the scaling ratio, since the margins are not being
     * scaled down, only the data is being scaled down from a higher percentage to lower percentage
     * Eg: 95% of the space is taken by the bars, 5% by the margins
     * Now if the sumOfPercent is 120% -> This needs to be scaled down to 95%, not 100%
     * since that's only space available to the bars
     */
    const scalingRatio = sumOfPercent !== 0 ? (sumOfPercent - totalMarginPercent) / 100 : 1;
 
    const bars = data.chartData!.map((point: IChartDataPoint, index: number) => {
      const color: string = point.color ? point.color : defaultPalette[Math.floor(Math.random() * 4 + 1)];
      const pointData = point.horizontalBarChartdata!.x ? point.horizontalBarChartdata!.x : 0;
      if (index > 0) {
        prevPosition += value;
      }
      value = (pointData / total) * 100;
      Iif (value < 0) {
        value = 0;
      } else Iif (value < 1 && value !== 0) {
        value = 1 / scalingRatio;
      } else {
        value = value / scalingRatio;
      }
      startingPoint.push(prevPosition);
 
      const xValue = point.horizontalBarChartdata!.x;
      const placeholderIndex = 1;
 
      // Render bar label instead of placeholder bar for absolute-scale variant
      if (index === placeholderIndex && this.props.variant === HorizontalBarChartVariant.AbsoluteScale) {
        if (this.props.hideLabels) {
          return <text key={index} />;
        }
 
        const barValue = data.chartData![0].horizontalBarChartdata!.x;
 
        return (
          <text
            key={index}
            x={`${this._isRTL ? 100 - startingPoint[index] : startingPoint[index]}%`}
            y={this._barHeight / 2}
            dominantBaseline="central"
            transform={`translate(${this._isRTL ? -4 : 4})`}
            className={this._classNames.barLabel}
            aria-hidden={true}
          >
            {formatValueWithSIPrefix(barValue)}
          </text>
        );
      }
 
      return (
        <rect
          key={index}
          x={`${
            this._isRTL
              ? 100 - startingPoint[index] - value - index * this.state.barSpacingInPercent
              : startingPoint[index] + index * this.state.barSpacingInPercent
          }%`}
          y={0}
          data-is-focusable={point.legend !== '' ? true : false}
          width={value + '%'}
          height={this._barHeight}
          fill={color}
          onMouseOver={point.legend !== '' ? this._hoverOn.bind(this, xValue, point) : undefined}
          onFocus={point.legend !== '' ? this._hoverOn.bind(this, xValue, point) : undefined}
          role="img"
          aria-label={this._getAriaLabel(point)}
          onBlur={this._hoverOff}
          onMouseLeave={this._hoverOff}
          className={this._classNames.barWrapper}
        />
      );
    });
    return bars;
  }
 
  private _closeCallout = () => {
    this.setState({
      isCalloutVisible: false,
    });
  };
 
  private _getAriaLabel = (point: IChartDataPoint): string => {
    const legend = point.xAxisCalloutData || point.legend;
    const benchMark = point.data;
    const yValue =
      point.yAxisCalloutData ||
      (point.horizontalBarChartdata ? `${point.horizontalBarChartdata.x}/${point.horizontalBarChartdata.y}` : 0);
    return (
      point.callOutAccessibilityData?.ariaLabel ||
      (legend ? `${legend}, ` : '') + `${yValue}.` + (benchMark ? 'with benchmark at ' + `${benchMark}, ` : '')
    );
  };
 
  private _isChartEmpty(): boolean {
    return !(this.props.data && this.props.data.length > 0);
  }
}