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

99.57% Statements 233/234
96.74% Branches 119/123
96.15% Functions 50/52
99.54% Lines 217/218

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 73025x   25x 25x 25x 25x   25x   25x 25x 25x   25x 25x                                                                                                                             25x 25x                                           48x           48x               48x             19x   48x                       48x     39x 39x 39x 39x 39x 39x 39x 39x             39x 39x 39x 39x 39x                               39x           39x                                               45x 45x 45x                         48x 41x 41x 41x 69x 69x 69x 69x     41x     48x 154x 154x     48x 348x     48x 4x                               48x 4x 4x 4x 4x                                 48x       48x 5x 5x                   48x 45x 45x         45x 90x 90x   180x 180x         152x 152x                 298x                                             152x   28x             28x               50x                             180x     45x               25x 3x                 25x 2x                   25x           5x 1x       4x         48x 39x 39x 39x 68x       5x     3x 3x     2x     68x   39x     48x 41x 41x         48x 65x 19x 46x 37x 9x 7x   2x     48x 65x 1x 64x 55x 9x 7x   2x                         48x                       23x             23x 33x 63x     23x 23x 23x 23x 63x 63x   63x 63x                 63x 22x   41x               23x 41x   22x 16x 6x 2x 4x 3x   1x       63x 19x   63x 6x   63x 36x   63x 1x   63x 6x   63x 54x                           23x 41x 1x 40x 3x   37x           23x         23x           23x 23x                         48x 25x 25x 21x 10x   11x     25x 45x 17x 28x 5x   23x       25x               48x 25x 25x 21x 2x   19x     25x 45x 1x 44x 5x   39x       25x     48x 39x 39x 39x     48x 29x     48x 61x 61x     48x 132x 132x     48x                       48x 306x                 48x 274x     48x 184x 184x 184x 184x 184x 184x           25x 39x   25x  
import { CartesianChart, IChildProps, IModifiedCartesianChartProps } from '../../components/CommonComponents/index';
import { IAccessibilityProps, IHeatMapChartData, IHeatMapChartDataPoint } from '../../types/IDataPoint';
import { scaleLinear as d3ScaleLinear } from 'd3-scale';
import { classNamesFunction, getId, memoizeFunction } from '@fluentui/react/lib/Utilities';
import { FocusZoneDirection } from '@fluentui/react-focus';
import { DirectionalHint } from '@fluentui/react/lib/Callout';
import { IProcessedStyleSet } from '@fluentui/react/lib/Styling';
import * as React from 'react';
import { IHeatMapChartProps, IHeatMapChartStyleProps, IHeatMapChartStyles } from './HeatMapChart.types';
import { ILegend, Legends } from '../Legends/index';
import { convertToLocaleString } from '../../utilities/locale-util';
import { ChartTypes, getAccessibleDataObject, XAxisTypes, YAxisType, getTypeOfAxis } from '../../utilities/utilities';
import { Target } from '@fluentui/react';
import { format as d3Format } from 'd3-format';
import { timeFormat as d3TimeFormat } from 'd3-time-format';
 
type DataSet = {
  dataSet: RectanglesGraphData;
  yAxisPoints: string[];
  xAxisPoints: string[];
};
type FlattenData = IHeatMapChartDataPoint & {
  legend: string;
};
interface IRectRef {
  data: FlattenData;
  refElement: SVGGElement;
}
type RectanglesGraphData = { [key: string]: FlattenData[] };
export interface IHeatMapChartState {
  /**
   * contains the selected legend string
   */
  selectedLegend: string;
  /**
   * contains the hovered legend string
   */
  activeLegend: string;
  /**
   * determines wethere to show or hide the callout
   * @default false
   */
  isCalloutVisible: boolean;
  /**
   * y value to be shown in callout
   */
  calloutYValue: string;
  /**
   * legend title to be shown in callout
   */
  calloutLegend: string;
  /**
   * color of the text in the callout
   */
  calloutTextColor: string;
  /**
   * The target that the Callout should try to position itself based on
   */
  target: Target;
  /**
   * ratio to show in the callout
   */
  ratio: [number, number] | null;
 
