Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>SchemeUserState</key>
<dict>
<key>SwiftUICharts.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>0</integer>
</dict>
</dict>
</dict>
</plist>
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
16 changes: 11 additions & 5 deletions Sources/SwiftUICharts/PieChart/PieChartRow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,38 @@

import SwiftUI

struct PieChartData {
var label: String = ""
var value: Double
}

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, +)
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, normalizedValue: normalized, label: slice.label))
}
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)
}
}
}
Expand Down
10 changes: 6 additions & 4 deletions Sources/SwiftUICharts/PieChart/PieChartView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,25 @@
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 showPercentage:Bool

@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) {
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", showPercentage: Bool? = false){
self.data = data
self.title = title
self.legend = legend
Expand All @@ -37,6 +38,7 @@ public struct PieChartView : View {
}
self.dropShadow = dropShadow!
self.valueSpecifier = valueSpecifier!
self.showPercentage = showPercentage
}

public var body: some View {
Expand All @@ -52,7 +54,7 @@ public struct PieChartView : View {
.font(.headline)
.foregroundColor(self.style.textColor)
}else{
Text("\(self.currentValue, specifier: self.valueSpecifier)")
Text("\(self.currentValue.label) \(self.currentValue, specifier: self.valueSpecifier)\(self.showPercentage ? "%" : "")")
.font(.headline)
.foregroundColor(self.style.textColor)
}
Expand Down