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 | 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 66x 22x 22x 22x 22x 22x 58x 58x 58x 58x 58x 58x 58x 58x 58x 58x 58x 58x 22x 56x 17x 114x 114x 113x 113x 113x 113x 113x 113x 113x 113x 113x 113x 113x 113x 134x 134x 134x 134x 134x 1x 58x 134x 58x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 4x 4x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 19x 19x 19x 19x 104x 19x 19x 19x 17x 19x 58x 58x 7x 58x 113x 891x 113x 113x 188x 188x 1636x 188x 113x 113x 58x 113x 113x 113x 113x 113x 113x 113x 188x 188x 188x 113x 113x 891x 891x 8748x 891x 891x 1636x 1636x 891x 891x 891x 8748x 113x 113x 113x 188x 188x 113x 113x 58x 113x 58x 134x 22x 4x 1x 3x 22x 7x 22x 58x 113x 113x 113x 188x 188x 112x 188x 188x 4x 7x 7x 188x 113x 58x 5x 5x 58x 224x 88x 136x 136x 58x 224x 88x 136x 136x 15x 136x 30x 136x 58x 1882x 1882x 60x 60x 56x 1882x 58x 134x 134x 134x 3762x 3762x 3762x 134x 1882x 1882x 134x 134x 224x 134x 134x 224x 224x 224x 1882x 1882x 1882x 1882x 4x 134x 134x 134x 132x 132x 134x 2x 2x 2x 2x 2x 134x 58x 1882x 1882x 4x 1878x 56x 1822x 58x 58x 2048x 58x 2044x 58x 247x 412x 8x 404x 412x 58x 4x 4x 4x 4x 4x 4x 58x 22x 1882x 1882x 1882x 1882x 1882x 1882x 1882x 22x 114x 189x 22x | import * as React from 'react'; import { max as d3Max, bisector } from 'd3-array'; import { pointer } from 'd3-selection'; import { select as d3Select } from 'd3-selection'; import { area as d3Area, stack as d3Stack, curveMonotoneX as d3CurveBasis, line as d3Line } from 'd3-shape'; import { classNamesFunction, find, getId, memoizeFunction } from '@fluentui/react/lib/Utilities'; import { IAccessibilityProps, CartesianChart, ICustomizedCalloutData, IAreaChartProps, IBasestate, ILineChartDataPoint, ILineChartPoints, IChildProps, IMargins, IAreaChartStyleProps, IAreaChartStyles, } from '../../index'; import { warnDeprecations } from '@fluentui/react/lib/Utilities'; import { calloutData, getXAxisType, ChartTypes, XAxisTypes, getTypeOfAxis, tooltipOfXAxislabels, getNextColor, getColorFromToken, } from '../../utilities/index'; import { ILegend, Legends } from '../Legends/index'; import { DirectionalHint } from '@fluentui/react/lib/Callout'; const getClassNames = classNamesFunction<IAreaChartStyleProps, IAreaChartStyles>(); // eslint-disable-next-line @typescript-eslint/no-explicit-any const bisect = bisector((d: any) => d.x).left; const COMPONENT_NAME = 'AREA CHART'; enum InterceptVisibility { show = 'visibility', hide = 'hidden', } export interface IAreaChartAreaPoint { xVal: string | number; values: IAreaChartDataSetPoint; } export interface IAreaChartDataSetPoint { [key: string]: number | string; } export interface IDPointType { values: { 0: number; 1: number; data: {} }; xVal: number | Date; } export interface IMapXToDataSet { [key: string]: ILineChartDataPoint[]; [key: number]: ILineChartDataPoint[]; } //by default d3-shape 3.2.0 limits the< path> data point precision to 3 digits(d3/d3-path#10) export interface IAreaChartState extends IBasestate { lineXValue: number; displayOfLine: InterceptVisibility; isCircleClicked: boolean; dataPointCalloutProps?: ICustomizedCalloutData; stackCalloutProps?: ICustomizedCalloutData; nearestCircleToHighlight: number | string | Date | null; xAxisCalloutAccessibilityData?: IAccessibilityProps; isShowCalloutPending: boolean; /** focused point */ activePoint: string; } export class AreaChartBase extends React.Component<IAreaChartProps, IAreaChartState> { // eslint-disable-next-line @typescript-eslint/no-explicit-any private _calloutPoints: any; private _createSet: (data: ILineChartPoints[]) => { colors: string[]; opacity: number[]; // eslint-disable-next-line @typescript-eslint/no-explicit-any stackedInfo: any; // eslint-disable-next-line @typescript-eslint/no-explicit-any calloutPoints: any; }; private _colors: string[]; private _opacity: number[]; private _uniqueIdForGraph: string; private _verticalLineId: string; private _circleId: string; private _uniqueCallOutID: string; // eslint-disable-next-line @typescript-eslint/no-explicit-any private _stackedData: any; private _chart: JSX.Element[]; private margins: IMargins; private _rectId: string; // eslint-disable-next-line @typescript-eslint/no-explicit-any private _xAxisRectScale: any; // determines if the given area chart has multiple stacked bar charts private _isMultiStackChart: boolean; private _tooltipId: string; private _highlightedCircleId: string; //enableComputationOptimization is used for optimized code to group data points by x value //from O(n^2) to O(n) using a map. private _enableComputationOptimization: boolean; private _firstRenderOptimization: boolean; private _emptyChartId: string; public constructor(props: IAreaChartProps) { super(props); this._createSet = memoizeFunction(this._createDataSet); this.state = { selectedLegend: '', activeLegend: '', hoverXValue: '', isCalloutVisible: false, refSelected: null, YValueHover: [], lineXValue: 0, displayOfLine: InterceptVisibility.hide, isCircleClicked: false, nearestCircleToHighlight: null, isShowCalloutPending: false, activePoint: '', }; warnDeprecations(COMPONENT_NAME, props, { showYAxisGridLines: 'Dont use this property. Lines are drawn by default', }); this._uniqueIdForGraph = getId('areaChart_'); this._verticalLineId = getId('verticalLine_'); this._circleId = getId('circle'); this._rectId = getId('rectangle'); this._tooltipId = getId('AreaChartTooltipID'); this._enableComputationOptimization = true; this._firstRenderOptimization = true; this._emptyChartId = getId('_AreaChart_empty'); } public componentDidUpdate() { if (this.state.isShowCalloutPending) { this.setState({ refSelected: `#${this._highlightedCircleId}`, isCalloutVisible: true, isShowCalloutPending: false, }); } } public render(): JSX.Element { if (!this._isChartEmpty()) { const { lineChartData, chartTitle } = this.props.data; const points = this._addDefaultColors(lineChartData); const { colors, opacity, stackedInfo, calloutPoints } = this._createSet(points); this._calloutPoints = calloutPoints; const isXAxisDateType = getXAxisType(points); this._colors = colors; this._opacity = opacity; this._stackedData = stackedInfo.stackedData; const legends: JSX.Element = this._getLegendData(points); const tickParams = { tickValues: this.props.tickValues, tickFormat: this.props.tickFormat, }; const calloutProps = { target: this.state.refSelected, isCalloutVisible: this.state.isCalloutVisible, directionalHint: DirectionalHint.topAutoEdge, YValueHover: this.state.YValueHover, hoverXValue: this.state.hoverXValue, id: `toolTip${this._uniqueCallOutID}`, gapSpace: 15, isBeakVisible: false, onDismiss: this._closeCallout, 'data-is-focusable': true, xAxisCalloutAccessibilityData: this.state.xAxisCalloutAccessibilityData, ...this.props.calloutProps, }; return ( <CartesianChart {...this.props} chartTitle={chartTitle} points={points} chartType={ChartTypes.AreaChart} calloutProps={calloutProps} legendBars={legends} isCalloutForStack xAxisType={isXAxisDateType ? XAxisTypes.DateAxis : XAxisTypes.NumericAxis} tickParams={tickParams} maxOfYVal={stackedInfo.maxOfYVal} getGraphData={this._getGraphData} getmargins={this._getMargins} customizedCallout={this._getCustomizedCallout()} onChartMouseLeave={this._handleChartMouseLeave} enableFirstRenderOptimization={this.props.enablePerfOptimization && this._firstRenderOptimization} /* eslint-disable react/jsx-no-bind */ // eslint-disable-next-line react/no-children-prop children={(props: IChildProps) => { this._xAxisRectScale = props.xScale; const ticks = this._xAxisRectScale.ticks(); const width1 = this._xAxisRectScale(ticks[ticks.length - 1]); const rectHeight = props.containerHeight! - this.margins.top!; return ( <> <g> <rect id={this._rectId} width={width1} height={rectHeight} fill={'transparent'} onMouseMove={this._onRectMouseMove} onMouseOut={this._onRectMouseOut} onMouseOver={this._onRectMouseMove} /> </g> <g>{this._chart}</g> </> ); }} /> ); } return ( <div id={this._emptyChartId} role={'alert'} style={{ opacity: '0' }} aria-label={'Graph has no data to display'} /> ); } private _getMargins = (margins: IMargins) => { this.margins = margins; }; private _onRectMouseMove = (mouseEvent: React.MouseEvent<SVGRectElement | SVGPathElement | SVGCircleElement>) => { mouseEvent.persist(); const { data } = this.props; const { lineChartData } = data; // This will get the value of the X when mouse is on the chart const xOffset = this._xAxisRectScale.invert(pointer(mouseEvent)[0], document.getElementById(this._rectId)!); const i = bisect(lineChartData![0].data, xOffset); const d0 = lineChartData![0].data[i - 1] as ILineChartDataPoint; const d1 = lineChartData![0].data[i] as ILineChartDataPoint; let pointToHighlight: string | Date | number | null = null; let index: null | number = null; const axisType = lineChartData![0].data.length > 0 ? (getTypeOfAxis(lineChartData![0].data[0].x, true) as XAxisTypes) : null; if (d0 === undefined && d1 !== undefined) { pointToHighlight = d1.x; index = i; } else Iif (d0 !== undefined && d1 === undefined) { pointToHighlight = d0.x; index = i - 1; } else { let x0; let point0; let point1; switch (axisType) { case XAxisTypes.DateAxis: x0 = new Date(xOffset).getTime(); point0 = (d0.x as Date).getTime(); point1 = (d1.x as Date).getTime(); pointToHighlight = Math.abs(x0 - point0) > Math.abs(x0 - point1) ? d1.x : d0.x; index = Math.abs(x0 - point0) > Math.abs(x0 - point1) ? i : i - 1; break; case XAxisTypes.NumericAxis: x0 = xOffset as number; point0 = d0.x as number; point1 = d1.x as number; pointToHighlight = Math.abs(x0 - point0) > Math.abs(x0 - point1) ? d1.x : d0.x; index = Math.abs(x0 - point0) > Math.abs(x0 - point1) ? i : i - 1; break; default: break; } } const { xAxisCalloutData, xAxisCalloutAccessibilityData } = lineChartData![0].data[index as number]; const formattedDate = pointToHighlight instanceof Date ? pointToHighlight.toLocaleString() : pointToHighlight; const modifiedXVal = pointToHighlight instanceof Date ? pointToHighlight.getTime() : pointToHighlight; // eslint-disable-next-line @typescript-eslint/no-explicit-any const found: any = find(this._calloutPoints, (element: { x: string | number }) => { return element.x === modifiedXVal; }); const nearestCircleToHighlight = axisType === XAxisTypes.DateAxis ? (pointToHighlight as Date).getTime() : pointToHighlight; const pointToHighlightUpdated = this.state.nearestCircleToHighlight !== nearestCircleToHighlight; // if no points need to be called out then don't show vertical line and callout card if (found && pointToHighlightUpdated && !this.state.isShowCalloutPending) { this.setState({ nearestCircleToHighlight: nearestCircleToHighlight, isCalloutVisible: false, isShowCalloutPending: true, lineXValue: this._xAxisRectScale(pointToHighlight), displayOfLine: InterceptVisibility.show, isCircleClicked: false, stackCalloutProps: found!, YValueHover: found.values, dataPointCalloutProps: found!, hoverXValue: xAxisCalloutData ? xAxisCalloutData : formattedDate, xAxisCalloutAccessibilityData, activePoint: '', }); } else { /* When above if condition is false but found=true, it means either 1). pointToHighlightUpdated is false. For this case we dont need to do anything. 2). isShowCalloutPending is true. For this case there will be no callout updation for the event. This condition has been added to prevent repeated callout flashing. Currently there is a fraction of second delay between hover event and subsequent callout refresh. In the meantime if another event is received, the callout continues to flash for the set of intermediate hover events. This does not cause any issue as the user interaction takes atleast a fraction of second and the final callout state is ultimately achieved. If a user performs very swift mouse maneuver, the intermediate events will be lost but the callout experience remains smooth. */ } Iif (!found) { this.setState({ isCalloutVisible: false, nearestCircleToHighlight: nearestCircleToHighlight, displayOfLine: InterceptVisibility.hide, isCircleClicked: false, }); } }; /** * just cleaning up the state which we have set in the mouse move event */ private _onRectMouseOut = () => { /**/ }; private _handleChartMouseLeave = () => { this.setState({ refSelected: null, isCalloutVisible: false, nearestCircleToHighlight: null, lineXValue: 0, displayOfLine: InterceptVisibility.hide, isCircleClicked: false, stackCalloutProps: undefined, dataPointCalloutProps: undefined, hoverXValue: undefined, YValueHover: [], }); }; // eslint-disable-next-line @typescript-eslint/no-explicit-any private _getStackedData = (keys: string[], dataSet: any) => { const stackedValues = d3Stack().keys(keys)(dataSet); const maxOfYVal = d3Max(stackedValues[stackedValues.length - 1], dp => dp[1])!; const stackedData: Array<IAreaChartDataSetPoint[]> = []; // eslint-disable-next-line @typescript-eslint/no-explicit-any stackedValues.forEach((layer: any) => { const currentStack: IAreaChartDataSetPoint[] = []; // eslint-disable-next-line @typescript-eslint/no-explicit-any layer.forEach((d: any) => { currentStack.push({ values: d, xVal: d.data.xVal, }); }); stackedData.push(currentStack); }); this._isMultiStackChart = stackedData && stackedData.length > 1 ? true : false; return { stackedData, maxOfYVal, }; }; private _createDataSet = (points: ILineChartPoints[]) => { Iif (this.props.enablePerfOptimization && this._enableComputationOptimization) { const allChartPoints: ILineChartDataPoint[] = []; const dataSet: IAreaChartDataSetPoint[] = []; const colors: string[] = []; const opacity: number[] = []; const calloutPoints = calloutData(points!); points && points.length && points.forEach((singleChartPoint: ILineChartPoints) => { colors.push(singleChartPoint.color!); opacity.push(singleChartPoint.opacity || 1); allChartPoints.push(...singleChartPoint.data); }); const mapOfXvalToListOfDataPoints: IMapXToDataSet = {}; allChartPoints.forEach((dataPoint: ILineChartDataPoint) => { const xValue = dataPoint.x instanceof Date ? dataPoint.x.toLocaleString() : dataPoint.x; // map of x value to the list of data points which share the same x value . if (mapOfXvalToListOfDataPoints[xValue]) { mapOfXvalToListOfDataPoints[xValue].push(dataPoint); } else { mapOfXvalToListOfDataPoints[xValue] = [dataPoint]; } }); Object.keys(mapOfXvalToListOfDataPoints).forEach((key: number | string) => { const value: ILineChartDataPoint[] = mapOfXvalToListOfDataPoints[key]; // eslint-disable-next-line @typescript-eslint/no-explicit-any const singleDataset: any = {}; value.forEach((singleDataPoint: ILineChartDataPoint, index: number) => { singleDataset.xVal = singleDataPoint.x; singleDataset[`chart${index}`] = singleDataPoint.y; }); dataSet.push(singleDataset); }); // get keys from dataset, used to create stacked data const keysLength: number = dataSet && Object.keys(dataSet[0])!.length; const keys: string[] = []; for (let i = 0; i < keysLength - 1; i++) { const keyVal = `chart${i}`; keys.push(keyVal); } // Stacked Info used to draw graph const stackedInfo = this._getStackedData(keys, dataSet); return { colors, opacity, keys, stackedInfo, calloutPoints, }; } else { const allChartPoints: ILineChartDataPoint[] = []; const dataSet: IAreaChartDataSetPoint[] = []; const colors: string[] = []; const opacity: number[] = []; const calloutPoints = calloutData(points!); points && points.length && points.forEach((singleChartPoint: ILineChartPoints) => { colors.push(singleChartPoint.color!); opacity.push(singleChartPoint.opacity || 1); allChartPoints.push(...singleChartPoint.data); }); let tempArr = allChartPoints; while (tempArr.length) { const valToCheck = tempArr[0].x instanceof Date ? tempArr[0].x.toLocaleString() : tempArr[0].x; const filteredChartPoints: ILineChartDataPoint[] = tempArr.filter( (point: ILineChartDataPoint) => (point.x instanceof Date ? point.x.toLocaleString() : point.x) === valToCheck, ); // eslint-disable-next-line @typescript-eslint/no-explicit-any const singleDataset: any = {}; filteredChartPoints.forEach((singleDataPoint: ILineChartDataPoint, index: number) => { singleDataset.xVal = singleDataPoint.x; singleDataset[`chart${index}`] = singleDataPoint.y; }); dataSet.push(singleDataset); // removing compared objects from array const val = tempArr[0].x instanceof Date ? tempArr[0].x.toLocaleString() : tempArr[0].x; tempArr = tempArr.filter( (point: ILineChartDataPoint) => (point.x instanceof Date ? point.x.toLocaleString() : point.x) !== val, ); } // get keys from dataset, used to create stacked data const keysLength: number = dataSet && Object.keys(dataSet[0])!.length; const keys: string[] = []; for (let i = 0; i < keysLength - 1; i++) { const keyVal = `chart${i}`; keys.push(keyVal); } // Stacked Info used to draw graph const stackedInfo = this._getStackedData(keys, dataSet); return { colors, opacity, keys, stackedInfo, calloutPoints, }; } }; private _getCustomizedCallout = () => { return this.props.onRenderCalloutPerStack ? this.props.onRenderCalloutPerStack(this.state.stackCalloutProps) : this.props.onRenderCalloutPerDataPoint ? this.props.onRenderCalloutPerDataPoint(this.state.dataPointCalloutProps) : null; }; private _getGraphData = ( // eslint-disable-next-line @typescript-eslint/no-explicit-any xAxis: any, // eslint-disable-next-line @typescript-eslint/no-explicit-any yAxis: any, containerHeight: number, containerWidth: number, xElement: SVGElement | null, ) => { this._chart = this._drawGraph(containerHeight, xAxis, yAxis, xElement!); }; private _onLegendClick(legend: string): void { if (this.state.selectedLegend === legend) { this.setState({ selectedLegend: '', }); } else { this.setState({ selectedLegend: legend, }); } } private _onLegendHover(legend: string): void { this.setState({ activeLegend: legend, }); } private _onLegendLeave(): void { this.setState({ activeLegend: '', }); } private _getLegendData = (points: ILineChartPoints[]): JSX.Element => { const data = points; const actions: ILegend[] = []; data.forEach((singleChartData: ILineChartPoints) => { const color: string = singleChartData.color!; const checkSimilarLegends = actions.filter( (leg: ILegend) => leg.title === singleChartData.legend && leg.color === color, ); Iif (checkSimilarLegends!.length > 0) { return; } const legend: ILegend = { title: singleChartData.legend, color: color, action: () => { this._onLegendClick(singleChartData.legend); }, hoverAction: () => { this._handleChartMouseLeave(); this._onLegendHover(singleChartData.legend); }, onMouseOutAction: () => { this._onLegendLeave(); }, }; actions.push(legend); }); return ( <Legends legends={actions} overflowProps={this.props.legendsOverflowProps} enabledWrapLines={this.props.enabledLegendsWrapLines} focusZonePropsInHoverCard={this.props.focusZonePropsForLegendsInHoverCard} {...this.props.legendProps} /> ); }; private _onDataPointClick = (func: (() => void) | undefined) => { Iif (func) { func(); } this.setState({ isCircleClicked: true }); }; private _getOpacity = (legend: string): number => { if (!this._isMultiStackChart) { return 0.7; } else { const opacity = this._legendHighlighted(legend) || this._noLegendHighlighted() ? 0.7 : 0.1; return opacity; } }; private _getLineOpacity = (legend: string): number => { if (!this._isMultiStackChart) { return 1; } else { let opacity = 0.3; if (this.state.isCalloutVisible) { opacity = 1; } if (!this._noLegendHighlighted()) { opacity = this._legendHighlighted(legend) ? 0 : 0.1; } return opacity; } }; private _updateCircleFillColor = (xDataPoint: number | Date, lineColor: string, circleId: string): string => { let fillColor = lineColor; if (this.state.nearestCircleToHighlight === xDataPoint || this.state.activePoint === circleId) { this._highlightedCircleId = circleId; if (!this.state.isCircleClicked) { fillColor = this.props.theme!.semanticColors.bodyBackground; } } return fillColor; }; // eslint-disable-next-line @typescript-eslint/no-explicit-any private _drawGraph = (containerHeight: number, xScale: any, yScale: any, xElement: SVGElement): JSX.Element[] => { const points = this._addDefaultColors(this.props.data.lineChartData); const { pointOptions, pointLineOptions } = this.props.data; const area = d3Area() // eslint-disable-next-line @typescript-eslint/no-explicit-any .x((d: any) => xScale(d.xVal)) // eslint-disable-next-line @typescript-eslint/no-explicit-any .y0((d: any) => yScale(d.values[0])) // eslint-disable-next-line @typescript-eslint/no-explicit-any .y1((d: any) => yScale(d.values[1])) .curve(d3CurveBasis); const line = d3Line() // eslint-disable-next-line @typescript-eslint/no-explicit-any .x((d: any) => xScale(d.xVal)) // eslint-disable-next-line @typescript-eslint/no-explicit-any .y((d: any) => yScale(d.values[1])) .curve(d3CurveBasis); const graph: JSX.Element[] = []; let lineColor: string; // eslint-disable-next-line @typescript-eslint/no-explicit-any this._stackedData.forEach((singleStackedData: Array<any>, index: number) => { graph.push( <React.Fragment key={`${index}-graph-${this._uniqueIdForGraph}`}> {this.props.enableGradient && ( <defs> <linearGradient id={`gradient_${index}`} x1="0%" x2="0%" y1="0%" y2="100%"> <stop offset="0" stopColor={this._colors[index]} /> <stop offset="100%" stopColor="transparent" /> </linearGradient> </defs> )} <path id={`${index}-line-${this._uniqueIdForGraph}`} d={line(singleStackedData)!} fill={'transparent'} strokeWidth={3} stroke={this._colors[index]} opacity={this._getLineOpacity(points[index]!.legend)} onMouseMove={this._onRectMouseMove} onMouseOut={this._onRectMouseOut} onMouseOver={this._onRectMouseMove} {...points[index]!.lineOptions} /> {singleStackedData.length === 1 ? ( <circle id={`${index}-graph-${this._uniqueIdForGraph}`} cx={xScale(singleStackedData[0].xVal)} cy={yScale(singleStackedData[0].values[1])} r={6} stroke={this._colors[index]} strokeWidth={3} fill={this._colors[index]} opacity={this._opacity[index]} fillOpacity={this._getOpacity(points[index]!.legend)} onMouseMove={this._onRectMouseMove} onMouseOut={this._onRectMouseOut} onMouseOver={this._onRectMouseMove} /> ) : ( <path id={`${index}-graph-${this._uniqueIdForGraph}`} d={area(singleStackedData)!} fill={this.props.enableGradient ? `url(#gradient_${index})` : this._colors[index]} opacity={this._opacity[index]} fillOpacity={this._getOpacity(points[index]!.legend)} onMouseMove={this._onRectMouseMove} onMouseOut={this._onRectMouseOut} onMouseOver={this._onRectMouseMove} {...(this.props.optimizeLargeData && { 'data-is-focusable': this._legendHighlighted(points[index]!.legend) || this._noLegendHighlighted(), role: 'img', 'aria-label': `${points[index].legend}, series ${index + 1} of ${points.length} with ${ points[index].data.length } data points.`, })} /> )} </React.Fragment>, ); }); const circleRadius = pointOptions && pointOptions.r ? Number(pointOptions.r) : 8; // eslint-disable-next-line @typescript-eslint/no-explicit-any this._stackedData.forEach((singleStackedData: Array<any>, index: number) => { Iif (points.length === index) { return; } if (!this.props.optimizeLargeData || singleStackedData.length === 1) { // Render circles for all data points graph.push( <g key={`${index}-dots-${this._uniqueIdForGraph}`} d={area(singleStackedData)!} clipPath="url(#clip)"> {singleStackedData.map((singlePoint: IDPointType, pointIndex: number) => { const circleId = `${this._circleId}_${index * this._stackedData[0].length + pointIndex}`; const xDataPoint = singlePoint.xVal instanceof Date ? singlePoint.xVal.getTime() : singlePoint.xVal; lineColor = points[index]!.color!; return ( <circle key={circleId} id={circleId} data-is-focusable={this._legendHighlighted(points[index]!.legend) || this._noLegendHighlighted()} cx={xScale(singlePoint.xVal)} cy={yScale(singlePoint.values[1])} stroke={lineColor} strokeWidth={3} fill={this._updateCircleFillColor(xDataPoint, lineColor, circleId)} onMouseOut={this._onRectMouseOut} onMouseOver={this._onRectMouseMove} onClick={this._onDataPointClick.bind(this, points[index]!.data[pointIndex].onDataPointClick!)} onFocus={() => this._handleFocus(index, pointIndex, circleId)} onBlur={this._handleBlur} {...pointOptions} r={this._getCircleRadius(xDataPoint, circleRadius, circleId)} role="img" aria-label={this._getAriaLabel(index, pointIndex)} /> ); })} </g>, ); } else E{ // Render circles for data points close to the mouse pointer only singleStackedData.forEach((singlePoint: IDPointType, pointIndex: number) => { const xDataPoint = singlePoint.xVal instanceof Date ? singlePoint.xVal.getTime() : singlePoint.xVal; Iif (this.state.nearestCircleToHighlight === xDataPoint) { const circleId = `${this._circleId}_${index * this._stackedData[0].length + pointIndex}`; lineColor = points[index]!.color!; graph.push( <circle key={circleId} id={circleId} cx={xScale(singlePoint.xVal)} cy={yScale(singlePoint.values[1])} stroke={lineColor} strokeWidth={3} fill={this._updateCircleFillColor(xDataPoint, lineColor, circleId)} onMouseOut={this._onRectMouseOut} onMouseOver={this._onRectMouseMove} onClick={this._onDataPointClick.bind(this, points[index]!.data[pointIndex].onDataPointClick!)} {...pointOptions} r={this._getCircleRadius(xDataPoint, circleRadius, circleId)} />, ); } }); } }); graph.push( <line id={this._verticalLineId} key={this._verticalLineId} x1={this.state.lineXValue} y1={0} x2={this.state.lineXValue} y2={containerHeight} strokeWidth={1} strokeDasharray={5.5} stroke={lineColor!} opacity={0.5} visibility={this.state.displayOfLine} {...pointLineOptions} />, ); const classNames = getClassNames(this.props.styles!, { theme: this.props.theme!, }); // 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(xScale); try { document.getElementById(this._tooltipId) && document.getElementById(this._tooltipId)!.remove(); // eslint-disable-next-line no-empty } catch (e) {} const tooltipProps = { tooltipCls: classNames.tooltip!, id: this._tooltipId, xAxis: xAxisElement, }; xAxisElement && tooltipOfXAxislabels(tooltipProps); } return graph; }; private _getCircleRadius = (xDataPoint: number, circleRadius: number, circleId: string): number => { const { isCircleClicked, nearestCircleToHighlight, activePoint } = this.state; if (isCircleClicked && nearestCircleToHighlight === xDataPoint) { return 1; } else if (nearestCircleToHighlight === xDataPoint || activePoint === circleId) { return circleRadius; } else { return 0; } }; 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 = (legend: string) => { return ( this.state.selectedLegend === legend || (this.state.selectedLegend === '' && this.state.activeLegend === legend) ); }; /** * This function checks if none of the legends is selected or hovered. */ private _noLegendHighlighted = () => { return this.state.selectedLegend === '' && this.state.activeLegend === ''; }; private _addDefaultColors = (lineChartData?: ILineChartPoints[]): ILineChartPoints[] => { return lineChartData ? lineChartData.map((item, index) => { let color: string; // isInverted property is applicable to v8 themes only if (typeof item.color === 'undefined') { color = getNextColor(index, 0, this.props.theme?.isInverted); } else { color = getColorFromToken(item.color, this.props.theme?.isInverted); } return { ...item, color }; }) : []; }; private _handleFocus = (lineIndex: number, pointIndex: number, circleId: string) => { const { x, y, xAxisCalloutData } = this.props.data.lineChartData![lineIndex].data[pointIndex]; const formattedDate = x instanceof Date ? x.toLocaleString() : x; const modifiedXVal = x instanceof Date ? x.getTime() : x; // eslint-disable-next-line @typescript-eslint/no-explicit-any const found: any = this._calloutPoints.find((e: { x: string | number }) => e.x === modifiedXVal); // Show details in the callout for the focused point only found.values = found.values.filter((e: { y: number }) => e.y === y); this.setState({ refSelected: `#${circleId}`, isCalloutVisible: true, hoverXValue: xAxisCalloutData ? xAxisCalloutData : formattedDate, YValueHover: found.values, stackCalloutProps: found, dataPointCalloutProps: found, activePoint: circleId, }); }; private _handleBlur = () => { this.setState({ refSelected: null, isCalloutVisible: false, hoverXValue: undefined, YValueHover: [], stackCalloutProps: undefined, dataPointCalloutProps: undefined, activePoint: '', }); }; private _getAriaLabel(lineIndex: number, pointIndex: number): string { const line = this.props.data.lineChartData![lineIndex]; const point = line.data[pointIndex]; const formattedDate = point.x instanceof Date ? point.x.toLocaleString() : point.x; const xValue = point.xAxisCalloutData || formattedDate; const legend = line.legend; const yValue = point.yAxisCalloutData || point.y; return point.callOutAccessibilityData?.ariaLabel || `${xValue}. ${legend}, ${yValue}.`; } private _isChartEmpty(): boolean { return !( ( this.props.data && this.props.data.lineChartData && this.props.data.lineChartData.length > 0 && this.props.data.lineChartData.filter(item => item.data.length === 0).length === 0 ) // if all the data sets have no data // filtering all items which have no data and checking if the length of the filtered array is 0 // which means chart is not empty ); } } |