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

Keep position on rotation #824

Merged
merged 1 commit into from
Mar 11, 2016
Merged
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
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