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 | 27x 27x 27x 27x 27x 27x 27x 27x 27x 27x 27x 6x 27x 317x 317x 317x 317x 317x 317x 317x 317x 317x 317x 317x 317x 317x 27x 317x 317x 1x 4x 1x 1x 316x 317x 594x 27x 275x 27x 380x 1x 380x 5x 5x 5x 380x 2x 8x 2x 378x 380x 115x 123x 697x 697x 697x 697x 697x 697x 697x 697x 697x 697x 697x 697x 697x 697x 697x 75x 75x 697x 697x 697x 697x 697x 697x 697x 697x 697x 697x 434x 434x 20x 20x 243x 243x 697x 697x 697x 19x 19x 19x 697x 697x 697x 697x 61x 636x 636x 697x 697x 697x 697x 697x 98x 697x 697x 697x 129x 568x 188x 380x 697x 697x 697x 1352x 1352x 1352x 1292x 27x 98x 317x 52x 52x 101x 101x 101x 27x 52x 52x 101x 27x 101x 101x 101x 101x 101x 101x 101x 318x 318x 318x 194x 7x 187x 187x 194x 194x 194x 194x 194x 194x 194x 317x 697x 317x 7x 317x 21x 21x 21x 1x 6x 6x 1x 1x 20x 20x 20x 21x 21x 21x 27x | import * as React from 'react'; import { lazy } from 'react'; import { IProcessedStyleSet } from '@fluentui/react/lib/Styling'; import { classNamesFunction, getId, getRTL } from '@fluentui/react/lib/Utilities'; import { Callout } from '@fluentui/react/lib/Callout'; import { FocusZone, FocusZoneDirection } from '@fluentui/react-focus'; import { ICartesianChartStyles, ICartesianChartStyleProps, IModifiedCartesianChartProps, IYValueHover, IHorizontalBarChartWithAxisDataPoint, } from '../../index'; import { convertToLocaleString } from '../../utilities/locale-util'; import { createNumericXAxis, createStringXAxis, IAxisData, getAccessibleDataObject, getDomainNRangeValues, createDateXAxis, createYAxis, createStringYAxis, IMargins, getMinMaxOfYAxis, XAxisTypes, YAxisType, createWrapOfXLabels, rotateXAxisLabels, Points, pointTypes, calculateLongestLabelWidth, createYAxisLabels, ChartTypes, wrapContent, } from '../../utilities/index'; import { LegendShape, Shape } from '../Legends/index'; import { SVGTooltipText } from '../../utilities/SVGTooltipText'; const getClassNames = classNamesFunction<ICartesianChartStyleProps, ICartesianChartStyles>(); const ChartHoverCard = lazy(() => import('../../utilities/ChartHoverCard/ChartHoverCard').then(module => ({ default: module.ChartHoverCard })), ); export interface ICartesianChartState { containerWidth: number; containerHeight: number; _width: number; _height: number; /* To update this values using setState in render method. * To avoid multiple re renders, Only first time setting the value. */ isRemoveValCalculated?: boolean; /* Used for when WrapXAxisLabels props appeared. * To display the total word (space separated words), Need to have more space than usual. * This height will get total height need to disaply total word. * These value need to be removed from actual svg height/graph height. * Defalut value is 0. And this values calculted when 'wrapXAxisLables' or 'showXAxisLablesTooltip' is true. */ _removalValueForTextTuncate?: number; startFromX: number; } /** * Cartesian chart used for * 1.draw X and Y axis of the chart * 2.Callout * 3.Fit parent Continer */ export class CartesianChartBase extends React.Component<IModifiedCartesianChartProps, ICartesianChartState> { private _classNames: IProcessedStyleSet<ICartesianChartStyles>; private chartContainer: HTMLDivElement; private legendContainer: HTMLDivElement; private minLegendContainerHeight: number = 32; private xAxisElement: SVGElement | null; private yAxisElement: SVGElement | null; private yAxisElementSecondary: SVGElement | null; private margins: IMargins; private idForGraph: string; private idForDefaultTabbableElement: string; private _reqID: number; private _isRtl: boolean = getRTL(); private _tickValues: (string | number)[]; private titleMargin: number; private _isFirstRender: boolean = true; // eslint-disable-next-line @typescript-eslint/no-explicit-any private _xScale: any; private isIntegralDataset: boolean = true; constructor(props: IModifiedCartesianChartProps) { super(props); this.state = { containerHeight: 0, containerWidth: 0, _width: this.props.width || 600, _height: this.props.height || 350, _removalValueForTextTuncate: 0, isRemoveValCalculated: true, startFromX: 0, }; this.idForGraph = getId('chart_'); this.titleMargin = 8; this.idForDefaultTabbableElement = getId('defaultTabbableElement_'); /** * In RTL mode, Only graph will be rendered left/right. We need to provide left and right margins manually. * So that, in RTL, left margins becomes right margins and viceversa. * As graph needs to be drawn perfecty, these values consider as default values. * Same margins using for all other cartesian charts. Can be accessible through 'getMargins' call back method. */ this.margins = { top: this.props.margins?.top ?? 20, bottom: this.props.margins?.bottom ?? 35, right: this._isRtl ? this.props.margins?.left ?? 40 : this.props.margins?.right ?? this.props?.secondaryYScaleOptions ? 40 : 20, left: this._isRtl ? this.props.margins?.right ?? this.props?.secondaryYScaleOptions ? 40 : 20 : this.props.margins?.left ?? 40, }; Iif (this.props.xAxisTitle !== undefined && this.props.xAxisTitle !== '') { this.margins.bottom! = this.props.margins?.bottom ?? 55; } Iif (this.props.yAxisTitle !== undefined && this.props.yAxisTitle !== '') { this.margins.left! = this._isRtl ? this.props.margins?.right ?? this.props?.secondaryYAxistitle ? 60 : 40 : this.props.margins?.left ?? 60; this.margins.right! = this._isRtl ? this.props.margins?.left ?? 60 : this.props.margins?.right ?? this.props?.secondaryYAxistitle ? 60 : 40; } } public componentDidMount(): void { this._fitParentContainer(); if ( this.props.chartType === ChartTypes.HorizontalBarChartWithAxis && this.props.showYAxisLables && this.yAxisElement ) { const maxYAxisLabelLength = calculateLongestLabelWidth( this.props.points.map((point: IHorizontalBarChartWithAxisDataPoint) => point.y), `.${this._classNames.yAxis} text`, ); if (this.state.startFromX !== maxYAxisLabelLength) { this.setState({ startFromX: maxYAxisLabelLength, }); } } else Iif (this.state.startFromX !== 0) { this.setState({ startFromX: 0, }); } this.isIntegralDataset = !this.props.points.some((point: { y: number }) => { return point.y % 1 !== 0; }); } public componentWillUnmount(): void { cancelAnimationFrame(this._reqID); } public componentDidUpdate(prevProps: IModifiedCartesianChartProps): void { if (prevProps.height !== this.props.height || prevProps.width !== this.props.width) { this._fitParentContainer(); } if ( !this.props.wrapXAxisLables && this.props.rotateXAxisLables && this.props.xAxisType! === XAxisTypes.StringAxis ) { const rotateLabelProps = { node: this.xAxisElement, xAxis: this._xScale, }; const rotatedHeight = rotateXAxisLabels(rotateLabelProps); Iif ( this.state.isRemoveValCalculated && this.state._removalValueForTextTuncate !== rotatedHeight! + this.margins.bottom! && rotatedHeight! > 0 ) { this.setState({ _removalValueForTextTuncate: rotatedHeight! + this.margins.bottom!, isRemoveValCalculated: false, }); } } if ( this.props.chartType === ChartTypes.HorizontalBarChartWithAxis && this.props.showYAxisLables && this.yAxisElement ) { const maxYAxisLabelLength = calculateLongestLabelWidth( this.props.points.map((point: IHorizontalBarChartWithAxisDataPoint) => point.y), `.${this._classNames.yAxis} text`, ); Iif (this.state.startFromX !== maxYAxisLabelLength) { this.setState({ startFromX: maxYAxisLabelLength, }); } } else Iif (this.state.startFromX !== 0) { this.setState({ startFromX: 0, }); } if (prevProps.points !== this.props.points) { this.isIntegralDataset = !this.props.points.some((point: { y: number }) => { return point.y % 1 !== 0; }); } } public render(): JSX.Element { const { calloutProps, points, chartType, chartHoverProps, svgFocusZoneProps, svgProps, culture, dateLocalizeOptions, timeFormatLocale, customDateTimeFormatter, } = this.props; Iif (this.props.parentRef) { this._fitParentContainer(); } const margin = { ...this.margins }; if (this.props.chartType === ChartTypes.HorizontalBarChartWithAxis) { if (!this._isRtl) { margin.left! += this.state.startFromX; } else E{ margin.right! += this.state.startFromX; } } // Callback for margins to the chart this.props.getmargins && this.props.getmargins(margin); let callout: JSX.Element | null = null; let children = null; if ( (this.props.enableFirstRenderOptimization && this.chartContainer) || !this.props.enableFirstRenderOptimization ) { this._isFirstRender = false; const XAxisParams = { domainNRangeValues: getDomainNRangeValues( points, this.props.getDomainMargins ? this.props.getDomainMargins(this.state.containerWidth) : this.margins, this.state.containerWidth, chartType, this._isRtl, this.props.xAxisType, this.props.barwidth!, this.props.tickValues!, // This is only used for Horizontal Bar Chart with Axis for y as string axis this.state.startFromX, ), containerHeight: this.state.containerHeight - this.state._removalValueForTextTuncate!, margins: this.margins, xAxisElement: this.xAxisElement!, showRoundOffXTickValues: true, xAxisCount: this.props.xAxisTickCount, xAxistickSize: this.props.xAxistickSize, tickPadding: this.props.tickPadding || this.props.showXAxisLablesTooltip ? 5 : 10, xAxisPadding: this.props.xAxisPadding, xAxisInnerPadding: this.props.xAxisInnerPadding, xAxisOuterPadding: this.props.xAxisOuterPadding, }; const YAxisParams = { margins: this.margins, containerWidth: this.state.containerWidth, containerHeight: this.state.containerHeight - this.state._removalValueForTextTuncate!, yAxisElement: this.yAxisElement, yAxisTickFormat: this.props.yAxisTickFormat!, yAxisTickCount: this.props.yAxisTickCount!, yMinValue: this.props.yMinValue || 0, yMaxValue: this.props.yMaxValue || 0, tickPadding: 10, maxOfYVal: this.props.maxOfYVal, yMinMaxValues: getMinMaxOfYAxis(points, chartType, this.props.yAxisType), // please note these padding default values must be consistent in here // and the parent chart(HBWA/Vertical etc..) for more details refer example // http://using-d3js.com/04_07_ordinal_scales.html yAxisPadding: this.props.yAxisPadding || 0, }; /** * These scales used for 2 purposes. * 1. To create x and y axis * 2. To draw the graph. * For area/line chart using same scales. For other charts, creating their own scales to draw the graph. */ // eslint-disable-next-line @typescript-eslint/no-explicit-any let xScale: any; let tickValues: (string | number)[]; switch (this.props.xAxisType!) { case XAxisTypes.NumericAxis: ({ xScale, tickValues } = createNumericXAxis(XAxisParams, this.props.chartType, culture)); break; case XAxisTypes.DateAxis: ({ xScale, tickValues } = createDateXAxis( XAxisParams, this.props.tickParams!, culture, dateLocalizeOptions, timeFormatLocale, customDateTimeFormatter, )); break; case XAxisTypes.StringAxis: ({ xScale, tickValues } = createStringXAxis( XAxisParams, this.props.tickParams!, this.props.datasetForXAxisDomain!, culture, )); break; default: ({ xScale, tickValues } = createNumericXAxis(XAxisParams, this.props.chartType, culture)); } this._xScale = xScale; this._tickValues = tickValues; /* * To enable wrapping of x axis tick values or to display complete x axis tick values, * we need to calculate how much space it needed to render the text. * No need to re-calculate every time the chart renders and same time need to get an update. So using setState. * Required space will be calculated first time chart rendering and if any width/height of chart updated. * */ if (this.props.wrapXAxisLables || this.props.showXAxisLablesTooltip) { const wrapLabelProps = { node: this.xAxisElement, xAxis: xScale, showXAxisLablesTooltip: this.props.showXAxisLablesTooltip || false, noOfCharsToTruncate: this.props.noOfCharsToTruncate || 4, }; const temp = xScale && (createWrapOfXLabels(wrapLabelProps) as number); // this value need to be updated for draw graph updated. So instead of using private value, using set state. Iif (this.state.isRemoveValCalculated && this.state._removalValueForTextTuncate !== temp) { this.setState({ _removalValueForTextTuncate: temp, isRemoveValCalculated: false }); } } /** * These scales used for 2 purposes. * 1. To create x and y axis * 2. To draw the graph. * For area/line chart using same scales. For other charts, creating their own scales to draw the graph. */ // eslint-disable-next-line @typescript-eslint/no-explicit-any let yScale: any; // eslint-disable-next-line @typescript-eslint/no-explicit-any let yScaleSecondary: any; const axisData: IAxisData = { yAxisDomainValues: [] }; if (this.props.yAxisType && this.props.yAxisType === YAxisType.StringAxis) { yScale = createStringYAxis( YAxisParams, this.props.stringDatasetForYAxisDomain!, this._isRtl, this.props.chartType, this.props.barwidth, culture, ); } else { Iif (this.props?.secondaryYScaleOptions) { const YAxisParamsSecondary = { margins: this.margins, containerWidth: this.state.containerWidth, containerHeight: this.state.containerHeight - this.state._removalValueForTextTuncate!, yAxisElement: this.yAxisElementSecondary, yAxisTickFormat: this.props.yAxisTickFormat!, yAxisTickCount: this.props.yAxisTickCount!, yMinValue: this.props.secondaryYScaleOptions?.yMinValue || 0, yMaxValue: this.props.secondaryYScaleOptions?.yMaxValue ?? 100, tickPadding: 10, maxOfYVal: this.props.secondaryYScaleOptions?.yMaxValue ?? 100, yMinMaxValues: getMinMaxOfYAxis(points, chartType), yAxisPadding: this.props.yAxisPadding, }; yScaleSecondary = createYAxis( YAxisParamsSecondary, this._isRtl, axisData, chartType, this.props.barwidth!, this.isIntegralDataset, true, ); } yScale = createYAxis( YAxisParams, this._isRtl, axisData, chartType, this.props.barwidth!, this.isIntegralDataset, ); } /* * To create y axis tick values by if specified truncating the rest of the text and showing elipsis or showing the whole string, * */ this.props.chartType === ChartTypes.HorizontalBarChartWithAxis && yScale && createYAxisLabels( this.yAxisElement, yScale, this.props.noOfCharsToTruncate || 4, this.props.showYAxisLablesTooltip || false, this.state.startFromX, this._isRtl, ); this.props.getAxisData && this.props.getAxisData(axisData); // Callback function for chart, returns axis this._getData(xScale, yScale); children = this.props.children({ ...this.state, xScale, yScale, yScaleSecondary, }); if (!this.props.hideTooltip && calloutProps!.isCalloutVisible) { callout = this._generateCallout(calloutProps, chartHoverProps); } } this._classNames = getClassNames(this.props.styles!, { theme: this.props.theme!, width: this.state._width, height: this.state._height, className: this.props.className, isRtl: this._isRtl, }); const svgDimensions = { width: this.state.containerWidth, height: this.state.containerHeight, }; let focusDirection; if (this.props.focusZoneDirection === FocusZoneDirection.vertical) { focusDirection = this.props.focusZoneDirection; } else if (this.props.focusZoneDirection) { focusDirection = this.props.focusZoneDirection; } else { focusDirection = FocusZoneDirection.horizontal; } const xAxisTitleMaximumAllowedWidth = svgDimensions.width - this.margins.left! - this.margins.right! - this.state.startFromX!; const yAxisTitleMaximumAllowedHeight = svgDimensions.height - this.margins.bottom! - this.margins.top! - this.state._removalValueForTextTuncate! - this.titleMargin; /** * We have use the {@link defaultTabbableElement } to fix * the Focus not landing on chart while tabbing, instead goes to legend. * This issue is observed in Area, line chart after performance optimization done in the PR {@link https://github.com/microsoft/fluentui/pull/27721 } * This issue is observed in Bar charts after the changes done by FocusZone team in the PR: {@link https://github.com/microsoft/fluentui/pull/24175 } * The issue in Bar Charts(VB and VSB) is due to a {@link FocusZone } update where previously an event listener was * attached on keydown to the window, so that whenever the tab key is pressed all outer FocusZone's * tab-indexes are updated (an outer FocusZone is a FocusZone that is not within another one). * But now after the above PR : they are attaching the * listeners to the FocusZone elements instead of the window. So in the first render cycle in Bar charts * bars are not created as in the first render cycle the size of the chart container is not known( or is 0) * which creates bars of height 0 so instead we do not create any bars and instead return empty fragments. * * We have tried 2 Approaches to fix the issue: * 1. Using the {@link elementRef} property of FocusZone where we dispatch event for tab keydown * after the second render cycle which triggers an update of the tab index in FocusZone. * But this is a hacky solution and not a proper fix and also elementRef is deprecated. * 2. Using the default tabbable element to fix the issue. */ return ( <div id={this.idForGraph} className={this._classNames.root} role={'presentation'} ref={(rootElem: HTMLDivElement) => (this.chartContainer = rootElem)} onMouseLeave={this._onChartLeave} > {!this._isFirstRender && <div id={this.idForDefaultTabbableElement} />} <FocusZone direction={focusDirection} className={this._classNames.chartWrapper} defaultTabbableElement={`#${this.idForDefaultTabbableElement}`} {...svgFocusZoneProps} > {this._isFirstRender && <div id={this.idForDefaultTabbableElement} />} <svg width={svgDimensions.width} height={svgDimensions.height} aria-label={this.props.chartTitle} style={{ display: 'block' }} {...svgProps} > <g ref={(e: SVGElement | null) => { this.xAxisElement = e; }} id={`xAxisGElement${this.idForGraph}`} // To add wrap of x axis lables feature, need to remove word height from svg height. transform={`translate(0, ${ svgDimensions.height - this.margins.bottom! - this.state._removalValueForTextTuncate! })`} className={this._classNames.xAxis} /> {this.props.xAxisTitle !== undefined && this.props.xAxisTitle !== '' && ( <SVGTooltipText content={this.props.xAxisTitle} textProps={{ x: this.margins.left! + this.state.startFromX + xAxisTitleMaximumAllowedWidth / 2, y: svgDimensions.height - this.titleMargin, className: this._classNames.axisTitle!, textAnchor: 'middle', }} maxWidth={xAxisTitleMaximumAllowedWidth} wrapContent={wrapContent} /> )} <g ref={(e: SVGElement | null) => { this.yAxisElement = e; }} id={`yAxisGElement${this.idForGraph}`} transform={`translate(${ this._isRtl ? svgDimensions.width - this.margins.right! - this.state.startFromX : this.margins.left! + this.state.startFromX }, 0)`} className={this._classNames.yAxis} /> {this.props.secondaryYScaleOptions && ( <g> <g ref={(e: SVGElement | null) => { this.yAxisElementSecondary = e; }} id={`yAxisGElementSecondary${this.idForGraph}`} transform={`translate(${ this._isRtl ? this.margins.left! : svgDimensions.width - this.margins.right! }, 0)`} className={this._classNames.yAxis} /> {this.props.secondaryYAxistitle !== undefined && this.props.secondaryYAxistitle !== '' && ( <SVGTooltipText content={this.props.secondaryYAxistitle} textProps={{ x: (yAxisTitleMaximumAllowedHeight - this.margins.bottom!) / 2 + this.state._removalValueForTextTuncate!, y: this._isRtl ? this.state.startFromX - this.titleMargin : svgDimensions.width - this.margins.right!, textAnchor: 'middle', transform: `translate(${ this._isRtl ? this.margins.right! / 2 - this.titleMargin : this.margins.right! / 2 + this.titleMargin }, ${svgDimensions.height - this.margins.bottom! - this.margins.top! - this.titleMargin})rotate(-90)`, className: this._classNames.axisTitle!, }} maxWidth={yAxisTitleMaximumAllowedHeight} wrapContent={wrapContent} /> )} </g> )} {children} {this.props.yAxisTitle !== undefined && this.props.yAxisTitle !== '' && ( <SVGTooltipText content={this.props.yAxisTitle} textProps={{ x: (yAxisTitleMaximumAllowedHeight - this.margins.bottom!) / 2 + this.state._removalValueForTextTuncate!, y: this._isRtl ? svgDimensions.width - this.margins.right! / 2 + this.titleMargin : this.margins.left! / 2 + this.state.startFromX - this.titleMargin, textAnchor: 'middle', transform: `translate(0, ${svgDimensions.height - this.margins.bottom! - this.margins.top! - this.titleMargin})rotate(-90)`, className: this._classNames.axisTitle!, }} maxWidth={yAxisTitleMaximumAllowedHeight} wrapContent={wrapContent} /> )} </svg> </FocusZone> {!this.props.hideLegend && ( <div ref={(e: HTMLDivElement) => (this.legendContainer = e)} className={this._classNames.legendContainer}> {this.props.legendBars} </div> )} {/** The callout is used for narration, so keep it mounted on the DOM */} {callout && <React.Suspense fallback={<div>Loading...</div>}>{callout}</React.Suspense>} </div> ); } /** * Dedicated function to return the Callout JSX Element , which can further be used to only call this when * only the calloutprops and charthover props changes. * @param calloutProps * @param chartHoverProps * @returns */ // eslint-disable-next-line @typescript-eslint/no-explicit-any private _generateCallout(calloutProps: any, chartHoverProps: any): JSX.Element { return ( <Callout hidden={!(!this.props.hideTooltip && calloutProps!.isCalloutVisible)} /** Keep the callout updated with details of focused/hovered chart element */ shouldUpdateWhenHidden={true} {...calloutProps} > {/** Given custom callout, then it will render */} {this.props.customizedCallout && this.props.customizedCallout} {/** single x point its corresponding y points of all the bars/lines in chart will render in callout */} {!this.props.customizedCallout && this.props.isCalloutForStack && this._multiValueCallout(calloutProps)} {/** single x point its corresponding y point of single line/bar in the chart will render in callout */} {!this.props.customizedCallout && !this.props.isCalloutForStack && ( <ChartHoverCard XValue={calloutProps.XValue} Legend={calloutProps.legend!} YValue={calloutProps.YValue!} color={calloutProps.color!} culture={this.props.culture} {...chartHoverProps} /> )} </Callout> ); } // TO DO: Write a common functional component for Multi value callout and divide sub count method // eslint-disable-next-line @typescript-eslint/no-explicit-any private _multiValueCallout = (calloutProps: any) => { const yValueHoverSubCountsExists: boolean = this._yValueHoverSubCountsExists(calloutProps.YValueHover); return ( <div className={this._classNames.calloutContentRoot}> <div className={this._classNames.calloutDateTimeContainer} style={yValueHoverSubCountsExists ? { marginBottom: '11px' } : {}} > <div className={this._classNames.calloutContentX} {...getAccessibleDataObject(calloutProps!.xAxisCalloutAccessibilityData, 'text', false)} > {convertToLocaleString(calloutProps!.hoverXValue, this.props.culture)} </div> </div> <div className={this._classNames.calloutInfoContainer} style={yValueHoverSubCountsExists ? { display: 'flex' } : {}} > {calloutProps!.YValueHover && calloutProps!.YValueHover.map((yValue: IYValueHover, index: number, yValues: IYValueHover[]) => { const isLast: boolean = index + 1 === yValues.length; const { shouldDrawBorderBottom = false } = yValue; return ( <div {...getAccessibleDataObject(yValue.callOutAccessibilityData, 'text', false)} key={`callout-content-${index}`} style={ yValueHoverSubCountsExists ? { display: 'inline-block', ...(shouldDrawBorderBottom && { borderBottom: `1px solid ${this.props.theme!.semanticColors.menuDivider}`, paddingBottom: '10px', }), } : { ...(shouldDrawBorderBottom && { borderBottom: `1px solid ${this.props.theme!.semanticColors.menuDivider}`, paddingBottom: '10px', }), } } > {this._getCalloutContent(yValue, index, yValueHoverSubCountsExists, isLast)} </div> ); })} {!!calloutProps.descriptionMessage && ( <div className={this._classNames.descriptionMessage}>{calloutProps.descriptionMessage}</div> )} </div> </div> ); }; private _yValueHoverSubCountsExists(yValueHover?: IYValueHover[]) { if (yValueHover) { return yValueHover.some( (yValue: { legend?: string; y?: number; color?: string; yAxisCalloutData?: string | { [id: string]: number }; }) => yValue.yAxisCalloutData && typeof yValue.yAxisCalloutData !== 'string', ); } return false; } private _getCalloutContent( xValue: IYValueHover, index: number, yValueHoverSubCountsExists: boolean, isLast: boolean, ): React.ReactNode { const marginStyle: React.CSSProperties = isLast ? {} : { marginRight: '16px' }; const toDrawShape = xValue.index !== undefined && xValue.index !== -1; const _classNames = getClassNames(this.props.styles!, { theme: this.props.theme!, width: this.state._width, height: this.state._height, className: this.props.className, isRtl: this._isRtl, lineColor: xValue.color, toDrawShape, }); const { culture } = this.props; const yValue = convertToLocaleString(xValue.y, culture); if (!xValue.yAxisCalloutData || typeof xValue.yAxisCalloutData === 'string') { return ( <div style={yValueHoverSubCountsExists ? marginStyle : {}}> {yValueHoverSubCountsExists && ( <div className="ms-fontWeight-semibold" style={{ fontSize: '12pt' }}> {xValue.legend!} ({yValue}) </div> )} <div id={`${index}_${xValue.y}`} className={_classNames.calloutBlockContainer}> {toDrawShape && ( <Shape svgProps={{ className: _classNames.shapeStyles, }} pathProps={{ fill: xValue.color }} shape={Points[xValue.index! % Object.keys(pointTypes).length] as LegendShape} /> )} <div> <div className={_classNames.calloutlegendText}> {xValue.legend}</div> <div className={_classNames.calloutContentY}> {convertToLocaleString( xValue.yAxisCalloutData ? xValue.yAxisCalloutData : xValue.y ?? xValue.data, culture, )} </div> </div> </div> </div> ); } else E{ const subcounts: { [id: string]: number } = xValue.yAxisCalloutData as { [id: string]: number }; return ( <div style={marginStyle}> <div className="ms-fontWeight-semibold" style={{ fontSize: '12pt' }}> {xValue.legend!} ({yValue}) </div> {Object.keys(subcounts).map((subcountName: string) => { return ( <div key={subcountName} className={_classNames.calloutBlockContainer}> <div className={_classNames.calloutlegendText}> {convertToLocaleString(subcountName, culture)}</div> <div className={_classNames.calloutContentY}> {convertToLocaleString(subcounts[subcountName], culture)} </div> </div> ); })} </div> ); } } /** * When screen resizes, along with screen, chart also auto adjusted. * This method used to adjust height and width of the charts. */ private _fitParentContainer(): void { const { containerWidth, containerHeight } = this.state; this._reqID = requestAnimationFrame(() => { let legendContainerHeight; if (this.props.hideLegend) { // If there is no legend, need not to allocate some space from total chart space. legendContainerHeight = 0; } else { const legendContainerComputedStyles = this.legendContainer && getComputedStyle(this.legendContainer); legendContainerHeight = ((this.legendContainer && this.legendContainer.getBoundingClientRect().height) || this.minLegendContainerHeight) + parseFloat((legendContainerComputedStyles && legendContainerComputedStyles.marginTop) || '0') + parseFloat((legendContainerComputedStyles && legendContainerComputedStyles.marginBottom) || '0'); } if (this.props.parentRef || this.chartContainer) { const container = this.props.parentRef ? this.props.parentRef : this.chartContainer; const currentContainerWidth = this.props.enableReflow && !this._isFirstRender ? Math.max(container.getBoundingClientRect().width, this._calculateChartMinWidth()) : container.getBoundingClientRect().width; const currentContainerHeight = container.getBoundingClientRect().height > legendContainerHeight ? container.getBoundingClientRect().height : 350; const shouldResize = containerWidth !== currentContainerWidth || containerHeight !== currentContainerHeight - legendContainerHeight; if (shouldResize) { this.setState({ containerWidth: currentContainerWidth, containerHeight: currentContainerHeight - legendContainerHeight, }); } } }); } // Call back to the chart. // eslint-disable-next-line @typescript-eslint/no-explicit-any private _getData = (xScale: any, yScale: any) => { this.props.getGraphData && this.props.getGraphData( xScale, yScale, this.state.containerHeight - this.state._removalValueForTextTuncate!, this.state.containerWidth, this.xAxisElement, this.yAxisElement, ); }; private _onChartLeave = (): void => { this.props.onChartMouseLeave && this.props.onChartMouseLeave(); }; private _calculateChartMinWidth = (): number => { let labelWidth = 10; // Total padding on the left and right sides of the label // Case: rotated labels Iif ( !this.props.wrapXAxisLables && this.props.rotateXAxisLables && this.props.xAxisType! === XAxisTypes.StringAxis ) { const longestLabelWidth = calculateLongestLabelWidth(this._tickValues, `.${this._classNames.xAxis} text`); labelWidth += Math.ceil(longestLabelWidth * Math.cos(Math.PI / 4)); } // Case: truncated labels else if (this.props.showXAxisLablesTooltip) { const tickValues = this._tickValues.map(val => { const numChars = this.props.noOfCharsToTruncate || 4; return val.toString().length > numChars ? `${val.toString().slice(0, numChars)}...` : val; }); const longestLabelWidth = calculateLongestLabelWidth(tickValues, `.${this._classNames.xAxis} text`); labelWidth += Math.ceil(longestLabelWidth); } // Case: wrapped labels else Iif (this.props.wrapXAxisLables) { const words: string[] = []; this._tickValues.forEach((val: string) => { words.push(...val.toString().split(/\s+/)); }); const longestLabelWidth = calculateLongestLabelWidth(words, `.${this._classNames.xAxis} text`); labelWidth += Math.max(Math.ceil(longestLabelWidth), 10); } // Default case else { const longestLabelWidth = calculateLongestLabelWidth(this._tickValues, `.${this._classNames.xAxis} text`); labelWidth += Math.ceil(longestLabelWidth); } let minChartWidth = this.margins.left! + this.margins.right! + labelWidth * (this._tickValues.length - 1); Iif ( [ChartTypes.GroupedVerticalBarChart, ChartTypes.VerticalBarChart, ChartTypes.VerticalStackedBarChart].includes( this.props.chartType, ) ) { const minDomainMargin = 8; minChartWidth += minDomainMargin * 2; } return minChartWidth; }; } |