Compare commits

...

3 Commits

Author SHA1 Message Date
Andras Samu 0aebcf4abc Added dynamic scaling to bar-chart 2019-06-26 21:06:08 +02:00
Andras Samu ef58fd9560 screenshot 2019-06-14 15:27:22 +02:00
Andras Samu 1ab98cba42 Updated readme with instructions 2019-06-14 15:19:55 +02:00
5 changed files with 60 additions and 10 deletions
+40 -1
View File
@@ -1,3 +1,42 @@
# SwiftUICharts
A description of this package.
Swift package for displaying charts effortlessly.
![SwiftUI Charts](./chartview.gif "SwiftUI Charts")
It supports currently:
* barcharts
* piecharts
### Installation:
It requires iOS 13 and xCode 11!
In xCode got to `File -> Swift Packages -> Add Package Dependency` and paste inthe repo's url: `https://github.com/AppPear/ChartView`
### Usage:
import the package in the file you would like to use it: `import SwiftUICharts`
You can display a Chart by adding a chart view to your parent view:
Barchart:
```swift
ChartView(data: [8,23,54,32,12,37,7,23,43], title: "Barchart")
```
Piechart:
```swift
PieChartView(data:[43,56,78,34], title: "Piechart")
```
You can optionally configure:
* legend
* background color
* accent color
```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))
```
+12 -4
View File
@@ -11,23 +11,31 @@ import SwiftUI
public struct ChartCell : View {
var value: Double
var index: Int = 0
var width: Float
var numberOfDataPoints: Int
var cellWidth: Double {
return Double(width)/(Double(numberOfDataPoints) * 1.5)
}
@State var scaleValue: Double = 0
public var body: some View {
Rectangle()
.frame(width: 6)
.cornerRadius(4)
ZStack {
Rectangle()
.cornerRadius(4)
}
.frame(width: CGFloat(self.cellWidth))
.scaleEffect(CGSize(width: 1, height: self.scaleValue), anchor: .bottom)
.onAppear(){
self.scaleValue = self.value
}
.animation(Animation.spring().delay(Double(self.index) * 0.04))
}
}
#if DEBUG
struct ChartCell_Previews : PreviewProvider {
static var previews: some View {
ChartCell(value: Double(0.75))
ChartCell(value: Double(0.75), width: 320, numberOfDataPoints: 12)
}
}
#endif
+7 -5
View File
@@ -14,11 +14,13 @@ public struct ChartRow : View {
data.max() ?? 0
}
public var body: some View {
HStack(alignment: .bottom, spacing: 14){
ForEach(0..<data.count) { i in
ChartCell(value: Double(self.data[i])/Double(self.maxValue), index: i)
}
}.padding([.trailing,.leading])
GeometryReader { geometry in
HStack(alignment: .bottom, spacing: (geometry.frame(in: .local).width-22)/CGFloat(self.data.count * 3)){
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)
}
}
}
+1
View File
@@ -39,6 +39,7 @@ public struct ChartView : View {
}.padding()
ChartRow(data: data)
.foregroundColor(self.accentColor)
.clipped()
if self.legend != nil {
Text(self.legend!)
.font(.headline)
BIN
View File
Binary file not shown.

After

Width:  |  Height:  |  Size: 170 KiB