Skip to content

Commit 3846879

Browse files
committed
adding in some convenience functions for scale and invert to assume bottom of the range is 0
1 parent 8458336 commit 3846879

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

Sources/SwiftViz/Scale.swift

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public protocol Scale {
6161
/// - Returns: `true` if the value is between the lower and upper domain values.
6262
func domainContains(_ value: InputType) -> Bool
6363

64-
/// Converts a value between the input _domain_ and output _range_.
64+
/// Converts a value comparing it to the input domain, transforming the value, and mapping it between the range values you provide.
6565
///
6666
/// Before scaling the value, the scale may transform or drop the value based on the setting of ``Scale/transformType``.
6767
///
@@ -121,6 +121,32 @@ public extension Scale {
121121
func domainContains(_ value: InputType) -> Bool {
122122
value >= domainLower && value <= domainHigher
123123
}
124+
125+
/// Converts a value comparing it to the input domain, transforming the value, and mapping it into values between `0` and to the upper bound you provide.
126+
///
127+
/// This method is a convenience method that sets the lower value of the range is `0`.
128+
/// Before scaling the value, the scale may transform or drop the value based on the setting of ``Scale/transformType``.
129+
///
130+
/// - Parameter inputValue: The value to be scaled.
131+
/// - Parameter to: The higher bounding value of the range to transform from.
132+
/// - Returns: a value within the bounds of the range values you provide, or `nil` if the value was dropped.
133+
func scale(_ domainValue: InputType, to upper: OutputType) -> OutputType? {
134+
self.scale(domainValue, from: 0, to: upper)
135+
}
136+
137+
/// Converts a value comparing it to the upper value of a range, mapping it to the input domain, and inverting scale's transform.
138+
///
139+
/// This method is a convenience method that sets the lower value of the range is `0`.
140+
/// The inverse of ``Scale/scale(_:to:)``.
141+
/// After converting the data back to the domain range, the scale may transform or drop the value based on the setting of ``Scale/transformType``.
142+
///
143+
/// - Parameter rangeValue: The value to be scaled back from the range values to the domain.
144+
/// - Parameter to: The higher bounding value of the range to transform from.
145+
/// - Returns: a value within the bounds of the range values you provide, or `nil` if the value was dropped.
146+
func invert(_ rangeValue: OutputType, to upper: OutputType) -> InputType? {
147+
self.invert(rangeValue, from: 0, to: upper)
148+
}
149+
124150
}
125151

126152
// NOTE(heckj): OTHER SCALES: make a PowScale (& maybe Sqrt, Log, Ln)

Tests/SwiftVizTests/ScaleFactoryTests.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,22 @@ class ScaleFactoryTests: XCTestCase {
6161
XCTAssertEqual(scale1.domainExtent, scale2.domainExtent)
6262
XCTAssertEqual(scale1.domainLower, low.timeIntervalSince1970)
6363
}
64+
65+
func testScaleConvenienceMethod() throws {
66+
let lin = LinearScale.create(0, 100.0)
67+
guard let result = lin.scale(5.0, to: 10.0) else {
68+
XCTFail()
69+
return
70+
}
71+
XCTAssertEqual(result, 0.5, accuracy: 0.001)
72+
}
73+
74+
func testInvertConvenienceMethod() throws {
75+
let lin = LinearScale.create(0, 100.0)
76+
guard let result = lin.invert(5.0, to: 10.0) else {
77+
XCTFail()
78+
return
79+
}
80+
XCTAssertEqual(result, 50, accuracy: 0.001)
81+
}
6482
}

0 commit comments

Comments
 (0)