  /**
   * description message to show in the callout
   */
  descriptionMessage: string;
  /**
   * id to give to callout for accesiblity purpose
   */
  calloutId: string;
  /**
   * Accessibility data for callout
   */
  callOutAccessibilityData?: IAccessibilityProps;
}
const getClassNames = classNamesFunction<IHeatMapChartStyleProps, IHeatMapChartStyles>();
export class HeatMapChartBase extends React.Component<IHeatMapChartProps, IHeatMapChartState> {
  private _classNames: IProcessedStyleSet<IHeatMapChartStyles>;
  private _stringXAxisDataPoints: string[];
  private _stringYAxisDataPoints: string[];
  private _createSet: (
    data: IHeatMapChartData[],
    xDate: string | undefined,
    xNum: string | undefined,
    yDate: string | undefined,
    yNum: string | undefined,
  ) => DataSet;
  private _dataSet: RectanglesGraphData;
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  private _colorScale: any;
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  private _xAxisScale: any;
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  private _yAxisScale: any;
  /**
   * This array contains ref for all the rectangles
   * drawn inside the chard
   */
  private _rectRefArray: { [key: string]: IRectRef } = {};
  private _xAxisType: XAxisTypes;
  private _yAxisType: YAxisType;
  private _calloutAnchorPoint: FlattenData | null;
  private _emptyChartId: string;
  public constructor(props: IHeatMapChartProps) {
    super(props);
    /**
     * below funciton creates a new data set from the prop
     * @data and also finds all the unique x-axis datapoints
     * and y-axis datapoints(which will render in the axis in graph)
     * we store this in a memoized function, because we want to calulate
     * this set whenever props changes.
     */
    this._createSet = memoizeFunction(
      (
        data: IHeatMapChartData[],
        xDate: string | undefined,
        xNum: string | undefined,
        yDate: string | undefined,
        yNum: string | undefined,
      ): DataSet => this._createNewDataSet(data, xDate, xNum, yDate, yNum),
    );
    this.state = {
      selectedLegend: '',
      activeLegend: '',
      isCalloutVisible: false,
      target: null,
      calloutLegend: '',
      calloutTextColor: '',
      calloutYValue: '',
      ratio: null,
      descriptionMessage: '',
      calloutId: '',
    };
    this._emptyChartId = getId('_HeatMap_empty');
  }
 
  public render(): React.ReactNode {
    const { x, y } = this._getXandY();
    this._xAxisType = getTypeOfAxis(x, true) as XAxisTypes;
    this._yAxisType = getTypeOfAxis(y, false) as YAxisType;
    const { data, xAxisDateFormatString, xAxisNumberFormatString, yAxisDateFormatString, yAxisNumberFormatString } =
      this.props;
    this._colorScale = this._getColorScale();
    const { dataSet, xAxisPoints, yAxisPoints } = this._createSet(
      data,
      xAxisDateFormatString,
      xAxisNumberFormatString,
      yAxisDateFormatString,
      yAxisNumberFormatString,
    );
    this._dataSet = dataSet;
    this._stringYAxisDataPoints = yAxisPoints;
    this._stringXAxisDataPoints = xAxisPoints;
    this._classNames = getClassNames(this.props.styles!, { theme: this.props.theme! });
    const calloutProps: IModifiedCartesianChartProps['calloutProps'] = {
      isBeakVisible: false,
      gapSpace: 8,
      ...this.props.calloutProps,
      isCalloutVisible: this.state.isCalloutVisible,
      id: this.state.calloutId,
      YValue: this.state.calloutYValue,
      legend: this.state.calloutLegend,
      color: this.state.calloutTextColor,
      target: this.state.target,
      styles: this._classNames.subComponentStyles.calloutStyles,
      directionalHint: DirectionalHint.topAutoEdge,
      onDismiss: this._closeCallout,
      ...getAccessibleDataObject(this.state.callOutAccessibilityData, 'text', false),
      preventDismissOnLostFocus: true,
    };
    const chartHoverProps: IModifiedCartesianChartProps['chartHoverProps'] = {
      ...(this.state.ratio && {
        ratio: this.state.ratio,
      }),
      descriptionMessage: this.state.descriptionMessage,
    };
    return !this._isChartEmpty() ? (
      <CartesianChart
        {...this.props}
        points={data}
        chartType={ChartTypes.HeatMapChart}
        xAxisType={XAxisTypes.StringAxis}
        yAxisType={YAxisType.StringAxis}
        calloutProps={calloutProps}
        chartHoverProps={chartHoverProps}
        styles={this._classNames.subComponentStyles!.cartesianStyles}
        datasetForXAxisDomain={this._stringXAxisDataPoints}
        stringDatasetForYAxisDomain={this._stringYAxisDataPoints}
        xAxisTickCount={this._stringXAxisDataPoints.length}
        xAxistickSize={0}
        xAxisPadding={0.02}
        yAxisPadding={0.02}
        svgFocusZoneProps={{
          direction: FocusZoneDirection.bidirectional,
        }}
        legendBars={this._createLegendBars()}
        onChartMouseLeave={this._handleChartMouseLeave}
        /* eslint-disable react/jsx-no-bind */
        // eslint-disable-next-line react/no-children-prop
        children={(props: IChildProps) => {
          this._xAxisScale = props.xScale;
          this._yAxisScale = props.yScale;
          return this._createRectangles();
        }}
      />
    ) : (
      <div
        id={this._emptyChartId}
        role={'alert'}
        style={{ opacity: '0' }}
        aria-label={'Graph has no data to display'}
      />
    );
  }
 
