Skip to content

Commit

Permalink
Merge pull request #824 from leoMehlig/rotation
Browse files Browse the repository at this point in the history
Keep position on rotation
  • Loading branch information
danielgindi committed Mar 11, 2016
2 parents 28d9a54 + afc1be6 commit 2a64290
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions Charts/Classes/Charts/BarLineChartViewBase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ public class BarLineChartViewBase: ChartViewBase, BarLineScatterCandleBubbleChar
/// Sets the minimum offset (padding) around the chart, defaults to 10
public var minOffset = CGFloat(10.0)

/// flag indicating if the chart should stay at the same position after a rotation or not. Default is false.
public var keepPositionOnRotation: Bool = false

/// the object representing the left y-axis
internal var _leftAxis: ChartYAxis!

Expand Down Expand Up @@ -135,6 +138,27 @@ public class BarLineChartViewBase: ChartViewBase, BarLineScatterCandleBubbleChar
#endif
}

public override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>)
{
//Saving current position of chart.
var oldPoint: CGPoint?
if (keepPositionOnRotation && (keyPath == "frame" || keyPath == "bounds"))
{
oldPoint = viewPortHandler.contentRect.origin
getTransformer(.Left).pixelToValue(&oldPoint!)
}

//Superclass transforms chart.
super.observeValueForKeyPath(keyPath, ofObject: object, change: change, context: context)

//Restoring old position of chart
if var newPoint = oldPoint where keepPositionOnRotation
{
getTransformer(.Left).pointValueToPixel(&newPoint)
viewPortHandler.centerViewPort(pt: newPoint, chart: self)
}
}

public override func drawRect(rect: CGRect)
{
super.drawRect(rect)
Expand Down

0 comments on commit 2a64290

Please sign in to comment.