Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 0aebcf4abc | |||
| ef58fd9560 | |||
| 1ab98cba42 |
@@ -1,3 +1,42 @@
|
||||
# SwiftUICharts
|
||||
|
||||
A description of this package.
|
||||
Swift package for displaying charts effortlessly.
|
||||
|
||||

|
||||
|
||||
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))
|
||||
```
|
||||
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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)
|
||||
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 170 KiB |
Reference in New Issue
Block a user