From f84e9bbf3aa68ec2052b11befad9de7f6a90fc22 Mon Sep 17 00:00:00 2001 From: nuomi1 Date: Tue, 26 Jan 2021 20:28:21 +0800 Subject: [PATCH] ChartViewBase cleanup (#4537) * refactor: remove _highlightPerTapEnabled * refactor: remove _drawUnitInChart * refactor: remove interceptTouchEvents * refactor: use abs * refactor: optional chaining * refactor: use first * refactor: use bounds directly * refactor: use max and min --- Source/Charts/Charts/ChartViewBase.swift | 60 +++++------------------- 1 file changed, 11 insertions(+), 49 deletions(-) diff --git a/Source/Charts/Charts/ChartViewBase.swift b/Source/Charts/Charts/ChartViewBase.swift index 887d895e51..ec243b14cd 100644 --- a/Source/Charts/Charts/ChartViewBase.swift +++ b/Source/Charts/Charts/ChartViewBase.swift @@ -72,16 +72,9 @@ open class ChartViewBase: NSUIView, ChartDataProvider, AnimatorDelegate } } - /// Flag that indicates if highlighting per tap (touch) is enabled - private var _highlightPerTapEnabled = true - /// If set to true, chart continues to scroll after touch up @objc open var dragDecelerationEnabled = true - /// if true, units are drawn next to the values in the chart - // TODO: This is used nowhere and can't be used by a consumer. Can we remove this property? - internal var _drawUnitInChart = false - /// The object representing the labels on the x-axis @objc open internal(set) lazy var xAxis = XAxis() @@ -142,9 +135,6 @@ open class ChartViewBase: NSUIView, ChartDataProvider, AnimatorDelegate /// The marker that is displayed when a value is clicked on the chart @objc open var marker: Marker? - // TODO: There is no way to modify this value. Should it exist? - private let interceptTouchEvents = false - /// An extra offset to be appended to the viewport's top @objc open var extraTopOffset: CGFloat = 0.0 @@ -248,13 +238,11 @@ open class ChartViewBase: NSUIView, ChartDataProvider, AnimatorDelegate if let data = data , data.entryCount >= 2 { - reference = fabs(max - min) + reference = abs(max - min) } else { - let absMin = fabs(min) - let absMax = fabs(max) - reference = absMin > absMax ? absMin : absMax + reference = Swift.max(abs(min), abs(max)) } @@ -268,8 +256,7 @@ open class ChartViewBase: NSUIView, ChartDataProvider, AnimatorDelegate open override func draw(_ rect: CGRect) { - let optionalContext = NSUIGraphicsGetCurrentContext() - guard let context = optionalContext else { return } + guard let context = NSUIGraphicsGetCurrentContext() else { return } if data === nil && !noDataText.isEmpty { @@ -337,12 +324,8 @@ open class ChartViewBase: NSUIView, ChartDataProvider, AnimatorDelegate /// Set this to false to prevent values from being highlighted by tap gesture. /// Values can still be highlighted via drag or programmatically. /// **default**: true - @objc open var highlightPerTapEnabled: Bool - { - get { return _highlightPerTapEnabled } - set { _highlightPerTapEnabled = newValue } - } - + @objc open var highlightPerTapEnabled: Bool = true + /// `true` if values can be highlighted via tap gesture, `false` ifnot. @objc open var isHighLightPerTapEnabled: Bool { @@ -366,10 +349,7 @@ open class ChartViewBase: NSUIView, ChartDataProvider, AnimatorDelegate // set the indices to highlight highlighted = highs ?? [] - lastHighlighted = highlighted.isEmpty - ? nil - : highlighted[0] - + lastHighlighted = highlighted.first // redraw the chart setNeedsDisplay() @@ -701,7 +681,6 @@ open class ChartViewBase: NSUIView, ChartDataProvider, AnimatorDelegate /// The center point of the chart (the whole View) in pixels. @objc open var midPoint: CGPoint { - let bounds = self.bounds return CGPoint(x: bounds.origin.x + bounds.size.width / 2.0, y: bounds.origin.y + bounds.size.height / 2.0) } @@ -859,12 +838,7 @@ open class ChartViewBase: NSUIView, ChartDataProvider, AnimatorDelegate } set { - switch newValue - { - case ..<0.0: _dragDecelerationFrictionCoef = 0 - case 1.0...: _dragDecelerationFrictionCoef = 0.999 - default: _dragDecelerationFrictionCoef = newValue - } + _dragDecelerationFrictionCoef = max(0, min(newValue, 0.999)) } } private var _dragDecelerationFrictionCoef: CGFloat = 0.9 @@ -895,33 +869,21 @@ open class ChartViewBase: NSUIView, ChartDataProvider, AnimatorDelegate open override func nsuiTouchesBegan(_ touches: Set, withEvent event: NSUIEvent?) { - if !interceptTouchEvents - { - super.nsuiTouchesBegan(touches, withEvent: event) - } + super.nsuiTouchesBegan(touches, withEvent: event) } open override func nsuiTouchesMoved(_ touches: Set, withEvent event: NSUIEvent?) { - if !interceptTouchEvents - { - super.nsuiTouchesMoved(touches, withEvent: event) - } + super.nsuiTouchesMoved(touches, withEvent: event) } open override func nsuiTouchesEnded(_ touches: Set, withEvent event: NSUIEvent?) { - if !interceptTouchEvents - { - super.nsuiTouchesEnded(touches, withEvent: event) - } + super.nsuiTouchesEnded(touches, withEvent: event) } open override func nsuiTouchesCancelled(_ touches: Set?, withEvent event: NSUIEvent?) { - if !interceptTouchEvents - { - super.nsuiTouchesCancelled(touches, withEvent: event) - } + super.nsuiTouchesCancelled(touches, withEvent: event) } }