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

Added categorized slices for piechart (labels per slice) #165

Open
wants to merge 20 commits into
base: master
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
/.build
/Packages
/*.xcodeproj
.swiftpm
7 changes: 0 additions & 7 deletions .swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata

This file was deleted.

This file was deleted.

Binary file not shown.
Binary file not shown.
Binary file not shown.

This file was deleted.

This file was deleted.

8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`**
Expand Down
14 changes: 14 additions & 0 deletions Sources/SwiftUICharts/Helpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions Sources/SwiftUICharts/PieChart/PieChartCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ struct PieSlice: Identifiable {
var startDeg: Double
var endDeg: Double
var value: Double
var label: String
var normalizedValue: Double
}

Expand Down
29 changes: 17 additions & 12 deletions Sources/SwiftUICharts/PieChart/PieChartRow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,42 +9,47 @@
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)
}
}
}

public var body: some View {
GeometryReader { geometry in
ZStack{
ForEach(0..<self.slices.count){ i in
PieChartCell(rect: geometry.frame(in: .local), startDeg: self.slices[i].startDeg, endDeg: self.slices[i].endDeg, index: i, backgroundColor: self.backgroundColor,accentColor: self.accentColor)
.scaleEffect(self.currentTouchedIndex == i ? 1.1 : 1)
.animation(Animation.spring())
if self.slices.count > 0 {
ForEach(0..<self.slices.count){ i in
PieChartCell(rect: geometry.frame(in: .local), startDeg: self.slices[i].startDeg, endDeg: self.slices[i].endDeg, index: i, backgroundColor: self.backgroundColor,accentColor: self.accentColor)
.scaleEffect(self.currentTouchedIndex == i ? 1.1 : 1)
.animation(Animation.spring())
}
}
}
.gesture(DragGesture()
Expand All @@ -69,9 +74,9 @@ public struct PieChartRow : View {
struct PieChartRow_Previews : PreviewProvider {
static var previews: some View {
Group {
PieChartRow(data:[8,23,54,32,12,37,7,23,43], backgroundColor: Color(red: 252.0/255.0, green: 236.0/255.0, blue: 234.0/255.0), accentColor: Color(red: 225.0/255.0, green: 97.0/255.0, blue: 76.0/255.0), showValue: Binding.constant(false), currentValue: Binding.constant(0))
PieChartRow(data:[PieChartData(value: 8), PieChartData(value: 23), PieChartData(value: 54), PieChartData(value: 32), PieChartData(value: 12), PieChartData(value: 37), PieChartData(value: 7), PieChartData(value: 23), PieChartData(value: 43)], backgroundColor: Color(red: 252.0/255.0, green: 236.0/255.0, blue: 234.0/255.0), accentColor: Color(red: 225.0/255.0, green: 97.0/255.0, blue: 76.0/255.0), showValue: Binding.constant(false), currentValue: Binding.constant(PieChartData(value: 0)))
.frame(width: 100, height: 100)
PieChartRow(data:[0], backgroundColor: Color(red: 252.0/255.0, green: 236.0/255.0, blue: 234.0/255.0), accentColor: Color(red: 225.0/255.0, green: 97.0/255.0, blue: 76.0/255.0), showValue: Binding.constant(false), currentValue: Binding.constant(0))
PieChartRow(data:[PieChartData(value: 0)], backgroundColor: Color(red: 252.0/255.0, green: 236.0/255.0, blue: 234.0/255.0), accentColor: Color(red: 225.0/255.0, green: 97.0/255.0, blue: 76.0/255.0), showValue: Binding.constant(false), currentValue: Binding.constant(PieChartData(value: 0)))
.frame(width: 100, height: 100)
}
}
Expand Down
16 changes: 10 additions & 6 deletions Sources/SwiftUICharts/PieChart/PieChartView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,26 @@
import SwiftUI

public struct PieChartView : View {
public var data: [Double]
public var data: [PieChartData]
public var title: String
public var legend: String?
public var style: ChartStyle
public var formSize:CGSize
public var dropShadow: Bool
public var valueSpecifier:String
public var prefix:String
public var postfix: String

@State private var showValue = false
@State private var currentValue: Double = 0 {
@State private var currentValue: PieChartData = PieChartData(value: 0) {
didSet{
if(oldValue != self.currentValue && self.showValue) {
if(oldValue.label != self.currentValue.label && self.showValue) {
HapticFeedback.playSelection()
}
}
}

public init(data: [Double], title: String, legend: String? = nil, style: ChartStyle = Styles.pieChartStyleOne, form: CGSize? = ChartForm.medium, dropShadow: Bool? = true, valueSpecifier: String? = "%.1f"){
public init(data: [PieChartData], title: String, legend: String? = nil, style: ChartStyle = Styles.pieChartStyleOne, form: CGSize? = ChartForm.medium, dropShadow: Bool? = true, valueSpecifier: String? = "%.1f", prefix: String? = "", postfix: String? = ""){
self.data = data
self.title = title
self.legend = legend
Expand All @@ -37,6 +39,8 @@ public struct PieChartView : View {
}
self.dropShadow = dropShadow!
self.valueSpecifier = valueSpecifier!
self.prefix = prefix!
self.postfix = postfix!
}

public var body: some View {
Expand All @@ -52,7 +56,7 @@ public struct PieChartView : View {
.font(.headline)
.foregroundColor(self.style.textColor)
}else{
Text("\(self.currentValue, specifier: self.valueSpecifier)")
Text("\(self.currentValue.label) \(self.prefix)\(self.currentValue.value, specifier: self.valueSpecifier)\(self.postfix)")
.font(.headline)
.foregroundColor(self.style.textColor)
}
Expand All @@ -78,7 +82,7 @@ public struct PieChartView : View {
#if DEBUG
struct PieChartView_Previews : PreviewProvider {
static var previews: some View {
PieChartView(data:[56,78,53,65,54], title: "Title", legend: "Legend")
PieChartView(data:[PieChartData(label: "Q1", value: 56), PieChartData(label: "Q2", value: 78), PieChartData(label: "Q3", value: 53), PieChartData(label: "Q4", value: 65), PieChartData(label: "Q5", value: 54)], title: "Title", legend: "Legend")
}
}
#endif