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 | 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 70x 70x 70x 37x 37x 37x 37x 37x 37x 37x 37x 22x 31x 21x 70x 70x 70x 70x 70x 70x 70x 70x 207x 70x 70x 128x 128x 37x 22x 70x 70x 70x 70x 207x 70x 207x 70x 22x 128x 68x 60x 60x 60x 60x 72x 72x 210x 210x 3x 1x 2x 3x 3x 1x 210x 72x 72x 37x 1x 1x 37x 7x 7x 7x 7x 37x 1x 22x 37x 6x 6x 70x 70x 70x 1x 1x 3x 1x 3x 1x 69x 22x 71x 71x 65x 6x 22x 139x 22x 70x 207x 37x 72x 213x 12x 201x 213x 22x | import * as React from 'react'; import { classNamesFunction, getId } from '@fluentui/react/lib/Utilities'; import { ScaleOrdinal } from 'd3-scale'; import { IProcessedStyleSet } from '@fluentui/react/lib/Styling'; import { Callout, DirectionalHint } from '@fluentui/react/lib/Callout'; import { FocusZone, FocusZoneDirection, FocusZoneTabbableElements } from '@fluentui/react-focus'; import { IAccessibilityProps, ChartHoverCard, ILegend, Legends } from '../../index'; import { Pie } from './Pie/index'; import { IChartDataPoint, IDonutChartProps, IDonutChartStyleProps, IDonutChartStyles } from './index'; import { getAccessibleDataObject, getColorFromToken, getNextColor } from '../../utilities/index'; import { convertToLocaleString } from '../../utilities/locale-util'; const getClassNames = classNamesFunction<IDonutChartStyleProps, IDonutChartStyles>(); const LEGEND_CONTAINER_HEIGHT = 40; export interface IDonutChartState { showHover?: boolean; value?: string | undefined; legend?: string | undefined; _width?: number | undefined; _height?: number | undefined; activeLegend?: string; color?: string | undefined; xCalloutValue?: string; yCalloutValue?: string; focusedArcId?: string; selectedLegend: string; dataPointCalloutProps?: IChartDataPoint; callOutAccessibilityData?: IAccessibilityProps; } export class DonutChartBase extends React.Component<IDonutChartProps, IDonutChartState> { public static defaultProps: Partial<IDonutChartProps> = { innerRadius: 0, hideLabels: true, }; public _colors: ScaleOrdinal<string, {}>; private _classNames: IProcessedStyleSet<IDonutChartStyles>; private _rootElem: HTMLElement | null; private _uniqText: string; /* eslint-disable @typescript-eslint/no-explicit-any */ private _currentHoverElement: any; private _calloutId: string; private _calloutAnchorPoint: IChartDataPoint | null; private _emptyChartId: string | null; public static getDerivedStateFromProps( nextProps: Readonly<IDonutChartProps>, prevState: Readonly<IDonutChartState>, ): Partial<IDonutChartState> | null { let widthState: { _width: number } | undefined; Iif (nextProps.width && nextProps.width !== prevState._width) { widthState = { _width: nextProps.width }; } let heightState: { _height: number } | undefined; Iif (nextProps.height && nextProps.height !== prevState._height) { heightState = { _height: nextProps.height - LEGEND_CONTAINER_HEIGHT }; } return { ...widthState, ...heightState }; } constructor(props: IDonutChartProps) { super(props); this.state = { showHover: false, value: '', legend: '', _width: this.props.width || 200, _height: this.props.height || 200, activeLegend: '', color: '', xCalloutValue: '', yCalloutValue: '', selectedLegend: '', focusedArcId: '', }; this._hoverCallback = this._hoverCallback.bind(this); this._focusCallback = this._focusCallback.bind(this); this._hoverLeave = this._hoverLeave.bind(this); this._calloutId = getId('callout'); this._uniqText = getId('_Pie_'); this._emptyChartId = getId('_DonutChart_empty'); } public componentDidMount(): void { if (this._rootElem) { this.setState({ _width: this._rootElem.offsetWidth, _height: this._rootElem.offsetHeight - LEGEND_CONTAINER_HEIGHT, }); } } public render(): JSX.Element { const { data, hideLegend = false } = this.props; const points = this._addDefaultColors(data?.chartData); this._classNames = getClassNames(this.props.styles!, { theme: this.props.theme!, width: this.state._width!, height: this.state._height!, color: this.state.color!, className: this.props.className!, }); const legendBars = this._createLegends(points); const donutMarginHorizontal = this.props.hideLabels ? 0 : 80; const donutMarginVertical = this.props.hideLabels ? 0 : 40; const outerRadius = Math.min(this.state._width! - donutMarginHorizontal, this.state._height! - donutMarginVertical) / 2; const chartData = this._elevateToMinimums(points.filter((d: IChartDataPoint) => d.data! >= 0)); const valueInsideDonut = this._valueInsideDonut(this.props.valueInsideDonut!, chartData!); return !this._isChartEmpty() ? ( <div className={this._classNames.root} ref={(rootElem: HTMLElement | null) => (this._rootElem = rootElem)} onMouseLeave={this._handleChartMouseLeave} > <FocusZone direction={FocusZoneDirection.horizontal} handleTabKey={FocusZoneTabbableElements.all}> <div> <svg className={this._classNames.chart} aria-label={data?.chartTitle} ref={(node: SVGElement | null) => this._setViewBox(node)} > <Pie width={this.state._width!} height={this.state._height!} outerRadius={outerRadius} innerRadius={this.props.innerRadius!} data={chartData!} onFocusCallback={this._focusCallback} hoverOnCallback={this._hoverCallback} hoverLeaveCallback={this._hoverLeave} uniqText={this._uniqText} onBlurCallback={this._onBlur} activeArc={this._getHighlightedLegend()} focusedArcId={this.state.focusedArcId || ''} href={this.props.href!} calloutId={this._calloutId} valueInsideDonut={this._toLocaleString(valueInsideDonut)} theme={this.props.theme!} showLabelsInPercent={this.props.showLabelsInPercent} hideLabels={this.props.hideLabels} /> </svg> </div> </FocusZone> <Callout target={this._currentHoverElement} alignTargetEdge={true} isBeakVisible={false} directionalHint={DirectionalHint.topAutoEdge} gapSpace={15} hidden={!(!this.props.hideTooltip && this.state.showHover)} id={this._calloutId} onDismiss={this._closeCallout} preventDismissOnLostFocus={true} /** Keep the callout updated with details of focused/hovered arc */ shouldUpdateWhenHidden={true} {...this.props.calloutProps!} {...getAccessibleDataObject(this.state.callOutAccessibilityData, 'text', false)} > {this.props.onRenderCalloutPerDataPoint ? ( this.props.onRenderCalloutPerDataPoint(this.state.dataPointCalloutProps!) ) : ( <ChartHoverCard Legend={this.state.xCalloutValue ? this.state.xCalloutValue : this.state.legend} YValue={this.state.yCalloutValue ? this.state.yCalloutValue : this.state.value} color={this.state.color} culture={this.props.culture} /> )} </Callout> {!hideLegend && <div className={this._classNames.legendContainer}>{legendBars}</div>} </div> ) : ( <div id={this._emptyChartId!} role={'alert'} style={{ opacity: '0' }} aria-label={'Graph has no data to display'} /> ); } private _closeCallout = () => { this.setState({ showHover: false, }); }; private _elevateToMinimums(data: IChartDataPoint[]) { let sumOfData = 0; const minPercent = 0.01; const elevatedData: IChartDataPoint[] = []; data.forEach(item => { sumOfData += item.data!; }); data.forEach(item => { elevatedData.push( minPercent * sumOfData > item.data! && item.data! > 0 ? { ...item, data: minPercent * sumOfData, yAxisCalloutData: item.yAxisCalloutData === undefined ? item.data!.toLocaleString() : item.yAxisCalloutData, } : item, ); }); return elevatedData; } private _setViewBox(node: SVGElement | null): void { if (node === null) { return; } const widthVal = node.parentElement ? node.parentElement.clientWidth : this.state._width; const heightVal = node.parentElement && node.parentElement?.offsetHeight > this.state._height! ? node.parentElement?.offsetHeight : this.state._height; const viewbox = `0 0 ${widthVal!} ${heightVal!}`; node.setAttribute('viewBox', viewbox); } private _createLegends(chartData: IChartDataPoint[]): JSX.Element { const legendDataItems = chartData.map((point: IChartDataPoint, index: number) => { const color: string = point.color!; // mapping data to the format Legends component needs const legend: ILegend = { title: point.legend!, color, action: () => { if (this.state.selectedLegend === point.legend) { this.setState({ selectedLegend: '' }); } else { this.setState({ selectedLegend: point.legend! }); } }, hoverAction: () => { this._handleChartMouseLeave(); this.setState({ activeLegend: point.legend! }); }, onMouseOutAction: () => { this.setState({ activeLegend: '' }); }, }; return legend; }); const legends = ( <Legends legends={legendDataItems} centerLegends overflowProps={this.props.legendsOverflowProps} focusZonePropsInHoverCard={this.props.focusZonePropsForLegendsInHoverCard} overflowText={this.props.legendsOverflowText} {...this.props.legendProps} /> ); return legends; } private _focusCallback = (data: IChartDataPoint, id: string, element: SVGPathElement): void => { this._currentHoverElement = element; this.setState({ /** Show the callout if highlighted arc is focused and Hide it if unhighlighted arc is focused */ showHover: this.state.selectedLegend === '' || this.state.selectedLegend === data.legend, value: data.data!.toString(), legend: data.legend, color: data.color!, xCalloutValue: data.xAxisCalloutData!, yCalloutValue: data.yAxisCalloutData!, focusedArcId: id, dataPointCalloutProps: data, callOutAccessibilityData: data.callOutAccessibilityData!, }); }; private _hoverCallback = (data: IChartDataPoint, e: React.MouseEvent<SVGPathElement>): void => { if (this._calloutAnchorPoint !== data) { this._calloutAnchorPoint = data; this._currentHoverElement = e; this.setState({ /** Show the callout if highlighted arc is hovered and Hide it if unhighlighted arc is hovered */ showHover: this.state.selectedLegend === '' || this.state.selectedLegend === data.legend, value: data.data!.toString(), legend: data.legend, color: data.color!, xCalloutValue: data.xAxisCalloutData!, yCalloutValue: data.yAxisCalloutData!, dataPointCalloutProps: data, callOutAccessibilityData: data.callOutAccessibilityData!, }); } }; private _onBlur = (): void => { this.setState({ focusedArcId: '' }); }; private _hoverLeave(): void { /**/ } private _handleChartMouseLeave = () => { this._calloutAnchorPoint = null; this.setState({ showHover: false }); }; private _valueInsideDonut(valueInsideDonut: string | number | undefined, data: IChartDataPoint[]) { const highlightedLegend = this._getHighlightedLegend(); if (valueInsideDonut !== undefined && (highlightedLegend !== '' || this.state.showHover)) { let legendValue = valueInsideDonut; data!.map((point: IChartDataPoint, index: number) => { if (point.legend === highlightedLegend || (this.state.showHover && point.legend === this.state.legend)) { legendValue = point.yAxisCalloutData ? point.yAxisCalloutData : point.data!; } return; }); return legendValue; } else { return valueInsideDonut; } } private _toLocaleString(data: string | number | undefined) { const localeString = convertToLocaleString(data, this.props.culture); if (!localeString) { return data; } return localeString?.toString(); } /** * This function returns * the selected legend if there is one * or the hovered legend if none of the legends is selected. * Note: This won't work in case of multiple legends selection. */ private _getHighlightedLegend() { return this.state.selectedLegend || this.state.activeLegend; } private _isChartEmpty(): boolean { return !( this.props.data && this.props.data.chartData && this.props.data.chartData!.filter((d: IChartDataPoint) => d.data! > 0).length > 0 ); } private _addDefaultColors = (donutChartDataPoint?: IChartDataPoint[]): IChartDataPoint[] => { return donutChartDataPoint ? donutChartDataPoint.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 }; }) : []; }; } |