-
Notifications
You must be signed in to change notification settings - Fork 644
Expand file tree
/
Copy pathPieChartView.swift
More file actions
88 lines (81 loc) · 3.44 KB
/
PieChartView.swift
File metadata and controls
88 lines (81 loc) · 3.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
//
// PieChartView.swift
// ChartView
//
// Created by András Samu on 2019. 06. 12..
// Copyright © 2019. András Samu. All rights reserved.
//
import SwiftUI
public struct PieChartView : View {
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: PieChartData = PieChartData(value: 0) {
didSet{
if(oldValue.label != self.currentValue.label && self.showValue) {
HapticFeedback.playSelection()
}
}
}
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
self.style = style
self.formSize = form!
if self.formSize == ChartForm.large {
self.formSize = ChartForm.extraLarge
}
self.dropShadow = dropShadow!
self.valueSpecifier = valueSpecifier!
self.prefix = prefix!
self.postfix = postfix!
}
public var body: some View {
ZStack{
Rectangle()
.fill(self.style.backgroundColor)
.cornerRadius(20)
.shadow(color: self.style.dropShadowColor, radius: self.dropShadow ? 12 : 0)
VStack(alignment: .leading){
HStack{
if(!showValue){
Text(self.title)
.font(.headline)
.foregroundColor(self.style.textColor)
}else{
Text("\(self.currentValue.label) \(self.prefix)\(self.currentValue.value, specifier: self.valueSpecifier)\(self.postfix)")
.font(.headline)
.foregroundColor(self.style.textColor)
}
Spacer()
Image(systemName: "chart.pie.fill")
.imageScale(.large)
.foregroundColor(self.style.legendTextColor)
}.padding()
PieChartRow(data: data, backgroundColor: self.style.backgroundColor, accentColor: self.style.accentColor, showValue: $showValue, currentValue: $currentValue)
.foregroundColor(self.style.accentColor).padding(self.legend != nil ? 0 : 12).offset(y:self.legend != nil ? 0 : -10)
if(self.legend != nil) {
Text(self.legend!)
.font(.headline)
.foregroundColor(self.style.legendTextColor)
.padding()
}
}
}.frame(width: self.formSize.width, height: self.formSize.height)
}
}
#if DEBUG
struct PieChartView_Previews : PreviewProvider {
static var previews: some View {
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