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

84.78% Statements 312/368
67.34% Branches 231/343
76.38% Functions 55/72
84.93% Lines 265/312

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 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 105122x 22x 22x 22x 22x           22x   22x 22x                               22x 22x                             22x 22x 22x   22x   22x                               22x 22x                     58x                           58x 58x                           58x 58x 58x 58x 58x       58x     65x 65x 293x 65x 293x     65x 65x       65x                                       65x       65x                                                   90x                                                       58x         33x 33x       33x 33x 33x 33x 33x   33x 33x 33x   264x 231x                 33x   231x   231x 33x 33x       33x                           33x                         33x                       231x 231x                                           462x               33x               58x       462x 462x                     462x             58x 128x 298x     22x 70x 70x 70x 70x 70x 70x 70x     58x 95x     58x                               58x                             58x               58x 62x         58x               90x               128x 128x 128x 128x 6x 15x 15x     122x 122x 487x   122x 122x     58x 432x     58x     8x 8x 8x 54x   8x   6x                 8x                     8x 8x           22x         8x   8x 8x 8x 8x                                         58x       58x 2x 2x               58x                                               58x         58x           58x                                       58x               126x       126x 598x 598x 106x       20x 86x 86x 12x               8x                     126x     76x 76x 76x 76x 76x 368x 368x         368x 368x 368x 190x 178x     178x   178x 178x 178x                     356x                               76x 72x 72x         76x 4x 4x 4x     4x         4x   76x     22x 7x 7x 7x 25x 25x 25x 14x 11x     11x   11x 11x     11x 11x                           22x                               7x 7x 7x         7x                           7x     22x 7x 7x 7x 7x 46x 46x         46x 46x 46x 19x 27x     27x   27x 27x 27x                     54x                               7x 7x 7x         7x                         7x     58x           22x                       22x 2x         22x           58x 65x 65x 65x 65x 293x   293x             1x 1x           293x   65x 4x             1x 1x             4x     65x                 65x     58x 92x 92x 92x                   58x 1119x                 58x 683x     58x 217x         217x 217x 217x 217x 217x                 22x 216x         21x     195x                         58x 92x       92x   92x 9x       9x   9x     9x   9x   9x   3x       414x 83x         83x     92x             22x 65x   293x     22x  
import * as React from 'react';
import { max as d3Max, min as d3Min } from 'd3-array';
import { line as d3Line } from 'd3-shape';
import { select as d3Select } from 'd3-selection';
import {
  scaleLinear as d3ScaleLinear,
  ScaleLinear as D3ScaleLinear,
  scaleBand as d3ScaleBand,
  scaleUtc as d3ScaleUtc,
} from 'd3-scale';
import { classNamesFunction, getId, getRTL } from '@fluentui/react/lib/Utilities';
import { IProcessedStyleSet, IPalette } from '@fluentui/react/lib/Styling';
import { DirectionalHint } from '@fluentui/react/lib/Callout';
import {
  IAccessibilityProps,
  CartesianChart,
  ChartHoverCard,
  IBasestate,
  IMargins,
  ILegend,
  IRefArrayData,
  IVerticalBarChartProps,
  IVerticalBarChartStyleProps,
  IVerticalBarChartStyles,
  IVerticalBarChartDataPoint,
  Legends,
  IChildProps,
  IYValueHover,
} from '../../index';
import { FocusZoneDirection } from '@fluentui/react-focus';
import {
  ChartTypes,
  IAxisData,
  getAccessibleDataObject,
  XAxisTypes,
  NumericAxis,
  getTypeOfAxis,
  tooltipOfXAxislabels,
  formatValueWithSIPrefix,
  getBarWidth,
  getScalePadding,
  isScalePaddingDefined,
  calculateAppropriateBarWidth,
} from '../../utilities/index';
 
enum CircleVisbility {
  show = 'visibility',
  hide = 'hidden',
}
const getClassNames = classNamesFunction<IVerticalBarChartStyleProps, IVerticalBarChartStyles>();
 
const MIN_DOMAIN_MARGIN = 8;
 
export interface IVerticalBarChartState extends IBasestate {
  dataPointCalloutProps?: IVerticalBarChartDataPoint; // define this in hover and focus
  /**
   * data point of x, where rectangle is hovered or focused
   */
  activeXdataPoint: number | string | Date | null;
  YValueHover: IYValueHover[];
  hoverXValue?: string | number | null;
  callOutAccessibilityData?: IAccessibilityProps;
  calloutLegend: string;
}
 
type ColorScale = (_p?: number) => string;
 
export class VerticalBarChartBase extends React.Component<IVerticalBarChartProps, IVerticalBarChartState> {
  public static defaultProps: Partial<IVerticalBarChartProps> = {
    maxBarWidth: 24,
  };
 
  private _points: IVerticalBarChartDataPoint[];
  private _barWidth: number;
  private _colors: string[];
  private _classNames: IProcessedStyleSet<IVerticalBarChartStyles>;
  private _refArray: IRefArrayData[];
  private _calloutId: string;
  private margins: IMargins;
  private _isRtl: boolean = getRTL();
  private _bars: JSX.Element[];
  private _xAxisLabels: string[];
  private _yMax: number;
  private _isHavingLine: boolean;
  private _tooltipId: string;
  private _xAxisType: XAxisTypes;
  private _calloutAnchorPoint: IVerticalBarChartDataPoint | null;
  private _domainMargin: number;
  private _emptyChartId: string;
  private _xAxisInnerPadding: number;
  private _xAxisOuterPadding: number;
 
