Skip to content

Commit

Permalink
Format download count number in weekly sparkline tooltip (dart-lang#8266
Browse files Browse the repository at this point in the history
)
  • Loading branch information
szakarias authored Nov 12, 2024
1 parent 0e70bf0 commit cf69358
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
14 changes: 14 additions & 0 deletions pkg/_pub_shared/lib/format/number_format.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,20 @@ String _toFixed(int value, int d) {
return (((value * 10) ~/ d) / 10).toStringAsFixed(1);
}

/// Formats an int [value] with commas as thousand seperators.
String formatWithThousandSeperators(int value) {
final digits = value.toString().split('');
final l = digits.length - 1;
final buffer = StringBuffer();
for (int j = 0; j <= l; j++) {
if (j > 0 && j % 3 == 0) {
buffer.write(',');
}
buffer.write(digits[l - j]);
}
return buffer.toString().split('').reversed.join();
}

/// Formats an int [value] to human readable chunk and suffix with at most 3
/// significant digits.
({String value, String suffix}) formatWith3SignificantDigits(int value) {
Expand Down
14 changes: 14 additions & 0 deletions pkg/_pub_shared/test/format/number_format_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,18 @@ void main() {
expect(formatWith3SignificantDigits(19000000000),
(value: '19.0', suffix: 'B'));
});

test('Number with thousand seperators', () {
// expect(formatWithThousandSeperators(1), '1');
expect(formatWithThousandSeperators(10), '10');
expect(formatWithThousandSeperators(100), '100');
expect(formatWithThousandSeperators(1000), '1,000');
expect(formatWithThousandSeperators(10000), '10,000');
expect(formatWithThousandSeperators(100000), '100,000');
expect(formatWithThousandSeperators(1000000), '1,000,000');
expect(formatWithThousandSeperators(10000000), '10,000,000');
expect(formatWithThousandSeperators(100000000), '100,000,000');
expect(formatWithThousandSeperators(1000000000), '1,000,000,000');
expect(formatWithThousandSeperators(10000000000), '10,000,000,000');
});
}
4 changes: 2 additions & 2 deletions pkg/web_app/lib/src/widget/weekly_sparkline/widget.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'dart:math';

import 'package:_pub_shared/format/encoding.dart';
import 'package:_pub_shared/format/number_format.dart';
import 'package:web/web.dart';

void create(HTMLElement element, Map<String, String> options) {
Expand Down Expand Up @@ -145,8 +146,7 @@ void drawChart(Element svg, HTMLDivElement toolTip, HTMLDivElement chartSubText,
final coords = computeCoordinates(selectedDay.date, selectedDay.downloads);
sparklineSpot.setAttribute('cy', '${coords.$2}');
sparklineCursor.setAttribute('transform', 'translate(${coords.$1}, 0)');

toolTip.text = '${selectedDay.downloads}';
toolTip.text = '${formatWithThousandSeperators(selectedDay.downloads)}';

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

0 comments on commit cf69358

Please sign in to comment.