Skip to content

Commit

Permalink
Added small, medium and large size formats
Browse files Browse the repository at this point in the history
  • Loading branch information
AppPear committed Jul 19, 2019
1 parent fcc2870 commit 4c48427
Show file tree
Hide file tree
Showing 9 changed files with 106 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,18 @@
<integer>0</integer>
</dict>
</dict>
<key>SuppressBuildableAutocreation</key>
<dict>
<key>SwiftUICharts</key>
<dict>
<key>primary</key>
<true/>
</dict>
<key>SwiftUIChartsTests</key>
<dict>
<key>primary</key>
<true/>
</dict>
</dict>
</dict>
</plist>
17 changes: 16 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
Swift package for displaying charts effortlessly.

![SwiftUI Charts](./chartview.gif "SwiftUI Charts")
![Chart forms](./chartforms.png "Chart forms")

It supports currently:
* barcharts
Expand Down Expand Up @@ -34,9 +35,23 @@ You can optionally configure:
* legend
* background color
* accent color
* size format

### Size format (only for bar charts yet!)
Can be
* small
* medium
* large

```swift
ChartView(data: [12,17,24,33,23,56], title: "Chart two", form: Form.small)
```

### Customizing color:
I added color constants, so you can predefine your color palette. To do so, you can find `Colors` struct in the ChartColors swift file.

```swift
ChartView(data: [12,17,24,33,36,31,27,23,14], title: "Title", legend: "Legend", backgroundColor:Color(red: 226.0/255.0, green: 250.0/255.0, blue: 231.0/255.0) , accentColor:Color(red: 114.0/255.0, green: 191.0/255.0, blue: 130.0/255.0))
ChartView(data: [12,17,24,33,23,56], title: "Chart two", backgroundColor:Colors.color3 , accentColor:Colors.color3Accent)
```


1 change: 0 additions & 1 deletion Sources/SwiftUICharts/ChartCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ public struct ChartCell : View {
self.scaleValue = self.value
}
.animation(Animation.spring().delay(Double(self.index) * 0.04))

}
}

Expand Down
38 changes: 38 additions & 0 deletions Sources/SwiftUICharts/ChartColors.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//
// File.swift
//
//
// Created by András Samu on 2019. 07. 19..
//

import Foundation
import SwiftUI
public struct Colors {
public static let color1:Color = Color(hexString: "#E2FAE7")
public static let color1Accent:Color = Color(hexString: "#72BF82")
public static let color2:Color = Color(hexString: "#EEF1FF")
public static let color2Accent:Color = Color(hexString: "#4266E8")
public static let color3:Color = Color(hexString: "#FCECEA")
public static let color3Accent:Color = Color(hexString: "#E1614C")
}


extension Color {
init(hexString: String) {
let hex = hexString.trimmingCharacters(in: CharacterSet.alphanumerics.inverted)
var int = UInt32()
Scanner(string: hex).scanHexInt32(&int)
let r, g, b: UInt32
switch hex.count {
case 3: // RGB (12-bit)
(r, g, b) = ((int >> 8) * 17, (int >> 4 & 0xF) * 17, (int & 0xF) * 17)
case 6: // RGB (24-bit)
(r, g, b) = (int >> 16, int >> 8 & 0xFF, int & 0xFF)
case 8: // ARGB (32-bit)
(r, g, b) = (int >> 16 & 0xFF, int >> 8 & 0xFF, int & 0xFF)
default:
(r, g, b) = (0, 0, 0)
}
self.init(red: Double(r) / 255, green: Double(g) / 255, blue: Double(b) / 255)
}
}
3 changes: 2 additions & 1 deletion Sources/SwiftUICharts/ChartRow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ public struct ChartRow : View {
ForEach(0..<self.data.count) { i in
ChartCell(value: Double(self.data[i])/Double(self.maxValue), index: i, width: Float(geometry.frame(in: .local).width - 22), numberOfDataPoints: self.data.count)
}
}.padding([.trailing,.leading], 13)
}
.padding([.trailing,.leading], 13)
}
}
}
Expand Down
42 changes: 29 additions & 13 deletions Sources/SwiftUICharts/ChartView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,53 +8,69 @@

import SwiftUI

