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

Update SwiftChart with Animations #10

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
74 changes: 66 additions & 8 deletions Chart/Chart.swift
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ protocol ChartDelegate {

:param: chart The chart that has been touched.
:param: indexes Each element of this array contains the index of the data that has been touched, one for each series.
If the series hasn't been touched, its index will be nil.
If the series hasn't been touched, its index will be nil.
:param: x The value on the x-axis that has been touched.
:param: left The distance from the left side of the chart.

Expand All @@ -29,6 +29,14 @@ protocol ChartDelegate {

*/
func didFinishTouchingChart(chart: Chart)

/**
Allows different animation's duration for each serie.

:param: serieIndex Index of the serie

*/
func animationDurationForSerieAtIndex(serieIndex: Int) -> CFTimeInterval
}

/**
Expand Down Expand Up @@ -164,6 +172,16 @@ class Chart: UIControl {
*/
var areaAlphaComponent: CGFloat = 0.1

/**
Whether the chart should animate or not, default is true.
*/
var animate: Bool = true

/**
Set the time for the animations. It's overridden by the delegate's method
*/
var animationDuration: CFTimeInterval?

// MARK: Private variables

private var highlightShapeLayer: CAShapeLayer!
Expand Down Expand Up @@ -217,7 +235,7 @@ class Chart: UIControl {
addSeries(s)
}
}

/**
Remove the series at the specified index.
*/
Expand Down Expand Up @@ -258,7 +276,7 @@ class Chart: UIControl {
}

private func drawChart() {

drawingHeight = bounds.height - bottomInset - topInset
drawingWidth = bounds.width

Expand Down Expand Up @@ -444,6 +462,11 @@ class Chart: UIControl {
lineLayer.lineWidth = lineWidth
lineLayer.lineJoin = kCALineJoinBevel

// Animating lines
if animate {
animateDrawingLine(layer: lineLayer, index: seriesIndex)
}

self.layer.addSublayer(lineLayer)

layerStore.append(lineLayer)
Expand Down Expand Up @@ -476,6 +499,11 @@ class Chart: UIControl {
}
areaLayer.lineWidth = 0

// Animate areas
if animate {
animateDrawingArea(layer: areaLayer, color: areaLayer.fillColor, index: seriesIndex)
}

self.layer.addSublayer(areaLayer)

layerStore.append(areaLayer)
Expand Down Expand Up @@ -539,7 +567,7 @@ class Chart: UIControl {

for (i, value) in enumerate(scaled) {
let x = CGFloat(value)


// Add vertical grid for each label, except axes on the left and right

Expand All @@ -548,7 +576,7 @@ class Chart: UIControl {
CGContextAddLineToPoint(context, x, bounds.height)
CGContextStrokePath(context)
}

if x == drawingWidth {
// Do not add label at the most right position
continue
Expand Down Expand Up @@ -642,6 +670,38 @@ class Chart: UIControl {

}

// MARK: - Animations

func animateDrawingLine(#layer: CAShapeLayer, index: Int) {
let animation = CABasicAnimation(keyPath: "strokeEnd")
animation.fromValue = 0
animation.toValue = 1
animation.duration = delegate!.animationDurationForSerieAtIndex(index) ?? animationDuration ?? 1.2

animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseOut)
animation.fillMode = kCAFillModeBoth
animation.removedOnCompletion = false

layer.strokeStart = 0
layer.strokeEnd = 1
layer.addAnimation(animation, forKey: animation.keyPath)

}

func animateDrawingArea(#layer: CAShapeLayer, color: CGColorRef, index: Int) {
let animation = CABasicAnimation(keyPath: "fillColor")
animation.fromValue = UIColor.clearColor().CGColor
animation.toValue = color
animation.duration = delegate!.animationDurationForSerieAtIndex(index) ?? animationDuration ?? 1.2

animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
animation.fillMode = kCAFillModeBoth
animation.removedOnCompletion = false

layer.addAnimation(animation, forKey: animation.keyPath)

}

// MARK: - Touch events

private func drawHighlightLineFromLeftPosition(left: CGFloat) {
Expand Down Expand Up @@ -788,6 +848,4 @@ class Chart: UIControl {
private class func intersectionOnXAxisBetween(p1: ChartPoint, and p2: ChartPoint) -> ChartPoint {
return (x: p1.x - (p2.x - p1.x) / (p2.y - p1.y) * p1.y, y: 0)
}
}


}
Empty file modified Chart/ChartColors.swift
100644 → 100755
Empty file.
3 changes: 1 addition & 2 deletions Chart/ChartSeries.swift
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,4 @@ class ChartSeries {
init(data: Array<(x: Double, y: Double)>) {
self.data = data.map ({ (Float($0.x), Float($0.y))})
}
}

}
Loading