Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding a minimum parameter to setVisibleXRange #119

Merged
merged 2 commits into from
Jun 2, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion Charts/Classes/Charts/BarLineChartViewBase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -619,7 +619,9 @@ public class BarLineChartViewBase: ChartViewBase, UIGestureRecognizerDelegate
}
else if (recognizer.state == UIGestureRecognizerState.Changed)
{
if (_isScaling)
var isZoomingOut = (recognizer.scale < 1);
var canZoomMore = isZoomingOut ? _viewPortHandler.canZoomOutMore() : _viewPortHandler.canZoomInMore();
if (_isScaling && canZoomMore)
{
var location = recognizer.locationInView(self);
location.x = location.x - _viewPortHandler.offsetLeft;
Expand Down Expand Up @@ -939,6 +941,16 @@ public class BarLineChartViewBase: ChartViewBase, UIGestureRecognizerDelegate
_viewPortHandler.setMinimumScaleX(xScale);
}

//Limits the maximal and minimal values count that can be visible by pinching and zooming.
//e.g. minRange=10, maxRange=100 no less than 10 values and no more that 100 values can be viewed
//at once without scrolling
public func setVisibleXRange(#minRange: CGFloat, maxRange: CGFloat)
{
var maxScale = _deltaX / (minRange);
var minScale = _deltaX / (maxRange);
_viewPortHandler.setScaleXRange(minScaleX: minScale, maxScaleX: maxScale);
}

/// Sets the size of the area (range on the y-axis) that should be maximum visible at once.
///
/// :param: yRange
Expand Down
24 changes: 24 additions & 0 deletions Charts/Classes/Utils/ChartViewPortHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ public class ChartViewPortHandler: NSObject
/// minimum scale value on the x-axis
private var _minScaleX = CGFloat(1.0);

/// maximum scale value on the x-axis
private var _maxScaleX = CGFloat(FLT_MAX);

/// contains the current scale factor of the x-axis
private var _scaleX = CGFloat(1.0);

Expand Down Expand Up @@ -208,6 +211,11 @@ public class ChartViewPortHandler: NSObject
// min scale-x is 1f
_scaleX = max(_minScaleX, matrix.a);

if (_scaleX > _maxScaleX)
{
_scaleX = _maxScaleX;
}

// min scale-y is 1f
_scaleY = max(_minScaleY, matrix.d);

Expand Down Expand Up @@ -393,4 +401,20 @@ public class ChartViewPortHandler: NSObject
{
return _transOffsetX <= 0.0 && _transOffsetY <= 0.0 ? true : false;
}

public func setScaleXRange(#minScaleX: CGFloat, maxScaleX: CGFloat)
{
_maxScaleX = maxScaleX;
setMinimumScaleX(minScaleX);
}

public func canZoomOutMore() -> Bool
{
return (_scaleX > _minScaleX);
}

public func canZoomInMore() -> Bool
{
return (_scaleX < _maxScaleX);
}
}