All files / src/components/StackedBarChart MultiStackedBarChart.base.tsx

88.67% Statements 188/212
71.87% Branches 138/192
80.43% Functions 37/46
88.2% Lines 172/195

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 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 65223x 23x   23x 23x                 23x 23x 23x 23x 23x   23x                                               23x 23x                   51x               51x 51x                         51x 51x 51x 51x 51x 51x 51x 51x     23x 39x 39x 39x           49x 49x 48x 48x 48x 48x 48x   48x             48x 48x   48x 48x 115x                 115x             48x                                                           1x                                 118x                   355x   118x 118x 118x   118x 118x 355x       118x   118x 118x 355x 355x 355x   355x 19x 336x     355x   355x           118x 7x 7x   7x     7x                       118x   118x 118x   118x 355x         355x 355x 237x   355x 355x 19x 336x     336x     355x   355x 355x   355x             355x         656x                                                       118x 5x 5x 5x 5x 5x                 5x 5x                                           113x                             113x                               118x       118x   118x   118x 118x                                                                                   23x                                     23x 656x     51x 48x 48x                 23x 1x         51x 51x 51x 51x 364x 121x 77x 77x 1x 2x 2x 1x   2x     2x                           2x       44x 211x 211x 713x   211x 101x   110x       1x     1x 1x     1x     110x       51x                     23x 1x         1x           23x 1x         23x           6x   6x 5x 5x                             23x       51x 2x 2x         23x       51x                       51x 355x                 51x 352x     51x 358x 358x 358x     51x 49x 118x 118x 355x     118x   49x     23x 51x       119x       23x  
import * as React from 'react';
import { classNamesFunction, getId, getRTL } from '@fluentui/react/lib/Utilities';
import { IProcessedStyleSet, IPalette } from '@fluentui/react/lib/Styling';
import { ILegend, Legends } from '../Legends/index';
import {
  IAccessibilityProps,
  IChartDataPoint,
  IChartProps,
  IMultiStackedBarChartProps,
  IMultiStackedBarChartStyles,
  IMultiStackedBarChartStyleProps,
  MultiStackedBarChartVariant,
} from './index';
import { Callout, DirectionalHint } from '@fluentui/react/lib/Callout';
import { FocusZone, FocusZoneDirection } from '@fluentui/react-focus';
import { convertToLocaleString } from '../../utilities/locale-util';
import { ChartHoverCard, formatValueWithSIPrefix, getAccessibleDataObject } from '../../utilities/index';
import { FocusableTooltipText } from '../../utilities/FocusableTooltipText';
 
const getClassNames = classNamesFunction<IMultiStackedBarChartStyleProps, IMultiStackedBarChartStyles>();
 
export interface IRefArrayData {
  legendText?: string;
  refElement?: SVGGElement;
}
 
export interface IMultiStackedBarChartState {
  isCalloutVisible: boolean;
  refArray: IRefArrayData[];
  selectedLegend: string;
  activeLegend: string;
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  refSelected: any;
  dataForHoverCard: number;
  color: string;
  xCalloutValue?: string;
  yCalloutValue?: string;
  dataPointCalloutProps?: IChartDataPoint;
  callOutAccessibilityData?: IAccessibilityProps;
  calloutLegend: string;
  barSpacingInPercent: number;
}
 
export class MultiStackedBarChartBase extends React.Component<IMultiStackedBarChartProps, IMultiStackedBarChartState> {
  public static defaultProps: Partial<IMultiStackedBarChartProps> = {
    barHeight: 12,
    hideRatio: [],
    hideDenominator: [],
  };
 
  private _classNames: IProcessedStyleSet<IMultiStackedBarChartStyles>;
  private _calloutId: string;
  private _calloutAnchorPoint: IChartDataPoint | null;
  private _longestBarTotalValue: number;
  private _isRTL: boolean = getRTL();
  private barChartSvgRef: React.RefObject<SVGSVGElement>;
  private _emptyChartId: string;
  private _barId: string;
  private _barIdPlaceholderPartToWhole: string;
  private _barIdEmpty: string;
 