  public constructor(props: IVerticalBarChartProps) {
    super(props);
    this.state = {
      color: '',
      dataForHoverCard: 0,
      isCalloutVisible: false,
      refSelected: null,
      selectedLegend: '',
      activeLegend: '',
      xCalloutValue: '',
      yCalloutValue: '',
      activeXdataPoint: null,
      YValueHover: [],
      hoverXValue: '',
      calloutLegend: '',
    };
    this._isHavingLine = this._checkForLine();
    this._calloutId = getId('callout');
    this._tooltipId = getId('VCTooltipID_');
    this._refArray = [];
    this._xAxisType =
      this.props.data! && this.props.data!.length > 0
        ? (getTypeOfAxis(this.props.data![0].x, true) as XAxisTypes)
        : XAxisTypes.StringAxis;
    this._emptyChartId = getId('_VBC_empty');
  }
 
  public render(): JSX.Element {
    this._adjustProps();
    this._xAxisLabels = this._points.map((point: IVerticalBarChartDataPoint) => point.x as string);
    this._yMax = Math.max(
      d3Max(this._points, (point: IVerticalBarChartDataPoint) => point.y)!,
      this.props.yMaxValue || 0,
    );
    const legendBars: JSX.Element = this._getLegendData(this._points, this.props.theme!.palette);
    this._classNames = getClassNames(this.props.styles!, {
      theme: this.props.theme!,
      legendColor: this.state.color,
    });
    const calloutProps = {
      isCalloutVisible: this.state.isCalloutVisible,
      directionalHint: DirectionalHint.topAutoEdge,
      id: `toolTip${this._calloutId}`,
      target: this.state.refSelected,
      isBeakVisible: false,
      ...(this._isHavingLine && {
        YValueHover: this.state.YValueHover,
        hoverXValue: this.state.hoverXValue,
      }),
      gapSpace: 15,
      color: this.state.color,
      legend: this.state.calloutLegend,
      XValue: this.state.xCalloutValue,
      YValue: this.state.yCalloutValue ? this.state.yCalloutValue : this.state.dataForHoverCard,
      onDismiss: this._closeCallout,
      preventDismissOnLostFocus: true,
      ...this.props.calloutProps,
      ...getAccessibleDataObject(this.state.callOutAccessibilityData),
    };
    const tickParams = {
      tickValues: this.props.tickValues,
      tickFormat: this.props.tickFormat,
    };
    return !this._isChartEmpty() ? (
      <CartesianChart
        {...this.props}
        points={this._points}
        chartType={ChartTypes.VerticalBarChart}
        xAxisType={this._xAxisType}
        calloutProps={calloutProps}
        tickParams={tickParams}
        {...(this._isHavingLine && this._noLegendHighlighted() && { isCalloutForStack: true })}
        legendBars={legendBars}
        datasetForXAxisDomain={this._xAxisLabels}
        barwidth={this._barWidth}
        focusZoneDirection={FocusZoneDirection.horizontal}
        customizedCallout={this._getCustomizedCallout()}
        getmargins={this._getMargins}
        getGraphData={this._getGraphData}
        getAxisData={this._getAxisData}
        onChartMouseLeave={this._handleChartMouseLeave}
        getDomainMargins={this._getDomainMargins}
        {...(this._xAxisType === XAxisTypes.StringAxis && {
          xAxisInnerPadding: this._xAxisInnerPadding,
          xAxisOuterPadding: this._xAxisOuterPadding,
        })}
        /* eslint-disable react/jsx-no-bind */
        // eslint-disable-next-line react/no-children-prop
        children={(props: IChildProps) => {
          return (
            <>
              <g>{this._bars}</g>
              {this._isHavingLine && (
                <g>
                  {this._createLine(
                    props.xScale!,
                    props.yScale!,
                    props.containerHeight,
                    props.containerWidth,
                    props.yScaleSecondary,
                  )}
                </g>
              )}
            </>
          );
        }}
      />
    ) : (
      <div
        id={this._emptyChartId}
        role={'alert'}
        style={{ opacity: '0' }}
        aria-label={'Graph has no data to display'}
      />
    );
  }
 
