Skip to content

Commit cf69358

Browse files
authored
Format download count number in weekly sparkline tooltip (#8266)
1 parent 0e70bf0 commit cf69358

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

pkg/_pub_shared/lib/format/number_format.dart

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,20 @@ String _toFixed(int value, int d) {
1919
return (((value * 10) ~/ d) / 10).toStringAsFixed(1);
2020
}
2121

22+
/// Formats an int [value] with commas as thousand seperators.
23+
String formatWithThousandSeperators(int value) {
24+
final digits = value.toString().split('');
25+
final l = digits.length - 1;
26+
final buffer = StringBuffer();
27+
for (int j = 0; j <= l; j++) {
28+
if (j > 0 && j % 3 == 0) {
29+
buffer.write(',');
30+
}
31+
buffer.write(digits[l - j]);
32+
}
33+
return buffer.toString().split('').reversed.join();
34+
}
35+
2236
/// Formats an int [value] to human readable chunk and suffix with at most 3
2337
/// significant digits.
2438
({String value, String suffix}) formatWith3SignificantDigits(int value) {

pkg/_pub_shared/test/format/number_format_test.dart

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,18 @@ void main() {
6666
expect(formatWith3SignificantDigits(19000000000),
6767
(value: '19.0', suffix: 'B'));
6868
});
69+
70+
test('Number with thousand seperators', () {
71+
// expect(formatWithThousandSeperators(1), '1');
72+
expect(formatWithThousandSeperators(10), '10');
73+
expect(formatWithThousandSeperators(100), '100');
74+
expect(formatWithThousandSeperators(1000), '1,000');
75+
expect(formatWithThousandSeperators(10000), '10,000');
76+
expect(formatWithThousandSeperators(100000), '100,000');
77+
expect(formatWithThousandSeperators(1000000), '1,000,000');
78+
expect(formatWithThousandSeperators(10000000), '10,000,000');
79+
expect(formatWithThousandSeperators(100000000), '100,000,000');
80+
expect(formatWithThousandSeperators(1000000000), '1,000,000,000');
81+
expect(formatWithThousandSeperators(10000000000), '10,000,000,000');
82+
});
6983
}

pkg/web_app/lib/src/widget/weekly_sparkline/widget.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import 'dart:math';
22

33
import 'package:_pub_shared/format/encoding.dart';
4+
import 'package:_pub_shared/format/number_format.dart';
45
import 'package:web/web.dart';
56

67
void create(HTMLElement element, Map<String, String> options) {
@@ -145,8 +146,7 @@ void drawChart(Element svg, HTMLDivElement toolTip, HTMLDivElement chartSubText,
145146
final coords = computeCoordinates(selectedDay.date, selectedDay.downloads);
146147
sparklineSpot.setAttribute('cy', '${coords.$2}');
147148
sparklineCursor.setAttribute('transform', 'translate(${coords.$1}, 0)');
148-
149-
toolTip.text = '${selectedDay.downloads}';
149+
toolTip.text = '${formatWithThousandSeperators(selectedDay.downloads)}';
150150

151151
final startDate = selectedDay.date.subtract(Duration(days: 7));
152152
chartSubText.text =

0 commit comments

Comments
 (0)