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

Utils Cleanup #3054

Merged
merged 24 commits into from
Jan 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
0cb56dd
Cleanup
jjatie Nov 25, 2017
35c7a6c
Merge branch 'master' into utils-cleanup
jjatie Nov 29, 2017
d732523
Merge branch 'master' into utils-cleanup
jjatie Nov 29, 2017
726814b
Merge remote-tracking branch 'upstream/master' into utils-cleanup
jjatie Dec 8, 2017
8c1f8c9
Pulled latest master
jjatie Dec 8, 2017
e3ad45f
Merge remote-tracking branch 'origin/utils-cleanup' into utils-cleanup
jjatie Dec 8, 2017
52c4978
Pulled latest master
jjatie Dec 8, 2017
5193596
Merge remote-tracking branch 'upstream/master' into utils-cleanup
jjatie Dec 11, 2017
8af6110
Pulled latest master
jjatie Dec 11, 2017
dbbadd3
Fix after pulling master
jjatie Dec 11, 2017
88f1811
Fixed using wrong axis (Issue #2257)
defranke Dec 18, 2017
7238ba9
fix #1830. credit from https://github.com/danielgindi/Charts/pull/204…
liuxuan30 Dec 25, 2017
64bc2d1
Merge pull request #3114 from defranke/HorizontalBarChart-Axis
liuxuan30 Dec 25, 2017
c8ae3ea
Removed redundant ivars in BarLineChartViewBase (#3043)
jjatie Dec 25, 2017
bbeb88d
Update 4.0.0 with master (#3135)
jjatie Dec 25, 2017
1658a63
Replaced relevant `ChartUtils` methods with `Double` extensions (#2994)
jjatie Dec 28, 2017
48ac6a6
add option to build demo projects unit tests on iOS (#3121)
liuxuan30 Dec 28, 2017
faf7ed5
Merge branch 'master' of https://github.com/danielgindi/Charts into u…
jjatie Dec 28, 2017
9706fa1
Update ViewPortHandler.swift (#3143)
ParkinWu Jan 1, 2018
0ced631
Refactored ChartUtils method into CGPoint extension (#3087)
jjatie Jan 3, 2018
32772e0
Finished cleanup
jjatie Jan 5, 2018
19f52d9
Merge remote-tracking branch 'upstream/master' into utils-cleanup
jjatie Jan 5, 2018
85fb250
Pulled master
jjatie Jan 5, 2018
e4b177b
Synced with 4.0.0
jjatie Jan 8, 2018
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
174 changes: 49 additions & 125 deletions Source/Charts/Utils/Fill.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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(
Expand All @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -287,7 +214,7 @@ open class Fill: NSObject
)

context.clip()
context.drawLinearGradient(_gradient!,
context.drawLinearGradient(gradient!,
start: startPoint,
end: endPoint,
options: [.drawsAfterEndLocation, .drawsBeforeStartLocation]
Expand All @@ -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()
}

}
49 changes: 19 additions & 30 deletions Source/Charts/Utils/Transformer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,50 +17,50 @@ 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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this a chained call? how about putting in on line. seems a little distractive

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a fairly standard Swift practice.

.translatedBy(x: CGFloat(-chartXMin), y: CGFloat(-chartYMin))
}

/// Prepares the matrix that contains all offsets.
@objc open func prepareMatrixOffset(inverted: Bool)
{
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)
}
}

Expand All @@ -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)
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
)
}

Expand Down
Loading