  private _createLine = (
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    xScale: any,
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    yScale: any,
    IcontainerHeight: number = 0,
    IcontainerWidth: number = 0,
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    yScaleSecondary?: any,
  ): React.ReactNode => {
    const isStringAxis = this._xAxisType === XAxisTypes.StringAxis;
    const { xBarScale } = this._getScales(containerHeight, containerWidth);
    const colorScale = this._createColors();
    const { theme } = this.props;
    const { data, lineLegendColor = theme!.palette.yellow, lineLegendText } = this.props;
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    const lineData: Array<any> = [];
    const line: JSX.Element[] = [];
    data &&
      data.forEach((item: IVerticalBarChartDataPoint, index: number) => {
        if (item.lineData && item.lineData.y) {
          lineData.push({
            x: item.x,
            y: item.lineData!.y,
            useSecondaryYScale: item.lineData!.useSecondaryYScale ?? false,
            point: item,
            index,
          });
        }
      });
    const linePath = d3Line()
      // eslint-disable-next-line @typescript-eslint/no-explicit-any
      .x((d: any) => (isStringAxis ? xBarScale(d.x) + 0.5 * xBarScale.bandwidth() : xScale(d.x)))
      // eslint-disable-next-line @typescript-eslint/no-explicit-any
      .y((d: any) => (d.useSecondaryYScale && yScaleSecondary ? yScaleSecondary(d.y) : yScale(d.y)));
    const shouldHighlight = this._legendHighlighted(lineLegendText!) || this._noLegendHighlighted() ? true : false;
    const lineBorderWidth = this.props.lineOptions?.lineBorderWidth
      ? Number.parseFloat(this.props.lineOptions!.lineBorderWidth!.toString())
      : 0;
 
    Iif (lineBorderWidth > 0) {
      line.push(
        <path
          key={getId('_VBC_line_')}
          id={getId('_VBC_line_')}
          opacity={shouldHighlight ? 1 : 0.1}
          d={linePath(lineData)!}
          fill="transparent"
          strokeLinecap="square"
          strokeWidth={3 + lineBorderWidth * 2}
          stroke={theme!.semanticColors.bodyBackground}
        />,
      );
    }
    line.push(
      <path
        key={getId('_VBC_line_')}
        id={getId('_VBC_line_')}
        opacity={shouldHighlight ? 1 : 0.1}
        d={linePath(lineData)!}
        fill="transparent"
        strokeLinecap="square"
        strokeWidth={3}
        stroke={lineLegendColor}
      />,
    );
 
    const dots: React.ReactNode[] = lineData.map(
      (
        item: {
          x: number | string;
          y: number;
          useSecondaryYScale: boolean;
          point: IVerticalBarChartDataPoint;
          index: number;
        },
        index: number,
      ) => {
        // Create an object to store line point ref so that the object can be passed by reference to the focus handler
        const circleRef: { refElement: SVGCircleElement | null } = { refElement: null };
        return (
          <circle
            key={index}
            id={getId('_VBC_point_')}
            cx={isStringAxis ? xBarScale(item.x) + 0.5 * xBarScale.bandwidth() : xScale(item.x)}
            cy={item.useSecondaryYScale && yScaleSecondary ? yScaleSecondary(item.y) : yScale(item.y)}
            onMouseOver={
              this._legendHighlighted(lineLegendText!)
                ? this._lineHover.bind(this, item.point)
                : this._onBarHover.bind(this, item.point, colorScale(item.y))
            }
            onMouseOut={this._onBarLeave}
            r={this._getCircleVisibilityAndRadius(item.x, lineLegendText!).radius}
            stroke={lineLegendColor}
            fill={this.props.theme!.semanticColors.bodyBackground}
            strokeWidth={3}
            visibility={this._getCircleVisibilityAndRadius(item.x, lineLegendText!).visibility}
            onClick={item.point.lineData?.onClick}
            // When no legend is highlighted: Line points are automatically displayed along with the bars
            // at the same x-axis point in the stack callout. So to prevent an increase in focusable elements
            // and avoid conveying duplicate info, make these line points non-focusable.
            data-is-focusable={this._legendHighlighted(lineLegendText!)}
            ref={e => (circleRef.refElement = e)}
            onFocus={this._lineFocus.bind(this, item.point, circleRef)}
            onBlur={this._handleChartMouseLeave}
          />
        );
      },
    );
 
    return (
      <>
        {line}
        {dots}
      </>
    );
  };
 
  private _getCircleVisibilityAndRadius = (
    xAxisPoint: string | number | Date,
    legend: string,
  ): { visibility: CircleVisbility; radius: number } => {
    const { selectedLegend, activeXdataPoint } = this.state;
    Iif (selectedLegend !== '') {
      if (xAxisPoint === activeXdataPoint && selectedLegend === legend) {
        return { visibility: CircleVisbility.show, radius: 8 };
      } else if (selectedLegend === legend) {
        // Don't hide the circle to keep it focusable. For more information,
        // see https://fuzzbomb.github.io/accessibility-demos/visually-hidden-focus-test.html
        return { visibility: CircleVisbility.show, radius: 0.3 };
      } else {
        return { visibility: CircleVisbility.hide, radius: 0 };
      }
    } else {
      return {
        visibility: activeXdataPoint === xAxisPoint ? CircleVisbility.show : CircleVisbility.hide,
        radius: 8,
      };
    }
  };
 
  private _checkForLine = (): boolean => {
    const { data } = this.props;
    return data!.some((item: IVerticalBarChartDataPoint) => item?.lineData?.y !== undefined);
  };
 
  private _adjustProps(): void {
    this._points = this.props.data || [];
    this._barWidth = getBarWidth(this.props.barWidth, this.props.maxBarWidth);
    const { palette } = this.props.theme!;
    this._colors = this.props.colors || [palette.blueLight, palette.blue, palette.blueMid, palette.blueDark];
    this._isHavingLine = this._checkForLine();
    this._xAxisInnerPadding = getScalePadding(this.props.xAxisInnerPadding, this.props.xAxisPadding, 2 / 3);
    this._xAxisOuterPadding = getScalePadding(this.props.xAxisOuterPadding, this.props.xAxisPadding, 0);
  }
 