  private _getXandY = (): { x: string | Date | number; y: string | Date | number } => {
    let x: string | Date | number = '';
    let y: string | Date | number = '';
    this.props.data.forEach((item: IHeatMapChartData) => {
      if (item.data && item.data.length > 0) {
        x = item.data[0].x;
        y = item.data[0].y;
        return { x, y };
      }
    });
    return { x, y };
  };
 
  private _getOpacity = (legendTitle: string): string => {
    const opacity = this._legendHighlighted(legendTitle) || this._noLegendHighlighted() ? '1' : '0.1';
    return opacity;
  };
 
  private _rectRefCallback = (rectElement: SVGGElement, index: number | string, dataPointObject: FlattenData): void => {
    this._rectRefArray[index] = { data: dataPointObject, refElement: rectElement };
  };
 
  private _onRectFocus = (id: string, data: FlattenData): void => {
    this.setState({
      target: this._rectRefArray[id].refElement,
      /** Show the callout if highlighted rectangle is focused and Hide it if unhighlighted rectangle is focused */
      isCalloutVisible: this.state.selectedLegend === '' || this.state.selectedLegend === data.legend,
      calloutYValue: `${data.rectText}`,
      calloutTextColor: Number.isNaN(data.value)
        ? this.props.theme!.semanticColors.bodyText
        : this._colorScale(data.value),
      calloutLegend: data.legend,
      ratio: data.ratio || null,
      descriptionMessage: data.descriptionMessage || '',
      calloutId: id,
      callOutAccessibilityData: data.callOutAccessibilityData,
    });
  };
 
  private _onRectMouseOver = (id: string, data: FlattenData, mouseEvent: React.MouseEvent<SVGGElement>): void => {
    mouseEvent.persist();
    if (this._calloutAnchorPoint !== data) {
      this._calloutAnchorPoint = data;
      this.setState({
        target: this._rectRefArray[id].refElement,
        /** Show the callout if highlighted rectangle is hovered and Hide it if unhighlighted rectangle is hovered */
        isCalloutVisible: this.state.selectedLegend === '' || this.state.selectedLegend === data.legend,
        calloutYValue: `${data.rectText}`,
        calloutTextColor: Number.isNaN(data.value)
          ? this.props.theme!.semanticColors.bodyText
          : this._colorScale(data.value),
        calloutLegend: data.legend,
        ratio: data.ratio || null,
        descriptionMessage: data.descriptionMessage || '',
        calloutId: id,
        callOutAccessibilityData: data.callOutAccessibilityData,
      });
    }
  };
 
