|
1 | 1 | import SwiftUI
|
2 | 2 |
|
3 |
| - |
4 | 3 | @available(iOS 13.0, *)
|
5 | 4 | /// Creates a horizontal chart that calculates percentile slices
|
6 |
| -public struct SwiftUIPercentChart : View { |
7 |
| - private var data: Array<Double> |
8 |
| - private var percentValue : Double |
9 |
| - private var applyPercent : Double{ |
10 |
| - if self.data.reduce(0, +) > percentValue{ |
11 |
| - return self.data.reduce(0, +) |
12 |
| - }else{ |
13 |
| - return self.percentValue |
14 |
| - } |
15 |
| - } |
16 |
| - private var colorData : Array<LinearGradient> |
17 |
| - private var applyColor : Array<LinearGradient>{ |
18 |
| - if self.colorData.isEmpty{ |
19 |
| - return ColorHelper().defaultColors |
20 |
| - }else{ |
21 |
| - return colorData |
22 |
| - } |
23 |
| - } |
24 |
| - @State var valueScale = CGFloat(1) |
25 |
| - private var screenSize = UIScreen.main.bounds |
26 |
| - private var screenRatio : Double |
27 |
| - private var backgroundColor : Color |
28 |
| - // Required variables |
29 |
| - public init(data:Array<Double> , percentValue : Double , colorData : Array<LinearGradient> , screenRatio : Double, backgroundColor : Color){ |
30 |
| - // Data of the graphics |
| 5 | +public struct SwiftUIPercentChart: View { |
| 6 | + private var data: [Double] |
| 7 | + private var percentValue: Double |
| 8 | + private var theme: Themes |
| 9 | + |
| 10 | + init(data: [Double] = [], percentValue: Double? = nil, theme: Themes = .dark) { |
31 | 11 | self.data = data
|
32 |
| - // The value that determines the percentage of the bar |
33 |
| - self.percentValue = percentValue |
34 |
| - // The value set the chart colors |
35 |
| - self.colorData = colorData |
36 |
| - // The value for setting the width of the chart according to the device screen (among to 0 - 1) |
37 |
| - self.screenRatio = screenRatio |
38 |
| - // The value set the backgorund bar color |
39 |
| - self.backgroundColor = backgroundColor |
| 12 | + self.percentValue = percentValue ?? 0 < Double(data.reduce(0, +)) ? Double(data.reduce(0, +)) : percentValue ?? Double(data.reduce(0, +)) |
| 13 | + self.theme = theme |
40 | 14 | }
|
41 |
| - public var body: some View{ |
42 |
| - ZStack(alignment:.leading){ |
43 |
| - Rectangle() |
44 |
| - .foregroundColor(self.backgroundColor.opacity(0.3)) |
45 |
| - .frame(width: screenSize.width * self.screenRatio, height: 20) |
46 |
| - .cornerRadius(16) |
47 |
| - HStack(spacing:0){ |
48 |
| - if #available(iOS 15.0, *){ |
49 |
| - if self.data.count > 0 { |
50 |
| - if self.data.count == 1{ |
| 15 | + |
| 16 | + public var body: some View { |
| 17 | + GeometryReader { proxy in |
| 18 | + ZStack { |
| 19 | + RoundedRectangle(cornerRadius: 16) |
| 20 | + .foregroundColor(.gray.opacity(0.2)) |
| 21 | + HStack(spacing: 0) { |
| 22 | + ForEach(Array(zip(data.indices, data)), id: \.0) { index, value in |
| 23 | + if index == 0 { |
| 24 | + Rectangle() |
| 25 | + .cornerRadius(cellRadius(by: index), corners: [.topLeft, .bottomLeft]) |
| 26 | + .foregroundColor(.themeColor(by: index, with: theme)) |
| 27 | + .frame(width: cellWidth(by: index, proxy.size.width)) |
| 28 | + } else if index == data.count - 1 { |
51 | 29 | Rectangle()
|
52 |
| - .frame(width: calWidth(value: self.data[0]) , height: 20) |
53 |
| - .cornerRadius(16) |
54 |
| - .foregroundStyle(self.applyColor[0]) |
55 |
| - |
56 |
| - }else{ |
| 30 | + .cornerRadius(cellRadius(by: index), corners: [.topRight, .bottomRight]) |
| 31 | + .foregroundColor(.themeColor(by: index, with: theme)) |
| 32 | + .frame(width: cellWidth(by: index, proxy.size.width)) |
| 33 | + } else { |
57 | 34 | Rectangle()
|
58 |
| - .frame(width: calWidth(value: self.data[0]), height: 20) |
59 |
| - .cornerRadius(16,corners:[.topLeft,.bottomLeft]) |
60 |
| - .foregroundStyle(self.applyColor[0]) |
61 |
| - ForEach(0..<self.data.count) { |
62 |
| - a in |
63 |
| - if a == 0{ |
64 |
| - |
65 |
| - } |
66 |
| - else if a == self.data.count - 1 { |
67 |
| - Rectangle() |
68 |
| - .frame(width: calWidth(value: self.data[a]), height: 20) |
69 |
| - .cornerRadius(16,corners:[.topRight,.bottomRight]) |
70 |
| - .foregroundStyle(self.applyColor[a % self.applyColor.count]) |
71 |
| - } |
72 |
| - else{ |
73 |
| - Rectangle() |
74 |
| - .frame(width: calWidth(value: self.data[a]), height: 20) |
75 |
| - .foregroundStyle(self.applyColor[a % self.applyColor.count]) |
76 |
| - |
77 |
| - } |
78 |
| - } |
| 35 | + .foregroundColor(.themeColor(by: index, with: theme)) |
| 36 | + .frame(width: cellWidth(by: index, proxy.size.width)) |
79 | 37 | }
|
80 | 38 | }
|
81 |
| - }else |
82 |
| - { |
83 |
| - Text("Make the app version ios 15") |
| 39 | + |
| 40 | + if percentValue > data.reduce(0, +) { |
| 41 | + Spacer() |
| 42 | + } |
84 | 43 | }
|
85 |
| - }.scaleEffect(self.valueScale) |
86 |
| - .onTouchGesture( |
87 |
| - touchBegan: { withAnimation { self.valueScale = 1.2 } }, |
88 |
| - touchEnd: { _ in withAnimation { self.valueScale = 1.0 } } |
89 |
| - ) |
| 44 | + } |
90 | 45 | }
|
91 | 46 | }
|
92 |
| - func calWidth(value : Double) -> Double{ |
93 |
| - return (value * (screenSize.width * self.screenRatio))/applyPercent |
| 47 | + |
| 48 | + private func cellCapacity(by index: Int) -> Double { |
| 49 | + return (data[index] * 100) / percentValue |
| 50 | + } |
| 51 | + |
| 52 | + private func cellWidth(by index: Int, _ width: CGFloat) -> Double { |
| 53 | + return (cellCapacity(by: index) * width) / 100 |
| 54 | + } |
| 55 | + |
| 56 | + private func cellRadius(by index: Int) -> Double { |
| 57 | + return 500 / cellCapacity(by: index) |
94 | 58 | }
|
95 | 59 | }
|
96 | 60 |
|
97 | 61 | #if DEBUG
|
98 | 62 | struct SwiftUIPercentChart_Previews : PreviewProvider {
|
99 | 63 | @available(iOS 13.0, *)
|
100 | 64 | static var previews: some View {
|
101 |
| - SwiftUIPercentChart(data: [10,20,30], percentValue: 100 , colorData: [] ,screenRatio: 0.8,backgroundColor: .primary) |
| 65 | + let screenSize = UIScreen.main.bounds |
| 66 | + |
| 67 | + VStack { |
| 68 | + ForEach(Themes.allCases, id: \.self) { theme in |
| 69 | + if #available(iOS 15.0, *) { |
| 70 | + VStack(alignment: .leading) { |
| 71 | + SwiftUIPercentChart(data: [50, 40, 30, 20, 30, 50, 30, 10, 20, 50], percentValue: 350, theme: theme) |
| 72 | + .frame(width: screenSize.width * 0.7, height: 10) |
| 73 | + Text(theme.rawValue) |
| 74 | + .bold() |
| 75 | + }.padding() |
| 76 | + .background(.ultraThinMaterial) |
| 77 | + .cornerRadius(10) |
| 78 | + } else { |
| 79 | + VStack(alignment: .leading) { |
| 80 | + SwiftUIPercentChart(data: [50, 40, 30, 20, 30, 50, 30, 10, 20, 50], percentValue: 350, theme: theme) |
| 81 | + .frame(width: screenSize.width * 0.7, height: 10) |
| 82 | + Text(theme.rawValue) |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + } |
102 | 87 | }
|
103 | 88 | }
|
104 | 89 | #endif
|
0 commit comments