Skip to content

Commit

Permalink
Bugfixes concerning viewport modifications.
Browse files Browse the repository at this point in the history
Modifications to the viewport using moveViewTo- will now wait for the view to receive a size before modifying the viewport
  • Loading branch information
danielgindi committed Apr 5, 2015
1 parent 0206bef commit 54517db
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 20 deletions.
59 changes: 39 additions & 20 deletions Charts/Classes/Charts/BarLineChartViewBase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -784,10 +784,17 @@ public class BarLineChartViewBase: ChartViewBase
/// Moves the left side of the current viewport to the specified x-index.
public func moveViewToX(xIndex: Int)
{
var pt = CGPoint(x: CGFloat(xIndex), y: 0.0);

getTransformer(.Left).pointValueToPixel(&pt);
_viewPortHandler.centerViewPort(pt: pt, chart: self);
if (_viewPortHandler.hasChartDimens)
{
var pt = CGPoint(x: CGFloat(xIndex), y: 0.0);

getTransformer(.Left).pointValueToPixel(&pt);
_viewPortHandler.centerViewPort(pt: pt, chart: self);
}
else
{
_sizeChangeEventActions.append({[weak self] () in self?.moveViewToX(xIndex); });
}
}

/// Centers the viewport to the specified y-value on the y-axis.
Expand All @@ -796,29 +803,41 @@ public class BarLineChartViewBase: ChartViewBase
/// :param: axis - which axis should be used as a reference for the y-axis
public func moveViewToY(yValue: CGFloat, axis: ChartYAxis.AxisDependency)
{
var valsInView = getDeltaY(axis) / _viewPortHandler.scaleY;

var pt = CGPoint(x: 0.0, y: yValue + valsInView / 2.0);

getTransformer(axis).pointValueToPixel(&pt);
_viewPortHandler.centerViewPort(pt: pt, chart: self);
if (_viewPortHandler.hasChartDimens)
{
var valsInView = getDeltaY(axis) / _viewPortHandler.scaleY;

var pt = CGPoint(x: 0.0, y: yValue + valsInView / 2.0);

getTransformer(axis).pointValueToPixel(&pt);
_viewPortHandler.centerViewPort(pt: pt, chart: self);
}
else
{
_sizeChangeEventActions.append({[weak self] () in self?.moveViewToY(yValue, axis: axis); });
}
}

/// This will move the left side of the current viewport to the specified
/// x-index on the x-axis, and center the viewport to the specified y-value
/// on the y-axis.
/// This will move the left side of the current viewport to the specified x-index on the x-axis, and center the viewport to the specified y-value on the y-axis.
///
/// :param: xIndex
/// :param: yValue
/// :param: axis - which axis should be used as a reference for the y-axis
public func moveViewTo(xIndex: Int, yValue: CGFloat, axis: ChartYAxis.AxisDependency)
public func moveViewTo(#xIndex: Int, yValue: CGFloat, axis: ChartYAxis.AxisDependency)
{
var valsInView = getDeltaY(axis) / _viewPortHandler.scaleY;

var pt = CGPoint(x: CGFloat(xIndex), y: yValue + valsInView / 2.0);

getTransformer(axis).pointValueToPixel(&pt);
_viewPortHandler.centerViewPort(pt: pt, chart: self);
if (_viewPortHandler.hasChartDimens)
{
var valsInView = getDeltaY(axis) / _viewPortHandler.scaleY;

var pt = CGPoint(x: CGFloat(xIndex), y: yValue + valsInView / 2.0);

getTransformer(axis).pointValueToPixel(&pt);
_viewPortHandler.centerViewPort(pt: pt, chart: self);
}
else
{
_sizeChangeEventActions.append({[weak self] () in self?.moveViewTo(xIndex: xIndex, yValue: yValue, axis: axis); });
}
}

/// Sets custom offsets for the current ViewPort (the offsets on the sides of the actual chart window). Setting this will prevent the chart from automatically calculating it's offsets. Use resetViewPortOffsets() to undo this.
Expand Down
14 changes: 14 additions & 0 deletions Charts/Classes/Charts/ChartViewBase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -728,6 +728,9 @@ public class ChartViewBase: UIView, ChartAnimatorDelegate
UIImageWriteToSavedPhotosAlbum(getChartImage(), nil, nil, nil);
}

internal typealias VoidClosureType = () -> ()
internal var _sizeChangeEventActions = [VoidClosureType]()

public override var bounds: CGRect
{
get
Expand All @@ -741,12 +744,23 @@ public class ChartViewBase: UIView, ChartAnimatorDelegate
if (_viewPortHandler !== nil)
{
_viewPortHandler.setChartDimens(width: newValue.size.width, height: newValue.size.height);

// Finish any pending viewport changes
while (!_sizeChangeEventActions.isEmpty)
{
_sizeChangeEventActions.removeAtIndex(0)();
}
}

notifyDataSetChanged();
}
}

public func clearPendingViewPortChanges()
{
_sizeChangeEventActions.removeAll(keepCapacity: false);
}

/// if true, value highlightning is enabled
public var isHighlightEnabled: Bool { return highlightEnabled; }

Expand Down
12 changes: 12 additions & 0 deletions Charts/Classes/Utils/ChartViewPortHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,18 @@ public class ChartViewPortHandler
}
}

public var hasChartDimens: Bool
{
if (_chartHeight > 0.0 && _chartWidth > 0.0)
{
return true;
}
else
{
return false;
}
}

public func restrainViewPort(#offsetLeft: CGFloat, offsetTop: CGFloat, offsetRight: CGFloat, offsetBottom: CGFloat)
{
_contentRect.origin.x = offsetLeft;
Expand Down

0 comments on commit 54517db

Please sign in to comment.