  public constructor(props: IMultiStackedBarChartProps) {
    super(props);
    this.state = {
      isCalloutVisible: false,
      refArray: [],
      selectedLegend: '',
      activeLegend: '',
      refSelected: null,
      dataForHoverCard: 0,
      color: '',
      xCalloutValue: '',
      yCalloutValue: '',
      calloutLegend: '',
      barSpacingInPercent: 0,
    };
    this._onLeave = this._onLeave.bind(this);
    this._onBarLeave = this._onBarLeave.bind(this);
    this._calloutId = getId('callout');
    this.barChartSvgRef = React.createRef<SVGSVGElement>();
    this._emptyChartId = getId('_MSBC_empty');
    this._barId = getId('_MSBC_rect_');
    this._barIdPlaceholderPartToWhole = getId('_MSBC_rect_partToWhole_');
    this._barIdEmpty = getId('_MSBC_rect_empty');
  }
 
  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 {
    if (!this._isChartEmpty()) {
      const { data, theme, culture } = this.props;
      this._adjustProps();
      const { palette } = theme!;
      const legends = this._getLegendData(data!, this.props.hideRatio!, palette);
      const { isCalloutVisible } = this.state;
 
      this._classNames = getClassNames(this.props.styles!, {
        legendColor: this.state.color,
        theme: theme!,
        variant: this.props.variant,
        hideLabels: this.props.hideLabels,
      });
 
      const legendName = this.state.xCalloutValue ? this.state.xCalloutValue : this.state.calloutLegend;
      const calloutYVal = this.state.yCalloutValue ? this.state.yCalloutValue : this.state.dataForHoverCard;
 
      this._longestBarTotalValue = this._computeLongestBarTotalValue();
      const bars: JSX.Element[] = data!.map((singleChartData: IChartProps, index: number) => {
        const singleChartBars = this._createBarsAndLegends(
          singleChartData!,
          this.props.barHeight!,
          palette,
          this.props.hideRatio![index],
          this.props.hideDenominator![index],
          this.props.href,
          index,
        );
        return (
          <div key={index} id={`_MSBC_bar-${index}`}>
            {singleChartBars}
          </div>
        );
      });
 
      return (
        <div className={this._classNames.root} onMouseLeave={this._handleChartMouseLeave}>
          {bars}
          {!this.props.hideLegend && <div className={this._classNames.legendContainer}>{legends}</div>}
          <Callout
            gapSpace={15}
            isBeakVisible={false}
            target={this.state.refSelected}
            setInitialFocus={true}
            hidden={!(!this.props.hideTooltip && isCalloutVisible)}
            directionalHint={DirectionalHint.topAutoEdge}
            id={this._calloutId}
            onDismiss={this._closeCallout}
            preventDismissOnLostFocus={true}
            /** Keep the callout updated with details of focused/hovered bar */
            shouldUpdateWhenHidden={true}
            {...this.props.calloutProps!}
            {...getAccessibleDataObject(this.state.callOutAccessibilityData, 'text', false)}
          >
            <>
              {this.props.onRenderCalloutPerDataPoint ? (
                this.props.onRenderCalloutPerDataPoint(this.state.dataPointCalloutProps)
              ) : (
                <ChartHoverCard Legend={legendName} YValue={calloutYVal} color={this.state.color} culture={culture} />
              )}
            </>
          </Callout>
        </div>
      );
    }
    return (
      <div
        id={this._emptyChartId}
        role={'alert'}
        style={{ opacity: '0' }}
        aria-label={'Graph has no data to display'}
      />
    );
  }
 
  /**
   * The Create bar 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
   */
 
  private _createBarsAndLegends(
    data: IChartProps,
    barHeight: number,
    palette: IPalette,
    hideRatio: boolean,
    hideDenominator: boolean,
    href?: string,
    barNo?: number,
  ): 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 { culture } = this.props;
    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 barTotalValue = data.chartData!.reduce(
      (acc: number, point: IChartDataPoint) => acc + (point.data ? point.data : 0),
      0,
    );
    const total =
      this.props.variant === MultiStackedBarChartVariant.AbsoluteScale ? this._longestBarTotalValue : barTotalValue;
 
    let sumOfPercent = 0;
    data.chartData!.map((point: IChartDataPoint, index: number) => {
      const pointData = point.data ? point.data : 0;
      const currValue = (pointData / total) * 100;
      let value = currValue ? currValue : 0;
 
      if (value < 1 && value !== 0) {
        value = 1;
      } else Iif (value > 99 && value !== 100) {
        value = 99;
      }
      sumOfPercent += value;
 
      return sumOfPercent;
    });
 
    // Include an imaginary placeholder bar with value equal to
    // the difference between longestBarTotalValue and barTotalValue
    // while calculating sumOfPercent to get correct scalingRatio for absolute-scale variant
    if (this.props.variant === MultiStackedBarChartVariant.AbsoluteScale) {
      let value = total === 0 ? 0 : ((total - barTotalValue) / total) * 100;
      Iif (value < 1 && value !== 0) {
        value = 1;
      } else Iif (value > 99 && value !== 100) {
        value = 99;
      }
      sumOfPercent += value;
    }
 
