From fd692f3357bd77a71111a293729d5d23d7e2599a Mon Sep 17 00:00:00 2001 From: Rich Fellure Date: Thu, 1 Aug 2024 12:29:09 -0600 Subject: [PATCH 1/2] Some simplifying. --- .../Shared/Models/Protocols/SharedProtocols.swift | 11 +++++++++++ .../Models/Protocols/SharedProtocolsExtensions.swift | 5 +---- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/Sources/SwiftUICharts/Shared/Models/Protocols/SharedProtocols.swift b/Sources/SwiftUICharts/Shared/Models/Protocols/SharedProtocols.swift index 51ff211c..0d17fd05 100644 --- a/Sources/SwiftUICharts/Shared/Models/Protocols/SharedProtocols.swift +++ b/Sources/SwiftUICharts/Shared/Models/Protocols/SharedProtocols.swift @@ -146,6 +146,17 @@ public protocol CTSingleDataSetProtocol: CTDataSetProtocol { } +extension CTSingleDataSetProtocol where Self.DataPoint: CTStandardDataPointProtocol { + + /// Get average for the DataSet + func average() -> Double { + dataPoints + .map(\.value) + .reduce(0, +) + .divide(by: Double(dataPoints.count)) + } +} + /** Protocol for data sets that require a multiple sets of data . */ diff --git a/Sources/SwiftUICharts/Shared/Models/Protocols/SharedProtocolsExtensions.swift b/Sources/SwiftUICharts/Shared/Models/Protocols/SharedProtocolsExtensions.swift index 4edf053c..783f1cf0 100644 --- a/Sources/SwiftUICharts/Shared/Models/Protocols/SharedProtocolsExtensions.swift +++ b/Sources/SwiftUICharts/Shared/Models/Protocols/SharedProtocolsExtensions.swift @@ -222,10 +222,7 @@ extension CTMultiDataSetProtocol where Self.DataSet.DataPoint: CTStandardDataPoi self.dataSets .compactMap { - $0.dataPoints - .map(\.value) - .reduce(0, +) - .divide(by: Double($0.dataPoints.count)) + $0.average() } .reduce(0, +) .divide(by: Double(self.dataSets.count)) From 9006c3fb21722845cb902642956162f32b7432e8 Mon Sep 17 00:00:00 2001 From: Rich Fellure Date: Thu, 1 Aug 2024 12:54:01 -0600 Subject: [PATCH 2/2] Remove breaking change, try different approach. --- .../Shared/Models/Protocols/SharedProtocols.swift | 11 ----------- .../Models/Protocols/SharedProtocolsExtensions.swift | 7 +++++-- 2 files changed, 5 insertions(+), 13 deletions(-) diff --git a/Sources/SwiftUICharts/Shared/Models/Protocols/SharedProtocols.swift b/Sources/SwiftUICharts/Shared/Models/Protocols/SharedProtocols.swift index 0d17fd05..51ff211c 100644 --- a/Sources/SwiftUICharts/Shared/Models/Protocols/SharedProtocols.swift +++ b/Sources/SwiftUICharts/Shared/Models/Protocols/SharedProtocols.swift @@ -146,17 +146,6 @@ public protocol CTSingleDataSetProtocol: CTDataSetProtocol { } -extension CTSingleDataSetProtocol where Self.DataPoint: CTStandardDataPointProtocol { - - /// Get average for the DataSet - func average() -> Double { - dataPoints - .map(\.value) - .reduce(0, +) - .divide(by: Double(dataPoints.count)) - } -} - /** Protocol for data sets that require a multiple sets of data . */ diff --git a/Sources/SwiftUICharts/Shared/Models/Protocols/SharedProtocolsExtensions.swift b/Sources/SwiftUICharts/Shared/Models/Protocols/SharedProtocolsExtensions.swift index 783f1cf0..37eb62e4 100644 --- a/Sources/SwiftUICharts/Shared/Models/Protocols/SharedProtocolsExtensions.swift +++ b/Sources/SwiftUICharts/Shared/Models/Protocols/SharedProtocolsExtensions.swift @@ -221,8 +221,11 @@ extension CTMultiDataSetProtocol where Self.DataSet.DataPoint: CTStandardDataPoi public func average() -> Double { self.dataSets - .compactMap { - $0.average() + .compactMap { dataSet -> Double in + dataSet.dataPoints + .map(\.value) + .reduce(0, +) + .divide(by: Double(dataSet.dataPoints.count)) } .reduce(0, +) .divide(by: Double(self.dataSets.count))