Skip to content

Commit

Permalink
Fixed axis drag locking (Completes ChartsOrg#2413)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielgindi committed Sep 8, 2017
1 parent f031ef3 commit 5e48ca7
Showing 1 changed file with 45 additions and 33 deletions.
78 changes: 45 additions & 33 deletions Source/Charts/Charts/BarLineChartViewBase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,8 @@ open class BarLineChartViewBase: ChartViewBase, BarLineScatterCandleBubbleChartD

fileprivate var _pinchZoomEnabled = false
fileprivate var _doubleTapToZoomEnabled = true
fileprivate var _dragEnabled = true
fileprivate var _dragYEnabled = true
fileprivate var _dragXEnabled = true
fileprivate var _dragYEnabled = true

fileprivate var _scaleXEnabled = true
fileprivate var _scaleYEnabled = true
Expand Down Expand Up @@ -126,7 +125,7 @@ open class BarLineChartViewBase: ChartViewBase, BarLineScatterCandleBubbleChartD
self.addGestureRecognizer(_panGestureRecognizer)

_doubleTapGestureRecognizer.isEnabled = _doubleTapToZoomEnabled
_panGestureRecognizer.isEnabled = _dragEnabled
_panGestureRecognizer.isEnabled = _dragXEnabled || _dragYEnabled

#if !os(tvOS)
_pinchGestureRecognizer = NSUIPinchGestureRecognizer(target: self, action: #selector(BarLineChartViewBase.pinchGestureRecognized(_:)))
Expand Down Expand Up @@ -672,23 +671,31 @@ open class BarLineChartViewBase: ChartViewBase, BarLineScatterCandleBubbleChartD
{
stopDeceleration()

if _data === nil
if _data === nil || !self.isDragEnabled
{ // If we have no data, we have nothing to pan and no data to highlight
return
}

// If drag is enabled and we are in a position where there's something to drag:
// * If we're zoomed in, then obviously we have something to drag.
// * If we have a drag offset - we always have something to drag
if self.isDragEnabled &&
(!self.hasNoDragOffset || !self.isFullyZoomedOut)
if !self.hasNoDragOffset || !self.isFullyZoomedOut
{
_isDragging = true

_closestDataSetToTouch = getDataSetByTouchPoint(point: recognizer.nsuiLocationOfTouch(0, inView: self))

let translation = recognizer.translation(in: self)
let didUserDrag = (self is HorizontalBarChartView) ? translation.y != 0.0 : translation.x != 0.0
var translation = recognizer.translation(in: self)
if !self.dragXEnabled
{
translation.x = 0.0
}
else if !self.dragYEnabled
{
translation.y = 0.0
}

let didUserDrag = translation.x != 0.0 || translation.y != 0.0

// Check to see if user dragged at all and if so, can the chart be dragged by the given amount
if didUserDrag && !performPanChange(translation: translation)
Expand Down Expand Up @@ -723,7 +730,16 @@ open class BarLineChartViewBase: ChartViewBase, BarLineScatterCandleBubbleChartD
if _isDragging
{
let originalTranslation = recognizer.translation(in: self)
let translation = CGPoint(x: originalTranslation.x - _lastPanPoint.x, y: originalTranslation.y - _lastPanPoint.y)
var translation = CGPoint(x: originalTranslation.x - _lastPanPoint.x, y: originalTranslation.y - _lastPanPoint.y)

if !self.dragXEnabled
{
translation.x = 0.0
}
else if !self.dragYEnabled
{
translation.y = 0.0
}

let _ = performPanChange(translation: translation)

Expand Down Expand Up @@ -855,11 +871,9 @@ open class BarLineChartViewBase: ChartViewBase, BarLineScatterCandleBubbleChartD
{
if gestureRecognizer == _panGestureRecognizer
{
let velocity = _panGestureRecognizer.velocity(in: self)
if _data === nil || !_dragEnabled ||
(self.hasNoDragOffset && self.isFullyZoomedOut && !self.isHighlightPerDragEnabled) ||
(!_dragYEnabled && fabs(velocity.y) > fabs(velocity.x)) ||
(!_dragXEnabled && fabs(velocity.y) < fabs(velocity.x))
if _data === nil ||
!isDragEnabled ||
(self.hasNoDragOffset && self.isFullyZoomedOut && !self.isHighlightPerDragEnabled)
{
return false
}
Expand Down Expand Up @@ -1515,31 +1529,22 @@ open class BarLineChartViewBase: ChartViewBase, BarLineScatterCandleBubbleChartD
{
get
{
return _dragEnabled
return _dragXEnabled || _dragYEnabled
}
set
{
if _dragEnabled != newValue
{
_dragEnabled = newValue
_dragYEnabled = newValue
_dragXEnabled = newValue
}
_dragYEnabled = newValue
_dragXEnabled = newValue
}
}

open var dragYEnabled: Bool
/// is dragging enabled? (moving the chart with the finger) for the chart (this does not affect scaling).
open var isDragEnabled: Bool
{
get
{
return _dragYEnabled
}
set
{
_dragYEnabled = newValue
}
return dragEnabled
}

/// is dragging on the X axis enabled?
open var dragXEnabled: Bool
{
get
Expand All @@ -1552,10 +1557,17 @@ open class BarLineChartViewBase: ChartViewBase, BarLineScatterCandleBubbleChartD
}
}

/// is dragging enabled? (moving the chart with the finger) for the chart (this does not affect scaling).
open var isDragEnabled: Bool
/// is dragging on the Y axis enabled?
open var dragYEnabled: Bool
{
return dragEnabled
get
{
return _dragYEnabled
}
set
{
_dragYEnabled = newValue
}
}

/// is scaling enabled? (zooming in and out by gesture) for the chart (this does not affect dragging).
Expand Down

0 comments on commit 5e48ca7

Please sign in to comment.