Ver Fonte

Apple Clamped where appropriate

Jacob Christie há 4 anos atrás
pai
commit
a8f7c49974

+ 1 - 4
Source/Charts/Components/AxisBase.swift

@@ -229,10 +229,7 @@ open class AxisBase: ComponentBase
     /// be aware that this number is not fixed and can only be approximated
     @objc open var labelCount: Int
     {
-        get
-        {
-            return _labelCount
-        }
+        get { _labelCount }
         set
         {
             let range = axisMinLabels...axisMaxLabels as ClosedRange

+ 7 - 11
Source/Charts/Components/ChartLimitLine.swift

@@ -29,7 +29,6 @@ open class ChartLimitLine: ComponentBase
     /// limit / maximum (the y-value or xIndex)
     @objc open var limit = Double(0.0)
     
-    private var _lineWidth = CGFloat(2.0)
     @objc open var lineColor = NSUIColor(red: 237.0/255.0, green: 91.0/255.0, blue: 91.0/255.0, alpha: 1.0)
     @objc open var lineDashPhase = CGFloat(0.0)
     @objc open var lineDashLengths: [CGFloat]?
@@ -60,15 +59,12 @@ open class ChartLimitLine: ComponentBase
     }
     
     /// set the line width of the chart (min = 0.2, max = 12); default 2
-    @objc open var lineWidth: CGFloat
-    {
-        get
-        {
-            return _lineWidth
-        }
-        set
-        {
-            _lineWidth = newValue.clamped(to: 0.2...12)
-        }
+    @objc
+    open var lineWidth: CGFloat {
+        get { _lineWidth }
+        set { _lineWidth = newValue }
     }
+
+    @Clamped(0.2...12)
+    private var _lineWidth: CGFloat = 2.0
 }

+ 7 - 15
Source/Charts/Data/Implementations/Standard/CandleChartDataSet.swift

@@ -53,25 +53,17 @@ open class CandleChartDataSet: LineScatterCandleRadarChartDataSet, CandleChartDa
     
     // MARK: - Styling functions and accessors
     
-    /// the space between the candle entries
-    ///
-    /// **default**: 0.1 (10%)
-    private var _barSpace: CGFloat = 0.1
-
     /// the space that is left out on the left and right side of each candle,
     /// **default**: 0.1 (10%), max 0.45, min 0.0
-    open var barSpace: CGFloat
-    {
-        get
-        {
-            return _barSpace
-        }
-        set
-        {
-            _barSpace = newValue.clamped(to: 0...0.45)
-        }
+    @objc
+    open var barSpace: CGFloat {
+        get { _barSpace }
+        set { _barSpace = newValue }
     }
     
+    @Clamped(0...0.45)
+    private var _barSpace: CGFloat = 0.1
+
     /// should the candle bars show?
     /// when false, only "ticks" will show
     ///

+ 7 - 13
Source/Charts/Data/Implementations/Standard/LineChartDataSet.swift

@@ -50,24 +50,18 @@ open class LineChartDataSet: LineRadarChartDataSet, LineChartDataSetProtocol
     ///
     /// **default**: Linear
     open var mode: Mode = Mode.linear
-    
-    private var _cubicIntensity = CGFloat(0.2)
-    
+
     /// Intensity for cubic lines (min = 0.05, max = 1)
     ///
     /// **default**: 0.2
-    open var cubicIntensity: CGFloat
-    {
-        get
-        {
-            return _cubicIntensity
-        }
-        set
-        {
-            _cubicIntensity = newValue.clamped(to: 0.05...1)
-        }
+    open var cubicIntensity: CGFloat {
+        get { _cubicIntensity }
+        set { _cubicIntensity = newValue }
     }
 
+    @Clamped(0.05...1)
+    private var _cubicIntensity = CGFloat(0.2)
+
     open var isDrawLineWithGradientEnabled = false
 
     open var gradientPositions: [CGFloat]?

+ 7 - 13
Source/Charts/Data/Implementations/Standard/LineRadarChartDataSet.swift

@@ -40,24 +40,18 @@ open class LineRadarChartDataSet: LineScatterCandleRadarChartDataSet, LineRadarC
     /// The alpha value that is used for filling the line surface,
     /// **default**: 0.33
     open var fillAlpha = CGFloat(0.33)
-    
-    private var _lineWidth = CGFloat(1.0)
-    
+
     /// line width of the chart (min = 0.0, max = 10)
     ///
     /// **default**: 1
-    open var lineWidth: CGFloat
-    {
-        get
-        {
-            return _lineWidth
-        }
-        set
-        {
-            _lineWidth = newValue.clamped(to: 0...10)
-        }
+    open var lineWidth: CGFloat {
+        get { _lineWidth }
+        set { _lineWidth = newValue }
     }
     
+    @Clamped(0...10)
+    private var _lineWidth = CGFloat(1.0)
+
     /// Set to `true` if the DataSet should be drawn filled (surface), and not just as a line.
     /// Disabling this will give great performance boost.
     /// Please note that this method uses the path clipping for drawing the filled area (with images, gradients and layers).

+ 0 - 19
Source/Charts/Utils/ChartUtils.swift

@@ -12,25 +12,6 @@
 import Foundation
 import CoreGraphics
 
-extension Comparable
-{
-    func clamped(to range: ClosedRange<Self>) -> Self
-    {
-        if self > range.upperBound
-        {
-            return range.upperBound
-        }
-        else if self < range.lowerBound
-        {
-            return range.lowerBound
-        }
-        else
-        {
-            return self
-        }
-    }
-}
-
 extension FloatingPoint
 {
     var DEG2RAD: Self

+ 8 - 2
Source/Charts/Utils/Property Wrappers/Clamped.swift

@@ -15,11 +15,11 @@ struct Clamped<Value: Comparable> {
 
     var wrappedValue: Value {
         get { storage }
-        set { storage = max(range.lowerBound, min(storage, range.upperBound)) }
+        set { storage = newValue.clamped(to: range) }
     }
 
     init(wrappedValue value: Value, _ range: ClosedRange<Value>) {
-        precondition(range.contains(value))
+        precondition(range.contains(value), "Initial value provided is outside of `range`")
         self.storage = value
         self.range = range
     }
@@ -30,3 +30,9 @@ extension Clamped where Value: Strideable, Value.Stride: SignedInteger {
         self.init(wrappedValue: value, ClosedRange(range))
     }
 }
+
+extension Comparable {
+    func clamped(to range: ClosedRange<Self>) -> Self {
+        max(range.lowerBound, min(self, range.upperBound))
+    }
+}