From 41ae51dbaaaf5e337b387d252d9b1a652d5f9b9d Mon Sep 17 00:00:00 2001 From: Harry Li Date: Thu, 23 Sep 2021 14:23:15 +1000 Subject: [PATCH 1/4] Fixed the use of partitioningIndex(where:) in ChartDataSet.entriesForXValue Fixed the rounding issue of ChartDataSet.entryIndex() --- Charts.xcodeproj/project.pbxproj | 2 +- .../xcshareddata/swiftpm/Package.resolved | 8 ++++---- .../Implementations/Standard/ChartDataSet.swift | 15 ++++++++++++--- 3 files changed, 17 insertions(+), 8 deletions(-) diff --git a/Charts.xcodeproj/project.pbxproj b/Charts.xcodeproj/project.pbxproj index 18abb7650e..d99145814f 100644 --- a/Charts.xcodeproj/project.pbxproj +++ b/Charts.xcodeproj/project.pbxproj @@ -1309,7 +1309,7 @@ repositoryURL = "https://github.com/apple/swift-algorithms"; requirement = { kind = upToNextMajorVersion; - minimumVersion = 0.0.2; + minimumVersion = 1.0.0; }; }; 22BBDA0625CC4C2F00435670 /* XCRemoteSwiftPackageReference "swift-snapshot-testing" */ = { diff --git a/Charts.xcworkspace/xcshareddata/swiftpm/Package.resolved b/Charts.xcworkspace/xcshareddata/swiftpm/Package.resolved index 1abcae916a..c78bdaf3f5 100644 --- a/Charts.xcworkspace/xcshareddata/swiftpm/Package.resolved +++ b/Charts.xcworkspace/xcshareddata/swiftpm/Package.resolved @@ -6,8 +6,8 @@ "repositoryURL": "https://github.com/apple/swift-algorithms", "state": { "branch": null, - "revision": "bb3bafeca0e164ece3403a9de646b7d38c07dd49", - "version": "0.0.2" + "revision": "b14b7f4c528c942f121c8b860b9410b2bf57825e", + "version": "1.0.0" } }, { @@ -15,8 +15,8 @@ "repositoryURL": "https://github.com/apple/swift-numerics", "state": { "branch": null, - "revision": "6b24333510e9044cf4716a07bed65eeed6bc6393", - "version": "0.0.8" + "revision": "0a23770641f65a4de61daf5425a37ae32a3fd00d", + "version": "1.0.1" } }, { diff --git a/Source/Charts/Data/Implementations/Standard/ChartDataSet.swift b/Source/Charts/Data/Implementations/Standard/ChartDataSet.swift index 703c0abe72..36af42f1fa 100644 --- a/Source/Charts/Data/Implementations/Standard/ChartDataSet.swift +++ b/Source/Charts/Data/Implementations/Standard/ChartDataSet.swift @@ -197,9 +197,11 @@ open class ChartDataSet: ChartBaseDataSet open override func entriesForXValue(_ xValue: Double) -> [ChartDataEntry] { let match: (ChartDataEntry) -> Bool = { $0.x == xValue } - let i = partitioningIndex(where: match) + var partitioned = self.entries + _ = partitioned.partition(by: match) + let i = partitioned.partitioningIndex(where: match) guard i < endIndex else { return [] } - return self[i...].prefix(while: match) + return partitioned[i...].prefix(while: match) } /// - Parameters: @@ -234,7 +236,14 @@ open class ChartDataSet: ChartBaseDataSet } case .closest: - break + // The closest value in the beginning of this function + // `var closest = partitioningIndex { $0.x >= xValue }` + // doesn't guarantee closest rounding method + if closest > 0 { + let distanceAfter = abs(self[closest].x - xValue) + let distanceBefore = abs(self[closest-1].x - xValue) + distanceBefore < distanceAfter ? closest -= 1 : () + } } // Search by closest to y-value From 85cc87d30d6ac68181380272eea1e4e9dc5d8147 Mon Sep 17 00:00:00 2001 From: Harry Li Date: Fri, 1 Apr 2022 16:03:05 +1100 Subject: [PATCH 2/4] Fix rounding method to "closest" if there is a result. --- .../Data/Implementations/Standard/ChartDataSet.swift | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/Source/Charts/Data/Implementations/Standard/ChartDataSet.swift b/Source/Charts/Data/Implementations/Standard/ChartDataSet.swift index 36af42f1fa..5716344b32 100644 --- a/Source/Charts/Data/Implementations/Standard/ChartDataSet.swift +++ b/Source/Charts/Data/Implementations/Standard/ChartDataSet.swift @@ -101,8 +101,9 @@ open class ChartDataSet: ChartBaseDataSet guard !isEmpty else { return } - let indexFrom = entryIndex(x: fromX, closestToY: .nan, rounding: .down) - let indexTo = entryIndex(x: toX, closestToY: .nan, rounding: .up) + let indexFrom = entryIndex(x: fromX, closestToY: .nan, rounding: .closest) + var indexTo = entryIndex(x: toX, closestToY: .nan, rounding: .up) + if indexTo == -1 { indexTo = entryIndex(x: toX, closestToY: .nan, rounding: .closest) } guard indexTo >= indexFrom else { return } // only recalculate y @@ -216,7 +217,7 @@ open class ChartDataSet: ChartBaseDataSet rounding: ChartDataSetRounding) -> Int { var closest = partitioningIndex { $0.x >= xValue } - guard closest < endIndex else { return -1 } + guard closest < endIndex else { return rounding == .closest ? (endIndex-1) : -1 } let closestXValue = self[closest].x From e2d2038175c2a31bd848c477095c2ea574283161 Mon Sep 17 00:00:00 2001 From: Harry Li Date: Sun, 3 Apr 2022 16:31:21 +1000 Subject: [PATCH 3/4] Add back `setNeedsDisplay` when deselect currently highlighted bar. --- Source/Charts/Charts/ChartViewBase.swift | 1 + 1 file changed, 1 insertion(+) diff --git a/Source/Charts/Charts/ChartViewBase.swift b/Source/Charts/Charts/ChartViewBase.swift index 2810facf5b..02742d915e 100644 --- a/Source/Charts/Charts/ChartViewBase.swift +++ b/Source/Charts/Charts/ChartViewBase.swift @@ -447,6 +447,7 @@ open class ChartViewBase: NSUIView, ChartDataProvider, AnimatorDelegate { delegate?.chartValueNothingSelected?(self) } + setNeedsDisplay() return } From 30538b364f48d62ea525efda7b5ed14bc08570fc Mon Sep 17 00:00:00 2001 From: Harry Li Date: Tue, 12 Apr 2022 20:25:14 +1000 Subject: [PATCH 4/4] Update closestXValue when rounding method is closest. --- Source/Charts/Data/Implementations/Standard/ChartDataSet.swift | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Source/Charts/Data/Implementations/Standard/ChartDataSet.swift b/Source/Charts/Data/Implementations/Standard/ChartDataSet.swift index 5716344b32..70d2da5582 100644 --- a/Source/Charts/Data/Implementations/Standard/ChartDataSet.swift +++ b/Source/Charts/Data/Implementations/Standard/ChartDataSet.swift @@ -219,7 +219,7 @@ open class ChartDataSet: ChartBaseDataSet var closest = partitioningIndex { $0.x >= xValue } guard closest < endIndex else { return rounding == .closest ? (endIndex-1) : -1 } - let closestXValue = self[closest].x + var closestXValue = self[closest].x switch rounding { case .up: @@ -244,6 +244,7 @@ open class ChartDataSet: ChartBaseDataSet let distanceAfter = abs(self[closest].x - xValue) let distanceBefore = abs(self[closest-1].x - xValue) distanceBefore < distanceAfter ? closest -= 1 : () + closestXValue = self[closest].x } }