  private _onRectBlurOrMouseOut = (): void => {
    /**/
  };
 
  private _handleChartMouseLeave = (): void => {
    this._calloutAnchorPoint = null;
    this.setState({
      isCalloutVisible: false,
    });
  };
 
  /**
   * This is the function which is responsible for
   * drawing the rectangle in the graph and also
   * attaching dom events to that rectangles
   */
  private _createRectangles = (): React.ReactNode => {
    const rectangles: JSX.Element[] = [];
    const yAxisDataPoints = this._stringYAxisDataPoints.slice().reverse();
    /**
     * yAxisDataPoint is noting but the DataPoint
     * which will be rendered on the y-axis
     */
    yAxisDataPoints.forEach((yAxisDataPoint: string) => {
      let index = 0;
      this._stringXAxisDataPoints.forEach((xAxisDataPoint: string) => {
        let rectElement: JSX.Element;
        const id = `x${xAxisDataPoint}y${yAxisDataPoint}`;
        if (this._dataSet[yAxisDataPoint][index]?.x === xAxisDataPoint) {
          /**
           * dataPointObject is an object where it contains information on single
           * data point such as x, y , value, rectText property of the rectangle
           */
          const dataPointObject = this._dataSet[yAxisDataPoint][index];
          rectElement = (
            <g
              key={id}
              role="img"
              aria-label={this._getAriaLabel(dataPointObject)}
              data-is-focusable={this._legendHighlighted(dataPointObject.legend) || this._noLegendHighlighted()}
              fillOpacity={this._getOpacity(dataPointObject.legend)}
              transform={`translate(${this._xAxisScale(dataPointObject.x)}, ${this._yAxisScale(dataPointObject.y)})`}
              ref={(gElement: SVGGElement) => {
                this._rectRefCallback(gElement, id, dataPointObject);
              }}
              onFocus={this._onRectFocus.bind(this, id, dataPointObject)}
              onBlur={this._onRectBlurOrMouseOut}
              onMouseOver={this._onRectMouseOver.bind(this, id, dataPointObject)}
              onMouseOut={this._onRectBlurOrMouseOut}
            >
              <rect
                fill={this._colorScale(dataPointObject.value)}
                width={this._xAxisScale.bandwidth()}
                height={this._yAxisScale.bandwidth()}
                onClick={dataPointObject.onClick}
              />
              <text
                dominantBaseline={'middle'}
                textAnchor={'middle'}
                className={this._classNames.text}
                transform={`translate(${this._xAxisScale.bandwidth() / 2}, ${this._yAxisScale.bandwidth() / 2})`}
              >
                {convertToLocaleString(dataPointObject.rectText, this.props.culture)}
              </text>
            </g>
          );
          index++;
        } else {
          const dataPointObject: FlattenData = {
            x: xAxisDataPoint,
            y: yAxisDataPoint,
            value: NaN,
            rectText: 'No data available',
            legend: '',
          };
          rectElement = (
            <g
              key={id}
              role="img"
              aria-label={this._getAriaLabel(dataPointObject)}
              data-is-focusable={this._noLegendHighlighted()}
              transform={`translate(${this._xAxisScale(dataPointObject.x)}, ${this._yAxisScale(dataPointObject.y)})`}
              ref={(gElement: SVGGElement) => {
                this._rectRefCallback(gElement, id, dataPointObject);
              }}
              onFocus={this._onRectFocus.bind(this, id, dataPointObject)}
              onBlur={this._onRectBlurOrMouseOut}
              onMouseOver={this._onRectMouseOver.bind(this, id, dataPointObject)}
              onMouseOut={this._onRectBlurOrMouseOut}
            >
              <rect
                fill={this.props.theme!.semanticColors.bodyBackground}
                width={this._xAxisScale.bandwidth()}
                height={this._yAxisScale.bandwidth()}
              />
            </g>
          );
        }
        rectangles.push(rectElement);
      });
    });
    return rectangles;
  };
  /**
   * when the legend is hovered we need to highlight
   * all the rectangles which fall under that category
   * and un-highlight the rest of them
   * @param legendTitle
   */
  private _onLegendHover(legendTitle: string): void {
    this.setState({
      activeLegend: legendTitle,
    });
  }
 
