-
Notifications
You must be signed in to change notification settings - Fork 652
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: new protocol for chained functions, and added support for expli…
…cit Y ranges. X coming as well
- Loading branch information
Showing
33 changed files
with
538 additions
and
351 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import SwiftUI | ||
|
||
public struct AxisLabels<Content: View>: View { | ||
var axisLabelsData = AxisLabelsData() | ||
let content: () -> Content | ||
// font | ||
// foregroundColor | ||
|
||
public init(@ViewBuilder content: @escaping () -> Content) { | ||
self.content = content | ||
} | ||
|
||
public var body: some View { | ||
HStack { | ||
VStack { | ||
ForEach(Array(axisLabelsData.axisYLabels.reversed().enumerated()), id: \.element) { index, axisYData in | ||
Text(axisYData) | ||
if index != axisLabelsData.axisYLabels.count - 1 { | ||
Spacer() | ||
} | ||
} | ||
} | ||
.padding([.trailing], 8.0) | ||
.padding([.bottom], axisLabelsData.axisXLabels.count > 0 ? 28.0 : 0) | ||
.frame(maxHeight: .infinity) | ||
VStack { | ||
self.content() | ||
HStack { | ||
ForEach(Array(axisLabelsData.axisXLabels.enumerated()), id: \.element) { index, axisXData in | ||
Text(axisXData) | ||
if index != axisLabelsData.axisXLabels.count - 1 { | ||
Spacer() | ||
} | ||
} | ||
} | ||
} | ||
.padding([.top, .bottom], 10.0) | ||
} | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
Sources/SwiftUICharts/Base/Axis/Extension/AxisLabels+Extension.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import SwiftUI | ||
|
||
extension AxisLabels { | ||
public func setAxisYLabels(_ labels: [String]) -> AxisLabels { | ||
self.axisLabelsData.axisYLabels = labels | ||
return self | ||
} | ||
|
||
public func setAxisXLabels(_ labels: [String]) -> AxisLabels { | ||
self.axisLabelsData.axisXLabels = labels | ||
return self | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
Sources/SwiftUICharts/Base/Axis/Model/AxisLablesData.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import SwiftUI | ||
|
||
public final class AxisLabelsData: ObservableObject { | ||
@Published public var axisYLabels: [String] = [] | ||
@Published public var axisXLabels: [String] = [] | ||
|
||
public init() { | ||
// no-op | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
import SwiftUI | ||
|
||
/// Protocol for any type of chart, to get access to underlying data | ||
public protocol ChartBase { | ||
public protocol ChartBase: View { | ||
var chartData: ChartData { get } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 14 additions & 12 deletions
26
Sources/SwiftUICharts/Base/Extensions/ChartBase+Extension.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,23 @@ | ||
import SwiftUI | ||
|
||
extension View where Self: ChartBase { | ||
|
||
/// Set data for a chart | ||
/// - Parameter data: array of `Double` | ||
/// - Returns: modified `View` with data attached | ||
public func data(_ data: [Double]) -> some View { | ||
chartData.data = data.map { ("", $0) } | ||
extension ChartBase { | ||
public func data(_ data: [Double]) -> some ChartBase { | ||
chartData.data = data.enumerated().map{ (index, value) in (Double(index), value) } | ||
return self | ||
.environmentObject(chartData) | ||
.environmentObject(ChartValue()) | ||
} | ||
|
||
public func data(_ data: [(String, Double)]) -> some View { | ||
public func data(_ data: [(Double, Double)]) -> some ChartBase { | ||
chartData.data = data | ||
return self | ||
.environmentObject(chartData) | ||
.environmentObject(ChartValue()) | ||
} | ||
|
||
public func rangeY(_ range: ClosedRange<FloatLiteralType>) -> some ChartBase{ | ||
chartData.rangeY = range | ||
return self | ||
} | ||
|
||
public func rangeX(_ range: ClosedRange<FloatLiteralType>) -> some ChartBase{ | ||
chartData.rangeX = range | ||
return self | ||
} | ||
} |
Oops, something went wrong.