Compare commits

..

2 Commits

Author SHA1 Message Date
Andras Samu 4c48427383 Added small, medium and large size formats 2019-07-19 09:53:09 +02:00
Andras Samu fcc2870b14 created license 2019-07-18 15:23:31 +02:00
10 changed files with 127 additions and 24 deletions
@@ -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>
+21
View File
@@ -0,0 +1,21 @@
MIT License
Copyright (c) 2019 Andras Samu
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
+16 -1
View File
@@ -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
@@ -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,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", 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,23,56], title: "Chart two", backgroundColor:Colors.color3 , accentColor:Colors.color3Accent)
```
-1
View File
@@ -28,7 +28,6 @@ public struct ChartCell : View {
self.scaleValue = self.value
}
.animation(Animation.spring().delay(Double(self.index) * 0.04))
}
}
+38
View File
@@ -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)
}
}
+2 -1
View File
@@ -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)
}
}
}
+29 -13
View File
@@ -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
+7 -7
View File
@@ -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
}
}
@@ -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)
}
}
+1 -1
View File
@@ -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
BIN
View File
Binary file not shown.

After

Width:  |  Height:  |  Size: 242 KiB