Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 | 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 79x 79x 79x 79x 79x 79x 79x 79x 77x 69x 71x 71x 22x 38x 109x 109x 107x 107x 140x 107x 107x 107x 107x 107x 107x 107x 107x 152x 2x 79x 259x 259x 259x 161x 16x 145x 98x 259x 79x 273x 273x 273x 661x 527x 527x 273x 527x 329x 198x 273x 79x 69x 69x 69x 37x 69x 79x 97x 97x 97x 97x 97x 97x 97x 97x 97x 97x 162x 162x 162x 162x 162x 162x 162x 97x 97x 259x 259x 518x 97x 79x 518x 518x 56x 56x 26x 30x 462x 22x 184x 184x 184x 184x 176x 176x 176x 176x 176x 22x 176x 176x 402x 402x 1054x 402x 402x 176x 176x 79x 152x 22x 79x 107x 140x 107x 22x 12x 3x 9x 22x 6x 22x 3x 107x 107x 5x 102x 102x 102x 102x 235x 625x 1258x 625x 368x 257x 8x 4x 4x 2x 257x 102x 102x 63x 63x 4x 2x 2x 1x 63x 102x 102x 22x 8x 8x 22x 8x 8x 8x 79x 79x 79x 2x 2x 79x 2x 22x 10x 10x 22x 10x 10x 10x 10x 10x 10x 10x 22x 22x 79x 79x 9x 9x 22x 1x 1x 22x 353x 853x 353x 353x 353x 353x 353x 79x 152x 152x 191x 152x 152x 67x 152x 353x 353x 353x 353x 947x 353x 353x 353x 213x 140x 345x 345x 345x 345x 345x 345x 345x 45x 345x 345x 345x 1x 2x 344x 344x 688x 140x 140x 140x 140x 140x 140x 119x 119x 21x 52x 11x 11x 140x 280x 152x 148x 148x 152x 4x 4x 4x 4x 4x 353x 79x 152x 152x 152x 154x 154x 85x 85x 67x 67x 67x 79x 152x 152x 79x 79x 152x 152x 152x 79x 753x 79x 563x 79x 182x 101x 101x 299x 299x 299x 101x 101x 101x 101x 101x 81x 81x 81x 81x 79x 152x 152x 152x 67x 67x 67x 67x 67x 67x 30x 154x 85x 85x 152x 22x 188x 420x 22x | import * as React from 'react'; import { max as d3Max, min as d3Min } from 'd3-array'; import { Axis as D3Axis } from 'd3-axis'; import { select as d3Select } from 'd3-selection'; import { scaleLinear as d3ScaleLinear, ScaleLinear as D3ScaleLinear, scaleBand as d3ScaleBand, scaleUtc as d3ScaleUtc, } from 'd3-scale'; import { classNamesFunction, getId, getRTL, warnDeprecations, memoizeFunction } from '@fluentui/react/lib/Utilities'; import { IPalette, IProcessedStyleSet } from '@fluentui/react/lib/Styling'; import { DirectionalHint } from '@fluentui/react/lib/Callout'; import { ILegend, Legends } from '../Legends/index'; import { IAccessibilityProps, CartesianChart, ChartHoverCard, IBasestate, IChildProps, IVerticalStackedBarDataPoint, IMargins, IVerticalStackedBarChartProps, IVerticalStackedBarChartStyleProps, IVerticalStackedBarChartStyles, IVerticalStackedChartProps, IVSChartDataPoint, ILineDataInVerticalStackedBarChart, IModifiedCartesianChartProps, } from '../../index'; import { FocusZoneDirection } from '@fluentui/react-focus'; import { ChartTypes, IAxisData, getAccessibleDataObject, XAxisTypes, getTypeOfAxis, tooltipOfXAxislabels, formatValueWithSIPrefix, getBarWidth, getScalePadding, isScalePaddingDefined, calculateAppropriateBarWidth, } from '../../utilities/index'; const getClassNames = classNamesFunction<IVerticalStackedBarChartStyleProps, IVerticalStackedBarChartStyles>(); type NumericAxis = D3Axis<number | { valueOf(): number }>; type NumericScale = D3ScaleLinear<number, number>; const COMPONENT_NAME = 'VERTICAL STACKED BAR CHART'; // When displaying gaps between bars, the max height of the gap is given in the // props. The actual gap is calculated with this multiplier, with a minimum gap // of 1 pixel. (If these values are changed, update the comment for barGapMax.) const barGapMultiplier = 0.2; const barGapMin = 1; const MIN_DOMAIN_MARGIN = 8; interface IRefArrayData { refElement?: SVGGElement | null; } type LinePoint = ILineDataInVerticalStackedBarChart & { index: number; xItem: IVerticalStackedChartProps }; type LineObject = { [key: string]: LinePoint[] }; type LineLegends = { title: string; color: string; }; enum CircleVisbility { show = 'visibility', hide = 'hidden', } type CalloutAnchorPointData = { xAxisDataPoint: string; chartDataPoint: IVSChartDataPoint; }; export interface IVerticalStackedBarChartState extends IBasestate { dataPointCalloutProps?: IVSChartDataPoint; stackCalloutProps?: IVerticalStackedChartProps; activeXAxisDataPoint: number | string | Date; callOutAccessibilityData?: IAccessibilityProps; calloutLegend: string; } export class VerticalStackedBarChartBase extends React.Component< IVerticalStackedBarChartProps, IVerticalStackedBarChartState > { public static defaultProps: Partial<IVerticalStackedBarChartProps> = { maxBarWidth: 24, }; private _points: IVerticalStackedChartProps[]; private _dataset: IVerticalStackedBarDataPoint[]; private _xAxisLabels: string[]; private _bars: JSX.Element[]; private _xAxisType: XAxisTypes; private _barWidth: number; private _calloutId: string; private _colors: string[]; private margins: IMargins; private _isRtl: boolean = getRTL(); private _createLegendsForLine: (data: IVerticalStackedChartProps[]) => LineLegends[]; private _lineObject: LineObject; private _tooltipId: string; private _yMax: number; private _calloutAnchorPoint: CalloutAnchorPointData | null; private _domainMargin: number; private _classNames: IProcessedStyleSet<IVerticalStackedBarChartStyles>; private _emptyChartId: string; private _xAxisInnerPadding: number; private _xAxisOuterPadding: number; public constructor(props: IVerticalStackedBarChartProps) { super(props); this.state = { isCalloutVisible: false, selectedLegend: '', activeLegend: '', refSelected: null, dataForHoverCard: 0, color: '', hoverXValue: '', YValueHover: [], xCalloutValue: '', yCalloutValue: '', activeXAxisDataPoint: '', calloutLegend: '', }; warnDeprecations(COMPONENT_NAME, props, { colors: 'IVSChartDataPoint.color', chartLabel: 'use your own title for chart', }); this._handleMouseOut = this._handleMouseOut.bind(this); this._calloutId = getId('callout'); this._tooltipId = getId('VSBCTooltipId_'); if (!this._isChartEmpty()) { this._adjustProps(); this._dataset = this._createDataSetLayer(); } this._createLegendsForLine = memoizeFunction((data: IVerticalStackedChartProps[]) => this._getLineLegends(data)); this._emptyChartId = getId('_VSBC_empty'); } public componentDidUpdate(prevProps: IVerticalStackedBarChartProps): void { Iif ( prevProps.height !== this.props.height || prevProps.width !== this.props.width || prevProps.data !== this.props.data ) { this._adjustProps(); this._dataset = this._createDataSetLayer(); } } public render(): React.ReactNode { if (!this._isChartEmpty()) { this._adjustProps(); const _isHavingLines = this.props.data.some( (item: IVerticalStackedChartProps) => item.lineData && item.lineData.length > 0, ); const shouldFocusWholeStack = this._toFocusWholeStack(_isHavingLines); const { isCalloutForStack = false } = this.props; this._dataset = this._createDataSetLayer(); const legendBars: JSX.Element = this._getLegendData( this._points, this.props.theme!.palette, this._createLegendsForLine(this.props.data), ); this._classNames = getClassNames(this.props.styles!, { theme: this.props.theme!, href: this.props.href!, }); const calloutProps: IModifiedCartesianChartProps['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.calloutLegend, XValue: this.state.xCalloutValue!, YValue: this.state.yCalloutValue ? this.state.yCalloutValue : this.state.dataForHoverCard, YValueHover: this.state.YValueHover, hoverXValue: this.state.hoverXValue, 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._dataset} chartType={ChartTypes.VerticalStackedBarChart} xAxisType={this._xAxisType} calloutProps={calloutProps} tickParams={tickParams} legendBars={legendBars} datasetForXAxisDomain={this._xAxisLabels} isCalloutForStack={shouldFocusWholeStack} barwidth={this._barWidth} focusZoneDirection={ isCalloutForStack || _isHavingLines ? FocusZoneDirection.horizontal : FocusZoneDirection.vertical } getmargins={this._getMargins} getGraphData={this._getGraphData} getAxisData={this._getAxisData} customizedCallout={this._getCustomizedCallout()} onChartMouseLeave={this._handleChartMouseLeave} getDomainMargins={this._getDomainMargins} {...(this._xAxisType === XAxisTypes.StringAxis && { xAxisInnerPadding: this._xAxisInnerPadding, xAxisOuterPadding: this._xAxisOuterPadding, })} /* eslint-disable react/jsx-no-bind */ // eslint-disable-next-line react/no-children-prop children={(props: IChildProps) => { return ( <> <g>{this._bars}</g> <g> {_isHavingLines && this._createLines( props.xScale!, props.yScale!, props.containerHeight!, props.containerWidth!, props.yScaleSecondary, )} </g> </> ); }} /> ); } return ( <div id={this._emptyChartId} role={'alert'} style={{ opacity: '0' }} aria-label={'Graph has no data to display'} /> ); } /** * This function tells us what to focus either the whole stack as focusable item. * or each individual item in the stack as focusable item. basically it depends * on the prop `isCalloutForStack` if it's false user can focus each individual bar * within the bar if it's true then user can focus whole bar as item. * but if we have lines in the chart then we force the user to focus only the whole * bar, even if isCalloutForStack is false */ private _toFocusWholeStack = (_isHavingLines: boolean): boolean => { const { isCalloutForStack = false } = this.props; let shouldFocusStackOnly: boolean = false; if (_isHavingLines) { if (this.state.selectedLegend !== '') { shouldFocusStackOnly = false; } else { shouldFocusStackOnly = true; } } else { shouldFocusStackOnly = isCalloutForStack; } return shouldFocusStackOnly; }; private _getFormattedLineData = (data: IVerticalStackedChartProps[]): LineObject => { const linesData: LinePoint[] = []; const formattedLineData: LineObject = {}; data.forEach((item: IVerticalStackedChartProps, index: number) => { if (item.lineData) { // injecting corresponding x data point in each of the line data // we inject index also , it will be helpful to draw lines when x axis is // of string type item.lineData.forEach(line => { linesData.push({ ...line, index, xItem: item, }); }); } }); linesData.forEach(item => { if (formattedLineData[item.legend]) { formattedLineData[item.legend].push(item); } else { formattedLineData[item.legend] = [item]; } }); return formattedLineData; }; private _getLineLegends = (data: IVerticalStackedChartProps[]): LineLegends[] => { const lineObject: LineObject = this._lineObject; const lineLegends: LineLegends[] = []; Object.keys(lineObject).forEach((item: string) => { lineLegends.push({ title: item, color: lineObject[item][0].color, }); }); return lineLegends; }; private _createLines = ( // eslint-disable-next-line @typescript-eslint/no-explicit-any xScale: any, yScale: NumericScale, containerHeight: number, containerWidth: number, secondaryYScale?: NumericScale, ): JSX.Element => { const lineObject: LineObject = this._getFormattedLineData(this.props.data); const lines: React.ReactNode[] = []; const borderForLines: React.ReactNode[] = []; const dots: React.ReactNode[] = []; const { theme } = this.props; const lineBorderWidth = this.props.lineOptions?.lineBorderWidth ? Number.parseFloat(this.props.lineOptions!.lineBorderWidth!.toString()) : 0; const xScaleBandwidthTranslate = this._xAxisType !== XAxisTypes.StringAxis ? 0 : xScale.bandwidth() / 2; Object.keys(lineObject).forEach((item: string, index: number) => { const shouldHighlight = this._legendHighlighted(item) || this._noLegendHighlighted(); // item is legend name for (let i = 1; i < lineObject[item].length; i++) { const x1 = xScale(lineObject[item][i - 1].xItem.xAxisPoint); const useSecondaryYScale = lineObject[item][i - 1].useSecondaryYScale && lineObject[item][i].useSecondaryYScale && secondaryYScale; const y1 = useSecondaryYScale ? secondaryYScale!(lineObject[item][i - 1].y) : yScale(lineObject[item][i - 1].y); const x2 = xScale(lineObject[item][i].xItem.xAxisPoint); const y2 = useSecondaryYScale ? secondaryYScale!(lineObject[item][i].y) : yScale(lineObject[item][i].y); Iif (lineBorderWidth > 0) { borderForLines.push( <line key={`${index}-${i}-BorderLine`} x1={x1} y1={y1} x2={x2} y2={y2} opacity={shouldHighlight ? 1 : 0.1} strokeWidth={3 + lineBorderWidth * 2} fill="transparent" strokeLinecap="round" stroke={theme!.semanticColors.bodyBackground} transform={`translate(${xScaleBandwidthTranslate}, 0)`} />, ); } lines.push( <line key={`${index}-${i}-line`} x1={x1} y1={y1} x2={x2} y2={y2} opacity={shouldHighlight ? 1 : 0.1} strokeWidth={3} strokeLinecap="round" stroke={lineObject[item][i].color} transform={`translate(${xScaleBandwidthTranslate}, 0)`} {...(this.state.selectedLegend === item && { onMouseOver: this._lineHover.bind(this, lineObject[item][i - 1]), onMouseLeave: this._lineHoverOut, })} />, ); } }); Object.keys(lineObject).forEach((item: string, index: number) => { lineObject[item].forEach((circlePoint: LinePoint, subIndex: number) => { // Create an object to store line point ref so that the object can be passed by reference to the focus handler const circleRef: { refElement: SVGCircleElement | null } = { refElement: null }; dots.push( <circle key={`${index}-${subIndex}-dot`} cx={xScale(circlePoint.xItem.xAxisPoint)} cy={ circlePoint.useSecondaryYScale && secondaryYScale ? secondaryYScale(circlePoint.y) : yScale(circlePoint.y) } onMouseOver={ this.state.selectedLegend === item ? this._lineHover.bind(this, circlePoint) : this._onStackHover.bind(this, circlePoint.xItem) } {...(this.state.selectedLegend === item && { onMouseLeave: this._lineHoverOut, })} r={this._getCircleVisibilityAndRadius(circlePoint.xItem.xAxisPoint, circlePoint.legend).radius} stroke={circlePoint.color} fill={this.props.theme!.semanticColors.bodyBackground} strokeWidth={3} visibility={this._getCircleVisibilityAndRadius(circlePoint.xItem.xAxisPoint, circlePoint.legend).visibility} transform={`translate(${xScaleBandwidthTranslate}, 0)`} // When no legend is highlighted: Line points are automatically displayed along with the bars // at the same x-axis point in the stack callout. So to prevent an increase in focusable elements // and avoid conveying duplicate info, make these line points non-focusable. data-is-focusable={this._legendHighlighted(item)} ref={e => (circleRef.refElement = e)} onFocus={this._lineFocus.bind(this, circlePoint, circleRef)} onBlur={this._lineHoverOut} />, ); }); }); return ( <> {borderForLines} {lines} {dots} </> ); }; private _getCircleVisibilityAndRadius = ( xAxisPoint: string | number | Date, legend: string, ): { visibility: CircleVisbility; radius: number } => { const { selectedLegend, activeXAxisDataPoint } = this.state; if (selectedLegend !== '') { Iif (xAxisPoint === activeXAxisDataPoint && selectedLegend === legend) { return { visibility: CircleVisbility.show, radius: 8 }; } else if (selectedLegend === legend) { return { visibility: CircleVisbility.show, radius: 0.3 }; } else { return { visibility: CircleVisbility.hide, radius: 0 }; } } else { return { visibility: activeXAxisDataPoint === xAxisPoint ? CircleVisbility.show : CircleVisbility.hide, radius: 8, }; } }; private _adjustProps(): void { this._points = this.props.data || []; this._barWidth = getBarWidth(this.props.barWidth, this.props.maxBarWidth); const { theme } = this.props; const { palette } = theme!; // eslint-disable-next-line deprecation/deprecation this._colors = this.props.colors || [palette.blueLight, palette.blue, palette.blueMid, palette.red, palette.black]; this._xAxisType = getTypeOfAxis(this.props.data[0].xAxisPoint, true) as XAxisTypes; this._lineObject = this._getFormattedLineData(this.props.data); this._xAxisInnerPadding = getScalePadding(this.props.xAxisInnerPadding, this.props.xAxisPadding, 2 / 3); this._xAxisOuterPadding = getScalePadding(this.props.xAxisOuterPadding, this.props.xAxisPadding, 0); } private _createDataSetLayer(): IVerticalStackedBarDataPoint[] { const tempArr: string[] = []; const dataset: IVerticalStackedBarDataPoint[] = this._points.map(singlePointData => { let total: number = 0; singlePointData.chartData!.forEach((point: IVSChartDataPoint) => { total = total + point.data; }); tempArr.push(singlePointData.xAxisPoint as string); return { x: singlePointData.xAxisPoint, y: total, }; }); this._xAxisLabels = tempArr; return dataset; } private _getMargins = (margins: IMargins) => { this.margins = margins; }; private _renderCallout(props?: IVSChartDataPoint): JSX.Element | null { return props ? ( <ChartHoverCard XValue={props.xAxisCalloutData} Legend={props.legend} YValue={props.yAxisCalloutData} color={props.color} culture={this.props.culture} /> ) : null; } private _getCustomizedCallout = () => { const _isHavingLines = this.props.data.some( (item: IVerticalStackedChartProps) => item.lineData && item.lineData.length > 0, ); return this.props.onRenderCalloutPerStack ? this.props.onRenderCalloutPerStack(this.state.stackCalloutProps) : this.props.onRenderCalloutPerDataPoint && !_isHavingLines ? this.props.onRenderCalloutPerDataPoint(this.state.dataPointCalloutProps, this._renderCallout) : null; }; private _onLegendClick(legendTitle: string): void { if (this.state.selectedLegend === legendTitle) { this.setState({ selectedLegend: '', }); } else { this.setState({ selectedLegend: legendTitle, }); } } private _onLegendHover(legendTitle: string): void { this.setState({ activeLegend: legendTitle, }); } private _onLegendLeave(): void { this.setState({ activeLegend: '', }); } private _getLegendData( data: IVerticalStackedChartProps[], palette: IPalette, lineLegends: LineLegends[], ): JSX.Element { if (this.props.hideLegend) { return <></>; } const defaultPalette: string[] = [palette.blueLight, palette.blue, palette.blueMid, palette.red, palette.black]; const actions: ILegend[] = []; const { allowHoverOnLegend = true } = this.props; data.forEach((singleChartData: IVerticalStackedChartProps) => { singleChartData.chartData.forEach((point: IVSChartDataPoint) => { const color: string = point.color ? point.color : defaultPalette[Math.floor(Math.random() * 4 + 1)]; const checkSimilarLegends = actions.filter((leg: ILegend) => leg.title === point.legend && leg.color === color); if (checkSimilarLegends!.length > 0) { return; } const legend: ILegend = { title: point.legend, color: color, action: () => { this._onLegendClick(point.legend); }, hoverAction: allowHoverOnLegend ? () => { this._handleChartMouseLeave(); this._onLegendHover(point.legend); } : undefined, onMouseOutAction: allowHoverOnLegend ? () => this._onLegendLeave() : undefined, }; actions.push(legend); }); }); const legendsOfLine: ILegend[] = []; if (lineLegends && lineLegends.length > 0) { lineLegends.forEach((point: LineLegends) => { const legend: ILegend = { title: point.title, color: point.color, isLineLegendInBarChart: true, action: () => { this._onLegendClick(point.title); }, hoverAction: allowHoverOnLegend ? () => { this._handleChartMouseLeave(); this._onLegendHover(point.title); } : undefined, onMouseOutAction: allowHoverOnLegend ? () => this._onLegendLeave() : undefined, }; legendsOfLine.push(legend); }); } const totalLegends: ILegend[] = legendsOfLine.concat(actions); return ( <Legends legends={totalLegends} overflowProps={this.props.legendsOverflowProps} enabledWrapLines={this.props.enabledLegendsWrapLines} focusZonePropsInHoverCard={this.props.focusZonePropsForLegendsInHoverCard} overflowText={this.props.legendsOverflowText} {...this.props.legendProps} /> ); } private _onRectHover( xAxisPoint: string, point: IVSChartDataPoint, color: string, mouseEvent: React.MouseEvent<SVGElement>, ): void { mouseEvent.persist(); this._onRectFocusHover(xAxisPoint, point, color, mouseEvent); } private _onRectFocusHover( xAxisPoint: string, point: IVSChartDataPoint, color: string, refSelected: React.MouseEvent<SVGElement> | SVGGElement, ): void { if (this._calloutAnchorPoint?.chartDataPoint !== point || this._calloutAnchorPoint?.xAxisDataPoint !== xAxisPoint) { this._calloutAnchorPoint = { chartDataPoint: point, xAxisDataPoint: xAxisPoint, }; this.setState({ refSelected, /** * Show the callout if highlighted bar is focused/hovered * and Hide it if unhighlighted bar is focused/hovered */ isCalloutVisible: this.state.selectedLegend === '' || this.state.selectedLegend === point.legend, calloutLegend: point.legend, dataForHoverCard: point.data, color, xCalloutValue: point.xAxisCalloutData ? point.xAxisCalloutData : xAxisPoint, yCalloutValue: point.yAxisCalloutData, dataPointCalloutProps: point, callOutAccessibilityData: point.callOutAccessibilityData, }); } } private _lineHover = (lineData: LinePoint, mouseEvent: React.MouseEvent<SVGElement>) => { mouseEvent.persist(); this._lineHoverFocus(lineData, mouseEvent); }; private _lineHoverOut = () => { this.setState({ refSelected: null, isCalloutVisible: false, xCalloutValue: '', yCalloutValue: '', activeXAxisDataPoint: '', color: '', }); }; private _lineFocus = (lineData: LinePoint, ref: { refElement: SVGCircleElement | null }) => { if (ref.refElement) { this._lineHoverFocus(lineData, ref.refElement); } }; private _lineHoverFocus = (lineData: LinePoint, refSelected: React.MouseEvent<SVGElement> | SVGCircleElement) => { this.setState({ refSelected, isCalloutVisible: true, xCalloutValue: `${lineData.xItem.xAxisPoint}`, yCalloutValue: `${lineData.yAxisCalloutData || lineData.data || lineData.y}`, activeXAxisDataPoint: lineData.xItem.xAxisPoint, color: lineData.color, }); }; private _onStackHover(stack: IVerticalStackedChartProps, mouseEvent: React.MouseEvent<SVGElement>): void { mouseEvent.persist(); this._onStackHoverFocus(stack, mouseEvent); } private _onStackHoverFocus( stack: IVerticalStackedChartProps, refSelected: React.MouseEvent<SVGElement> | SVGGElement, ): void { const lineData = stack.lineData; const isLinesPresent: boolean = lineData !== undefined && lineData.length > 0; if (isLinesPresent) { lineData!.forEach((item: ILineDataInVerticalStackedBarChart & { shouldDrawBorderBottom?: boolean }) => { item.data = item.data || item.y; item.shouldDrawBorderBottom = true; }); } this.setState({ refSelected, isCalloutVisible: true, YValueHover: isLinesPresent ? [...lineData!.sort((a, b) => (a.data! < b.data! ? 1 : -1)), ...stack.chartData.slice().reverse()] : stack.chartData.slice().reverse(), hoverXValue: stack.xAxisPoint instanceof Date ? stack.xAxisPoint.toLocaleDateString() : stack.xAxisPoint, stackCalloutProps: stack, activeXAxisDataPoint: stack.xAxisPoint, callOutAccessibilityData: stack.stackCallOutAccessibilityData, }); } private _onRectFocus(point: IVSChartDataPoint, xAxisPoint: string, color: string, ref: IRefArrayData): void { Iif (ref.refElement) { this._onRectFocusHover(xAxisPoint, point, color, ref.refElement); } } private _onStackFocus(stack: IVerticalStackedChartProps, groupRef: IRefArrayData): void { Iif (groupRef.refElement) { this._onStackHoverFocus(stack, groupRef.refElement); } } private _handleMouseOut = (): void => { /**/ }; private _handleChartMouseLeave = (): void => { this._calloutAnchorPoint = null; this.setState({ isCalloutVisible: false, activeXAxisDataPoint: '', }); }; private _onClick( data: IVerticalStackedChartProps | IVSChartDataPoint, mouseEvent: React.MouseEvent<SVGElement>, ): void { this.props.onBarClick?.(mouseEvent, data); this.props.href ? (window.location.href = this.props.href) : ''; } private _getBarGapAndScale( bars: IVSChartDataPoint[], yBarScale: NumericScale, defaultTotalHeight?: number, ): { readonly gapHeight: number; readonly heightValueScale: number; } { const { barGapMax = 0 } = this.props; // When displaying gaps between the bars, the height of each bar is // adjusted so that the total of all bars is not changed by the gaps const totalData = bars.reduce((iter, value) => iter + value.data, 0); const totalHeight = defaultTotalHeight ?? yBarScale(totalData); const gaps = barGapMax && bars.length - 1; const gapHeight = gaps && Math.max(barGapMin, Math.min(barGapMax, (totalHeight * barGapMultiplier) / gaps)); const heightValueScale = (totalHeight - gapHeight * gaps) / totalData; return { gapHeight, heightValueScale, } as const; } private _createBar = ( // eslint-disable-next-line @typescript-eslint/no-explicit-any xBarScale: any, yBarScale: NumericScale, containerHeight: number, xElement: SVGElement, ): JSX.Element[] => { const { barCornerRadius = 0, barMinimumHeight = 0 } = this.props; const _isHavingLines = this.props.data.some( (item: IVerticalStackedChartProps) => item.lineData && item.lineData.length > 0, ); const shouldFocusWholeStack = this._toFocusWholeStack(_isHavingLines); if (this._xAxisType === XAxisTypes.StringAxis) { // Setting the bar width here is safe because there are no dependencies earlier in the code // that rely on the width of bars in vertical bar charts with string x-axis. this._barWidth = getBarWidth(this.props.barWidth, this.props.maxBarWidth, xBarScale.bandwidth()); } const bars = this._points.map((singleChartData: IVerticalStackedChartProps, indexNumber: number) => { let yPoint = containerHeight - this.margins.bottom!; const xPoint = xBarScale( this._xAxisType === XAxisTypes.NumericAxis ? (singleChartData.xAxisPoint as number) : this._xAxisType === XAxisTypes.DateAxis ? (singleChartData.xAxisPoint as Date) : (singleChartData.xAxisPoint as string), ); const xScaleBandwidthTranslate = this._xAxisType !== XAxisTypes.StringAxis ? -this._barWidth / 2 : (xBarScale.bandwidth() - this._barWidth) / 2; let barTotalValue = 0; // Removing datapoints with zero data const barsToDisplay = singleChartData.chartData.filter(point => point.data > 0); Iif (!barsToDisplay.length) { return undefined; } const { gapHeight, heightValueScale } = this._getBarGapAndScale(barsToDisplay, yBarScale); if (heightValueScale < 0) { return undefined; } const singleBar = barsToDisplay.map((point: IVSChartDataPoint, index: number) => { const color = point.color ? point.color : this._colors[index]; const ref: IRefArrayData = {}; const shouldHighlight = this._legendHighlighted(point.legend) || this._noLegendHighlighted() ? true : false; this._classNames = getClassNames(this.props.styles!, { theme: this.props.theme!, shouldHighlight: shouldHighlight, href: this.props.href, }); const rectFocusProps = !shouldFocusWholeStack && { 'data-is-focusable': !this.props.hideTooltip && shouldHighlight, 'aria-label': this._getAriaLabel(singleChartData, point), onMouseOver: this._onRectHover.bind(this, singleChartData.xAxisPoint, point, color), onMouseMove: this._onRectHover.bind(this, singleChartData.xAxisPoint, point, color), onMouseLeave: this._handleMouseOut, onFocus: this._onRectFocus.bind(this, point, singleChartData.xAxisPoint, color, ref), onBlur: this._handleMouseOut, onClick: this._onClick.bind(this, point), role: 'img', }; let barHeight = heightValueScale * point.data; if (barHeight < Math.max(Math.ceil((heightValueScale * this._yMax) / 100.0), barMinimumHeight)) { barHeight = Math.max(Math.ceil((heightValueScale * this._yMax) / 100.0), barMinimumHeight); } yPoint = yPoint - barHeight - (index ? gapHeight : 0); barTotalValue += point.data; // If set, apply the corner radius to the top of the final bar if (barCornerRadius && barHeight > barCornerRadius && index === barsToDisplay.length - 1) { return ( <path key={index + indexNumber + `${shouldFocusWholeStack}`} className={this._classNames.opacityChangeOnHover} d={` M ${xPoint} ${yPoint + barCornerRadius} a ${barCornerRadius} ${barCornerRadius} 0 0 1 ${barCornerRadius} ${-barCornerRadius} h ${this._barWidth - 2 * barCornerRadius} a ${barCornerRadius} ${barCornerRadius} 0 0 1 ${barCornerRadius} ${barCornerRadius} v ${barHeight - barCornerRadius} h ${-this._barWidth} z `} fill={color} ref={e => (ref.refElement = e)} transform={`translate(${xScaleBandwidthTranslate}, 0)`} {...rectFocusProps} /> ); } Iif (barHeight < 0) { return <React.Fragment key={index + indexNumber}> </React.Fragment>; } return ( <rect key={index + indexNumber} className={this._classNames.opacityChangeOnHover} x={xPoint} y={yPoint} width={this._barWidth} height={barHeight} fill={color} ref={e => (ref.refElement = e)} {...rectFocusProps} transform={`translate(${xScaleBandwidthTranslate}, 0)`} /> ); }); const groupRef: IRefArrayData = {}; const stackFocusProps = shouldFocusWholeStack && { 'data-is-focusable': !this.props.hideTooltip, 'aria-label': this._getAriaLabel(singleChartData), onMouseOver: this._onStackHover.bind(this, singleChartData), onMouseMove: this._onStackHover.bind(this, singleChartData), onMouseLeave: this._handleMouseOut, onFocus: this._onStackFocus.bind(this, singleChartData, groupRef), onBlur: this._handleMouseOut, onClick: this._onClick.bind(this, singleChartData), role: 'img', }; let showLabel = false; let barLabel = 0; if (!this.props.hideLabels) { if (this._noLegendHighlighted()) { showLabel = true; barLabel = barTotalValue; } else { barsToDisplay.forEach(point => { if (this._legendHighlighted(point.legend)) { showLabel = true; barLabel += point.data; } }); } } return ( <g key={indexNumber + `${shouldFocusWholeStack}`}> <g id={`${indexNumber}-singleBar`} ref={e => (groupRef.refElement = e)} {...stackFocusProps}> {singleBar} </g> {!this.props.hideLabels && this._barWidth >= 16 && showLabel && ( <text x={xPoint + this._barWidth / 2} y={yPoint - 6} textAnchor="middle" className={this._classNames.barLabel} aria-label={`Total: ${barLabel}`} role="img" transform={`translate(${xScaleBandwidthTranslate}, 0)`} > {formatValueWithSIPrefix(barLabel)} </text> )} </g> ); }); // Removing un wanted tooltip div from DOM, when prop not provided. if (!this.props.showXAxisLablesTooltip) { try { document.getElementById(this._tooltipId) && document.getElementById(this._tooltipId)!.remove(); // eslint-disable-next-line no-empty } catch (e) {} } // Used to display tooltip at x axis labels. if (!this.props.wrapXAxisLables && this.props.showXAxisLablesTooltip) { const xAxisElement = d3Select(xElement).call(xBarScale); try { document.getElementById(this._tooltipId) && document.getElementById(this._tooltipId)!.remove(); // eslint-disable-next-line no-empty } catch (e) {} const tooltipProps = { tooltipCls: this._classNames.tooltip!, id: this._tooltipId, xAxis: xAxisElement, }; xAxisElement && tooltipOfXAxislabels(tooltipProps); } return bars.filter((bar): bar is JSX.Element => !!bar); }; private _getScales = (containerHeight: number, containerWidth: number) => { const yMax = this._yMax; const yBarScale = d3ScaleLinear() .domain([0, yMax]) .range([0, containerHeight - this.margins.bottom! - this.margins.top!]); if (this._xAxisType === XAxisTypes.NumericAxis) { const xMax = d3Max(this._dataset, (point: IVerticalStackedBarDataPoint) => point.x as number)!; const xMin = d3Min(this._dataset, (point: IVerticalStackedBarDataPoint) => point.x as number)!; const xBarScale = d3ScaleLinear() .domain(this._isRtl ? [xMax, xMin] : [xMin, xMax]) .nice() .range([this.margins.left! + this._domainMargin, containerWidth - this.margins.right! - this._domainMargin]); return { xBarScale, yBarScale }; } Iif (this._xAxisType === XAxisTypes.DateAxis) { const sDate = d3Min(this._dataset, (point: IVerticalStackedBarDataPoint) => { return point.x as Date; })!; const lDate = d3Max(this._dataset, (point: IVerticalStackedBarDataPoint) => { return point.x as Date; })!; const xBarScale = d3ScaleUtc() .domain(this._isRtl ? [lDate, sDate] : [sDate, lDate]) .range([this.margins.left! + this._domainMargin, containerWidth - this.margins.right! - this._domainMargin]); return { xBarScale, yBarScale }; } const xBarScale = d3ScaleBand() .domain(this._xAxisLabels) .range( this._isRtl ? [containerWidth - this.margins.right! - this._domainMargin, this.margins.left! + this._domainMargin] : [this.margins.left! + this._domainMargin, containerWidth - this.margins.right! - this._domainMargin], ) .paddingInner(this._xAxisInnerPadding) .paddingOuter(this._xAxisOuterPadding); return { xBarScale, yBarScale }; }; private _getGraphData = ( // eslint-disable-next-line @typescript-eslint/no-explicit-any xScale: any, yScale: NumericAxis, containerHeight: number, containerWidth: number, xElement: SVGElement | null, ) => { const { xBarScale, yBarScale } = this._getScales(containerHeight, containerWidth); return (this._bars = this._createBar(xBarScale, yBarScale, containerHeight, xElement!)); }; private _closeCallout = () => { this.setState({ isCalloutVisible: false, }); }; private _getAxisData = (yAxisData: IAxisData) => { if (yAxisData && yAxisData.yAxisDomainValues.length) { const { yAxisDomainValues: domainValue } = yAxisData; this._yMax = Math.max(domainValue[domainValue.length - 1], this.props.yMaxValue || 0); } }; /** * This function checks if the given legend is highlighted or not. * A legend can be highlighted in 2 ways: * 1. selection: if the user clicks on it * 2. hovering: if there is no selected legend and the user hovers over it */ private _legendHighlighted = (legendTitle: string) => { return ( this.state.selectedLegend === legendTitle || (this.state.selectedLegend === '' && this.state.activeLegend === legendTitle) ); }; /** * This function checks if none of the legends is selected or hovered. */ private _noLegendHighlighted = () => { return this.state.selectedLegend === '' && this.state.activeLegend === ''; }; private _getAriaLabel = (singleChartData: IVerticalStackedChartProps, point?: IVSChartDataPoint): string => { if (!point) { /** if shouldFocusWholeStack is true */ const xValue = singleChartData.xAxisCalloutData || singleChartData.xAxisPoint; const pointValues = singleChartData.chartData .map(pt => { const legend = pt.legend; const yValue = pt.yAxisCalloutData || pt.data; return `${legend}, ${yValue}.`; }) .join(' '); const lineValues = singleChartData.lineData ?.map(ln => { const legend = ln.legend; const yValue = ln.yAxisCalloutData || ln.data || ln.y; return `${legend}, ${yValue}.`; }) .join(' '); return ( singleChartData.stackCallOutAccessibilityData?.ariaLabel || `${xValue}. ${pointValues}` + (lineValues ? ` ${lineValues}` : '') ); } /** if shouldFocusWholeStack is false */ const xValue = singleChartData.xAxisCalloutData || point.xAxisCalloutData || singleChartData.xAxisPoint; const legend = point.legend; const yValue = point.yAxisCalloutData || point.data; return point.callOutAccessibilityData?.ariaLabel || `${xValue}. ${legend}, ${yValue}.`; }; private _getDomainMargins = (containerWidth: number): IMargins => { this._domainMargin = MIN_DOMAIN_MARGIN; /** Total width available to render the bars */ const totalWidth = containerWidth - (this.margins.left! + MIN_DOMAIN_MARGIN) - (this.margins.right! + MIN_DOMAIN_MARGIN); if (this._xAxisType === XAxisTypes.StringAxis) { Iif (isScalePaddingDefined(this.props.xAxisOuterPadding, this.props.xAxisPadding)) { // Setting the domain margin for string x-axis to 0 because the xAxisOuterPadding prop is now available // to adjust the space before the first bar and after the last bar. this._domainMargin = 0; } else if (this.props.barWidth !== 'auto') { /** Rate at which the space between the bars changes wrt the bar width */ const barGapRate = this._xAxisInnerPadding / (1 - this._xAxisInnerPadding); // Update the bar width so that when CartesianChart rerenders, // the following calculations don't use the previous bar width. this._barWidth = getBarWidth(this.props.barWidth, this.props.maxBarWidth); /** Total width required to render the bars. Directly proportional to bar width */ const reqWidth = (this._xAxisLabels.length + (this._xAxisLabels.length - 1) * barGapRate) * this._barWidth; if (totalWidth >= reqWidth) { // Center align the chart by setting equal left and right margins for domain this._domainMargin = MIN_DOMAIN_MARGIN + (totalWidth - reqWidth) / 2; } } } else { const data = (this.props.data?.map(point => point.xAxisPoint) as number[] | Date[] | undefined) || []; this._barWidth = getBarWidth( this.props.barWidth, this.props.maxBarWidth, calculateAppropriateBarWidth(data, totalWidth), ); this._domainMargin = MIN_DOMAIN_MARGIN + this._barWidth / 2; } return { ...this.margins, left: this.margins.left! + this._domainMargin, right: this.margins.right! + this._domainMargin, }; }; private _isChartEmpty(): boolean { return !( this.props.data && this.props.data.length > 0 && this.props.data.filter(item => item.chartData.length === 0).length === 0 ); } } |