  private _getMargins = (margins: IMargins) => {
    this.margins = margins;
  };
 
  private _renderContentForBothLineAndBars = (point: IVerticalBarChartDataPoint): JSX.Element => {
    const { YValueHover, hoverXValue } = this._getCalloutContentForLineAndBar(point);
    const content: JSX.Element[] = YValueHover.map((item: IYValueHover, index: number) => {
      return (
        <ChartHoverCard
          key={index}
          Legend={item.legend}
          {...(index === 0 && { XValue: `${hoverXValue || item.data}` })}
          color={item.color}
          YValue={item.data || item.y}
          culture={this.props.culture}
        />
      );
    });
    return <>{content}</>;
  };
  private _renderContentForOnlyBars = (props: IVerticalBarChartDataPoint): JSX.Element => {
    const { useSingleColor = false } = this.props;
    return (
      <>
        <ChartHoverCard
          XValue={props.xAxisCalloutData || (props.x as string)}
          Legend={props.legend}
          YValue={props.yAxisCalloutData || props.y}
          color={!useSingleColor && props.color ? props.color : this._createColors()(props.y)}
          culture={this.props.culture}
        />
      </>
    );
  };
 
  private _renderCallout = (props?: IVerticalBarChartDataPoint): JSX.Element | null => {
    return props
      ? this._isHavingLine
        ? this._renderContentForBothLineAndBars(props)
        : this._renderContentForOnlyBars(props)
      : null;
  };
 
  private _getCustomizedCallout = () => {
    return this.props.onRenderCalloutPerDataPoint
      ? this.props.onRenderCalloutPerDataPoint(this.state.dataPointCalloutProps, this._renderCallout)
      : null;
  };
 
  private _getGraphData = (
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    xScale: any,
    yScale: NumericAxis,
    containerHeight: number,
    containerWidth: number,
    xElement?: SVGElement | null,
  ) => {
    return (this._bars =
      this._xAxisType === XAxisTypes.NumericAxis
        ? this._createNumericBars(containerHeight, containerWidth, xElement!)
        : this._xAxisType === XAxisTypes.DateAxis
        ? this._createDateBars(containerHeight, containerWidth, xElement!)
        : this._createStringBars(containerHeight, containerWidth, xElement!));
  };
 
  private _createColors(): D3ScaleLinear<string, string> | ColorScale {
    const increment = this._colors.length <= 1 ? 1 : 1 / (this._colors.length - 1);
    const { useSingleColor = false } = this.props;
    if (useSingleColor) {
      return (_p?: number) => {
        const { theme, colors } = this.props;
        return colors && colors.length > 0 ? colors[0] : theme!.palette.blueLight;
      };
    }
    const domainValues = [];
    for (let i = 0; i < this._colors.length; i++) {
      domainValues.push(increment * i * this._yMax);
    }
    const colorScale = d3ScaleLinear<string>().domain(domainValues).range(this._colors);
    return colorScale;
  }
 
  private _refCallback = (element: SVGRectElement, legendTitle: string): void => {
    this._refArray.push({ index: legendTitle, refElement: element });
  };
 
  private _getCalloutContentForLineAndBar = (
    point: IVerticalBarChartDataPoint,
  ): { YValueHover: IYValueHover[]; hoverXValue: string | number | null } => {
    const YValueHover: IYValueHover[] = [];
    const { theme, useSingleColor = false } = this.props;
    const { data, lineLegendText, lineLegendColor = theme!.palette.yellow } = this.props;
    const selectedPoint = data!.filter((xDataPoint: IVerticalBarChartDataPoint) => xDataPoint.x === point.x);
    // there might be no y value of the line for the hovered bar. so we need to check this condition
    if (this._isHavingLine && selectedPoint[0].lineData?.y !== undefined) {
      // callout data for the  line
      YValueHover.push({
        legend: lineLegendText,
        color: lineLegendColor,
        y: selectedPoint[0].lineData?.y,
        data: selectedPoint[0].lineData?.yAxisCalloutData,
        yAxisCalloutData: selectedPoint[0].lineData?.yAxisCalloutData,
      });
    }
    // callout data for the bar
    YValueHover.push({
      legend: selectedPoint[0].legend,
      y: selectedPoint[0].y,
      color: !useSingleColor
        ? selectedPoint[0].color
          ? selectedPoint[0].color
          : this._createColors()(selectedPoint[0].y)
        : this._createColors()(1),
      data: selectedPoint[0].yAxisCalloutData,
      yAxisCalloutData: selectedPoint[0].yAxisCalloutData,
    });
    const hoverXValue = point.x instanceof Date ? point.x.toLocaleString() : point.x.toString();
    return {
      YValueHover,
      hoverXValue: point.xAxisCalloutData || hoverXValue,
    };
  };
 