  /**
   * when the mouse is out from the legend , we need
   * to show the graph in initial mode.
   */
  private _onLegendLeave(): void {
    this.setState({
      activeLegend: '',
    });
  }
  /**
   * @param legendTitle
   * when the legend is clicked we need to highlight
   * all the rectangles which fall under that category
   * and un highlight the rest of them
   */
  private _onLegendClick(legendTitle: string): void {
    /**
     * check if the legend is already selceted,
     * if yes, un-select the legend, else
     * set the selected legend state to legendTitle
     */
    if (this.state.selectedLegend === legendTitle) {
      this.setState({
        selectedLegend: '',
      });
    } else {
      this.setState({
        selectedLegend: legendTitle,
      });
    }
  }
  private _createLegendBars = (): JSX.Element => {
    const { data, legendProps } = this.props;
    const legends: ILegend[] = [];
    data.forEach((item: IHeatMapChartData) => {
      const legend: ILegend = {
        title: item.legend,
        color: this._colorScale(item.value),
        action: () => {
          this._onLegendClick(item.legend);
        },
        hoverAction: () => {
          this._handleChartMouseLeave();
          this._onLegendHover(item.legend);
        },
        onMouseOutAction: () => {
          this._onLegendLeave();
        },
      };
      legends.push(legend);
    });
    return <Legends {...legendProps} legends={legends} />;
  };
 
  private _getColorScale = () => {
    const { domainValuesForColorScale, rangeValuesForColorScale } = this.props;
    return d3ScaleLinear()
      .domain(domainValuesForColorScale)
      .range(rangeValuesForColorScale as unknown as number[]);
  };
 
  private _getXIndex = (value: string | Date | number): string => {
    if (this._xAxisType === XAxisTypes.DateAxis) {
      return `${(value as Date).getTime()}`;
    } else if (this._xAxisType === XAxisTypes.StringAxis) {
      return value as string;
    } else if (this._xAxisType === XAxisTypes.NumericAxis) {
      return `${value}`;
    } else {
      return '';
    }
  };
  private _getYIndex = (value: string | Date | number): string => {
    if (this._yAxisType === YAxisType.DateAxis) {
      return `${(value as Date).getTime()}`;
    } else if (this._yAxisType === YAxisType.StringAxis) {
      return value as string;
    } else if (this._yAxisType === YAxisType.NumericAxis) {
      return `${value}`;
    } else {
      return '';
    }
  };
 
  /**
   * This will create a new data set based on the prop
   * @data
   * We will be using This data set to contsruct our rectangles
   * in the chart, we use this data set becuase, when we loop in this
   * data and build the heat map, it will support accessibility as
   * specified in the figma
   */
 
