Skip to content

Commit

Permalink
merging tick and label'd tick concepts
Browse files Browse the repository at this point in the history
  • Loading branch information
heckj committed Mar 11, 2022
1 parent ca22e89 commit 48e78ce
Show file tree
Hide file tree
Showing 134 changed files with 11,141 additions and 41,536 deletions.
6 changes: 3 additions & 3 deletions Sources/SwiftViz/Axis.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ public enum ChartOrientation {

/// The identifier for a dimension the chart visualizes.
public enum AxisDimension {
/// The `x` axis, typically - but uniformly - horizontal.
/// The `x` axis, often displayed horizontally.
case x
/// The `y` axis, typically - but uniformly - vertical.
/// The `y` axis, often displayed vertically.
case y
/// The `z` axis, typically - but uniformly - depth.
/// The `z` axis, often displayed foreground to background.
case z
}

Expand Down
1 change: 0 additions & 1 deletion Sources/SwiftViz/Documentation.docc/Documentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,3 @@ Loosely based on the APIs and the visualization constructs created by Mike Bosto
- ``SwiftViz/AxisDimension``
- ``SwiftViz/ChartOrientation``
- ``Tick``
- ``TickLabel``
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,5 @@
### Generating Ticks

- ``SwiftViz/LinearScale/DoubleScale/ticks(rangeLower:rangeHigher:)``
- ``SwiftViz/LinearScale/DoubleScale/ticks(_:from:to:)``
- ``SwiftViz/LinearScale/DoubleScale/labeledTickValues(_:from:to:)``
- ``SwiftViz/LinearScale/DoubleScale/validatedTickLabels(_:from:to:)``
- ``SwiftViz/LinearScale/DoubleScale/tickValues(_:from:to:)``

Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,5 @@
### Generating Ticks

- ``SwiftViz/LinearScale/FloatScale/ticks(rangeLower:rangeHigher:)``
- ``SwiftViz/LinearScale/FloatScale/ticks(_:from:to:)``
- ``SwiftViz/LinearScale/FloatScale/labeledTickValues(_:from:to:)``
- ``SwiftViz/LinearScale/FloatScale/validatedTickLabels(_:from:to:)``
- ``SwiftViz/LinearScale/FloatScale/tickValues(_:from:to:)``

4 changes: 1 addition & 3 deletions Sources/SwiftViz/Documentation.docc/LinearScale_IntScale.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,5 @@
### Generating Ticks

- ``SwiftViz/LinearScale/IntScale/ticks(rangeLower:rangeHigher:)``
- ``SwiftViz/LinearScale/IntScale/ticks(_:from:to:)``
- ``SwiftViz/LinearScale/IntScale/labeledTickValues(_:from:to:)``
- ``SwiftViz/LinearScale/IntScale/validatedTickLabels(_:from:to:)``
- ``SwiftViz/LinearScale/IntScale/tickValues(_:from:to:)``

4 changes: 1 addition & 3 deletions Sources/SwiftViz/Documentation.docc/LogScale_DoubleScale.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,5 @@
### Generating Ticks

- ``SwiftViz/LogScale/DoubleScale/ticks(rangeLower:rangeHigher:)``
- ``SwiftViz/LogScale/DoubleScale/ticks(_:from:to:)``
- ``SwiftViz/LogScale/DoubleScale/labeledTickValues(_:from:to:)``
- ``SwiftViz/LogScale/DoubleScale/validatedTickLabels(_:from:to:)``
- ``SwiftViz/LogScale/DoubleScale/tickValues(_:from:to:)``

4 changes: 1 addition & 3 deletions Sources/SwiftViz/Documentation.docc/LogScale_FloatScale.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,5 @@
### Generating Ticks

- ``SwiftViz/LogScale/FloatScale/ticks(rangeLower:rangeHigher:)``
- ``SwiftViz/LogScale/FloatScale/ticks(_:from:to:)``
- ``SwiftViz/LogScale/FloatScale/labeledTickValues(_:from:to:)``
- ``SwiftViz/LogScale/FloatScale/validatedTickLabels(_:from:to:)``
- ``SwiftViz/LogScale/FloatScale/tickValues(_:from:to:)``

4 changes: 1 addition & 3 deletions Sources/SwiftViz/Documentation.docc/LogScale_IntScale.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,5 @@
### Generating Ticks

- ``SwiftViz/LogScale/IntScale/ticks(rangeLower:rangeHigher:)``
- ``SwiftViz/LogScale/IntScale/ticks(_:from:to:)``
- ``SwiftViz/LogScale/IntScale/labeledTickValues(_:from:to:)``
- ``SwiftViz/LogScale/IntScale/validatedTickLabels(_:from:to:)``
- ``SwiftViz/LogScale/IntScale/tickValues(_:from:to:)``

Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,5 @@
### Generating Ticks

- ``SwiftViz/PowerScale/DoubleScale/ticks(rangeLower:rangeHigher:)``
- ``SwiftViz/PowerScale/DoubleScale/ticks(_:from:to:)``
- ``SwiftViz/PowerScale/DoubleScale/labeledTickValues(_:from:to:)``
- ``SwiftViz/PowerScale/DoubleScale/validatedTickLabels(_:from:to:)``
- ``SwiftViz/PowerScale/DoubleScale/tickValues(_:from:to:)``

4 changes: 2 additions & 2 deletions Sources/SwiftViz/Documentation.docc/Tick.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

### Creating Ticks

- ``SwiftViz/Tick/init(value:location:)-8f3kt``
- ``SwiftViz/Tick/init(value:location:)-99fw1``
- ``SwiftViz/Tick/init(value:location:formatter:)-9r04p``
- ``SwiftViz/Tick/init(value:location:formatter:)-ceri``

### Inspecting Ticks

Expand Down
6 changes: 1 addition & 5 deletions Sources/SwiftViz/Documentation.docc/TickScale.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,8 @@

### Generating Ticks for a Scale

- ``SwiftViz/TickScale/ticks(_:from:to:)``
- ``SwiftViz/TickScale/tickValues(_:from:to:)``
- ``SwiftViz/TickScale/ticks(rangeLower:rangeHigher:)-1vugh``
- ``SwiftViz/TickScale/ticks(rangeLower:rangeHigher:)-4uxek``
- ``SwiftViz/TickScale/ticks(rangeLower:rangeHigher:)-5n5j1``

### Generating and Validating TickLabels

- ``SwiftViz/TickScale/validatedTickLabels(_:from:to:)``
- ``SwiftViz/TickScale/labeledTickValues(_:from:to:)``
6 changes: 3 additions & 3 deletions Sources/SwiftViz/LinearScale.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public enum LinearScale {

/// The number of ticks desired when creating the scale.
///
/// This number may not match the number of ticks returned by ``TickScale/ticks(_:from:to:)``
/// This number may not match the number of ticks returned by ``TickScale/tickValues(_:from:to:)``
public let desiredTicks: Int

/// Creates a new linear scale for the upper and lower bounds of the domain you provide.
Expand Down Expand Up @@ -97,7 +97,7 @@ public enum LinearScale {

/// The number of ticks desired when creating the scale.
///
/// This number may not match the number of ticks returned by ``TickScale/ticks(_:from:to:)``
/// This number may not match the number of ticks returned by ``TickScale/tickValues(_:from:to:)``
public let desiredTicks: Int

/// Creates a new linear scale for the upper and lower bounds of the domain you provide.
Expand Down Expand Up @@ -168,7 +168,7 @@ public enum LinearScale {

/// The number of ticks desired when creating the scale.
///
/// This number may not match the number of ticks returned by ``TickScale/ticks(_:from:to:)``
/// This number may not match the number of ticks returned by ``TickScale/tickValues(_:from:to:)``
public let desiredTicks: Int

/// Creates a new linear scale for the upper and lower bounds of the domain you provide.
Expand Down
6 changes: 3 additions & 3 deletions Sources/SwiftViz/LogScale.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public enum LogScale {

/// The number of ticks desired when creating the scale.
///
/// This number may not match the number of ticks returned by ``TickScale/ticks(_:from:to:)``
/// This number may not match the number of ticks returned by ``TickScale/tickValues(_:from:to:)``
public let desiredTicks: Int

/// Creates a new logarithmic scale for the upper and lower bounds of the domain you provide.
Expand Down Expand Up @@ -104,7 +104,7 @@ public enum LogScale {

/// The number of ticks desired when creating the scale.
///
/// This number may not match the number of ticks returned by ``TickScale/ticks(_:from:to:)``
/// This number may not match the number of ticks returned by ``TickScale/tickValues(_:from:to:)``
public let desiredTicks: Int

/// Creates a new logarithmic scale for the upper and lower bounds of the domain you provide.
Expand Down Expand Up @@ -178,7 +178,7 @@ public enum LogScale {

/// The number of ticks desired when creating the scale.
///
/// This number may not match the number of ticks returned by ``TickScale/ticks(_:from:to:)``
/// This number may not match the number of ticks returned by ``TickScale/tickValues(_:from:to:)``
public let desiredTicks: Int

/// Creates a new logarithmic scale for the upper and lower bounds of the domain you provide.
Expand Down
15 changes: 13 additions & 2 deletions Sources/SwiftViz/Tick.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,23 @@ public struct Tick<InputType, OutputType: Numeric>: Identifiable where OutputTyp
/// The location where the tick should be placed within a chart's range.
public let rangeLocation: OutputType

public let formatter: Formatter?

public var label: String {
guard let formatter = formatter else {
return String("\(value)")
}
return formatter.string(for: value) ?? ""
}

/// Creates a new tick
/// - Parameters:
/// - value: The value at the tick's location.
/// - location: The location of the tick within the range for a scale.
public init(value: InputType, location: OutputType) {
public init(value: InputType, location: OutputType, formatter: Formatter? = nil) {
self.value = value
rangeLocation = location
self.formatter = formatter
}

/// Creates a new tick.
Expand All @@ -39,8 +49,9 @@ public struct Tick<InputType, OutputType: Numeric>: Identifiable where OutputTyp
/// - Parameters:
/// - value: The value at the tick's location.
/// - location: The location of the tick within the range for a scale.
public init?(value: InputType, location: OutputType) where OutputType: Real {
public init?(value: InputType, location: OutputType, formatter: Formatter? = nil) where OutputType: Real {
self.value = value
self.formatter = formatter
if location.isNaN {
return nil
} else {
Expand Down
52 changes: 0 additions & 52 deletions Sources/SwiftViz/TickLabel.swift

This file was deleted.

56 changes: 2 additions & 54 deletions Sources/SwiftViz/TickScale.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import Numerics
public protocol TickScale: Scale {
/// The number of ticks desired when creating the scale.
///
/// This number may not match the number of ticks returned by ``TickScale/ticks(_:from:to:)``
/// This number may not match the number of ticks returned by ``TickScale/tickValues(_:from:to:)``
var desiredTicks: Int { get }
}

Expand All @@ -27,7 +27,7 @@ public extension TickScale where OutputType: Real {
/// - Parameter lower: The lower value of the range the scale maps to.
/// - Parameter higher: The higher value of the range the scale maps to.
/// - Returns: A list of tick values validated against the domain, and range based on the setting of ``Scale/transformType``
func ticks(_ inputValues: [InputType], from lower: OutputType, to higher: OutputType) -> [Tick<InputType, OutputType>] {
func tickValues(_ inputValues: [InputType], from lower: OutputType, to higher: OutputType) -> [Tick<InputType, OutputType>] {
inputValues.compactMap { inputValue in
if domainContains(inputValue),
let rangeValue = scale(inputValue, from: lower, to: higher)
Expand All @@ -52,58 +52,6 @@ public extension TickScale where OutputType: Real {
return nil
}
}

/// Converts an array of values with matching strings, that are within the scale's domain and returns a list of tick labels using the strings you provide.
///
/// Takes an set of labelled input values and returns the relevant set of TickLabels converted
/// to the correct location values associated with the provided range.
/// - Parameters:
/// - inputValues: A tuple of (InputValue, String) that is the labelled value
/// - range: a ClosedRange representing the representing
/// the range we are mapping the values into with the scale
func labeledTickValues(_ inputValues: [(InputType, String)], from lower: OutputType, to higher: OutputType) -> [TickLabel<OutputType>] {
inputValues.compactMap { inputTuple in
let (inputValue, stringValue) = inputTuple
if domainContains(inputValue) {
if let location = scale(inputValue, from: lower, to: higher) {
switch transformType {
case .none:
return TickLabel(rangeLocation: location, value: stringValue)
case .drop:
if location > higher || location < lower {
return nil
}
return TickLabel(rangeLocation: location, value: stringValue)
case .clamp:
if location > higher {
return TickLabel(rangeLocation: higher, value: stringValue)
} else if location < lower {
return TickLabel(rangeLocation: lower, value: stringValue)
}
return TickLabel(rangeLocation: location, value: stringValue)
}
}
}
return nil
}
}

/// Validates a set of TickLabels against a given scale, removing any that don't match the scale's domain.
///
/// - Parameter inputTickLabels: an array of TickLabels to validate against the scale
/// - Parameter range: a ClosedRange representing the representing
/// the range we are mapping the values into with the scale.
func validatedTickLabels(_ inputTickLabels: [TickLabel<OutputType>], from lower: OutputType, to higher: OutputType) -> [TickLabel<OutputType>] {
inputTickLabels.compactMap { tickLabel in
guard let inputValue = invert(tickLabel.rangeLocation, from: lower, to: higher) else {
return nil
}
if domainContains(inputValue) {
return tickLabel
}
return nil
}
}
}

public extension TickScale where InputType == Int, OutputType: Real {
Expand Down
34 changes: 4 additions & 30 deletions Tests/SwiftVizTests/ExternalPackageTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,54 +13,28 @@ final class PackagingTests: XCTestCase {
func testManualTicks() {
let scale = LinearScale.DoubleScale(from: 0.0, to: 10.0, transform: .none)
// verifies the method is visible externally - else this won't compile
let ticks = scale.ticks([2.0], from: 0.0, to: 10.0)
let ticks = scale.tickValues([2.0], from: 0.0, to: 10.0)
XCTAssertEqual(ticks.count, 1)
}

func testManualTicksOutsideRangeNone() {
let scale = LinearScale.DoubleScale(from: 0.0, to: 10.0, transform: .none)
let ticks = scale.ticks([2.0, 4.0, 8.0, 16.0], from: 0.0, to: 10.0)
let ticks = scale.tickValues([2.0, 4.0, 8.0, 16.0], from: 0.0, to: 10.0)
XCTAssertEqual(ticks.count, 3)
}

func testManualTicksOutsideRangeClamped() {
let scale = LinearScale.DoubleScale(from: 0.0, to: 10.0, transform: .clamp)
let ticks = scale.ticks([2.0, 4.0, 8.0, 16.0], from: 0.0, to: 10.0)
let ticks = scale.tickValues([2.0, 4.0, 8.0, 16.0], from: 0.0, to: 10.0)
XCTAssertEqual(ticks.count, 3)
}

func testManualTicksOutsideRangeDropped() {
let scale = LinearScale.DoubleScale(from: 0.0, to: 10.0, transform: .drop)
let ticks = scale.ticks([2.0, 4.0, 8.0, 16.0], from: 0.0, to: 10.0)
let ticks = scale.tickValues([2.0, 4.0, 8.0, 16.0], from: 0.0, to: 10.0)
XCTAssertEqual(ticks.count, 3)
}

func testManualTickLabelValidation() {
let scale = LinearScale.DoubleScale(from: 0.0, to: 10.0, transform: .none)
let manualLabels = [
TickLabel(rangeLocation: Float(-1.0), value: "-1.0"),
TickLabel(rangeLocation: Float(1.0), value: "1.0"),
TickLabel(rangeLocation: Float(10.0), value: "10.0"),
TickLabel(rangeLocation: Float(100.0), value: "100.0"),
]
let validatedSet = scale.validatedTickLabels(manualLabels, from: 0.0, to: 10.0)
XCTAssertEqual(validatedSet.count, 2)
}

func testManualTickLabelsThroughScale() {
let scale = LinearScale.DoubleScale(from: 0.0, to: 10.0, transform: .none)
let labeledValues = [
(Double(-1.0), "-1"),
(Double(1.0), "1"),
(Double(10.0), "10"),
(Double(100.0), "100"),
]
let validatedSet = scale.labeledTickValues(labeledValues, from: 0.0, to: 10.0)
XCTAssertEqual(validatedSet.count, 2)
XCTAssertEqual(validatedSet[0].value, "1")
XCTAssertEqual(validatedSet[1].value, "10")
}

func testScaleTransform() {
let scale = LinearScale.DoubleScale(from: 0.0, to: 10.0, transform: .none)
// default isClamped is false - no clamping
Expand Down
Loading

0 comments on commit 48e78ce

Please sign in to comment.