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

Fix float precision issue in sideTitles, #1473 #1479

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
@@ -1,5 +1,6 @@
## nextVersion
* **FEATURE** (by @Dartek12): Added gradient to [FlLine](https://github.com/imaNNeo/fl_chart/blob/master/repo_files/documentations/base_chart.md#FlLine), #1197
* **BUGFIX** (by @imaNNeo): Fix [float precision issue](https://stackoverflow.com/a/57491365/3543738) in sideTitles, #1473

## 0.64.0
* **BUGFIX** (by @Anas35) Fix Tooltip not displaying when value from BackgroundBarChartRodData is less than zero. #1345.
Expand Down
29 changes: 29 additions & 0 deletions example/ios/Podfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
PODS:
- Flutter (1.0.0)
- path_provider_foundation (0.0.1):
- Flutter
- FlutterMacOS
- url_launcher_ios (0.0.1):
- Flutter

DEPENDENCIES:
- Flutter (from `Flutter`)
- path_provider_foundation (from `.symlinks/plugins/path_provider_foundation/darwin`)
- url_launcher_ios (from `.symlinks/plugins/url_launcher_ios/ios`)

EXTERNAL SOURCES:
Flutter:
:path: Flutter
path_provider_foundation:
:path: ".symlinks/plugins/path_provider_foundation/darwin"
url_launcher_ios:
:path: ".symlinks/plugins/url_launcher_ios/ios"

SPEC CHECKSUMS:
Flutter: f04841e97a9d0b0a8025694d0796dd46242b2854
path_provider_foundation: 29f094ae23ebbca9d3d0cec13889cd9060c0e943
url_launcher_ios: 08a3dfac5fb39e8759aeb0abbd5d9480f30fc8b4

PODFILE CHECKSUM: ef19549a9bc3046e7bb7d2fab4d021637c0c58a3

COCOAPODS: 1.13.0
75 changes: 73 additions & 2 deletions example/ios/Runner.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
archiveVersion = 1;
classes = {
};
objectVersion = 50;
objectVersion = 54;
objects = {

/* Begin PBXBuildFile section */
1498D2341E8E89220040F4C2 /* GeneratedPluginRegistrant.m in Sources */ = {isa = PBXBuildFile; fileRef = 1498D2331E8E89220040F4C2 /* GeneratedPluginRegistrant.m */; };
3B3967161E833CAA004F5970 /* AppFrameworkInfo.plist in Resources */ = {isa = PBXBuildFile; fileRef = 3B3967151E833CAA004F5970 /* AppFrameworkInfo.plist */; };
5D3F081B63EC9B3A946844DA /* Pods_Runner.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 06A47E3243ABDE701C1B259D /* Pods_Runner.framework */; };
74858FAF1ED2DC5600515810 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 74858FAE1ED2DC5600515810 /* AppDelegate.swift */; };
97C146FC1CF9000F007C117D /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 97C146FA1CF9000F007C117D /* Main.storyboard */; };
97C146FE1CF9000F007C117D /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 97C146FD1CF9000F007C117D /* Assets.xcassets */; };
Expand All @@ -29,12 +30,16 @@
/* End PBXCopyFilesBuildPhase section */

/* Begin PBXFileReference section */
06A47E3243ABDE701C1B259D /* Pods_Runner.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_Runner.framework; sourceTree = BUILT_PRODUCTS_DIR; };
1498D2321E8E86230040F4C2 /* GeneratedPluginRegistrant.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = GeneratedPluginRegistrant.h; sourceTree = "<group>"; };
1498D2331E8E89220040F4C2 /* GeneratedPluginRegistrant.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GeneratedPluginRegistrant.m; sourceTree = "<group>"; };
3B3967151E833CAA004F5970 /* AppFrameworkInfo.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; name = AppFrameworkInfo.plist; path = Flutter/AppFrameworkInfo.plist; sourceTree = "<group>"; };
4F5DA8BD3BF791E081EEB28A /* Pods-Runner.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Runner.debug.xcconfig"; path = "Target Support Files/Pods-Runner/Pods-Runner.debug.xcconfig"; sourceTree = "<group>"; };
74858FAD1ED2DC5600515810 /* Runner-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "Runner-Bridging-Header.h"; sourceTree = "<group>"; };
74858FAE1ED2DC5600515810 /* AppDelegate.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
7AFA3C8E1D35360C0083082E /* Release.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; name = Release.xcconfig; path = Flutter/Release.xcconfig; sourceTree = "<group>"; };
905CD23AF7EB07828B45A749 /* Pods-Runner.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Runner.release.xcconfig"; path = "Target Support Files/Pods-Runner/Pods-Runner.release.xcconfig"; sourceTree = "<group>"; };
95C47DA37FAFE0A8C6F9659F /* Pods-Runner.profile.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Runner.profile.xcconfig"; path = "Target Support Files/Pods-Runner/Pods-Runner.profile.xcconfig"; sourceTree = "<group>"; };
9740EEB21CF90195004384FC /* Debug.xcconfig */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xcconfig; name = Debug.xcconfig; path = Flutter/Debug.xcconfig; sourceTree = "<group>"; };
9740EEB31CF90195004384FC /* Generated.xcconfig */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xcconfig; name = Generated.xcconfig; path = Flutter/Generated.xcconfig; sourceTree = "<group>"; };
97C146EE1CF9000F007C117D /* Runner.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Runner.app; sourceTree = BUILT_PRODUCTS_DIR; };
Expand All @@ -49,12 +54,32 @@
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
5D3F081B63EC9B3A946844DA /* Pods_Runner.framework in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
/* End PBXFrameworksBuildPhase section */