  private _onBarHover(
    point: IVerticalBarChartDataPoint,
    color: string,
    mouseEvent: React.MouseEvent<SVGElement>,
  ): void {
    mouseEvent.persist();
 
    const { YValueHover, hoverXValue } = this._getCalloutContentForLineAndBar(point);
    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,
        dataForHoverCard: point.y,
        calloutLegend: point.legend!,
        color: point.color || color,
        // To display callout value, if no callout value given, taking given point.x value as a string.
        xCalloutValue:
          point.xAxisCalloutData || (point.x instanceof Date ? point.x.toLocaleDateString() : point.x.toString()),
        yCalloutValue: point.yAxisCalloutData!,
        dataPointCalloutProps: point,
        // Hovering over a bar should highlight corresponding line points only when no legend is selected
        activeXdataPoint: this._noLegendHighlighted() ? point.x : null,
        YValueHover,
        hoverXValue,
        callOutAccessibilityData: point.callOutAccessibilityData,
      });
    }
  }
 
  private _onBarLeave = (): void => {
    /**/
  };
 
  private _handleChartMouseLeave = (): void => {
    this._calloutAnchorPoint = null;
    this.setState({
      isCalloutVisible: false,
      activeXdataPoint: null,
      YValueHover: [],
      hoverXValue: '',
    });
  };
 
  private _onBarFocus = (point: IVerticalBarChartDataPoint, refArrayIndexNumber: number, color: string): void => {
    const { YValueHover, hoverXValue } = this._getCalloutContentForLineAndBar(point);
    this._refArray.forEach((obj: IRefArrayData, index: number) => {
      Iif (obj.index === point.legend! && refArrayIndexNumber === index) {
        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: point.y,
          color: point.color || color,
          xCalloutValue:
            point.xAxisCalloutData || (point.x instanceof Date ? point.x.toLocaleDateString() : point.x.toString()),
          yCalloutValue: point.yAxisCalloutData!,
          dataPointCalloutProps: point,
          activeXdataPoint: point.x,
          YValueHover,
          hoverXValue,
          callOutAccessibilityData: point.callOutAccessibilityData,
        });
      }
    });
  };
 
  private _lineHover = (point: IVerticalBarChartDataPoint, mouseEvent: React.MouseEvent<SVGElement>) => {
    mouseEvent.persist();
    this._lineHoverFocus(point, mouseEvent);
  };
 
  private _lineFocus = (point: IVerticalBarChartDataPoint, ref: { refElement: SVGCircleElement | null }) => {
    Iif (ref.refElement) {
      this._lineHoverFocus(point, ref.refElement);
    }
  };
 
  private _lineHoverFocus = (
    point: IVerticalBarChartDataPoint,
    refSelected: React.MouseEvent<SVGElement> | SVGCircleElement,
  ) => {
    const { theme } = this.props;
    const { lineLegendText = '', lineLegendColor = theme!.palette.yellow } = this.props;
    this.setState({
      refSelected,
      isCalloutVisible: true,
      calloutLegend: lineLegendText,
      dataForHoverCard: point.lineData!.y,
      color: lineLegendColor,
      xCalloutValue:
        point.xAxisCalloutData || (point.x instanceof Date ? point.x.toLocaleDateString() : point.x.toString()),
      yCalloutValue: point.lineData!.yAxisCalloutData,
      dataPointCalloutProps: point,
      activeXdataPoint: point.x,
    });
  };
 
  private _getScales = (
    containerHeight: number,
    containerWidth: number,
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
  ): { xBarScale: any; yBarScale: any } => {
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    let xBarScale: any;
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    const yBarScale: any = d3ScaleLinear()
      .domain([0, this._yMax])
      .range([0, containerHeight - this.margins.bottom! - this.margins.top!]);
 
    if (this._xAxisType === XAxisTypes.NumericAxis) {
      const xMax = d3Max(this._points, (point: IVerticalBarChartDataPoint) => point.x as number)!;
      const xMin = d3Min(this._points, (point: IVerticalBarChartDataPoint) => point.x as number)!;
      xBarScale = d3ScaleLinear()
        .domain(this._isRtl ? [xMax, xMin] : [xMin, xMax])
        .nice()
        .range([this.margins.left! + this._domainMargin, containerWidth - this.margins.right! - this._domainMargin]);
    } else if (this._xAxisType === XAxisTypes.DateAxis) {
      const sDate = d3Min(this._points, (point: IVerticalBarChartDataPoint) => point.x as Date)!;
      const lDate = d3Max(this._points, (point: IVerticalBarChartDataPoint) => point.x as Date)!;
      xBarScale = d3ScaleUtc()
        .domain([sDate, lDate])
        .range(
          this._isRtl
            ? [containerWidth - this.margins.right! - this._domainMargin, this.margins.left! + this._domainMargin]
            : [this.margins.left! + this._domainMargin, containerWidth - this.margins.right! - this._domainMargin],
        );
    } else {
      xBarScale = d3ScaleBand()
        .domain(this._xAxisLabels)
        .range(
          this._isRtl
            ? [containerWidth - this.margins.right! - this._domainMargin, this.margins.left! + this._domainMargin]
            : [this.margins.left! + this._domainMargin, containerWidth - this.margins.right! - this._domainMargin],
        )
        .paddingInner(this._xAxisInnerPadding)
        .paddingOuter(this._xAxisOuterPadding);
    }
 
    return { xBarScale, yBarScale };
  };
 
  private _createNumericBars(containerHeight: number, containerWidth: number, xElement: SVGElement): JSX.Element[] {
    const { useSingleColor = false } = this.props;
    const { xBarScale, yBarScale } = this._getScales(containerHeight, containerWidth);
    const colorScale = this._createColors();
    const bars = this._points.map((point: IVerticalBarChartDataPoint, index: number) => {
      const shouldHighlight = this._legendHighlighted(point.legend!) || this._noLegendHighlighted() ? true : false;
      this._classNames = getClassNames(this.props.styles!, {
        theme: this.props.theme!,
        legendColor: this.state.color,
        shouldHighlight,
      });
      const barHeight: number = Math.max(yBarScale(point.y), 0);
      let adjustedBarHeight = 0;
      if (barHeight <= 0) {
        return <React.Fragment key={point.x as string}> </React.Fragment>;
      } else Iif (barHeight <= Math.ceil(yBarScale(this._yMax) / 100.0)) {
        adjustedBarHeight = Math.ceil(yBarScale(this._yMax) / 100.0);
      } else {
        adjustedBarHeight = barHeight;
      }
      const xPoint = xBarScale(point.x as number) - this._barWidth / 2;
      const yPoint = containerHeight - this.margins.bottom! - adjustedBarHeight;
      return (
        <g key={point.x as string}>
          <rect
            id={getId('_VBC_bar_')}
            x={xPoint}
            className={this._classNames.opacityChangeOnHover}
            y={yPoint}
            width={this._barWidth}
            data-is-focusable={!this.props.hideTooltip && shouldHighlight}
            height={adjustedBarHeight}
            ref={(e: SVGRectElement) => {
              this._refCallback(e, point.legend!);
            }}
            onClick={point.onClick}
            onMouseOver={this._onBarHover.bind(this, point, colorScale(point.y))}
            aria-label={this._getAriaLabel(point)}
            role="img"
            onMouseLeave={this._onBarLeave}
            onFocus={this._onBarFocus.bind(this, point, index, colorScale(point.y))}
            onBlur={this._onBarLeave}
            fill={point.color && !useSingleColor ? point.color : colorScale(point.y)}
          />
          {this._renderBarLabel(xPoint, yPoint, point.y, point.legend!)}
        </g>
      );
    });
    // Removing un wanted tooltip div from DOM, when prop not provided.
    if (!this.props.showXAxisLablesTooltip) {
      try {
        document.getElementById(this._tooltipId) && document.getElementById(this._tooltipId)!.remove();
        // eslint-disable-next-line no-empty
      } catch (e) {}
    }
    // Used to display tooltip at x axis labels.
    if (!this.props.wrapXAxisLables && this.props.showXAxisLablesTooltip) {
      const xAxisElement = d3Select(xElement).call(xBarScale);
      try {
        document.getElementById(this._tooltipId) && document.getElementById(this._tooltipId)!.remove();
        // eslint-disable-next-line no-empty
      } catch (e) {}
      const tooltipProps = {
        tooltipCls: this._classNames.tooltip!,
        id: this._tooltipId,
        xAxis: xAxisElement,
      };
      xAxisElement && tooltipOfXAxislabels(tooltipProps);
    }
    return bars;
  }
 
  private _createStringBars(containerHeight: number, containerWidth: number, xElement: SVGElement): JSX.Element[] {
    const { xBarScale, yBarScale } = this._getScales(containerHeight, containerWidth);
    const colorScale = this._createColors();
    const bars = this._points.map((point: IVerticalBarChartDataPoint, index: number) => {
      const barHeight: number = Math.max(yBarScale(point.y), 0);
      let adjustedBarHeight = 0;
      if (barHeight <= 0) {
        return <React.Fragment key={point.x instanceof Date ? point.x.getTime() : point.x}> </React.Fragment>;
      } else Iif (barHeight <= Math.ceil(yBarScale(this._yMax) / 100.0)) {
        adjustedBarHeight = Math.ceil(yBarScale(this._yMax) / 100.0);
      } else {
        adjustedBarHeight = barHeight;
      }
      const xPoint = xBarScale(point.x);
      const yPoint = containerHeight - this.margins.bottom! - adjustedBarHeight;
      // Setting the bar width here is safe because there are no dependencies earlier in the code
      // that rely on the width of bars in vertical bar charts with string x-axis.
      this._barWidth = getBarWidth(this.props.barWidth, this.props.maxBarWidth, xBarScale.bandwidth());
      return (
        <g
          key={point.x instanceof Date ? point.x.getTime() : point.x}
          transform={`translate(${0.5 * (xBarScale.bandwidth() - this._barWidth)}, 0)`}
        >
          <rect
            id={getId('_VBC_bar_')}
            x={xPoint}
            y={yPoint}
            width={this._barWidth}
            height={adjustedBarHeight}
            aria-label={this._getAriaLabel(point)}
            role="img"
            ref={(e: SVGRectElement) => {
              this._refCallback(e, point.legend!);
            }}
            onClick={point.onClick}
            onMouseOver={this._onBarHover.bind(this, point, colorScale(point.y))}
            onMouseLeave={this._onBarLeave}
            onBlur={this._onBarLeave}
            data-is-focusable={!this.props.hideTooltip}
            onFocus={this._onBarFocus.bind(this, point, index, colorScale(point.y))}
            fill={point.color ? point.color : colorScale(point.y)}
          />
          {this._renderBarLabel(xPoint, yPoint, point.y, point.legend!)}
        </g>
      );
    });
 
    // Removing un wanted tooltip div from DOM, when prop not provided.
    if (!this.props.showXAxisLablesTooltip) {
      try {
        document.getElementById(this._tooltipId) && document.getElementById(this._tooltipId)!.remove();
        // eslint-disable-next-line no-empty
      } catch (e) {}
    }
    // Used to display tooltip at x axis labels.
    Iif (!this.props.wrapXAxisLables && this.props.showXAxisLablesTooltip) {
      const xAxisElement = d3Select(xElement).call(xBarScale);
      try {
        document.getElementById(this._tooltipId) && document.getElementById(this._tooltipId)!.remove();
        // eslint-disable-next-line no-empty
      } catch (e) {}
      const tooltipProps = {
        tooltipCls: this._classNames.tooltip!,
        id: this._tooltipId,
        xAxis: xAxisElement,
        showTooltip: this.props.showXAxisLablesTooltip,
      };
      xAxisElement && tooltipOfXAxislabels(tooltipProps);
    }
    return bars;
  }
 
  private _createDateBars(containerHeight: number, containerWidth: number, xElement: SVGElement): JSX.Element[] {
    const { useSingleColor = false } = this.props;
    const { xBarScale, yBarScale } = this._getScales(containerHeight, containerWidth);
    const colorScale = this._createColors();
    const bars = this._points.map((point: IVerticalBarChartDataPoint, index: number) => {
      const shouldHighlight = this._legendHighlighted(point.legend!) || this._noLegendHighlighted() ? true : false;
      this._classNames = getClassNames(this.props.styles!, {
        theme: this.props.theme!,
        legendColor: this.state.color,
        shouldHighlight: shouldHighlight,
      });
      const barHeight: number = Math.max(yBarScale(point.y), 0);
      let adjustedBarHeight = 0;
      if (barHeight <= 0) {
        return <React.Fragment key={point.x instanceof Date ? point.x.getTime() : point.x}> </React.Fragment>;
      } else Iif (barHeight <= Math.ceil(yBarScale(this._yMax) / 100.0)) {
        adjustedBarHeight = Math.ceil(yBarScale(this._yMax) / 100.0);
      } else {
        adjustedBarHeight = barHeight;
      }
      const xPoint = xBarScale(point.x as number) - this._barWidth / 2;
      const yPoint = containerHeight - this.margins.bottom! - adjustedBarHeight;
      return (
        <g key={point.x instanceof Date ? point.x.getTime() : point.x}>
          <rect
            id={getId('_VBC_bar_')}
            x={xPoint}
            className={this._classNames.opacityChangeOnHover}
            y={yPoint}
            width={this._barWidth}
            data-is-focusable={!this.props.hideTooltip && shouldHighlight}
            height={adjustedBarHeight}
            ref={(e: SVGRectElement) => {
              this._refCallback(e, point.legend!);
            }}
            onClick={point.onClick}
            onMouseOver={this._onBarHover.bind(this, point, colorScale(point.y))}
            aria-label={this._getAriaLabel(point)}
            role="img"
            onMouseLeave={this._onBarLeave}
            onFocus={this._onBarFocus.bind(this, point, index, colorScale(point.y))}
            onBlur={this._onBarLeave}
            fill={point.color && !useSingleColor ? point.color : colorScale(point.y)}
          />
          {this._renderBarLabel(xPoint, yPoint, point.y, point.legend!)}
        </g>
      );
    });
    // Removing un wanted tooltip div from DOM, when prop not provided.
    if (!this.props.showXAxisLablesTooltip) {
      try {
        document.getElementById(this._tooltipId) && document.getElementById(this._tooltipId)!.remove();
        // eslint-disable-next-line no-empty
      } catch (e) {}
    }
    // Used to display tooltip at x axis labels.
    Iif (!this.props.wrapXAxisLables && this.props.showXAxisLablesTooltip) {
      const xAxisElement = d3Select(xElement).call(xBarScale);
      try {
        document.getElementById(this._tooltipId) && document.getElementById(this._tooltipId)!.remove();
        // eslint-disable-next-line no-empty
      } catch (e) {}
      const tooltipProps = {
        tooltipCls: this._classNames.tooltip!,
        id: this._tooltipId,
        xAxis: xAxisElement,
      };
      xAxisElement && tooltipOfXAxislabels(tooltipProps);
    }
    return bars;
  }
 
  private _closeCallout = () => {
    this.setState({
      isCalloutVisible: false,
    });
  };
 
  private _onLegendClick(legendTitle: string): void {
    if (this.state.selectedLegend === legendTitle) {
      this.setState({
        selectedLegend: '',
      });
    } else {
      this.setState({
        selectedLegend: legendTitle,
      });
    }
  }
 
  private _onLegendHover(legendTitle: string): void {
    this.setState({
      activeLegend: legendTitle,
    });
  }
 
  private _onLegendLeave(): void {
    this.setState({
      activeLegend: '',
    });
  }
 
  private _getLegendData = (data: IVerticalBarChartDataPoint[], palette: IPalette): JSX.Element => {
    const { theme, useSingleColor } = this.props;
    const { lineLegendText, lineLegendColor = theme!.palette.yellow } = this.props;
    const actions: ILegend[] = [];
    data.forEach((point: IVerticalBarChartDataPoint, _index: number) => {
      const color: string = !useSingleColor ? point.color! : this._createColors()(1);
      // mapping data to the format Legends component needs
      const legend: ILegend = {
        title: point.legend!,
        color,
        action: () => {
          this._onLegendClick(point.legend!);
        },
        hoverAction: () => {
          this._handleChartMouseLeave();
          this._onLegendHover(point.legend!);
        },
        onMouseOutAction: () => {
          this._onLegendLeave();
        },
      };
      actions.push(legend);
    });
    if (this._isHavingLine && lineLegendText && lineLegendColor) {
      const lineLegend: ILegend = {
        title: lineLegendText,
        color: lineLegendColor,
        action: () => {
          this._onLegendClick(lineLegendText);
        },
        hoverAction: () => {
          this._handleChartMouseLeave();
          this._onLegendHover(lineLegendText);
        },
        onMouseOutAction: () => {
          this._onLegendLeave();
        },
        isLineLegendInBarChart: true,
      };
      actions.unshift(lineLegend);
    }
    const legends = (
      <Legends
        legends={actions}
        enabledWrapLines={this.props.enabledLegendsWrapLines}
        overflowProps={this.props.legendsOverflowProps}
        focusZonePropsInHoverCard={this.props.focusZonePropsForLegendsInHoverCard}
        overflowText={this.props.legendsOverflowText}
        {...this.props.legendProps}
      />
    );
    return legends;
  };
 
  private _getAxisData = (yAxisData: IAxisData) => {
    if (yAxisData && yAxisData.yAxisDomainValues.length) {
      const { yAxisDomainValues: domainValue } = yAxisData;
      this._yMax = Math.max(domainValue[domainValue.length - 1], this.props.yMaxValue || 0);
    }
  };
 
  /**
   * 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: IVerticalBarChartDataPoint): string => {
    const xValue = point.xAxisCalloutData
      ? point.xAxisCalloutData
      : point.x instanceof Date
      ? point.x.toLocaleString()
      : point.x;
    const legend = point.legend;
    const yValue = point.yAxisCalloutData || point.y;
    const lineLegend = this.props.lineLegendText || 'Line';
    const lineYValue = point.lineData?.yAxisCalloutData || point.lineData?.y;
    return (
      point.callOutAccessibilityData?.ariaLabel ||
      `${xValue}. ` +
        (legend ? `${legend}, ` : '') +
        `${yValue}.` +
        (typeof lineYValue !== 'undefined' ? ` ${lineLegend}, ${lineYValue}.` : '')
    );
  };
 
  private _renderBarLabel(xPoint: number, yPoint: number, barValue: number, legend: string) {
    if (
      this.props.hideLabels ||
      this._barWidth < 16 ||
      !(this._legendHighlighted(legend) || this._noLegendHighlighted())
    ) {
      return null;
    }
 
    return (
      <text
        x={xPoint + this._barWidth / 2}
        y={yPoint - 6}
        textAnchor="middle"
        className={this._classNames.barLabel}
        aria-hidden={true}
      >
        {formatValueWithSIPrefix(barValue)}
      </text>
    );
  }
 
  private _getDomainMargins = (containerWidth: number): IMargins => {
    this._domainMargin = MIN_DOMAIN_MARGIN;
 
    /** Total width available to render the bars */
    const totalWidth =
      containerWidth - (this.margins.left! + MIN_DOMAIN_MARGIN) - (this.margins.right! + MIN_DOMAIN_MARGIN);
 
    if (this._xAxisType === XAxisTypes.StringAxis) {
      Iif (isScalePaddingDefined(this.props.xAxisOuterPadding, this.props.xAxisPadding)) {
        // Setting the domain margin for string x-axis to 0 because the xAxisOuterPadding prop is now available
        // to adjust the space before the first bar and after the last bar.
        this._domainMargin = 0;
      } else if (this.props.barWidth !== 'auto') {
        /** Rate at which the space between the bars changes wrt the bar width */
        const barGapRate = this._xAxisInnerPadding / (1 - this._xAxisInnerPadding);
        // Update the bar width so that when CartesianChart rerenders,
        // the following calculations don't use the previous bar width.
        this._barWidth = getBarWidth(this.props.barWidth, this.props.maxBarWidth);
        /** Total width required to render the bars. Directly proportional to bar width */
        const reqWidth = (this._xAxisLabels.length + (this._xAxisLabels.length - 1) * barGapRate) * this._barWidth;
 
        if (totalWidth >= reqWidth) {
          // Center align the chart by setting equal left and right margins for domain
          this._domainMargin = MIN_DOMAIN_MARGIN + (totalWidth - reqWidth) / 2;
        }
      }
    } else {
      const data = (this.props.data?.map(point => point.x) as number[] | Date[] | undefined) || [];
      this._barWidth = getBarWidth(
        this.props.barWidth,
        this.props.maxBarWidth,
        calculateAppropriateBarWidth(data, totalWidth),
      );
      this._domainMargin = MIN_DOMAIN_MARGIN + this._barWidth / 2;
    }
 
    return {
      ...this.margins,
      left: this.margins.left! + this._domainMargin,
      right: this.margins.right! + this._domainMargin,
    };
  };
 
  private _isChartEmpty(): boolean {
    return (
      this._points.length === 0 ||
      (d3Max(this._points, (point: IVerticalBarChartDataPoint) => point.y)! <= 0 && !this._isHavingLine)
    );
  }
}