diff --git a/Source/Charts/Utils/Fill.swift b/Source/Charts/Utils/Fill.swift index 1294b3efc0..1eacfd4a88 100644 --- a/Source/Charts/Utils/Fill.swift +++ b/Source/Charts/Utils/Fill.swift @@ -27,79 +27,25 @@ public enum FillType: Int @objc(ChartFill) open class Fill: NSObject { - private var _type: FillType = FillType.empty - private var _color: CGColor? - private var _gradient: CGGradient? - private var _gradientAngle: CGFloat = 0.0 - private var _gradientStartOffsetPercent: CGPoint = CGPoint() - private var _gradientStartRadiusPercent: CGFloat = 0.0 - private var _gradientEndOffsetPercent: CGPoint = CGPoint() - private var _gradientEndRadiusPercent: CGFloat = 0.0 - private var _image: CGImage? - private var _layer: CGLayer? - // MARK: Properties - - @objc open var type: FillType - { - return _type - } - - @objc open var color: CGColor? - { - return _color - } - - @objc open var gradient: CGGradient? - { - return _gradient - } - - @objc open var gradientAngle: CGFloat - { - return _gradientAngle - } - - @objc open var gradientStartOffsetPercent: CGPoint - { - return _gradientStartOffsetPercent - } - - @objc open var gradientStartRadiusPercent: CGFloat - { - return _gradientStartRadiusPercent - } - - @objc open var gradientEndOffsetPercent: CGPoint - { - return _gradientEndOffsetPercent - } - - @objc open var gradientEndRadiusPercent: CGFloat - { - return _gradientEndRadiusPercent - } - - @objc open var image: CGImage? - { - return _image - } - - @objc open var layer: CGLayer? - { - return _layer - } - + + @objc open private(set) var type: FillType = .empty + @objc open private(set) var color: CGColor? + @objc open private(set) var gradient: CGGradient? + @objc open private(set) var gradientAngle: CGFloat = 0.0 + @objc open private(set) var gradientStartOffsetPercent = CGPoint.zero + @objc open private(set) var gradientStartRadiusPercent: CGFloat = 0.0 + @objc open private(set) var gradientEndOffsetPercent = CGPoint.zero + @objc open private(set) var gradientEndRadiusPercent: CGFloat = 0.0 + @objc open private(set) var image: CGImage? + @objc open private(set) var layer: CGLayer? + // MARK: Constructors - - public override init() - { - } - + @objc public init(CGColor: CGColor) { - _type = .color - _color = CGColor + type = .color + color = CGColor } @objc public convenience init(color: NSUIColor) @@ -109,9 +55,9 @@ open class Fill: NSObject @objc public init(linearGradient: CGGradient, angle: CGFloat) { - _type = .linearGradient - _gradient = linearGradient - _gradientAngle = angle + type = .linearGradient + gradient = linearGradient + gradientAngle = angle } @objc public init( @@ -122,12 +68,12 @@ open class Fill: NSObject endRadiusPercent: CGFloat ) { - _type = .radialGradient - _gradient = radialGradient - _gradientStartOffsetPercent = startOffsetPercent - _gradientStartRadiusPercent = startRadiusPercent - _gradientEndOffsetPercent = endOffsetPercent - _gradientEndRadiusPercent = endRadiusPercent + type = .radialGradient + gradient = radialGradient + gradientStartOffsetPercent = startOffsetPercent + gradientStartRadiusPercent = startRadiusPercent + gradientEndOffsetPercent = endOffsetPercent + gradientEndRadiusPercent = endRadiusPercent } @objc public convenience init(radialGradient: CGGradient) @@ -141,31 +87,21 @@ open class Fill: NSObject ) } - @objc public init(CGImage: CGImage, tiled: Bool) + @objc public init(CGImage: CGImage, tiled: Bool = false) { - _type = tiled ? .tiledImage : .image - _image = CGImage + type = tiled ? .tiledImage : .image + image = CGImage } - @objc public convenience init(image: NSUIImage, tiled: Bool) + @objc public convenience init(image: NSUIImage, tiled: Bool = false) { self.init(CGImage: image.cgImage!, tiled: tiled) } - - @objc public convenience init(CGImage: CGImage) - { - self.init(CGImage: CGImage, tiled: false) - } - - @objc public convenience init(image: NSUIImage) - { - self.init(image: image, tiled: false) - } - + @objc public init(CGLayer: CGLayer) { - _type = .layer - _layer = CGLayer + type = .layer + layer = CGLayer } // MARK: Constructors @@ -209,26 +145,16 @@ open class Fill: NSObject return Fill(radialGradient: radialGradient) } - @objc open class func fillWithCGImage(_ CGImage: CGImage, tiled: Bool) -> Fill + @objc open class func fillWithCGImage(_ CGImage: CGImage, tiled: Bool = false) -> Fill { return Fill(CGImage: CGImage, tiled: tiled) } - @objc open class func fillWithImage(_ image: NSUIImage, tiled: Bool) -> Fill + @objc open class func fillWithImage(_ image: NSUIImage, tiled: Bool = false) -> Fill { return Fill(image: image, tiled: tiled) } - - @objc open class func fillWithCGImage(_ CGImage: CGImage) -> Fill - { - return Fill(CGImage: CGImage) - } - - @objc open class func fillWithImage(_ image: NSUIImage) -> Fill - { - return Fill(image: image) - } - + @objc open class func fillWithCGLayer(_ CGLayer: CGLayer) -> Fill { return Fill(CGLayer: CGLayer) @@ -241,39 +167,40 @@ open class Fill: NSObject context: CGContext, rect: CGRect) { - let fillType = _type + let fillType = type if fillType == .empty { return } context.saveGState() + defer { context.restoreGState() } switch fillType { case .color: - context.setFillColor(_color!) + context.setFillColor(color!) context.fillPath() case .image: context.clip() - context.draw(_image!, in: rect) + context.draw(image!, in: rect) case .tiledImage: context.clip() - context.draw(_image!, in: rect, byTiling: true) + context.draw(image!, in: rect, byTiling: true) case .layer: context.clip() - context.draw(_layer!, in: rect) + context.draw(layer!, in: rect) case .linearGradient: - let radians = (360.0 - _gradientAngle).DEG2RAD + let radians = (360.0 - gradientAngle).DEG2RAD let centerPoint = CGPoint(x: rect.midX, y: rect.midY) let xAngleDelta = cos(radians) * rect.width / 2.0 let yAngleDelta = sin(radians) * rect.height / 2.0 @@ -287,7 +214,7 @@ open class Fill: NSObject ) context.clip() - context.drawLinearGradient(_gradient!, + context.drawLinearGradient(gradient!, start: startPoint, end: endPoint, options: [.drawsAfterEndLocation, .drawsBeforeStartLocation] @@ -299,25 +226,22 @@ open class Fill: NSObject let radius = max(rect.width, rect.height) / 2.0 context.clip() - context.drawRadialGradient(_gradient!, + context.drawRadialGradient(gradient!, startCenter: CGPoint( - x: centerPoint.x + rect.width * _gradientStartOffsetPercent.x, - y: centerPoint.y + rect.height * _gradientStartOffsetPercent.y + x: centerPoint.x + rect.width * gradientStartOffsetPercent.x, + y: centerPoint.y + rect.height * gradientStartOffsetPercent.y ), - startRadius: radius * _gradientStartRadiusPercent, + startRadius: radius * gradientStartRadiusPercent, endCenter: CGPoint( - x: centerPoint.x + rect.width * _gradientEndOffsetPercent.x, - y: centerPoint.y + rect.height * _gradientEndOffsetPercent.y + x: centerPoint.x + rect.width * gradientEndOffsetPercent.x, + y: centerPoint.y + rect.height * gradientEndOffsetPercent.y ), - endRadius: radius * _gradientEndRadiusPercent, + endRadius: radius * gradientEndRadiusPercent, options: [.drawsAfterEndLocation, .drawsBeforeStartLocation] ) case .empty: break } - - context.restoreGState() } - } diff --git a/Source/Charts/Utils/Transformer.swift b/Source/Charts/Utils/Transformer.swift index ca99fb2360..1465325af7 100644 --- a/Source/Charts/Utils/Transformer.swift +++ b/Source/Charts/Utils/Transformer.swift @@ -17,37 +17,37 @@ import CoreGraphics open class Transformer: NSObject { /// matrix to map the values to the screen pixels - internal var _matrixValueToPx = CGAffineTransform.identity + internal var matrixValueToPx = CGAffineTransform.identity /// matrix for handling the different offsets of the chart - internal var _matrixOffset = CGAffineTransform.identity + internal var matrixOffset = CGAffineTransform.identity - internal var _viewPortHandler: ViewPortHandler + internal var viewPortHandler: ViewPortHandler @objc public init(viewPortHandler: ViewPortHandler) { - _viewPortHandler = viewPortHandler + self.viewPortHandler = viewPortHandler } /// Prepares the matrix that transforms values to pixels. Calculates the scale factors from the charts size and offsets. @objc open func prepareMatrixValuePx(chartXMin: Double, deltaX: CGFloat, deltaY: CGFloat, chartYMin: Double) { - var scaleX = (_viewPortHandler.contentWidth / deltaX) - var scaleY = (_viewPortHandler.contentHeight / deltaY) + var scaleX = (viewPortHandler.contentWidth / deltaX) + var scaleY = (viewPortHandler.contentHeight / deltaY) - if CGFloat.infinity == scaleX + if .infinity == scaleX { scaleX = 0.0 } - if CGFloat.infinity == scaleY + if .infinity == scaleY { scaleY = 0.0 } // setup all matrices - _matrixValueToPx = CGAffineTransform.identity - _matrixValueToPx = _matrixValueToPx.scaledBy(x: scaleX, y: -scaleY) - _matrixValueToPx = _matrixValueToPx.translatedBy(x: CGFloat(-chartXMin), y: CGFloat(-chartYMin)) + matrixValueToPx = CGAffineTransform.identity + .scaledBy(x: scaleX, y: -scaleY) + .translatedBy(x: CGFloat(-chartXMin), y: CGFloat(-chartYMin)) } /// Prepares the matrix that contains all offsets. @@ -55,12 +55,12 @@ open class Transformer: NSObject { if !inverted { - _matrixOffset = CGAffineTransform(translationX: _viewPortHandler.offsetLeft, y: _viewPortHandler.chartHeight - _viewPortHandler.offsetBottom) + matrixOffset = CGAffineTransform(translationX: viewPortHandler.offsetLeft, y: viewPortHandler.chartHeight - viewPortHandler.offsetBottom) } else { - _matrixOffset = CGAffineTransform(scaleX: 1.0, y: -1.0) - _matrixOffset = _matrixOffset.translatedBy(x: _viewPortHandler.offsetLeft, y: -_viewPortHandler.offsetTop) + matrixOffset = CGAffineTransform(scaleX: 1.0, y: -1.0) + .translatedBy(x: viewPortHandler.offsetLeft, y: -viewPortHandler.offsetTop) } } @@ -69,10 +69,7 @@ open class Transformer: NSObject open func pointValuesToPixel(_ points: inout [CGPoint]) { let trans = valueToPixelMatrix - for i in 0 ..< points.count - { - points[i] = points[i].applying(trans) - } + points = points.map { $0.applying(trans) } } open func pointValueToPixel(_ point: inout CGPoint) @@ -126,22 +123,14 @@ open class Transformer: NSObject open func rectValuesToPixel(_ rects: inout [CGRect]) { let trans = valueToPixelMatrix - - for i in 0 ..< rects.count - { - rects[i] = rects[i].applying(trans) - } + rects = rects.map { $0.applying(trans) } } /// Transforms the given array of touch points (pixels) into values on the chart. open func pixelsToValues(_ pixels: inout [CGPoint]) { let trans = pixelToValueMatrix - - for i in 0 ..< pixels.count - { - pixels[i] = pixels[i].applying(trans) - } + pixels = pixels.map { $0.applying(trans) } } /// Transforms the given touch point (pixels) into a value on the chart. @@ -169,8 +158,8 @@ open class Transformer: NSObject @objc open var valueToPixelMatrix: CGAffineTransform { return - _matrixValueToPx.concatenating(_viewPortHandler.touchMatrix - ).concatenating(_matrixOffset + matrixValueToPx.concatenating(viewPortHandler.touchMatrix) + .concatenating(matrixOffset ) } diff --git a/Source/Charts/Utils/TransformerHorizontalBarChart.swift b/Source/Charts/Utils/TransformerHorizontalBarChart.swift index d7e657bd25..1a7d5579cd 100644 --- a/Source/Charts/Utils/TransformerHorizontalBarChart.swift +++ b/Source/Charts/Utils/TransformerHorizontalBarChart.swift @@ -20,13 +20,13 @@ open class TransformerHorizontalBarChart: Transformer { if !inverted { - _matrixOffset = CGAffineTransform(translationX: _viewPortHandler.offsetLeft, y: _viewPortHandler.chartHeight - _viewPortHandler.offsetBottom) + matrixOffset = CGAffineTransform(translationX: viewPortHandler.offsetLeft, y: viewPortHandler.chartHeight - viewPortHandler.offsetBottom) } else { - _matrixOffset = CGAffineTransform(scaleX: -1.0, y: 1.0) - _matrixOffset = _matrixOffset.translatedBy(x: -(_viewPortHandler.chartWidth - _viewPortHandler.offsetRight), - y: _viewPortHandler.chartHeight - _viewPortHandler.offsetBottom) + matrixOffset = CGAffineTransform(scaleX: -1.0, y: 1.0) + .translatedBy(x: -(viewPortHandler.chartWidth - viewPortHandler.offsetRight), + y: viewPortHandler.chartHeight - viewPortHandler.offsetBottom) } } } diff --git a/Source/Charts/Utils/ViewPortHandler.swift b/Source/Charts/Utils/ViewPortHandler.swift old mode 100755 new mode 100644 index 4fde6eca50..e2eef62a97 --- a/Source/Charts/Utils/ViewPortHandler.swift +++ b/Source/Charts/Utils/ViewPortHandler.swift @@ -17,45 +17,44 @@ import CoreGraphics open class ViewPortHandler: NSObject { /// matrix used for touch events - private var _touchMatrix = CGAffineTransform.identity - + @objc open private(set) var touchMatrix = CGAffineTransform.identity + /// this rectangle defines the area in which graph values can be drawn - private var _contentRect = CGRect() - - private var _chartWidth = CGFloat(0.0) - private var _chartHeight = CGFloat(0.0) + @objc open private(set) var contentRect = CGRect() + @objc open private(set) var chartWidth: CGFloat = 0 + @objc open private(set) var chartHeight: CGFloat = 0 + /// minimum scale value on the y-axis - private var _minScaleY = CGFloat(1.0) - + @objc open private(set) var minScaleY: CGFloat = 1.0 + /// maximum scale value on the y-axis - private var _maxScaleY = CGFloat.greatestFiniteMagnitude - + @objc open private(set) var maxScaleY = CGFloat.greatestFiniteMagnitude + /// minimum scale value on the x-axis - private var _minScaleX = CGFloat(1.0) - + @objc open private(set) var minScaleX: CGFloat = 1.0 + /// maximum scale value on the x-axis - private var _maxScaleX = CGFloat.greatestFiniteMagnitude - + @objc open private(set) var maxScaleX = CGFloat.greatestFiniteMagnitude + /// contains the current scale factor of the x-axis - private var _scaleX = CGFloat(1.0) - + @objc open private(set) var scaleX: CGFloat = 1.0 + /// contains the current scale factor of the y-axis - private var _scaleY = CGFloat(1.0) - + @objc open private(set) var scaleY: CGFloat = 1.0 + /// current translation (drag distance) on the x-axis - private var _transX = CGFloat(0.0) - + @objc open private(set) var transX: CGFloat = 0 + /// current translation (drag distance) on the y-axis - private var _transY = CGFloat(0.0) - + @objc open private(set) var transY: CGFloat = 0 + /// offset that allows the chart to be dragged over its bounds on the x-axis - private var _transOffsetX = CGFloat(0.0) - + private var transOffsetX: CGFloat = 0 + /// offset that allows the chart to be dragged over its bounds on the x-axis - private var _transOffsetY = CGFloat(0.0) - - /// Constructor - don't forget calling setChartDimens(...) + private var transOffsetY: CGFloat = 0 + @objc public init(width: CGFloat, height: CGFloat) { super.init() @@ -70,100 +69,79 @@ open class ViewPortHandler: NSObject let offsetRight = self.offsetRight let offsetBottom = self.offsetBottom - _chartHeight = height - _chartWidth = width + chartHeight = height + chartWidth = width restrainViewPort(offsetLeft: offsetLeft, offsetTop: offsetTop, offsetRight: offsetRight, offsetBottom: offsetBottom) } @objc open var hasChartDimens: Bool { - if _chartHeight > 0.0 && _chartWidth > 0.0 - { - return true - } - else - { - return false - } + return chartHeight > 0.0 + && chartWidth > 0.0 } @objc open func restrainViewPort(offsetLeft: CGFloat, offsetTop: CGFloat, offsetRight: CGFloat, offsetBottom: CGFloat) { - _contentRect.origin.x = offsetLeft - _contentRect.origin.y = offsetTop - _contentRect.size.width = _chartWidth - offsetLeft - offsetRight - _contentRect.size.height = _chartHeight - offsetBottom - offsetTop + contentRect.origin.x = offsetLeft + contentRect.origin.y = offsetTop + contentRect.size.width = chartWidth - offsetLeft - offsetRight + contentRect.size.height = chartHeight - offsetBottom - offsetTop } @objc open var offsetLeft: CGFloat { - return _contentRect.origin.x + return contentRect.origin.x } @objc open var offsetRight: CGFloat { - return _chartWidth - _contentRect.size.width - _contentRect.origin.x + return chartWidth - contentRect.size.width - contentRect.origin.x } @objc open var offsetTop: CGFloat { - return _contentRect.origin.y + return contentRect.origin.y } @objc open var offsetBottom: CGFloat { - return _chartHeight - _contentRect.size.height - _contentRect.origin.y + return chartHeight - contentRect.size.height - contentRect.origin.y } @objc open var contentTop: CGFloat { - return _contentRect.origin.y + return contentRect.origin.y } @objc open var contentLeft: CGFloat { - return _contentRect.origin.x + return contentRect.origin.x } @objc open var contentRight: CGFloat { - return _contentRect.origin.x + _contentRect.size.width + return contentRect.origin.x + contentRect.size.width } @objc open var contentBottom: CGFloat { - return _contentRect.origin.y + _contentRect.size.height + return contentRect.origin.y + contentRect.size.height } @objc open var contentWidth: CGFloat { - return _contentRect.size.width + return contentRect.size.width } @objc open var contentHeight: CGFloat { - return _contentRect.size.height - } - - @objc open var contentRect: CGRect - { - return _contentRect + return contentRect.size.height } - + @objc open var contentCenter: CGPoint { - return CGPoint(x: _contentRect.origin.x + _contentRect.size.width / 2.0, y: _contentRect.origin.y + _contentRect.size.height / 2.0) - } - - @objc open var chartHeight: CGFloat - { - return _chartHeight - } - - @objc open var chartWidth: CGFloat - { - return _chartWidth + return CGPoint(x: contentRect.origin.x + contentRect.size.width / 2.0, y: contentRect.origin.y + contentRect.size.height / 2.0) } // MARK: - Scaling/Panning etc. @@ -171,16 +149,15 @@ open class ViewPortHandler: NSObject /// Zooms by the specified zoom factors. @objc open func zoom(scaleX: CGFloat, scaleY: CGFloat) -> CGAffineTransform { - return _touchMatrix.scaledBy(x: scaleX, y: scaleY) + return touchMatrix.scaledBy(x: scaleX, y: scaleY) } /// Zooms around the specified center @objc open func zoom(scaleX: CGFloat, scaleY: CGFloat, x: CGFloat, y: CGFloat) -> CGAffineTransform { - var matrix = _touchMatrix.translatedBy(x: x, y: y) - matrix = matrix.scaledBy(x: scaleX, y: scaleY) - matrix = matrix.translatedBy(x: -x, y: -y) - return matrix + return touchMatrix.translatedBy(x: x, y: y) + .scaledBy(x: scaleX, y: scaleY) + .translatedBy(x: -x, y: -y) } /// Zooms in by 1.4, x and y are the coordinates (in pixels) of the zoom center. @@ -204,7 +181,7 @@ open class ViewPortHandler: NSObject /// Sets the scale factor to the specified values. @objc open func setZoom(scaleX: CGFloat, scaleY: CGFloat) -> CGAffineTransform { - var matrix = _touchMatrix + var matrix = touchMatrix matrix.a = scaleX matrix.d = scaleY return matrix @@ -213,22 +190,22 @@ open class ViewPortHandler: NSObject /// Sets the scale factor to the specified values. x and y is pivot. @objc open func setZoom(scaleX: CGFloat, scaleY: CGFloat, x: CGFloat, y: CGFloat) -> CGAffineTransform { - var matrix = _touchMatrix + var matrix = touchMatrix matrix.a = 1.0 matrix.d = 1.0 matrix = matrix.translatedBy(x: x, y: y) - matrix = matrix.scaledBy(x: scaleX, y: scaleY) - matrix = matrix.translatedBy(x: -x, y: -y) + .scaledBy(x: scaleX, y: scaleY) + .translatedBy(x: -x, y: -y) return matrix } /// Resets all zooming and dragging and makes the chart fit exactly it's bounds. @objc open func fitScreen() -> CGAffineTransform { - _minScaleX = 1.0 - _minScaleY = 1.0 + minScaleX = 1.0 + minScaleY = 1.0 - return CGAffineTransform.identity + return .identity } /// Translates to the specified point. @@ -237,7 +214,7 @@ open class ViewPortHandler: NSObject let translateX = pt.x - offsetLeft let translateY = pt.y - offsetTop - let matrix = _touchMatrix.concatenating(CGAffineTransform(translationX: -translateX, y: -translateY)) + let matrix = touchMatrix.concatenating(CGAffineTransform(translationX: -translateX, y: -translateY)) return matrix } @@ -250,160 +227,91 @@ open class ViewPortHandler: NSObject let translateX = pt.x - offsetLeft let translateY = pt.y - offsetTop - let matrix = _touchMatrix.concatenating(CGAffineTransform(translationX: -translateX, y: -translateY)) + let matrix = touchMatrix.concatenating(CGAffineTransform(translationX: -translateX, y: -translateY)) refresh(newMatrix: matrix, chart: chart, invalidate: true) } /// call this method to refresh the graph with a given matrix @objc @discardableResult open func refresh(newMatrix: CGAffineTransform, chart: ChartViewBase, invalidate: Bool) -> CGAffineTransform { - _touchMatrix = newMatrix + touchMatrix = newMatrix // make sure scale and translation are within their bounds - limitTransAndScale(matrix: &_touchMatrix, content: _contentRect) + limitTransAndScale(matrix: &touchMatrix, content: contentRect) chart.setNeedsDisplay() - return _touchMatrix + return touchMatrix } /// limits the maximum scale and X translation of the given matrix - private func limitTransAndScale(matrix: inout CGAffineTransform, content: CGRect?) + private func limitTransAndScale(matrix: inout CGAffineTransform, content: CGRect) { // min scale-x is 1 - _scaleX = min(max(_minScaleX, matrix.a), _maxScaleX) + scaleX = min(max(minScaleX, matrix.a), maxScaleX) // min scale-y is 1 - _scaleY = min(max(_minScaleY, matrix.d), _maxScaleY) + scaleY = min(max(minScaleY, matrix.d), maxScaleY) + let width = content.width + let height = content.height + + let maxTransX = -width * (scaleX - 1.0) + transX = min(max(matrix.tx, maxTransX - transOffsetX), transOffsetX) - var width: CGFloat = 0.0 - var height: CGFloat = 0.0 - - if content != nil - { - width = content!.width - height = content!.height - } - - let maxTransX = -width * (_scaleX - 1.0) - _transX = min(max(matrix.tx, maxTransX - _transOffsetX), _transOffsetX) - - let maxTransY = height * (_scaleY - 1.0) - _transY = max(min(matrix.ty, maxTransY + _transOffsetY), -_transOffsetY) + let maxTransY = height * (scaleY - 1.0) + transY = max(min(matrix.ty, maxTransY + transOffsetY), -transOffsetY) - matrix.tx = _transX - matrix.a = _scaleX - matrix.ty = _transY - matrix.d = _scaleY + matrix.tx = transX + matrix.a = scaleX + matrix.ty = transY + matrix.d = scaleY } /// Sets the minimum scale factor for the x-axis @objc open func setMinimumScaleX(_ xScale: CGFloat) { - var newValue = xScale - - if newValue < 1.0 - { - newValue = 1.0 - } - - _minScaleX = newValue - - limitTransAndScale(matrix: &_touchMatrix, content: _contentRect) + minScaleX = max(xScale, 1) + limitTransAndScale(matrix: &touchMatrix, content: contentRect) } /// Sets the maximum scale factor for the x-axis @objc open func setMaximumScaleX(_ xScale: CGFloat) { - var newValue = xScale - - if newValue == 0.0 - { - newValue = CGFloat.greatestFiniteMagnitude - } - - _maxScaleX = newValue - - limitTransAndScale(matrix: &_touchMatrix, content: _contentRect) + maxScaleX = xScale == 0 ? .greatestFiniteMagnitude : xScale + limitTransAndScale(matrix: &touchMatrix, content: contentRect) } /// Sets the minimum and maximum scale factors for the x-axis @objc open func setMinMaxScaleX(minScaleX: CGFloat, maxScaleX: CGFloat) { - var newMin = minScaleX - var newMax = maxScaleX - - if newMin < 1.0 - { - newMin = 1.0 - } - if newMax == 0.0 - { - newMax = CGFloat.greatestFiniteMagnitude - } - - _minScaleX = newMin - _maxScaleX = maxScaleX - - limitTransAndScale(matrix: &_touchMatrix, content: _contentRect) + self.minScaleX = max(minScaleX, 1) + self.maxScaleX = maxScaleX == 0 ? .greatestFiniteMagnitude : maxScaleX + limitTransAndScale(matrix: &touchMatrix, content: contentRect) } /// Sets the minimum scale factor for the y-axis @objc open func setMinimumScaleY(_ yScale: CGFloat) { - var newValue = yScale - - if newValue < 1.0 - { - newValue = 1.0 - } - - _minScaleY = newValue - - limitTransAndScale(matrix: &_touchMatrix, content: _contentRect) + minScaleY = max(yScale, 1) + limitTransAndScale(matrix: &touchMatrix, content: contentRect) } /// Sets the maximum scale factor for the y-axis @objc open func setMaximumScaleY(_ yScale: CGFloat) { - var newValue = yScale - - if newValue == 0.0 - { - newValue = CGFloat.greatestFiniteMagnitude - } - - _maxScaleY = newValue - - limitTransAndScale(matrix: &_touchMatrix, content: _contentRect) + maxScaleY = yScale == 0 ? .greatestFiniteMagnitude : yScale + limitTransAndScale(matrix: &touchMatrix, content: contentRect) } @objc open func setMinMaxScaleY(minScaleY: CGFloat, maxScaleY: CGFloat) { - var minScaleY = minScaleY, maxScaleY = maxScaleY - - if minScaleY < 1.0 - { - minScaleY = 1.0 - } - - if maxScaleY == 0.0 - { - maxScaleY = CGFloat.greatestFiniteMagnitude - } - - _minScaleY = minScaleY - _maxScaleY = maxScaleY - - limitTransAndScale(matrix: &_touchMatrix, content: _contentRect) - } - @objc open var touchMatrix: CGAffineTransform - { - return _touchMatrix + self.minScaleY = max(minScaleY, 1) + self.maxScaleY = maxScaleY == 0 ? .greatestFiniteMagnitude : maxScaleY + limitTransAndScale(matrix: &touchMatrix, content: contentRect) } - + // MARK: - Boundaries Check @objc open func isInBoundsX(_ x: CGFloat) -> Bool @@ -423,74 +331,26 @@ open class ViewPortHandler: NSObject @objc open func isInBoundsLeft(_ x: CGFloat) -> Bool { - return _contentRect.origin.x <= x + 1.0 + return contentRect.origin.x <= x + 1.0 } @objc open func isInBoundsRight(_ x: CGFloat) -> Bool { let x = floor(x * 100.0) / 100.0 - return (_contentRect.origin.x + _contentRect.size.width) >= x - 1.0 + return (contentRect.origin.x + contentRect.size.width) >= x - 1.0 } @objc open func isInBoundsTop(_ y: CGFloat) -> Bool { - return _contentRect.origin.y <= y + return contentRect.origin.y <= y } @objc open func isInBoundsBottom(_ y: CGFloat) -> Bool { let normalizedY = floor(y * 100.0) / 100.0 - return (_contentRect.origin.y + _contentRect.size.height) >= normalizedY - } - - /// - returns: The current x-scale factor - @objc open var scaleX: CGFloat - { - return _scaleX - } - - /// - returns: The current y-scale factor - @objc open var scaleY: CGFloat - { - return _scaleY - } - - /// - returns: The minimum x-scale factor - @objc open var minScaleX: CGFloat - { - return _minScaleX - } - - /// - returns: The minimum y-scale factor - @objc open var minScaleY: CGFloat - { - return _minScaleY - } - - /// - returns: The minimum x-scale factor - @objc open var maxScaleX: CGFloat - { - return _maxScaleX + return (contentRect.origin.y + contentRect.size.height) >= normalizedY } - - /// - returns: The minimum y-scale factor - @objc open var maxScaleY: CGFloat - { - return _maxScaleY - } - - /// - returns: The translation (drag / pan) distance on the x-axis - @objc open var transX: CGFloat - { - return _transX - } - - /// - returns: The translation (drag / pan) distance on the y-axis - @objc open var transY: CGFloat - { - return _transY - } - + /// if the chart is fully zoomed out, return true @objc open var isFullyZoomedOut: Bool { @@ -500,54 +360,54 @@ open class ViewPortHandler: NSObject /// - returns: `true` if the chart is fully zoomed out on it's y-axis (vertical). @objc open var isFullyZoomedOutY: Bool { - return !(_scaleY > _minScaleY || _minScaleY > 1.0) + return !(scaleY > minScaleY || minScaleY > 1.0) } /// - returns: `true` if the chart is fully zoomed out on it's x-axis (horizontal). @objc open var isFullyZoomedOutX: Bool { - return !(_scaleX > _minScaleX || _minScaleX > 1.0) + return !(scaleX > minScaleX || minScaleX > 1.0) } /// Set an offset in pixels that allows the user to drag the chart over it's bounds on the x-axis. @objc open func setDragOffsetX(_ offset: CGFloat) { - _transOffsetX = offset + transOffsetX = offset } /// Set an offset in pixels that allows the user to drag the chart over it's bounds on the y-axis. @objc open func setDragOffsetY(_ offset: CGFloat) { - _transOffsetY = offset + transOffsetY = offset } /// - returns: `true` if both drag offsets (x and y) are zero or smaller. @objc open var hasNoDragOffset: Bool { - return _transOffsetX <= 0.0 && _transOffsetY <= 0.0 + return transOffsetX <= 0.0 && transOffsetY <= 0.0 } /// - returns: `true` if the chart is not yet fully zoomed out on the x-axis @objc open var canZoomOutMoreX: Bool { - return _scaleX > _minScaleX + return scaleX > minScaleX } /// - returns: `true` if the chart is not yet fully zoomed in on the x-axis @objc open var canZoomInMoreX: Bool { - return _scaleX < _maxScaleX + return scaleX < maxScaleX } /// - returns: `true` if the chart is not yet fully zoomed out on the y-axis @objc open var canZoomOutMoreY: Bool { - return _scaleY > _minScaleY + return scaleY > minScaleY } /// - returns: `true` if the chart is not yet fully zoomed in on the y-axis @objc open var canZoomInMoreY: Bool { - return _scaleY < _maxScaleY + return scaleY < maxScaleY } }