Skip to content

Commit 49de9d1

Browse files
authored
fix(react-charting): Add scaling factor to heights of VerticalStacked bars (#34021)
1 parent 9f6e043 commit 49de9d1

File tree

4 files changed

+83
-15
lines changed

4 files changed

+83
-15
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"type": "patch",
3+
"comment": "Add scaling factor to heights of Vertical Stacked bars",
4+
"packageName": "@fluentui/react-charting",
5+
"email": "[email protected]",
6+
"dependentChangeType": "patch"
7+
}

packages/charts/react-charting/src/components/VerticalStackedBarChart/VerticalStackedBarChart.base.tsx

+16-6
Original file line numberDiff line numberDiff line change
@@ -845,20 +845,30 @@ export class VerticalStackedBarChartBase
845845
): {
846846
readonly gapHeight: number;
847847
readonly heightValueScale: number;
848+
readonly adjustedTotalHeight: number;
848849
} {
849850
const { barGapMax = 0 } = this.props;
850851

851852
// When displaying gaps between the bars, the height of each bar is
852853
// adjusted so that the total of all bars is not changed by the gaps
853-
const totalData = bars.reduce((iter, value) => iter + value.data, 0);
854+
const totalData = bars.reduce((iter, value) => iter + Math.abs(value.data), 0);
854855
const totalHeight = defaultTotalHeight ?? yBarScale(totalData);
856+
let sumOfPercent = 0;
857+
bars.forEach(point => {
858+
let value = (Math.abs(point.data) / totalData) * 100;
859+
if (value < 1 && value !== 0) {
860+
value = 1;
861+
}
862+
sumOfPercent += value;
863+
});
864+
const scalingRatio = sumOfPercent !== 0 ? sumOfPercent / 100 : 1;
855865
const gaps = barGapMax && bars.length - 1;
856866
const gapHeight = gaps && Math.max(barGapMin, Math.min(barGapMax, (totalHeight * barGapMultiplier) / gaps));
857-
const heightValueScale = (totalHeight - gapHeight * gaps) / totalData;
858-
867+
const heightValueScale = (totalHeight - gapHeight * gaps) / (totalData * scalingRatio);
859868
return {
860869
gapHeight,
861870
heightValueScale,
871+
adjustedTotalHeight: sumOfPercent,
862872
} as const;
863873
}
864874

@@ -902,7 +912,7 @@ export class VerticalStackedBarChartBase
902912
return undefined;
903913
}
904914

905-
const { gapHeight, heightValueScale } = this._getBarGapAndScale(barsToDisplay, yBarScale);
915+
const { gapHeight, heightValueScale, adjustedTotalHeight } = this._getBarGapAndScale(barsToDisplay, yBarScale);
906916

907917
if (heightValueScale < 0) {
908918
return undefined;
@@ -939,8 +949,8 @@ export class VerticalStackedBarChartBase
939949
};
940950

