Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

charting: Grouped vertical bar chart code refactor. #15838

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"type": "none",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this be patch, minor or major?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi raghu,

All other PR's (which were cherry-picked and ported to master) are with type:none only. And while adding change file they are only 2 options(Prerelease and None). So I have added None.

"comment": "Grouped vertical bar chart code refactor cherry pick to port master",
"packageName": "@fluentui/react-charting",
"email": "v-jasha@microsoft.com",
"dependentChangeType": "none",
"date": "2020-11-05T06:59:32.711Z"
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as React from 'react';
import { styled } from '@fluentui/react/lib/Utilities';
import { IAreaChartProps, IAreaChartStyleProps, IAreaChartStyles } from './AreaChart.types';
import { IAreaChartProps, IAreaChartStyleProps, IAreaChartStyles } from '../../index';
import { AreaChartBase } from '../AreaChart/AreaChart.base';
import { getStyles } from './AreaChart.styles';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ import * as React from 'react';
import { IProcessedStyleSet, mergeStyles } 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,
} from '../../index';
import {
ChartHoverCard,
createNumericXAxis,
createStringXAxis,
getDomainNRangeValues,
Expand All @@ -19,9 +21,8 @@ import {
getMinMaxOfYAxis,
XAxisTypes,
YAxisType,
createWrapOfXLabels,
} from '../../utilities/index';
import { ChartHoverCard } from '../../utilities/ChartHoverCard/index';
import { FocusZone, FocusZoneDirection } from '@fluentui/react-focus';

const getClassNames = classNamesFunction<ICartesianChartStyleProps, ICartesianChartStyles>();

Expand All @@ -30,6 +31,17 @@ export interface ICartesianChartState {
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;
}

/**
Expand Down Expand Up @@ -57,6 +69,8 @@ export class CartesianChartBase extends React.Component<IModifiedCartesianChartP
containerWidth: 0,
_width: this.props.width || 600,
_height: this.props.height || 350,
_removalValueForTextTuncate: 0,
isRemoveValCalculated: true,
};
this.idForGraph = getId('chart_');
/**
Expand Down Expand Up @@ -109,14 +123,13 @@ export class CartesianChartBase extends React.Component<IModifiedCartesianChartP
showRoundOffXTickValues: true,
xAxisCount: this.props.xAxisTickCount,
xAxistickSize: this.props.xAxistickSize,
tickPadding: this.props.tickPadding,
xAxisPadding: this.props.xAxisPadding,
tickPadding: this.props.xAxisPadding,
};

const YAxisParams = {
margins: this.margins,
containerWidth: this.state.containerWidth,
containerHeight: this.state.containerHeight,
containerHeight: this.state.containerHeight - this.state._removalValueForTextTuncate!,
yAxisElement: this.yAxisElement,
yAxisTickFormat: this.props.yAxisTickFormat!,
yAxisTickCount: this.props.yAxisTickCount!,
Expand All @@ -138,16 +151,30 @@ export class CartesianChartBase extends React.Component<IModifiedCartesianChartP
let xScale: any;
switch (this.props.xAxisType!) {
case XAxisTypes.NumericAxis:
xScale = createNumericXAxis(XAxisParams, this._isRtl);
xScale = createNumericXAxis(XAxisParams);
break;
case XAxisTypes.DateAxis:
xScale = createDateXAxis(XAxisParams, this.props.tickParams!, this._isRtl);
xScale = createDateXAxis(XAxisParams, this.props.tickParams!);
break;
case XAxisTypes.StringAxis:
xScale = createStringXAxis(XAxisParams, this.props.tickParams!, this.props.datasetForXAxisDomain!);
break;
default:
xScale = createNumericXAxis(XAxisParams, this._isRtl);
xScale = createNumericXAxis(XAxisParams);
}

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.
if (this.state.isRemoveValCalculated && this.state._removalValueForTextTuncate !== temp) {
this.setState({ _removalValueForTextTuncate: temp, isRemoveValCalculated: false });
}
}

/**
Expand Down Expand Up @@ -205,7 +232,10 @@ export class CartesianChartBase extends React.Component<IModifiedCartesianChartP
this.xAxisElement = e;
}}
id={`xAxisGElement${this.idForGraph}`}
transform={`translate(0, ${svgDimensions.height - this.margins.bottom!})`}
// 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}
/>
<g
Expand Down Expand Up @@ -388,6 +418,12 @@ export class CartesianChartBase extends React.Component<IModifiedCartesianChartP
// 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.containerWidth);
this.props.getGraphData(
xScale,
yScale,
this.state.containerHeight - this.state._removalValueForTextTuncate!,
this.state.containerWidth,
this.xAxisElement,
);
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ import { IFocusZoneProps, FocusZoneDirection } from '@fluentui/react-focus';
import { ICalloutProps } from '@fluentui/react/lib/Callout';
import { ILegendsProps } from '../Legends/index';
import { IMargins } from '../../types/index';
import { ChartTypes, XAxisTypes, YAxisType } from '../../utilities/index';
import { IChartHoverCardProps } from '../../utilities/ChartHoverCard/index';
import { ChartTypes, IChartHoverCardProps, XAxisTypes, YAxisType } from '../../utilities/index';

export interface ICartesianChartStyleProps {
/**
Expand Down Expand Up @@ -370,6 +369,12 @@ export interface IModifiedCartesianChartProps extends ICartesianChartProps {
*/
getmargins?: (margins: IMargins) => void;

/**
* This is a call back method to the chart from cartesian chart.
* params are xScale, yScale, containerHeight, containerWidth. These values were used to draw the graph.
* It also contians an optional param xAxisElement - defines as x axis scale element.
* This param used to enable feature word wrap of Xaxis.
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
getGraphData?: any;

Expand Down Expand Up @@ -412,7 +417,6 @@ export interface IModifiedCartesianChartProps extends ICartesianChartProps {
datasetForXAxisDomain?: string[];

/** Own callout design */
// need to add type here
// eslint-disable-next-line @typescript-eslint/no-explicit-any
customizedCallout?: any;

Expand Down
Loading