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

92.56% Statements 224/242
64.35% Branches 139/216
85.71% Functions 42/49
92.41% Lines 195/211

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 68924x 24x 24x 24x 24x   24x   24x                 24x           24x 24x                         24x                                 24x                     57x                   57x 57x                           57x 57x 57x 57x       57x           51x 51x 51x 205x 51x 205x     51x 51x       51x                               51x       51x                                         75x                   24x 63x 63x 63x 63x     57x 80x     57x 5x 5x 5x 25x 5x       5x         5x           5x                         57x       57x 51x         57x               75x           24x 2x 2x 2x 1x 3x 3x     1x 1x 3x   1x 1x     57x 306x     57x     13x 13x 13x 53x 13x 13x 53x 13x       13x         13x             13x               13x     24x         12x   12x 12x         8x 8x                                   57x       57x 3x 3x               57x                                                             57x           80x 257x 257x 63x       63x     63x   66x       17x         17x       17x         61x           61x 61x 61x 61x 372x 372x 372x   61x 247x 247x 36x   247x         247x 247x 118x     129x     5x   124x       129x                           240x                           61x     24x 9x 3x     3x 3x 3x 3x 3x 4x 4x   3x 3x 4x 4x 4x                                   24x           16x 16x 16x 64x 64x 30x     34x         34x         34x                               66x                               16x 13x 13x         16x 3x 3x 3x 3x       3x         3x   16x     57x           24x 5x 1x 1x                   4x             24x 2x 2x             24x 1x 1x               57x 54x 54x 54x 212x           212x       5x     2x 2x     1x     212x     54x                 54x     57x 77x   2x 2x     57x 169x 169x 169x   24x  
import * as React from 'react';
import { max as d3Max } from 'd3-array';
import { select as d3Select } from 'd3-selection';
import { scaleLinear as d3ScaleLinear, ScaleLinear as D3ScaleLinear, scaleBand as d3ScaleBand } 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 { ILegend } from '../../components/Legends/Legends.types';
import { Legends } from '../../components/Legends/Legends';
import {
  IAccessibilityProps,
  IBasestate,
  IHorizontalBarChartWithAxisDataPoint,
  IRefArrayData,
  IMargins,
} from '../../types/IDataPoint';
import { IChildProps, IYValueHover } from '../CommonComponents/CartesianChart.types';
import { CartesianChart } from '../CommonComponents/CartesianChart';
import {
  IHorizontalBarChartWithAxisProps,
  IHorizontalBarChartWithAxisStyleProps,
  IHorizontalBarChartWithAxisStyles,
} from './HorizontalBarChartWithAxis.types';
import { FocusZoneDirection } from '@fluentui/react-focus';
import {
  ChartHoverCard,
  ChartTypes,
  IAxisData,
  getAccessibleDataObject,
  YAxisType,
  XAxisTypes,
  NumericAxis,
  StringAxis,
  getTypeOfAxis,
  getNextColor,
} from '../../utilities/index';
 
const getClassNames = classNamesFunction<IHorizontalBarChartWithAxisStyleProps, IHorizontalBarChartWithAxisStyles>();
export interface IHorizontalBarChartWithAxisState extends IBasestate {
  selectedLegendTitle: string;
  dataPointCalloutProps?: IHorizontalBarChartWithAxisDataPoint; // define this in hover and focus
  /**
   * data point of x, where rectangle is hovered or focused
   */
  activeXdataPoint: number | null;
  YValueHover: IYValueHover[];
  hoverXValue?: string | number | null;
  callOutAccessibilityData?: IAccessibilityProps;
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  tooltipElement?: any;
}
 
type ColorScale = (_p?: number) => string;
 
export class HorizontalBarChartWithAxisBase extends React.Component<
  IHorizontalBarChartWithAxisProps,
  IHorizontalBarChartWithAxisState