    /**
     * 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 / (100 - totalMarginPercent) : 1;
 
    let prevPosition = 0;
    let value = 0;
 
    const bars = data.chartData!.map((point: IChartDataPoint, index: number) => {
      const color: string = point.color
        ? point.color
        : point.placeHolder
        ? palette.neutralLight
        : defaultPalette[Math.floor(Math.random() * 4 + 1)];
      const pointData = point.data ? point.data : 0;
      if (index > 0) {
        prevPosition += value;
      }
      value = (pointData / total) * 100 ? (pointData / total) * 100 : 0;
      if (value < 1 && value !== 0) {
        value = 1 / scalingRatio;
      } else Iif (value > 99 && value !== 100) {
        value = 99 / scalingRatio;
      } else {
        value = value / scalingRatio;
      }
 
      startingPoint.push(prevPosition);
 
      const styles = this.props.styles;
      const shouldHighlight = this._legendHighlighted(point.legend!) || this._noLegendHighlighted() ? true : false;
 
      this._classNames = getClassNames(styles!, {
        theme: this.props.theme!,
        shouldHighlight,
        href,
        variant: this.props.variant,
        hideLabels: this.props.hideLabels,
      });
      return (
        <g
          key={index}
          className={point.placeHolder ? this._classNames.placeHolderOnHover : this._classNames.opacityChangeOnHover}
          ref={(e: SVGGElement) => {
            this._refCallback(e, point.legend!);
          }}
          data-is-focusable={!this.props.hideTooltip && shouldHighlight}
          onFocus={this._onBarFocus.bind(this, pointData, color, point)}
          onBlur={this._onBarLeave}
          role="img"
          aria-label={this._getAriaLabel(point)}
          onMouseOver={point.placeHolder ? undefined : this._onBarHover.bind(this, pointData, color, point)}
          onMouseMove={point.placeHolder ? undefined : this._onBarHover.bind(this, pointData, color, point)}
          onMouseLeave={point.placeHolder ? undefined : this._onBarLeave}
          onClick={href ? (point.placeHolder ? undefined : this._redirectToUrl.bind(this, href)) : point.onClick}
        >
          <rect
            key={index}
            id={`${this._barId}-${barNo}-${index}`}
            x={`${
              this._isRTL
                ? 100 - startingPoint[index] - value - this.state.barSpacingInPercent * index
                : startingPoint[index] + this.state.barSpacingInPercent * index
            }%`}
            y={0}
            width={value + '%'}
            height={barHeight}
            fill={color}
          />
        </g>
      );
    });
    if (this.props.variant === MultiStackedBarChartVariant.AbsoluteScale && !this.props.hideLabels) {
      let showLabel = false;
      let barLabel = 0;
      if (this._noLegendHighlighted()) {
        showLabel = true;
        barLabel = barTotalValue;
      } else E{
        data.chartData?.forEach(point => {
          Iif (this._legendHighlighted(point.legend!)) {
            showLabel = true;
            barLabel += point.data ? point.data : 0;
          }
        });
      }
      if (showLabel) {
        bars.push(
          <text
            key="text"
            x={`${
              this._isRTL
                ? 100 - (startingPoint[startingPoint.length - 1] || 0) - value - totalMarginPercent
                : (startingPoint[startingPoint.length - 1] || 0) + value + totalMarginPercent
            }%`}
            textAnchor={'start'}
            y={barHeight / 2}
            dominantBaseline="central"
            transform={`translate(${this._isRTL ? -4 : 4})`}
            className={this._classNames.barLabel}
            aria-label={`Total: ${barLabel}`}
            role="img"
          >
            {formatValueWithSIPrefix(barLabel)}
          </text>,
        );
      }
    } else {
      // Render placeholder bars only for part-to-whole variant
      Iif (data.chartData!.length === 0) {
        bars.push(
          <g key={0} className={this._classNames.noData} onClick={this._redirectToUrl.bind(this, href)}>
            <rect
              key={0}
              id={this._barIdPlaceholderPartToWhole}
              x={'0%'}
              y={0}
              width={'100%'}
              height={barHeight}
              fill={palette.neutralLight}
            />
          </g>,
        );
      }
      Iif (barTotalValue === 0) {
        bars.push(
          <g key={'empty'} className={this._classNames.noData} onClick={this._redirectToUrl.bind(this, href)}>
            <rect
              key={0}
              id={this._barIdEmpty}
              x={'0%'}
              y={0}
              width={'100%'}
              height={barHeight}
              fill={palette.neutralLight}
            />
          </g>,
        );
      }
    }
    const hideNumber = hideRatio === undefined ? false : hideRatio;
 
    // Hide right side text of chart title for absolute-scale variant
    const showRatio =
      this.props.variant !== MultiStackedBarChartVariant.AbsoluteScale && !hideNumber && data!.chartData!.length === 2;
    const showNumber =
      this.props.variant !== MultiStackedBarChartVariant.AbsoluteScale && !hideNumber && data!.chartData!.length === 1;
 
    const getChartData = () => convertToLocaleString(data!.chartData![0].data ? data!.chartData![0].data : 0, culture);
    return (
      <div className={this._classNames.singleChartRoot}>
        <FocusZone direction={FocusZoneDirection.horizontal}>
          <div className={this._classNames.chartTitle}>
            {data!.chartTitle && (
              <FocusableTooltipText
                className={this._classNames.chartTitleLeft}
                content={data!.chartTitle}
                accessibilityData={data!.chartTitleAccessibilityData}
              />
            )}
            {showRatio && (
              <div {...getAccessibleDataObject(data!.chartDataAccessibilityData, 'text', false)}>
                <span className={this._classNames.ratioNumerator}>{getChartData()}</span>
                {!hideDenominator && (
                  <span className={this._classNames.ratioDenominator}>
                    {' / ' + convertToLocaleString(barTotalValue, culture)}
                  </span>
                )}
              </div>
            )}
            {showNumber && (
              <div
                className={this._classNames.ratioNumerator}
                {...getAccessibleDataObject(data!.chartDataAccessibilityData, 'text', false)}
              >
                {getChartData()}
              </div>
            )}
          </div>
        </FocusZone>
        <FocusZone direction={FocusZoneDirection.horizontal}>
          <div className={this._classNames.chartWrapper}>
            <svg ref={this.barChartSvgRef} className={this._classNames.chart} aria-label={data?.chartTitle}>
              {bars}
            </svg>
          </div>
        </FocusZone>
      </div>
    );
  }
 
  private _onBarFocus(pointData: number, color: string, point: IChartDataPoint): void {
    this.state.refArray.forEach((obj: IRefArrayData) => {
      Iif (obj.legendText === point.legend!) {
        this.setState({
          refSelected: obj.refElement,
          /** Show the callout if highlighted bar is focused and Hide it if unhighlighted bar is focused */
          isCalloutVisible: this.state.selectedLegend === '' || this.state.selectedLegend === point.legend!,
          calloutLegend: point.legend!,
          dataForHoverCard: pointData,
          color,
          xCalloutValue: point.xAxisCalloutData!,
          yCalloutValue: point.yAxisCalloutData!,
          dataPointCalloutProps: point,
          callOutAccessibilityData: point.callOutAccessibilityData!,
        });
      }
    });
  }
 
  private _refCallback(element: SVGGElement, legendTitle: string): void {
    this.state.refArray.push({ legendText: legendTitle, refElement: element });
  }
 
  private _adjustProps = (): void => {
    const { theme, className, styles, width, barHeight } = this.props;
    this._classNames = getClassNames(styles!, {
      legendColor: this.state.color,
      theme: theme!,
      width,
      className,
      barHeight,
    });
  };
 
  private _onHover(legendTitle: string): void {
    this.setState({
      activeLegend: legendTitle,
    });
  }
 
  private _getLegendData = (data: IChartProps[], hideRatio: boolean[], palette: IPalette): JSX.Element => {
    const defaultPalette: string[] = [palette.blueLight, palette.blue, palette.blueMid, palette.red, palette.black];
    const actions: ILegend[] = [];
    data.forEach((singleChartData: IChartProps, index: number) => {
      const validChartData = singleChartData.chartData!.filter((_: IChartDataPoint) => !_.placeHolder);
      if (validChartData!.length < 3) {
        const hideNumber = hideRatio[index] === undefined ? false : hideRatio[index];
        if (hideNumber) {
          validChartData!.forEach((point: IChartDataPoint) => {
            const color: string = point.color ? point.color : defaultPalette[Math.floor(Math.random() * 4 + 1)];
            const checkSimilarLegends = actions.filter(
              (leg: ILegend) => leg.title === point.legend! && leg.color === color,
            );
            Iif (checkSimilarLegends!.length > 0) {
              return;
            }
            const legend: ILegend = {
              title: point.legend!,
              color,
              action: () => {
                this._onClick(point.legend!);
              },
              hoverAction: () => {
                this._handleChartMouseLeave();
                this._onHover(point.legend!);
              },
              onMouseOutAction: () => {
                this._onLeave();
              },
            };
            actions.push(legend);
          });
        }
      } else {
        validChartData!.forEach((point: IChartDataPoint) => {
          const color: string = point.color ? point.color : defaultPalette[Math.floor(Math.random() * 4 + 1)];
          const checkSimilarLegends = actions.filter(
            (leg: ILegend) => leg.title === point.legend! && leg.color === color,
          );
          if (checkSimilarLegends!.length > 0) {
            return;
          }
          const legend: ILegend = {
            title: point.legend!,
            color,
            action: () => {
              this._onClick(point.legend!);
            },
            hoverAction: () => {
              this._handleChartMouseLeave();
              this._onHover(point.legend!);
            },
            onMouseOutAction: () => {
              this._onLeave();
            },
          };
          actions.push(legend);
        });
      }
    });
    return (
      <Legends
        legends={actions}
        overflowProps={this.props.legendsOverflowProps}
        focusZonePropsInHoverCard={this.props.focusZonePropsForLegendsInHoverCard}
        overflowText={this.props.legendsOverflowText}
        {...this.props.legendProps}
      />
    );
  };
 
  private _onClick(legendTitle: string): void {
    Iif (this.state.selectedLegend === legendTitle) {
      this.setState({
        selectedLegend: '',
      });
    } else {
      this.setState({
        selectedLegend: legendTitle,
      });
    }
  }
 
  private _onLeave(): void {
    this.setState({
      activeLegend: '',
    });
  }
 
  private _onBarHover(
    pointData: number,
    color: string,
    point: IChartDataPoint,
    mouseEvent: React.MouseEvent<SVGPathElement>,
  ): void {
    mouseEvent.persist();
 
    if (this._calloutAnchorPoint !== point) {
      this._calloutAnchorPoint = point;
      this.setState({
        refSelected: mouseEvent,
        /** Show the callout if highlighted bar is hovered and Hide it if unhighlighted bar is hovered */
        isCalloutVisible: this.state.selectedLegend === '' || this.state.selectedLegend === point.legend!,
        calloutLegend: point.legend!,
        dataForHoverCard: pointData,
        color,
        xCalloutValue: point.xAxisCalloutData!,
        yCalloutValue: point.yAxisCalloutData!,
        dataPointCalloutProps: point,
        callOutAccessibilityData: point.callOutAccessibilityData!,
      });
    }
  }
 
  private _onBarLeave(): void {
    /**/
  }
 
  private _handleChartMouseLeave = () => {
    this._calloutAnchorPoint = null;
    this.setState({
      isCalloutVisible: false,
    });
  };
 
  private _redirectToUrl(href: string | undefined): void {
    href ? (window.location.href = href) : '';
  }
 
  private _closeCallout = () => {
    this.setState({
      isCalloutVisible: false,
    });
  };
 
  /**
   * This function checks if the given legend is highlighted or not.
   * A legend can be highlighted in 2 ways:
   * 1. selection: if the user clicks on it
   * 2. hovering: if there is no selected legend and the user hovers over it
   */
  private _legendHighlighted = (legendTitle: string) => {
    return (
      this.state.selectedLegend === legendTitle ||
      (this.state.selectedLegend === '' && this.state.activeLegend === legendTitle)
    );
  };
 
  /**
   * This function checks if none of the legends is selected or hovered.
   */
  private _noLegendHighlighted = () => {
    return this.state.selectedLegend === '' && this.state.activeLegend === '';
  };
 
  private _getAriaLabel = (point: IChartDataPoint): string => {
    const legend = point.xAxisCalloutData || point.legend;
    const yValue = point.yAxisCalloutData || point.data || 0;
    return point.callOutAccessibilityData?.ariaLabel || (legend ? `${legend}, ` : '') + `${yValue}.`;
  };
 
  private _computeLongestBarTotalValue = () => {
    let longestBarTotalValue = 0;
    this.props.data!.forEach(({ chartData }) => {
      const barTotalValue = chartData!.reduce(
        (acc: number, point: IChartDataPoint) => acc + (point.data ? point.data : 0),
        0,
      );
      longestBarTotalValue = Math.max(longestBarTotalValue, barTotalValue);
    });
    return longestBarTotalValue;
  };
 
  private _isChartEmpty(): boolean {
    return !(
      this.props.data &&
      this.props.data.length > 0 &&
      this.props.data.filter(
        item => item.chartData && item.chartData.length === 0 && (!item.chartTitle || item.chartTitle === ''),
      ).length === 0
    );
  }
}