From 23bf320998d135ce14281e692dda7f4f216154c2 Mon Sep 17 00:00:00 2001 From: Tho Do Date: Fri, 13 May 2022 14:46:43 +0100 Subject: [PATCH 1/2] Update ChartData.swift Fix the wrong implementation of max function --- Source/Charts/Data/Implementations/Standard/ChartData.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Source/Charts/Data/Implementations/Standard/ChartData.swift b/Source/Charts/Data/Implementations/Standard/ChartData.swift index e5e22d0ac2..81fc37b3a8 100644 --- a/Source/Charts/Data/Implementations/Standard/ChartData.swift +++ b/Source/Charts/Data/Implementations/Standard/ChartData.swift @@ -422,7 +422,7 @@ open class ChartData: NSObject, ExpressibleByArrayLiteral /// The DataSet object with the maximum number of entries or null if there are no DataSets. @objc open var maxEntryCountSet: Element? { - return self.max { $0.entryCount > $1.entryCount } + return self.max { $0.entryCount < $1.entryCount } } } From b44cc744ad2b49478f1bd6d8a6876a9c0d9f852e Mon Sep 17 00:00:00 2001 From: Tho Do Date: Fri, 13 May 2022 15:03:44 +0100 Subject: [PATCH 2/2] Add testMaxEntryCountSet --- Tests/ChartsTests/ChartDataTests.swift | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/Tests/ChartsTests/ChartDataTests.swift b/Tests/ChartsTests/ChartDataTests.swift index 5b697f710b..0520aba227 100644 --- a/Tests/ChartsTests/ChartDataTests.swift +++ b/Tests/ChartsTests/ChartDataTests.swift @@ -64,4 +64,18 @@ class ChartDataTests: XCTestCase { XCTAssertTrue(data.dataSet(forLabel: SetLabels.badLabel, ignorecase: true) == nil) XCTAssertTrue(data.dataSet(forLabel: SetLabels.badLabel, ignorecase: false) == nil) } + + func testMaxEntryCountSet() throws { + let dataSet1 = BarChartDataSet(entries: (0 ..< 4).map { BarChartDataEntry(x: Double($0), y: Double($0)) }, + label: "data-set-1") + let dataSet2 = BarChartDataSet(entries: (0 ..< 3).map { BarChartDataEntry(x: Double($0), y: Double($0)) }, + label: "data-set-2") + let dataSet3 = BarChartDataSet(entries: [BarChartDataEntry(x: 0, y: 0)], + label: "data-set-3") + let data = BarChartData(dataSets: [dataSet1, dataSet2, dataSet3]) + + let maxEntryCountSet = try XCTUnwrap(data.maxEntryCountSet as? BarChartDataSet) + + XCTAssertEqual(maxEntryCountSet, dataSet1) + } }