Skip to content

Commit

Permalink
ChartViewBase cleanup (#4537)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
nuomi1 authored Jan 26, 2021
1 parent dea1cc5 commit 857db24
Showing 1 changed file with 11 additions and 49 deletions.
60 changes: 11 additions & 49 deletions Source/Charts/Charts/ChartViewBase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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))
}


Expand All @@ -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
{
Expand Down Expand Up @@ -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
{
Expand All @@ -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()
Expand Down Expand Up @@ -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)
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -895,33 +869,21 @@ open class ChartViewBase: NSUIView, ChartDataProvider, AnimatorDelegate

open override func nsuiTouchesBegan(_ touches: Set<NSUITouch>, withEvent event: NSUIEvent?)
{
if !interceptTouchEvents
{
super.nsuiTouchesBegan(touches, withEvent: event)
}
super.nsuiTouchesBegan(touches, withEvent: event)
}

open override func nsuiTouchesMoved(_ touches: Set<NSUITouch>, withEvent event: NSUIEvent?)
{
if !interceptTouchEvents
{
super.nsuiTouchesMoved(touches, withEvent: event)
}
super.nsuiTouchesMoved(touches, withEvent: event)
}

open override func nsuiTouchesEnded(_ touches: Set<NSUITouch>, withEvent event: NSUIEvent?)
{
if !interceptTouchEvents
{
super.nsuiTouchesEnded(touches, withEvent: event)
}
super.nsuiTouchesEnded(touches, withEvent: event)
}

open override func nsuiTouchesCancelled(_ touches: Set<NSUITouch>?, withEvent event: NSUIEvent?)
{
if !interceptTouchEvents
{
super.nsuiTouchesCancelled(touches, withEvent: event)
}
super.nsuiTouchesCancelled(touches, withEvent: event)
}
}

0 comments on commit 857db24

Please sign in to comment.