Skip to content

Commit 2e2354b

Browse files
Anush2303Anush
andauthored
feat(react-charts): Add support for negative Y values in GVBC (#34687)
Co-authored-by: Anush <[email protected]>
1 parent 0c73880 commit 2e2354b

File tree

8 files changed

+566
-182
lines changed

8 files changed

+566
-182
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"type": "patch",
3+
"comment": "Add negative y value support in GVBC",
4+
"packageName": "@fluentui/react-charts",
5+
"email": "[email protected]",
6+
"dependentChangeType": "patch"
7+
}

packages/charts/react-charts/library/etc/react-charts.api.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -606,6 +606,7 @@ export interface GroupedVerticalBarChartProps extends CartesianChartProps {
606606
isCalloutForStack?: boolean;
607607
maxBarWidth?: number;
608608
mode?: 'default' | 'plotly';
609+
roundCorners?: boolean;
609610
styles?: GroupedVerticalBarChartStyles;
610611
useSingleColor?: boolean;
611612
xAxisInnerPadding?: number;

packages/charts/react-charts/library/src/components/CommonComponents/CartesianChart.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,9 @@ export const CartesianChart: React.FunctionComponent<ModifiedCartesianChartProps
255255
yMaxValue: props.yMaxValue || 0,
256256
tickPadding: 10,
257257
maxOfYVal: props.maxOfYVal,
258-
yMinMaxValues: getMinMaxOfYAxis(points, chartType, props.yAxisType),
258+
yMinMaxValues: props.getMinMaxOfYAxis
259+
? props.getMinMaxOfYAxis(points, props.yAxisType)
260+
: getMinMaxOfYAxis(points, chartType, props.yAxisType),
259261
// please note these padding default values must be consistent in here
260262
// and the parent chart(HBWA/Vertical etc..) for more details refer example
261263
// http://using-d3js.com/04_07_ordinal_scales.html

packages/charts/react-charts/library/src/components/GroupedVerticalBarChart/GroupedVerticalBarChart.tsx

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ const X1_INNER_PADDING = 0.1;
5050
// => space_between_bars = (x1_inner_padding / (1 - x1_inner_padding)) * bar_width
5151
/** Rate at which the space between the bars in a group changes wrt the bar width */
5252
const BAR_GAP_RATE = X1_INNER_PADDING / (1 - X1_INNER_PADDING);
53+
const VERTICAL_BAR_GAP = 1;
54+
const MIN_BAR_HEIGHT = 1;
5355

5456
// This interface used for - While forming datapoints from given prop "data" in code
5557
interface GVDataPoint {
@@ -82,6 +84,7 @@ export const GroupedVerticalBarChart: React.FC<GroupedVerticalBarChartProps> = R
8284
let _xAxisInnerPadding: number = 0;
8385
let _xAxisOuterPadding: number = 0;
8486
const cartesianChartRef = React.useRef<Chart>(null);
87+
const Y_ORIGIN: number = 0;
8588

8689
const [color, setColor] = React.useState<string>('');
8790
const [dataForHoverCard, setDataForHoverCard] = React.useState<number>(0);
@@ -411,16 +414,29 @@ export const GroupedVerticalBarChart: React.FC<GroupedVerticalBarChartProps> = R
411414
// To align the centers of the generated bandwidth and the calculated one when they differ,
412415
// use the following addend.
413416
const xPoint = xScale1(datasetKey) + (xScale1.bandwidth() - _barWidth) / 2;
414-
const yPoint = Math.max(yBarScale(pointData.data), 0);
415417
const startColor = pointData.color ? pointData.color : getNextColor(index, 0);
416418

419+
const yBaseline = yBarScale(Y_ORIGIN);
420+
let yPositiveStart = yBaseline;
421+
let yNegativeStart = yBaseline;
422+
let yPoint = Y_ORIGIN;
423+
424+
const barGap = (VERTICAL_BAR_GAP / 2) * (index > 0 ? 2 : 0);
425+
const height = Math.max(yBarScale(Y_ORIGIN) - yBarScale(Math.abs(pointData.data)), MIN_BAR_HEIGHT);
426+
if (pointData.data >= Y_ORIGIN) {
427+
yPositiveStart -= height + barGap;
428+
yPoint = yPositiveStart;
429+
} else {
430+
yPoint = yNegativeStart + barGap;
431+
yNegativeStart = yPoint + height;
432+
}
417433
// Not rendering data with 0.
418434
pointData.data &&
419435
singleGroup.push(
420436
<React.Fragment key={`${singleSet.indexNum}-${index}`}>
421437
<rect
422438
className={classes.opacityChangeOnHover}
423-
height={Math.max(containerHeight! - _margins.bottom! - yBarScale(pointData.data), 0)}
439+
height={height}
424440
width={_barWidth}
425441
x={xPoint}
426442
y={yPoint}
@@ -453,7 +469,7 @@ export const GroupedVerticalBarChart: React.FC<GroupedVerticalBarChartProps> = R
453469
<text
454470
key={`${singleSet.indexNum}-${index}`}
455471
x={xPoint + _barWidth / 2}
456-
y={yPoint - 6}
472+
y={pointData.data >= Y_ORIGIN ? yPositiveStart - 6 : yNegativeStart + 12}
457473
textAnchor="middle"
458474
className={classes.barLabel}
459475
aria-hidden={true}

packages/charts/react-charts/library/src/components/GroupedVerticalBarChart/GroupedVerticalBarChart.types.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,12 @@ export interface GroupedVerticalBarChartProps extends CartesianChartProps {
8787
* @default 'default'
8888
*/
8989
mode?: 'default' | 'plotly';
90+
91+
/**
92+
* @default false
93+
* The prop used to enable rounded corners for the chart.
94+
*/
95+
roundCorners?: boolean;
9096
}
9197

9298
/**

0 commit comments

Comments
 (0)