Skip to content

Commit

Permalink
Concider fraction digits when formatting chart side titles, #1267
Browse files Browse the repository at this point in the history
  • Loading branch information
imaNNeo committed Oct 7, 2023
1 parent ce49b92 commit d293877
Show file tree
Hide file tree
Showing 17 changed files with 1,144 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,11 @@ class SideTitlesWidget extends StatelessWidget {
max: axisMax,
appliedInterval: interval,
sideTitles: sideTitles,
formattedValue: Utils().formatNumber(metaData.axisValue),
formattedValue: Utils().formatNumber(
axisMin,
axisMax,
metaData.axisValue,
),
axisSide: side,
parentAxisSize: axisViewSize,
axisPosition: metaData.axisPixelLocation,
Expand Down
49 changes: 39 additions & 10 deletions lib/src/utils/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -211,33 +211,62 @@ class Utils {
/// kilo (thousands) number
static const double kilo = 1000;

/// Returns count of fraction digits of a value
int getFractionDigits(double value) {
if (value >= 1) {
return 1;
} else if (value >= 0.1) {
return 2;
} else if (value >= 0.01) {
return 3;
} else if (value >= 0.001) {
return 4;
} else if (value >= 0.0001) {
return 5;
} else if (value >= 0.00001) {
return 6;
} else if (value >= 0.000001) {
return 7;
} else if (value >= 0.0000001) {
return 8;
} else if (value >= 0.00000001) {
return 9;
} else if (value >= 0.000000001) {
return 10;
}
return 1;
}

/// Formats and add symbols (K, M, B) at the end of number.
///
/// if number is larger than [billion], it returns a short number like 13.3B,
/// if number is larger than [million], it returns a short number line 43M,
/// if number is larger than [kilo], it returns a short number like 4K,
/// otherwise it returns number itself.
/// also it removes .0, at the end of number for simplicity.
String formatNumber(double number) {
final isNegative = number < 0;
String formatNumber(double axisMin, double axisMax, double axisValue) {
final isNegative = axisValue < 0;

if (isNegative) {
number = number.abs();
axisValue = axisValue.abs();
}

String resultNumber;
String symbol;
if (number >= billion) {
resultNumber = (number / billion).toStringAsFixed(1);
if (axisValue >= billion) {
resultNumber = (axisValue / billion).toStringAsFixed(1);
symbol = 'B';
} else if (number >= million) {
resultNumber = (number / million).toStringAsFixed(1);
} else if (axisValue >= million) {
resultNumber = (axisValue / million).toStringAsFixed(1);
symbol = 'M';
} else if (number >= kilo) {
resultNumber = (number / kilo).toStringAsFixed(1);
} else if (axisValue >= kilo) {
resultNumber = (axisValue / kilo).toStringAsFixed(1);
symbol = 'K';
} else {
resultNumber = number.toStringAsFixed(1);
final diff = (axisMin - axisMax).abs();
resultNumber = axisValue.toStringAsFixed(
getFractionDigits(diff),
);
symbol = '';
}

Expand Down
6 changes: 3 additions & 3 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ dependencies:
sdk: flutter

dev_dependencies:
build_runner: ^2.4.2
build_runner: ^2.4.6
flutter_test:
sdk: flutter
mockito: ^5.4.0
mockito: ^5.4.2
vector_math: ^2.1.4 #Added to use in some generated codes of mockito
very_good_analysis: ^5.0.0
very_good_analysis: ^5.1.0

screenshots:
- description: 'LineChartSample1 and LineChartSample2'
Expand Down
6 changes: 3 additions & 3 deletions test/chart/bar_chart/bar_chart_painter_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -795,7 +795,7 @@ void main() {
when(mockUtils.normalizeBorderSide(any, any)).thenReturn(BorderSide.none);
when(mockUtils.calculateRotationOffset(any, any)).thenReturn(Offset.zero);
when(mockUtils.getBestInitialIntervalValue(any, any, any)).thenReturn(0);
when(mockUtils.formatNumber(captureAny)).thenAnswer((inv) {
when(mockUtils.formatNumber(any, any, captureAny)).thenAnswer((inv) {
final value = inv.positionalArguments[0] as double;
return '${value.toInt()}';
});
Expand Down Expand Up @@ -987,7 +987,7 @@ void main() {
when(mockUtils.normalizeBorderSide(any, any)).thenReturn(BorderSide.none);
when(mockUtils.calculateRotationOffset(any, any)).thenReturn(Offset.zero);
when(mockUtils.getBestInitialIntervalValue(any, any, any)).thenReturn(0);
when(mockUtils.formatNumber(captureAny)).thenAnswer((inv) {
when(mockUtils.formatNumber(any, any, captureAny)).thenAnswer((inv) {
final value = inv.positionalArguments[0] as double;
return '${value.toInt()}';
});
Expand Down Expand Up @@ -1182,7 +1182,7 @@ void main() {
when(mockUtils.normalizeBorderSide(any, any)).thenReturn(BorderSide.none);
when(mockUtils.calculateRotationOffset(any, any)).thenReturn(Offset.zero);
when(mockUtils.getBestInitialIntervalValue(any, any, any)).thenReturn(0);
when(mockUtils.formatNumber(captureAny)).thenAnswer((inv) {
when(mockUtils.formatNumber(any, any, captureAny)).thenAnswer((inv) {
final value = inv.positionalArguments[0] as double;
return '${value.toInt()}';
});
Expand Down
Loading

0 comments on commit d293877

Please sign in to comment.