/* Begin PBXGroup section */
3E426055A90FF197987466F2 /* Pods */ = {
isa = PBXGroup;
children = (
4F5DA8BD3BF791E081EEB28A /* Pods-Runner.debug.xcconfig */,
905CD23AF7EB07828B45A749 /* Pods-Runner.release.xcconfig */,
95C47DA37FAFE0A8C6F9659F /* Pods-Runner.profile.xcconfig */,
);
name = Pods;
path = Pods;
sourceTree = "<group>";
};
7D81FA6B330011F05D782E95 /* Frameworks */ = {
isa = PBXGroup;
children = (
06A47E3243ABDE701C1B259D /* Pods_Runner.framework */,
);
name = Frameworks;
sourceTree = "<group>";
};
9740EEB11CF90186004384FC /* Flutter */ = {
isa = PBXGroup;
children = (
Expand All @@ -72,6 +97,8 @@
9740EEB11CF90186004384FC /* Flutter */,
97C146F01CF9000F007C117D /* Runner */,
97C146EF1CF9000F007C117D /* Products */,
3E426055A90FF197987466F2 /* Pods */,
7D81FA6B330011F05D782E95 /* Frameworks */,
);
sourceTree = "<group>";
};
Expand Down Expand Up @@ -105,12 +132,14 @@
isa = PBXNativeTarget;
buildConfigurationList = 97C147051CF9000F007C117D /* Build configuration list for PBXNativeTarget "Runner" */;
buildPhases = (
181828416FC74716E515F1DE /* [CP] Check Pods Manifest.lock */,
9740EEB61CF901F6004384FC /* Run Script */,
97C146EA1CF9000F007C117D /* Sources */,
97C146EB1CF9000F007C117D /* Frameworks */,
97C146EC1CF9000F007C117D /* Resources */,
9705A1C41CF9048500538489 /* Embed Frameworks */,
3B06AD1E1E4923F5004D2608 /* Thin Binary */,
27C90C58091E21A7ACF1CA26 /* [CP] Embed Pods Frameworks */,
);
buildRules = (
);
Expand All @@ -127,7 +156,7 @@
97C146E61CF9000F007C117D /* Project object */ = {
isa = PBXProject;
attributes = {
LastUpgradeCheck = 1300;
LastUpgradeCheck = 1430;
ORGANIZATIONNAME = "";
TargetAttributes = {
97C146ED1CF9000F007C117D = {
Expand Down Expand Up @@ -169,12 +198,53 @@
/* End PBXResourcesBuildPhase section */

/* Begin PBXShellScriptBuildPhase section */
181828416FC74716E515F1DE /* [CP] Check Pods Manifest.lock */ = {
isa = PBXShellScriptBuildPhase;
buildActionMask = 2147483647;
files = (
);
inputFileListPaths = (
);
inputPaths = (
"${PODS_PODFILE_DIR_PATH}/Podfile.lock",
"${PODS_ROOT}/Manifest.lock",
);
name = "[CP] Check Pods Manifest.lock";
outputFileListPaths = (
);
outputPaths = (
"$(DERIVED_FILE_DIR)/Pods-Runner-checkManifestLockResult.txt",
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
shellScript = "diff \"${PODS_PODFILE_DIR_PATH}/Podfile.lock\" \"${PODS_ROOT}/Manifest.lock\" > /dev/null\nif [ $? != 0 ] ; then\n # print error to STDERR\n echo \"error: The sandbox is not in sync with the Podfile.lock. Run 'pod install' or update your CocoaPods installation.\" >&2\n exit 1\nfi\n# This output is used by Xcode 'outputs' to avoid re-running this script phase.\necho \"SUCCESS\" > \"${SCRIPT_OUTPUT_FILE_0}\"\n";
showEnvVarsInLog = 0;
};
27C90C58091E21A7ACF1CA26 /* [CP] Embed Pods Frameworks */ = {
isa = PBXShellScriptBuildPhase;
buildActionMask = 2147483647;
files = (
);
inputFileListPaths = (
"${PODS_ROOT}/Target Support Files/Pods-Runner/Pods-Runner-frameworks-${CONFIGURATION}-input-files.xcfilelist",
);
name = "[CP] Embed Pods Frameworks";
outputFileListPaths = (
"${PODS_ROOT}/Target Support Files/Pods-Runner/Pods-Runner-frameworks-${CONFIGURATION}-output-files.xcfilelist",
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
shellScript = "\"${PODS_ROOT}/Target Support Files/Pods-Runner/Pods-Runner-frameworks.sh\"\n";
showEnvVarsInLog = 0;
};
3B06AD1E1E4923F5004D2608 /* Thin Binary */ = {
isa = PBXShellScriptBuildPhase;
alwaysOutOfDate = 1;
buildActionMask = 2147483647;
files = (
);
inputPaths = (
"${TARGET_BUILD_DIR}/${INFOPLIST_PATH}",
);
name = "Thin Binary";
outputPaths = (
Expand All @@ -185,6 +255,7 @@
};
9740EEB61CF901F6004384FC /* Run Script */ = {
isa = PBXShellScriptBuildPhase;
alwaysOutOfDate = 1;
buildActionMask = 2147483647;
files = (
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1300"
LastUpgradeVersion = "1430"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
3 changes: 3 additions & 0 deletions example/ios/Runner.xcworkspace/contents.xcworkspacedata

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 13 additions & 1 deletion lib/src/chart/base/axis_chart/axis_chart_helper.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import 'dart:math';

import 'package:fl_chart/fl_chart.dart';
import 'package:fl_chart/src/extensions/double_extension.dart';
import 'package:fl_chart/src/utils/utils.dart';
import 'package:flutter/material.dart';

Expand Down Expand Up @@ -45,15 +48,24 @@ class AxisChartHelper {
if (minIncluded && !firstPositionOverlapsWithMin) {
yield min;
}
final intervalPrecision = pow(10, interval.precisionCount).toInt();
while (axisSeek <= end + epsilon) {
yield axisSeek;
axisSeek += interval;
axisSeek = _roundTo(
axisSeek + interval,
intervalPrecision,
);
}
if (maxIncluded && !lastPositionOverlapsWithMax) {
yield max;
}
}

/// Fixes the float precisionIssue
/// https://stackoverflow.com/a/57491365/3543738
double _roundTo(double value, int precision) =>
(value * precision).round() / precision;

/// Calculate translate offset to keep [SideTitle] child
/// placed inside its corresponding axis.
/// The offset will translate the child to the closest edge inside
Expand Down
16 changes: 16 additions & 0 deletions lib/src/extensions/double_extension.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
extension DoubleExtension on double {
int get precisionCount {
if (this % 1.0 == 0) {
return 0;
}
final str = toString();
final eDashIndex = str.indexOf('e-');
if (eDashIndex > -1) {
final firstPart = str.substring(0, eDashIndex);
final firstPartPrecision = double.parse(firstPart).precisionCount;
final secondPart = str.substring(eDashIndex + 2);
return firstPartPrecision + int.parse(secondPart);
}
return toString().length - 2;
}
}
3 changes: 2 additions & 1 deletion lib/src/utils/utils.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'dart:math' as math;
import 'dart:math';

import 'package:fl_chart/src/extensions/double_extension.dart';
import 'package:flutter/material.dart';

class Utils {
Expand Down Expand Up @@ -162,7 +163,7 @@ class Utils {
}

final inputString = input.toString();
var precisionCount = inputString.length - 2;
var precisionCount = input.precisionCount;

var zeroCount = 0;
for (var i = 2; i <= inputString.length; i++) {
Expand Down
18 changes: 16 additions & 2 deletions test/chart/base/axis_chart/axis_chart_helper_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,20 @@ import 'package:flutter_test/flutter_test.dart';
void main() {
const tolerance = 0.0001;
group('iterateThroughAxis()', () {
test('test 0', () {
final results = <double>[];
final axisValues = AxisChartHelper().iterateThroughAxis(
min: 0,
max: 0.001,
interval: 0.000000001,
baseLine: 0,
);
for (final axisValue in axisValues) {
results.add(axisValue);
}
expect(results.length, 1000001);
});

test('test 1', () {
final results = <double>[];
final axisValues = AxisChartHelper().iterateThroughAxis(
Expand Down Expand Up @@ -76,7 +90,7 @@ void main() {
expect(results[4], 10);
});

test('test 4', () {
test('test 5', () {
final results = <double>[];
final axisValues = AxisChartHelper().iterateThroughAxis(
min: 0,
Expand All @@ -95,7 +109,7 @@ void main() {
expect(results[2], 9);
});

test('test 4', () {
test('test 6', () {
final results = <double>[];
final axisValues = AxisChartHelper().iterateThroughAxis(
min: 35,
Expand Down
21 changes: 21 additions & 0 deletions test/extensions/double_extension_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import 'package:fl_chart/src/extensions/double_extension.dart';
import 'package:flutter_test/flutter_test.dart';

void main() {
test('test double.precision', () {
expect(0.0.precisionCount, 0);
expect(0.1.precisionCount, 1);
expect(0.02.precisionCount, 2);
expect(3.000.precisionCount, 0);
expect(3.001.precisionCount, 3);
expect(0.23232323.precisionCount, 8);
expect(0.493234898.precisionCount, 9);
expect(0.4932348985.precisionCount, 10);
expect(0.0000000005.precisionCount, 10);
expect(0.0000000000005.precisionCount, 13);
expect(0.0000000000000005.precisionCount, 16);
expect(0.00000000000000000005.precisionCount, 20);
expect(0.4343828875389984.precisionCount, 16);
expect(0.0000000005389984.precisionCount, 16);
});
}