> {
  private _points: IHorizontalBarChartWithAxisDataPoint[];
  private _barHeight: number;
  private _colors: string[];
  private _classNames: IProcessedStyleSet<IHorizontalBarChartWithAxisStyles>;
  private _refArray: IRefArrayData[];
  private _calloutId: string;
  private margins: IMargins;
  private _isRtl: boolean = getRTL();
  private _bars: JSX.Element[];
  private _yAxisLabels: string[];
  private _xMax: number;
  private _tooltipId: string;
  private _xAxisType: XAxisTypes;
  private _yAxisType: YAxisType;
  private _calloutAnchorPoint: IHorizontalBarChartWithAxisDataPoint | null;
 
  public constructor(props: IHorizontalBarChartWithAxisProps) {
    super(props);
    this.state = {
      color: '',
      dataForHoverCard: 0,
      isCalloutVisible: false,
      isLegendSelected: false,
      isLegendHovered: false,
      refSelected: null,
      selectedLegendTitle: '',
      xCalloutValue: '',
      yCalloutValue: '',
      activeXdataPoint: null,
      YValueHover: [],
      hoverXValue: '',
    };
    this._calloutId = getId('callout');
    this._tooltipId = getId('HBCWATooltipID_');
    this._refArray = [];
    this._xAxisType =
      this.props.data! && this.props.data!.length > 0
        ? (getTypeOfAxis(this.props.data![0].x, true) as XAxisTypes)
        : XAxisTypes.NumericAxis;
    this._yAxisType =
      this.props.data! && this.props.data!.length > 0
        ? (getTypeOfAxis(this.props.data![0].y, false) as YAxisType)
        : YAxisType.StringAxis;
  }
 
  public render(): JSX.Element {
    this._adjustProps();
    const reversedBars = [...this._points].reverse();
    this._yAxisLabels = reversedBars.map((point: IHorizontalBarChartWithAxisDataPoint) => point.y as string);
    this._xMax = Math.max(
      d3Max(this._points, (point: IHorizontalBarChartWithAxisDataPoint) => point.x)!,
      this.props.xMaxValue || 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,
      gapSpace: 15,
      color: this.state.color,
      legend: this.state.selectedLegendTitle,
      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 (
      <CartesianChart
        {...this.props}
        points={this._points}
        chartType={ChartTypes.HorizontalBarChartWithAxis}
        xAxisType={this._xAxisType}
        yAxisType={this._yAxisType}
        stringDatasetForYAxisDomain={this._yAxisLabels}
        calloutProps={calloutProps}
        tickParams={tickParams}
        legendBars={legendBars}
        barwidth={this._barHeight}
        focusZoneDirection={FocusZoneDirection.vertical}
        customizedCallout={this._getCustomizedCallout()}
        getmargins={this._getMargins}
        getGraphData={this._getGraphData}
        getAxisData={this._getAxisData}
        onChartMouseLeave={this._handleChartMouseLeave}
        /* eslint-disable react/jsx-no-bind */
        // eslint-disable-next-line react/no-children-prop
        children={(props: IChildProps) => {
          return (
            <>
              <g>{this._bars}</g>
            </>
          );
        }}
      />
    );
  }
 
  private _adjustProps(): void {
    this._points = this.props.data || [];
    this._barHeight = this.props.barHeight || 32;
    const { palette } = this.props.theme!;
    this._colors = this.props.colors || [palette.blueLight, palette.blue, palette.blueMid, palette.blueDark];
  }
 
  private _getMargins = (margins: IMargins) => {
    this.margins = margins;
  };
 
  private _renderContentForOnlyBars = (point: IHorizontalBarChartWithAxisDataPoint): JSX.Element => {
    const { useSingleColor = false } = this.props;
    let selectedPointIndex = 0;
    this.props.data!.forEach((yDataPoint: IHorizontalBarChartWithAxisDataPoint, index: number) => {
      if (yDataPoint.y === point.y) {
        selectedPointIndex = index;
      }
    });
    let color: string;
    Iif (useSingleColor) {
      //if useSingle color , then check if user has given a palette or not
      // and pick the first color from that or else from our paltette.
      color = this.props.colors ? this._createColors()(1) : getNextColor(1, 0, this.props.theme?.isInverted);
    } else {
      color = point.color
        ? point.color
        : this.props.colors
        ? this._createColors()(point.x)
        : getNextColor(selectedPointIndex, 0, this.props.theme?.isInverted);
    }
    return (
      <>
        <ChartHoverCard
          XValue={point.xAxisCalloutData || point.x.toString()}
          Legend={point.legend}
          YValue={point.yAxisCalloutData || point.y}
          color={color}
          culture={this.props.culture}
        />
      </>
    );
  };
 
  private _renderCallout = (props?: IHorizontalBarChartWithAxisDataPoint): JSX.Element | null => {
    return props ? this._renderContentForOnlyBars(props) : null;
  };
 
  private _getCustomizedCallout = () => {
    return this.props.onRenderCalloutPerDataPoint
      ? this.props.onRenderCalloutPerDataPoint(this.state.dataPointCalloutProps, this._renderCallout)
      : null;
  };
 
  private _getGraphData = (
    xScale: NumericAxis,
    yScale: NumericAxis | StringAxis,
    containerHeight: number,
    containerWidth: number,
    xElement?: SVGElement | null,
    yElement?: SVGElement | null,
  ) => {
    return (this._bars =
      this._yAxisType === YAxisType.NumericAxis
        ? this._createNumericBars(containerHeight, containerWidth, xElement!, yElement!)
        : this._createStringBars(containerHeight, containerWidth, xElement!, yElement!));
  };
 
  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._xMax);
    }
    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 _getCalloutContentForBar = (
    point: IHorizontalBarChartWithAxisDataPoint,
  ): { YValueHover: IYValueHover[]; hoverXValue: string | number | null } => {
    const YValueHover: IYValueHover[] = [];
    const { useSingleColor = false } = this.props;
    const { data } = this.props;
    const selectedPoint = data!.filter((yDataPoint: IHorizontalBarChartWithAxisDataPoint) => yDataPoint.y === point.y);
    let selectedPointIndex = 0;
    data!.forEach((yDataPoint: IHorizontalBarChartWithAxisDataPoint, index: number) => {
      if (yDataPoint.y === point.y) {
        selectedPointIndex = index;
      }
    });
    let color: string;
    Iif (useSingleColor) {
      //if useSingle color , then check if user has given a palette or not
      // and pick the first color from that or else from our paltette.
      color = this.props.colors ? this._createColors()(1) : getNextColor(1, 0, this.props.theme?.isInverted);
    } else {
      color = selectedPoint[0].color
        ? selectedPoint[0].color
        : this.props.colors
        ? this._createColors()(selectedPoint[0].x)
        : getNextColor(selectedPointIndex, 0, this.props.theme?.isInverted);
    }
    // callout data for the bar
    YValueHover.push({
      legend: selectedPoint[0].legend,
      // For HBCWA x and y Values are swapped
      y: selectedPoint[0].x,
      color,
      data: selectedPoint[0].yAxisCalloutData,
      yAxisCalloutData: selectedPoint[0].yAxisCalloutData,
    });
    return { YValueHover, hoverXValue: point.yAxisCalloutData || point.y.toString() };
  };
 
  private _onBarHover(
    point: IHorizontalBarChartWithAxisDataPoint,
    color: string,
    mouseEvent: React.MouseEvent<SVGElement>,
  ): void {
    mouseEvent.persist();
 
    const { YValueHover, hoverXValue } = this._getCalloutContentForBar(point);
    if (
      (this.state.isLegendSelected === false ||
        (this.state.isLegendSelected && this.state.selectedLegendTitle === point.legend)) &&
      this._calloutAnchorPoint !== point
    ) {
      this._calloutAnchorPoint = point;
      this.setState({
        refSelected: mouseEvent,
        isCalloutVisible: true,
        dataForHoverCard: point.x,
        selectedLegendTitle: point.legend!,
        color: this.props.useSingleColor ? color : point.color,
        // To display callout value, if no callout value given, taking given point.x value as a string.
        xCalloutValue: point.yAxisCalloutData! || point.y.toString(),
        yCalloutValue: point.xAxisCalloutData || point.x.toString(),
        dataPointCalloutProps: point,
        activeXdataPoint: point.x,
        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: IHorizontalBarChartWithAxisDataPoint,
    refArrayIndexNumber: number,
    color: string,
  ): void => {
    Iif (
      this.state.isLegendSelected === false ||
      (this.state.isLegendSelected && this.state.selectedLegendTitle === point.legend)
    ) {
      const { YValueHover, hoverXValue } = this._getCalloutContentForBar(point);
      this._refArray.forEach((obj: IRefArrayData, index: number) => {
        Iif (refArrayIndexNumber === index) {
          this.setState({
            refSelected: obj.refElement,
            isCalloutVisible: true,
            selectedLegendTitle: point.legend!,
            dataForHoverCard: point.x,
            color: this.props.useSingleColor ? color : point.color,
            xCalloutValue: point.yAxisCalloutData || point.y.toString(),
            yCalloutValue: point.xAxisCalloutData! || point.x.toString(),
            dataPointCalloutProps: point,
            activeXdataPoint: point.x,
            YValueHover,
            hoverXValue,
            callOutAccessibilityData: point.callOutAccessibilityData,
          });
        }
      });
    }
  };
 
  private _getScales = (
    containerHeight: number,
    containerWidth: number,
    isNumericScale: boolean,
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
  ): { xBarScale: any; yBarScale: any } => {
    if (isNumericScale) {
      const xMax = d3Max(this._points, (point: IHorizontalBarChartWithAxisDataPoint) => point.x as number)!;
      const yMax = d3Max(this._points, (point: IHorizontalBarChartWithAxisDataPoint) => point.y as number)!;
      const xBarScale = d3ScaleLinear()
        .domain(this._isRtl ? [xMax, 0] : [0, xMax])
        .nice()
        .range([this.margins.left!, containerWidth - this.margins.right!]);
      const yBarScale = d3ScaleLinear()
        .domain([0, yMax])
        .range([containerHeight - this.margins.bottom!, this.margins.top!]);
      return { xBarScale, yBarScale };
    } else {
      const xMax = d3Max(this._points, (point: IHorizontalBarChartWithAxisDataPoint) => point.x as number)!;
      // please note these padding default values must be consistent in here
      // and CatrtesianChartBase w for more details refer example
      // http://using-d3js.com/04_07_ordinal_scales.html
      const yBarScale = d3ScaleBand()
        .domain(this._yAxisLabels)
        .range([containerHeight - this.margins.bottom! - this._barHeight / 2, this.margins.top! + this._barHeight / 2])
        .padding(this.props.yAxisPadding || 0);
 
      const xBarScale = d3ScaleLinear()
        .domain(this._isRtl ? [xMax, 0] : [0, xMax])
        .nice()
        .range([this.margins.left!, containerWidth - this.margins.right!]);
      return { xBarScale, yBarScale };
    }
  };
 
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  private _createNumericBars(
    containerHeight: number,
    containerWidth: number,
    xElement: SVGElement,
    yElement: SVGElement,
  ): JSX.Element[] {
    const { useSingleColor = false } = this.props;
    const { xBarScale, yBarScale } = this._getScales(containerHeight, containerWidth, true);
    const sortedBars: IHorizontalBarChartWithAxisDataPoint[] = [...this._points];
    sortedBars.sort((a, b) => {
      const aValue = typeof a.y === 'number' ? a.y : parseFloat(a.y);
      const bValue = typeof b.y === 'number' ? b.y : parseFloat(b.y);
      return bValue - aValue;
    });
    const bars = sortedBars.map((point: IHorizontalBarChartWithAxisDataPoint, index: number) => {
      let shouldHighlight = true;
      if (this.state.isLegendHovered || this.state.isLegendSelected) {
        shouldHighlight = this.state.selectedLegendTitle === point.legend;
      }
      this._classNames = getClassNames(this.props.styles!, {
        theme: this.props.theme!,
        legendColor: this.state.color,
        shouldHighlight,
      });
      const barHeight: number = Math.max(yBarScale(point.y), 0);
      if (barHeight < 1) {
        return <React.Fragment key={point.x}> </React.Fragment>;
      }
      let color: string;
      if (useSingleColor) {
        //if useSingle color , then check if user has given a palette or not
        // and pick the first color from that or else from our paltette.
        color = this.props.colors ? this._createColors()(1) : getNextColor(1, 0, this.props.theme?.isInverted);
      } else {
        color = this.props.colors
          ? this._createColors()(point.x)
          : getNextColor(index, 0, this.props.theme?.isInverted);
      }
      return (
        <rect
          key={point.y}
          x={this._isRtl ? xBarScale(point.x) : this.margins.left!}
          className={this._classNames.opacityChangeOnHover}
          y={yBarScale(point.y) - this._barHeight / 2}
          data-is-focusable={shouldHighlight}
          width={
            this._isRtl
              ? containerWidth - this.margins.right! - Math.max(xBarScale(point.x), 0)
              : Math.max(xBarScale(point.x), 0) - this.margins.left!
          }
          height={this._barHeight}
          ref={(e: SVGRectElement) => {
            this._refCallback(e, point.legend!);
          }}
          onClick={point.onClick}
          onMouseOver={this._onBarHover.bind(this, point, color)}
          aria-label={this._getAriaLabel(point)}
          role="img"
          aria-labelledby={`toolTip${this._calloutId}`}
          onMouseLeave={this._onBarLeave}
          onFocus={this._onBarFocus.bind(this, point, index, color)}
          onBlur={this._onBarLeave}
          fill={point.color && !useSingleColor ? point.color : color}
        />
      );
    });
    return bars;
  }
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  private _tooltipOfYAxislabels(ytooltipProps: any) {
    const { tooltipCls, yAxis, id } = ytooltipProps;
    Iif (yAxis === null) {
      return null;
    }
    const div = d3Select('body').append('div').attr('id', id).attr('class', tooltipCls).style('opacity', 0);
    const aa = yAxis!.selectAll('#BaseSpan')._groups[0];
    const baseSpanLength = aa && Object.keys(aa)!.length;
    const originalDataArray: string[] = [];
    for (let i = 0; i < baseSpanLength; i++) {
      const originalData = aa[i].dataset && (Object.values(aa[i].dataset)[0] as string);
      originalDataArray.push(originalData);
    }
    const tickObject = yAxis!.selectAll('.tick')._groups[0];
    const tickObjectLength = tickObject && Object.keys(tickObject)!.length;
    for (let i = 0; i < tickObjectLength; i++) {
      const d1 = tickObject[i];
      d3Select(d1)
        // eslint-disable-next-line @typescript-eslint/no-explicit-any
        .on('mouseover', (event: any, d) => {
          Iif (!this.state.tooltipElement) {
            div.style('opacity', 0.9);
            div
              .html(originalDataArray[i])
              .style('left', event.pageX + 'px')
              .style('top', event.pageY - 28 + 'px');
          }
        })
        .on('mouseout', d => {
          div.style('opacity', 0);
        });
    }
  }
 
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  private _createStringBars(
    containerHeight: number,
    containerWidth: number,
    xElement: SVGElement,
    yElement: SVGElement,
  ): JSX.Element[] {
    const { xBarScale, yBarScale } = this._getScales(containerHeight, containerWidth, false);
    const { useSingleColor = false } = this.props;
    const bars = this._points.map((point: IHorizontalBarChartWithAxisDataPoint, index: number) => {
      const barHeight: number = Math.max(yBarScale(point.y), 0);
      if (barHeight < 1) {
        return <React.Fragment key={point.x}> </React.Fragment>;
      }
      let color: string;
      Iif (useSingleColor) {
        //if useSingle color , then check if user has given a palette or not
        // and pick the first color from that or else from our paltette.
        color = this.props.colors ? this._createColors()(1) : getNextColor(1, 0, this.props.theme?.isInverted);
      } else {
        color = this.props.colors
          ? this._createColors()(point.x)
          : getNextColor(index, 0, this.props.theme?.isInverted);
      }
 
      return (
        <rect
          transform={`translate(0,${0.5 * (yBarScale.bandwidth() - this._barHeight)})`}
          key={point.x}
          x={this._isRtl ? xBarScale(point.x) : this.margins.left!}
          y={yBarScale(point.y)}
          width={
            this._isRtl
              ? containerWidth - this.margins.right! - Math.max(xBarScale(point.x), 0)
              : Math.max(xBarScale(point.x), 0) - this.margins.left!
          }
          height={this._barHeight}
          aria-labelledby={`toolTip${this._calloutId}`}
          aria-label={this._getAriaLabel(point)}
          role="img"
          ref={(e: SVGRectElement) => {
            this._refCallback(e, point.legend!);
          }}
          onClick={point.onClick}
          onMouseOver={this._onBarHover.bind(this, point, color)}
          onMouseLeave={this._onBarLeave}
          onBlur={this._onBarLeave}
          data-is-focusable={true}
          onFocus={this._onBarFocus.bind(this, point, index, color)}
          fill={point.color && !useSingleColor ? point.color : color}
        />
      );
    });
 
    // Removing un wanted tooltip div from DOM, when prop not provided, for proper cleanup
    // of unwanted DOM elements, to prevent flacky behaviour in tooltips , that might occur
    // in creating tooltips when tooltips are enabled( as we try to recreate a tspan with this._tooltipId)
    if (!this.props.showYAxisLablesTooltip) {
      try {
        document.getElementById(this._tooltipId) && document.getElementById(this._tooltipId)!.remove();
        //eslint-disable-next-line no-empty
      } catch (e) {}
    }
    // Used to display tooltip at y axis labels.
    if (this.props.showYAxisLablesTooltip) {
      const yAxisElement = d3Select(yElement).call(yBarScale);
      if (!this.state.tooltipElement) {
        try {
          document.getElementById(this._tooltipId) && document.getElementById(this._tooltipId)!.remove();
          //eslint-disable-next-line no-empty
        } catch (e) {}
      }
      const ytooltipProps = {
        tooltipCls: this._classNames.tooltip!,
        id: this._tooltipId,
        yAxis: yAxisElement,
      };
      yAxisElement && this._tooltipOfYAxislabels(ytooltipProps);
    }
    return bars;
  }
 
  private _closeCallout = () => {
    this.setState({
      isCalloutVisible: false,
    });
  };
 
  private _onLegendClick(customMessage: string): void {
    if (this.state.isLegendSelected) {
      if (this.state.selectedLegendTitle === customMessage) {
        this.setState({
          isLegendSelected: false,
          selectedLegendTitle: customMessage,
        });
      } else E{
        this.setState({
          selectedLegendTitle: customMessage,
        });
      }
    } else {
      this.setState({
        isLegendSelected: true,
        selectedLegendTitle: customMessage,
      });
    }
  }
 
  private _onLegendHover(customMessage: string): void {
    if (this.state.isLegendSelected === false) {
      this.setState({
        isLegendHovered: true,
        selectedLegendTitle: customMessage,
      });
    }
  }
 
  private _onLegendLeave(isLegendFocused?: boolean): void {
    if (!!isLegendFocused || this.state.isLegendSelected === false) {
      this.setState({
        isLegendHovered: false,
        selectedLegendTitle: '',
        isLegendSelected: isLegendFocused ? false : this.state.isLegendSelected,
      });
    }
  }
 
  private _getLegendData = (data: IHorizontalBarChartWithAxisDataPoint[], palette: IPalette): JSX.Element => {
    const { useSingleColor } = this.props;
    const actions: ILegend[] = [];
    data.forEach((point: IHorizontalBarChartWithAxisDataPoint, _index: number) => {
      const color: string = useSingleColor
        ? this.props.colors
          ? this._createColors()(1)
          : getNextColor(1, 0, this.props.theme?.isInverted)
        : point.color!;
      // 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: (isLegendSelected?: boolean) => {
          this._onLegendLeave(isLegendSelected);
        },
      };
      actions.push(legend);
    });
    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) {
      // For HBCWA x and y Values are swapped
      const { yAxisDomainValues: domainValue } = yAxisData;
      this._xMax = Math.max(domainValue[domainValue.length - 1], this.props.xMaxValue || 0);
    }
  };
  private _getAriaLabel = (point: IHorizontalBarChartWithAxisDataPoint): string => {
    const xValue = point.xAxisCalloutData || point.x;
    const yValue = point.yAxisCalloutData || point.y;
    return point.callOutAccessibilityData?.ariaLabel || `${xValue}. ` + `${yValue}.`;
  };
}