public struct Form {
public static let small = CGSize(width:180, height:120)
public static let medium = CGSize(width:180, height:240)
public static let large = CGSize(width:360, height:120)
public static let detail = CGSize(width:180, height:120)
}

public struct ChartView : View {
public var data: [Int]
public var title: String
public var legend: String?
public var backgroundColor:Color
public var accentColor:Color

public init(data: [Int], title: String, legend: String? = nil,backgroundColor:Color = Color(red: 238.0/255.0, green: 241.0/255.0, blue: 254.0/255.0),accentColor:Color = Color(red: 66.0/255.0, green: 102.0/255.0, blue: 232.0/255.0) ){
public var formSize:CGSize
var isFullWidth:Bool {
return self.formSize == Form.large
}
public init(data: [Int], title: String, legend: String? = nil,backgroundColor:Color = Colors.color1,accentColor:Color = Colors.color1Accent, form: CGSize = Form.medium ){
self.data = data
self.title = title
self.legend = legend
self.backgroundColor = backgroundColor
self.accentColor = accentColor
self.formSize = form
}

public var body: some View {
ZStack{
Rectangle()
.fill(self.backgroundColor)
.cornerRadius(20)
.fill(self.backgroundColor)
.cornerRadius(20)
VStack(alignment: .leading){
HStack{
Text(self.title)
.font(.headline)
.font(.headline)
if(self.formSize == Form.large && self.legend != nil) {
Text(self.legend!)
.font(.callout)
.foregroundColor(self.accentColor)
}
Spacer()
Image(systemName: "waveform.path.ecg")
.imageScale(.large)
.foregroundColor(self.accentColor)
}.padding()
.imageScale(.large)
.foregroundColor(self.accentColor)
}.padding()
ChartRow(data: data)
.foregroundColor(self.accentColor)
.clipped()
if self.legend != nil {
.foregroundColor(self.accentColor)
.clipped()
if self.legend != nil && self.formSize == Form.medium {
Text(self.legend!)
.font(.headline)
.foregroundColor(self.accentColor)
.padding()
}
}
}.frame(width: 180, height: 240)
}.frame(minWidth: self.formSize.width, maxWidth: self.isFullWidth ? .infinity : self.formSize.width, minHeight: self.formSize.height, maxHeight: self.formSize.height)
}
}

#if DEBUG
struct ChartView_Previews : PreviewProvider {
static var previews: some View {
ChartView(data: [8,23,54,32,12,37,7,23,43], title: "Title")
ChartView(data: [8,23,54,32,12,37,7,23,43], title: "Title", legend: "Legendary")
}
}
#endif
14 changes: 7 additions & 7 deletions Sources/SwiftUICharts/PieChartCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ public struct PieChartCell : View {
var accentColor:Color
public var body: some View {
path
.fill()
.foregroundColor(self.accentColor)
.overlay(path.stroke(self.backgroundColor, lineWidth: 2))
.scaleEffect(self.show ? 1 : 0)
.animation(Animation.spring().delay(Double(self.index) * 0.04))
.onAppear(){
.fill()
.foregroundColor(self.accentColor)
.overlay(path.stroke(self.backgroundColor, lineWidth: 2))
.scaleEffect(self.show ? 1 : 0)
.animation(Animation.spring().delay(Double(self.index) * 0.04))
.onAppear(){
self.show = true
}
}
Expand All @@ -58,7 +58,7 @@ struct PieChartCell_Previews : PreviewProvider {
static var previews: some View {
GeometryReader { geometry in
PieChartCell(rect: geometry.frame(in: .local),startDeg: 0.0,endDeg: 90.0, index: 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))
}.frame(width:100, height:100)
}.frame(width:100, height:100)

}
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/SwiftUICharts/PieChartView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public struct PieChartView : View {
public var backgroundColor:Color
public var accentColor:Color

public init(data: [Int], title: String, legend: String? = nil, backgroundColor:Color = Color(red: 252.0/255.0, green: 236.0/255.0, blue: 234.0/255.0),accentColor:Color = Color(red: 225.0/255.0, green: 97.0/255.0, blue: 76.0/255.0)){
public init(data: [Int], title: String, legend: String? = nil, backgroundColor:Color = Colors.color3,accentColor:Color = Colors.color3Accent){
self.data = data
self.title = title
self.legend = legend
Expand Down
Binary file added chartforms.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 4c48427

Please sign in to comment.