  private _createNewDataSet = (
    data: IHeatMapChartData[],
    xAxisDateFormatString: string | undefined,
    xAxisNumberFormatString: string | undefined,
    yAxisDateFormatString: string | undefined,
    yAxisNumberFormatString: string | undefined,
  ): DataSet => {
    /**
     * please do not destructure any of the props here,
     * instead send them as parameter to this functions so that
     * this functions get called whenever the prop changes
     */
    const flattenData: FlattenData[] = [];
    /**
     * below for each loop will store all the datapoints in the one array.
     * basically it will flatten the nestesd array (data prop) into single array
     * of object. where each object contains x, y, rectText , value and legend propety of single
     * data point.
     */
    data.forEach((item: IHeatMapChartData) => {
      item.data.forEach((point: IHeatMapChartDataPoint) => {
        flattenData.push({ ...point, legend: item.legend });
      });
    });
    const yPoints: RectanglesGraphData = {};
    const uniqueYPoints: { [key: string]: '1' } = {};
    const uniqueXPoints: { [key: string]: '1' } = {};
    flattenData.forEach((item: FlattenData) => {
      const posX = this._getXIndex(item.x);
      const posY = this._getYIndex(item.y);
 
      uniqueXPoints[posX] = '1';
      uniqueYPoints[posY] = '1';
      /** we will check if the property(posY) is already there in object, if  Yes,
       *  then we will append the item in the Array related to the pos, if not
       *  then we will simply append the item in the new Array and
       *  assign that array to the  property (posY) in the Object
       *  and finally we will get the array of Objects associated to each
       *  property (which is nothing but y data point) and object in the
       *  array are noting but x data points associated to the property y
       */
      if (yPoints[posY]) {
        yPoints[posY] = [...yPoints[posY], item];
      } else {
        yPoints[posY] = [item];
      }
    });
    /**
     * we will now sort(ascending) the array's of y data point based on the x value
     * sorting is important to achive the accessibility order of the
     * rectangles and then format the x and y datapoints respectively
     */
    Object.keys(yPoints).forEach((item: string) => {
      yPoints[item]
        .sort((a: IHeatMapChartDataPoint, b: IHeatMapChartDataPoint) => {
          if (this._xAxisType === XAxisTypes.StringAxis) {
            return (a.x as string).toLowerCase() > (b.x as string).toLowerCase() ? 1 : -1;
          } else if (this._xAxisType === XAxisTypes.DateAxis) {
            return (a.x as Date).getTime() - (b.x as Date).getTime();
          } else if (this._xAxisType === XAxisTypes.NumericAxis) {
            return +(a.x as string) > +(b.x as string) ? 1 : -1;
          } else {
            return a.x > b.x ? 1 : -1;
          }
        })
        .forEach((datapoint: IHeatMapChartDataPoint) => {
          if (this._xAxisType === XAxisTypes.DateAxis) {
            datapoint.x = this._getStringFormattedDate(datapoint.x as string, xAxisDateFormatString);
          }
          if (this._xAxisType === XAxisTypes.NumericAxis) {
            datapoint.x = this._getStringFormattedNumber(datapoint.x as string, xAxisNumberFormatString);
          }
          if (this._xAxisType === XAxisTypes.StringAxis) {
            datapoint.x = this._getFormattedLabelForXAxisDataPoint(datapoint.x as string);
          }
          if (this._yAxisType === YAxisType.DateAxis) {
            datapoint.y = this._getStringFormattedDate(datapoint.y as string, yAxisDateFormatString);
          }
          if (this._yAxisType === YAxisType.NumericAxis) {
            datapoint.y = this._getStringFormattedNumber(datapoint.y as string, yAxisNumberFormatString);
          }
          if (this._yAxisType === YAxisType.StringAxis) {
            datapoint.y = this._getFormattedLabelForYAxisDataPoint(datapoint.y as string);
          }
        });
    });
    /**
     * if  y-axis data points are of type date or number or if we have string formatter,
     * then we need to change data points  to their respective string
     * format, becuase in the private variable this._stringYAxisDatapoints, points will be stored in
     * string format. and in here `yPoint` are not so we need to change, so that
     * function `this._createRectangles` should work perfetcly while looping, and  if we don't change
     * then `this._createRectangles` will fail while looping, causing the error
     * Cannot read property 'forEach' of undefined
     */
 
    Object.keys(yPoints).forEach((yPoint: string) => {
      if (this._yAxisType === YAxisType.DateAxis) {
        yPoints[this._getStringFormattedDate(yPoint, yAxisDateFormatString)] = yPoints[yPoint];
      } else if (this._yAxisType === YAxisType.NumericAxis) {
        yPoints[`${this._getStringFormattedNumber(yPoint, yAxisNumberFormatString)}`] = yPoints[yPoint];
      } else {
        yPoints[this._getFormattedLabelForYAxisDataPoint(yPoint)] = yPoints[yPoint];
      }
    });
    /**
     * assigning new data set
     */
    const dataSet = yPoints;
    /**
     * These are the Y axis data points which will get rendered in the
     * Y axis in graph
     */
    const yAxisPoints = this._getYAxisDataPoints(uniqueYPoints);
    /**
     * These are the x axis data points which will get rendered in the
     * x axis in the graph
     */
 
    const xAxisPoints = this._getXAxisDataPoints(uniqueXPoints);
    return {
      dataSet,
      yAxisPoints,
      xAxisPoints,
    };
  };
 
