Skip to content

Commit 8a94b16

Browse files
committed
fix(frontend): Correctly display small usage percentages
The feature usage chart panel was rounding very small percentages (e.g., 0.01%) down to "0.0%", which was inaccurate and hid information from the user. This change adjusts the formatting logic in the tooltip to: - Display percentages between 0.01% and 0.1% with two decimal places. - Display percentages smaller than 0.01% with their full precision to avoid rounding them to zero. This ensures that the tooltip provides the most accurate information available, especially for features with very low usage. Unit tests for the component have been updated to verify the new formatting behavior.
1 parent 0908131 commit 8a94b16

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

frontend/src/static/js/components/test/webstatus-feature-usage-chart-panel.test.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,36 @@ describe('WebstatusFeatureUsageChartPanel', () => {
122122
'Chrome: 0.0%',
123123
);
124124

125+
// Test value between 0.01% and 0.1%
126+
const chromeTestDataPoint5: ChromeUsageStat = {
127+
timestamp: '2024-01-01',
128+
usage: 0.0009, // 0.09%
129+
};
130+
expect(chromeConfig.valueExtractor(chromeTestDataPoint5)).to.equal(0.09);
131+
expect(chromeConfig.tooltipExtractor!(chromeTestDataPoint5)).to.equal(
132+
'Chrome: 0.09%',
133+
);
134+
135+
// Test value of 0.01%
136+
const chromeTestDataPoint6: ChromeUsageStat = {
137+
timestamp: '2024-01-01',
138+
usage: 0.0001, // 0.01%
139+
};
140+
expect(chromeConfig.valueExtractor(chromeTestDataPoint6)).to.equal(0.01);
141+
expect(chromeConfig.tooltipExtractor!(chromeTestDataPoint6)).to.equal(
142+
'Chrome: 0.01%',
143+
);
144+
145+
// Test value less than 0.01%
146+
const chromeTestDataPoint7: ChromeUsageStat = {
147+
timestamp: '2024-01-01',
148+
usage: 0.00005, // 0.005%
149+
};
150+
expect(chromeConfig.valueExtractor(chromeTestDataPoint7)).to.equal(0.005);
151+
expect(chromeConfig.tooltipExtractor!(chromeTestDataPoint7)).to.equal(
152+
'Chrome: 0.005%',
153+
);
154+
125155
// Assert that there are no additional series configurations
126156
expect(additionalSeriesConfigs).to.be.undefined;
127157
});

frontend/src/static/js/components/webstatus-feature-usage-chart-panel.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,26 @@ export class WebstatusFeatureUsageChartPanel extends WebstatusLineChartPanel<Bro
4343
if (percentage >= 100) {
4444
return 100;
4545
}
46-
// Round to one decimal place.
46+
// If percentage is very small, pass it through without rounding.
47+
if (percentage > 0 && percentage < 0.1) {
48+
return percentage;
49+
}
50+
// Otherwise, round to one decimal place.
4751
return Math.round(percentage * 10) / 10;
4852
}
4953

5054
private formatPercentageForDisplay(percentage: number): string {
5155
if (percentage === 100) {
5256
return '100';
5357
}
58+
// For very small values, show the raw value.
59+
if (percentage > 0 && percentage < 0.01) {
60+
return String(percentage);
61+
}
62+
// For other small values, show 2 decimal places.
63+
if (percentage > 0 && percentage < 0.1) {
64+
return percentage.toFixed(2);
65+
}
5466
return percentage.toFixed(1);
5567
}
5668

0 commit comments

Comments
 (0)