Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Concider fraction digits when formatting chart side titles, #1267 #1454

Merged
merged 2 commits into from
Oct 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* **BUGFIX** (by @Anas35) Fix Tooltip not displaying when value from BackgroundBarChartRodData is less than zero. #1345.
* **BUGFIX** (by @imaNNeo) Fix Negative BarChartRodStackItem are not drawn correctly bug, #1347
* **BUGFIX** (by @imaNNeo) Fix bar_chart_helper minY calculation bug, #1388
* **IMPROVEMENT** (by @imaNNeo) Consider fraction digits when formatting chart side titles, #1267

## 0.63.0
* **BUGFIX** (by @imaNNeo) Fix PieChart crash on web-renderer html by ignoring `sectionsSpace` when `Path.combine()` does not work (it's flutter engine [issue](https://github.com/flutter/flutter/issues/44572)), #955
Expand Down
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