  /**
   * This function will return the final sorted and formatted x-axis points
   * which will be rendered on the x-axis
   * @param points
   * @returns x-axis points
   */
  private _getXAxisDataPoints = (points: { [key: string]: '1' }): string[] => {
    let xAxisPoints: string[] = [];
    const unFormattedXAxisDataPoints = Object.keys(points).sort((a: string, b: string) => {
      if (this._xAxisType === XAxisTypes.DateAxis || this._xAxisType === XAxisTypes.NumericAxis) {
        return +a - +b;
      } else {
        return a.toLowerCase() > b.toLowerCase() ? 1 : -1;
      }
    });
    xAxisPoints = unFormattedXAxisDataPoints.map((xPoint: string) => {
      if (this._xAxisType === XAxisTypes.DateAxis) {
        return this._getStringFormattedDate(xPoint, this.props.xAxisDateFormatString);
      } else if (this._xAxisType === XAxisTypes.NumericAxis) {
        return this._getStringFormattedNumber(xPoint, this.props.xAxisNumberFormatString);
      } else {
        return this._getFormattedLabelForXAxisDataPoint(xPoint);
      }
    });
 
    return xAxisPoints;
  };
  /**
   * This function will return the final sorted and formatted y-axis points
   * which will be rendered on the y-axis
   * @param points
   * @returns yaxis points
   */
  private _getYAxisDataPoints = (points: { [key: string]: '1' }): string[] => {
    let yAxisPoints: string[] = [];
    const unFormattedYAxisDataPoints = Object.keys(points).sort((a: string, b: string) => {
      if (this._yAxisType === YAxisType.DateAxis || this._yAxisType === YAxisType.NumericAxis) {
        return +a - +b;
      } else {
        return a.toLowerCase() > b.toLowerCase() ? 1 : -1;
      }
    });
    yAxisPoints = unFormattedYAxisDataPoints.map((yPoint: string) => {
      if (this._yAxisType === YAxisType.DateAxis) {
        return this._getStringFormattedDate(yPoint, this.props.yAxisDateFormatString);
      } else if (this._yAxisType === YAxisType.NumericAxis) {
        return this._getStringFormattedNumber(yPoint, this.props.yAxisNumberFormatString);
      } else {
        return this._getFormattedLabelForYAxisDataPoint(yPoint);
      }
    });
 
    return yAxisPoints;
  };
 
  private _getStringFormattedDate = (point: string, formatString?: string): string => {
    const date = new Date();
    date.setTime(+point);
    return d3TimeFormat(formatString || '%b/%d')(date);
  };
 
  private _getStringFormattedNumber = (point: string, formatString?: string): string => {
    return d3Format(formatString || '.2~s')(+point);
  };
 
  private _getFormattedLabelForXAxisDataPoint = (point: string): string => {
    const { xAxisStringFormatter } = this.props;
    return xAxisStringFormatter ? xAxisStringFormatter(point) : point;
  };
 
  private _getFormattedLabelForYAxisDataPoint = (point: string): string => {
    const { yAxisStringFormatter } = this.props;
    return yAxisStringFormatter ? yAxisStringFormatter(point) : point;
  };
 
  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: FlattenData): string => {
    const xValue = point.x;
    const yValue = point.y;
    const legend = point.legend;
    const zValue = point.ratio ? `${point.ratio[0]}/${point.ratio[1]}` : point.rectText || point.value;
    const description = point.descriptionMessage;
    return (
      point.callOutAccessibilityData?.ariaLabel ||
      `${xValue}, ${yValue}. ${legend}, ${zValue}.` + (description ? ` ${description}.` : '')
    );
  };
 
  private _isChartEmpty(): boolean {
    return !(this.props.data && this.props.data.length > 0);
  }
}