diff --git a/.gitignore b/.gitignore index 02c08753..0c62eec1 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ /.build /Packages /*.xcodeproj +.swiftpm diff --git a/.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata b/.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata deleted file mode 100644 index 919434a6..00000000 --- a/.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata +++ /dev/null @@ -1,7 +0,0 @@ - - - - - diff --git a/.swiftpm/xcode/package.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist b/.swiftpm/xcode/package.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist deleted file mode 100644 index 18d98100..00000000 --- a/.swiftpm/xcode/package.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist +++ /dev/null @@ -1,8 +0,0 @@ - - - - - IDEDidComputeMac32BitWarning - - - diff --git a/.swiftpm/xcode/package.xcworkspace/xcuserdata/andrassamu.xcuserdatad/UserInterfaceState.xcuserstate b/.swiftpm/xcode/package.xcworkspace/xcuserdata/andrassamu.xcuserdatad/UserInterfaceState.xcuserstate deleted file mode 100644 index 057e951c..00000000 Binary files a/.swiftpm/xcode/package.xcworkspace/xcuserdata/andrassamu.xcuserdatad/UserInterfaceState.xcuserstate and /dev/null differ diff --git a/.swiftpm/xcode/package.xcworkspace/xcuserdata/roderic.xcuserdatad/UserInterfaceState.xcuserstate b/.swiftpm/xcode/package.xcworkspace/xcuserdata/roderic.xcuserdatad/UserInterfaceState.xcuserstate deleted file mode 100644 index aae6dfe7..00000000 Binary files a/.swiftpm/xcode/package.xcworkspace/xcuserdata/roderic.xcuserdatad/UserInterfaceState.xcuserstate and /dev/null differ diff --git a/.swiftpm/xcode/package.xcworkspace/xcuserdata/samuandris.xcuserdatad/UserInterfaceState.xcuserstate b/.swiftpm/xcode/package.xcworkspace/xcuserdata/samuandris.xcuserdatad/UserInterfaceState.xcuserstate deleted file mode 100644 index 40db97a0..00000000 Binary files a/.swiftpm/xcode/package.xcworkspace/xcuserdata/samuandris.xcuserdatad/UserInterfaceState.xcuserstate and /dev/null differ diff --git a/.swiftpm/xcode/xcuserdata/andrassamu.xcuserdatad/xcschemes/xcschememanagement.plist b/.swiftpm/xcode/xcuserdata/andrassamu.xcuserdatad/xcschemes/xcschememanagement.plist deleted file mode 100644 index 1be8e537..00000000 --- a/.swiftpm/xcode/xcuserdata/andrassamu.xcuserdatad/xcschemes/xcschememanagement.plist +++ /dev/null @@ -1,27 +0,0 @@ - - - - - SchemeUserState - - SwiftUICharts.xcscheme_^#shared#^_ - - orderHint - 0 - - - SuppressBuildableAutocreation - - SwiftUICharts - - primary - - - SwiftUIChartsTests - - primary - - - - - diff --git a/.swiftpm/xcode/xcuserdata/samuandris.xcuserdatad/xcschemes/xcschememanagement.plist b/.swiftpm/xcode/xcuserdata/samuandris.xcuserdatad/xcschemes/xcschememanagement.plist deleted file mode 100644 index 443dea97..00000000 --- a/.swiftpm/xcode/xcuserdata/samuandris.xcuserdatad/xcschemes/xcschememanagement.plist +++ /dev/null @@ -1,14 +0,0 @@ - - - - - SchemeUserState - - SwiftUICharts.xcscheme_^#shared#^_ - - orderHint - 3 - - - - diff --git a/README.md b/README.md index d2cc1e55..54a94a6c 100644 --- a/README.md +++ b/README.md @@ -198,7 +198,13 @@ BarChartView(data: [8,23,54,32,12,37,7,23,43], title: "Title", form: ChartForm.s You can add a pie chart with the following code: ```swift - PieChartView(data: [8,23,54,32], title: "Title", legend: "Legendary") // legend is optional +PieChartView(data: [PieChartData(value: 8), PieChartData(value: 23), PieChartData(value: 54), PieChartData(value: 32)], title: "Title", legend: "Legendary") // legend is optional +``` + +You can add labels to the values of the pie chart with the following code: + +```swift +PieChartView(data: [PieChartData(label: "Q1", value: 8), PieChartData(label: "Q2", value: 23), PieChartData(label: "Q3", value: 54), PieChartData(label: "Q4", value: 32)], title: "Title", legend: "Legendary") // legend is optional ``` **Turn drop shadow off by adding to the Initialiser: `dropShadow: false`** diff --git a/Sources/SwiftUICharts/Helpers.swift b/Sources/SwiftUICharts/Helpers.swift index f1c5e5ac..9a58c276 100644 --- a/Sources/SwiftUICharts/Helpers.swift +++ b/Sources/SwiftUICharts/Helpers.swift @@ -213,6 +213,20 @@ public class ChartData: ObservableObject, Identifiable { } } +public struct PieChartData { + var label: String = "" + var value: Double + + public init(value: Double) { + self.value = value + } + + public init(label: String, value: Double) { + self.label = label + self.value = value + } +} + public class MultiLineChartData: ChartData { var gradient: GradientColor diff --git a/Sources/SwiftUICharts/PieChart/PieChartCell.swift b/Sources/SwiftUICharts/PieChart/PieChartCell.swift index f511165e..1d1a4cca 100644 --- a/Sources/SwiftUICharts/PieChart/PieChartCell.swift +++ b/Sources/SwiftUICharts/PieChart/PieChartCell.swift @@ -13,6 +13,7 @@ struct PieSlice: Identifiable { var startDeg: Double var endDeg: Double var value: Double + var label: String var normalizedValue: Double } diff --git a/Sources/SwiftUICharts/PieChart/PieChartRow.swift b/Sources/SwiftUICharts/PieChart/PieChartRow.swift index a462cd96..63764eb9 100644 --- a/Sources/SwiftUICharts/PieChart/PieChartRow.swift +++ b/Sources/SwiftUICharts/PieChart/PieChartRow.swift @@ -9,31 +9,34 @@ import SwiftUI public struct PieChartRow : View { - var data: [Double] + var data: [PieChartData] + var labeledData: [(String, Double)]? var backgroundColor: Color var accentColor: Color var slices: [PieSlice] { var tempSlices:[PieSlice] = [] var lastEndDeg:Double = 0 - let maxValue = data.reduce(0, +) + let maxValue = data.map({ + $0.value + }).reduce(0, +) for slice in data { - let normalized:Double = Double(slice)/Double(maxValue) + let normalized:Double = Double(slice.value)/Double(maxValue) let startDeg = lastEndDeg let endDeg = lastEndDeg + (normalized * 360) lastEndDeg = endDeg - tempSlices.append(PieSlice(startDeg: startDeg, endDeg: endDeg, value: slice, normalizedValue: normalized)) + tempSlices.append(PieSlice(startDeg: startDeg, endDeg: endDeg, value: slice.value, label: slice.label, normalizedValue: normalized)) } return tempSlices } @Binding var showValue: Bool - @Binding var currentValue: Double + @Binding var currentValue: PieChartData @State private var currentTouchedIndex = -1 { didSet { if oldValue != currentTouchedIndex { showValue = currentTouchedIndex != -1 - currentValue = showValue ? slices[currentTouchedIndex].value : 0 + currentValue = showValue ? PieChartData(label: slices[currentTouchedIndex].label, value: slices[currentTouchedIndex].value) : PieChartData(value: 0) } } } @@ -41,10 +44,12 @@ public struct PieChartRow : View { public var body: some View { GeometryReader { geometry in ZStack{ - ForEach(0.. 0 { + ForEach(0..