941951
let barHeight = heightValueScale * point.data;
942-
if (barHeight < Math.max(Math.ceil((heightValueScale * this._yMax) / 100.0), barMinimumHeight)) {
943-
barHeight = Math.max(Math.ceil((heightValueScale * this._yMax) / 100.0), barMinimumHeight);
952+
if (barHeight < Math.max((heightValueScale * adjustedTotalHeight) / 100.0, barMinimumHeight)) {
953+
barHeight = Math.max((heightValueScale * adjustedTotalHeight) / 100.0, barMinimumHeight);
944954
}
945955
yPoint = yPoint - barHeight - (index ? gapHeight : 0);
946956
barTotalValue += point.data;

packages/charts/react-charting/src/components/VerticalStackedBarChart/__snapshots__/VerticalStackedBarChartRTL.test.tsx.snap

+9-9
Original file line numberDiff line numberDiff line change
@@ -293,12 +293,12 @@ exports[`Vertical stacked bar chart - Screen resolution Should remain unchanged
293293
opacity: ;
294294
}
295295
fill="#00188f"
296-
height="3"
296+
height="2.37"
297297
rx="0"
298298
transform="translate(0, 0)"
299299
width="16"
300300
x="279"
301-
y="249.26"
301+
y="249.89"
302302
/>
303303
</g>
304304
<text
@@ -314,7 +314,7 @@ exports[`Vertical stacked bar chart - Screen resolution Should remain unchanged
314314
text-anchor="middle"
315315
transform="translate(0, 0)"
316316
x="287"
317-
y="243.26"
317+
y="243.89"
318318
>
319319
2.5
320320
</text>
@@ -1282,12 +1282,12 @@ exports[`Vertical stacked bar chart - Screen resolution Should remain unchanged
12821282
opacity: ;
12831283
}
12841284
fill="#00188f"
1285-
height="3"
1285+
height="2.37"
12861286
rx="0"
12871287
transform="translate(0, 0)"
12881288
width="16"
12891289
x="279"
1290-
y="249.26"
1290+
y="249.89"
12911291
/>
12921292
</g>
12931293
<text
@@ -1303,7 +1303,7 @@ exports[`Vertical stacked bar chart - Screen resolution Should remain unchanged
13031303
text-anchor="middle"
13041304
transform="translate(0, 0)"
13051305
x="287"
1306-
y="243.26"
1306+
y="243.89"
13071307
>
13081308
2.5
13091309
</text>
@@ -2271,12 +2271,12 @@ exports[`Vertical stacked bar chart - Subcomponent Line Should render the vertic
22712271
opacity: ;
22722272
}
22732273
fill="#00188f"
2274-
height="3"
2274+
height="2.37"
22752275
rx="0"
22762276
transform="translate(0, 0)"
22772277
width="16"
22782278
x="279"
2279-
y="249.26"
2279+
y="249.89"
22802280
/>
22812281
</g>
22822282
<text
@@ -2292,7 +2292,7 @@ exports[`Vertical stacked bar chart - Subcomponent Line Should render the vertic
22922292
text-anchor="middle"
22932293
transform="translate(0, 0)"
22942294
x="287"
2295-
y="243.26"
2295+
y="243.89"
22962296
>
22972297
2.5
22982298
</text>

packages/react-examples/src/react-charting/VerticalStackedBarChart/VerticalStackedBarChart.Basic.Example.tsx

+51
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,55 @@ export class VerticalStackedBarChartBasicExample extends React.Component<{}, IVe
123123
xAxisCalloutData: '2020/04/30',
124124
yAxisCalloutData: '31%',
125125
},
126+
{
127+
legend: 'Metadata4',
128+
data: 10,
129+
color: getColorFromToken(DataVizPalette.color4),
130+
xAxisCalloutData: '2020/04/30',
131+
yAxisCalloutData: '1%',
132+
},
133+
{
134+
legend: 'Metadata5',
135+
data: 23,
136+
color: getColorFromToken(DataVizPalette.color5),
137+
xAxisCalloutData: '2020/04/30',
138+
yAxisCalloutData: '2%',
139+
},
140+
{
141+
legend: 'Metadata6',
142+
data: 0.4,
143+
color: getColorFromToken(DataVizPalette.color6),
144+
xAxisCalloutData: '2020/04/30',
145+
yAxisCalloutData: '3%',
146+
},
147+
{
148+
legend: 'Metadata7',
149+
data: 0.5,
150+
color: getColorFromToken(DataVizPalette.color7),
151+
xAxisCalloutData: '2020/04/30',
152+
yAxisCalloutData: '4%',
153+
},
154+
{
155+
legend: 'Metadata8',
156+
data: 0.3,
157+
color: getColorFromToken(DataVizPalette.color8),
158+
xAxisCalloutData: '2020/04/30',
159+
yAxisCalloutData: '5%',
160+
},
161+
{
162+
legend: 'Metadata9',
163+
data: 0.7,
164+
color: getColorFromToken(DataVizPalette.color9),
165+
xAxisCalloutData: '2020/04/30',
166+
yAxisCalloutData: '6%',
167+
},
168+
{
169+
legend: 'Metadata10',
170+
data: 0.1,
171+
color: getColorFromToken(DataVizPalette.color10),
172+
xAxisCalloutData: '2020/04/30',
173+
yAxisCalloutData: '7%',
174+
},
126175
];
127176

128177
const secondChartPoints: IVSChartDataPoint[] = [
@@ -341,6 +390,7 @@ export class VerticalStackedBarChartBasicExample extends React.Component<{}, IVe
341390
xAxisTitle={this.state.showAxisTitles ? 'Number of days' : undefined}
342391
enableGradient={this.state.enableGradient}
343392
roundCorners={this.state.roundCorners}
393+
roundedTicks={true}
344394
/>
345395
</div>
346396
)}
@@ -363,6 +413,7 @@ export class VerticalStackedBarChartBasicExample extends React.Component<{}, IVe
363413
enableReflow={true}
364414
enableGradient={this.state.enableGradient}
365415
roundCorners={this.state.roundCorners}
416+
roundedTicks={true}
366417
/>
367418
</div>
368419
)}

0 commit comments

Comments
 (0)