Stat: Fix math for percent change value heights when sparkline is not rendered (#112599)

* Stat: Fix math for percent change value heights when sparkline is not rendered

* add tests
This commit is contained in:
Paul Marbach
2025-10-20 13:59:49 +00:00
committed by GitHub
parent dacfa2afed
commit 1d23c6cf88
2 changed files with 35 additions and 3 deletions
@@ -100,6 +100,28 @@ describe('BigValueLayout', () => {
);
expect(layout).toBeInstanceOf(WideWithChartLayout);
});
it.each([
['wide layout', {}],
['non-wide layout', { disableWideLayout: true }],
])('should shrink the value if percent change is shown for %s', (_, propsOverride) => {
const baseProps: Partial<Props> = {
width: 300,
height: 100,
sparkline: undefined,
alignmentFactors: {
text: '1000',
title: '12',
},
...propsOverride,
};
const layout = buildLayout(getProps(baseProps));
const layoutWithPercentChange = buildLayout(
getProps({ ...baseProps, value: { text: '25', numeric: 25, percentChange: 20 } })
);
expect(layoutWithPercentChange.valueFontSize).toBeLessThan(layout.valueFontSize);
});
});
describe('percentChangeColor', () => {
@@ -171,7 +171,7 @@ export abstract class BigValueLayout {
return {
containerStyles,
iconSize: iconSize,
iconSize,
};
}
@@ -294,11 +294,16 @@ export class WideNoChartLayout extends BigValueLayout {
const valueWidthPercent = this.titleToAlignTo?.length ? 0.3 : 1.0;
if (this.valueToAlignTo.length) {
let valueHeight = this.maxTextHeight;
if (props.value.percentChange != null) {
// percent change uses 40% of value height, so we want to scale the value font size accordingly
valueHeight = valueHeight * 0.75;
}
// initial value size
this.valueFontSize = calculateFontSize(
this.valueToAlignTo,
this.maxTextWidth * valueWidthPercent,
this.maxTextHeight,
valueHeight,
LINE_HEIGHT,
undefined,
VALUE_FONT_WEIGHT
@@ -479,10 +484,15 @@ export class StackedWithNoChartLayout extends BigValueLayout {
}
if (this.valueToAlignTo.length) {
let valueHeight = this.maxTextHeight - titleHeight;
if (props.value.percentChange != null) {
// percent change uses 40% of value height, so we want to scale the value font size accordingly
valueHeight = valueHeight * 0.75;
}
this.valueFontSize = calculateFontSize(
this.valueToAlignTo,
this.maxTextWidth,
this.maxTextHeight - titleHeight,
valueHeight,
LINE_HEIGHT,
undefined,
VALUE_FONT_WEIGHT