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

add animation for data lines and points #18

Closed
wants to merge 5 commits into from
Closed
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
22 changes: 11 additions & 11 deletions Example/SwiftChart/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@
<rect key="frame" x="0.0" y="64" width="600" height="44"/>
<autoresizingMask key="autoresizingMask"/>
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" tableViewCell="81l-tA-PDc" id="K0G-XP-MHb">
<rect key="frame" x="0.0" y="0.0" width="567" height="43.5"/>
<rect key="frame" x="0.0" y="0.0" width="567" height="43"/>
<autoresizingMask key="autoresizingMask"/>
<subviews>
<label opaque="NO" multipleTouchEnabled="YES" contentMode="left" text="Basic Chart" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" id="3Gw-YT-nZG">
<rect key="frame" x="15" y="0.0" width="550" height="43.5"/>
<label opaque="NO" multipleTouchEnabled="YES" contentMode="left" text="Basic Chart (animated)" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" id="3Gw-YT-nZG">
<rect key="frame" x="15" y="0.0" width="550" height="43"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<fontDescription key="fontDescription" type="system" pointSize="16"/>
<color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
Expand All @@ -39,11 +39,11 @@
<rect key="frame" x="0.0" y="108" width="600" height="44"/>
<autoresizingMask key="autoresizingMask"/>
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" tableViewCell="hRS-g3-2Vm" id="sE7-tv-gYf">
<rect key="frame" x="0.0" y="0.0" width="567" height="43.5"/>
<rect key="frame" x="0.0" y="0.0" width="567" height="43"/>
<autoresizingMask key="autoresizingMask"/>
<subviews>
<label opaque="NO" multipleTouchEnabled="YES" contentMode="left" text="Multiple, partially filled series" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" id="I9W-zY-1WJ">
<rect key="frame" x="15" y="0.0" width="550" height="43.5"/>
<rect key="frame" x="15" y="0.0" width="550" height="43"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<fontDescription key="fontDescription" type="system" pointSize="16"/>
<color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
Expand All @@ -56,11 +56,11 @@
<rect key="frame" x="0.0" y="152" width="600" height="44"/>
<autoresizingMask key="autoresizingMask"/>
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" tableViewCell="v8v-Dn-gbi" id="4X6-Y5-Ai1">
<rect key="frame" x="0.0" y="0.0" width="567" height="43.5"/>
<rect key="frame" x="0.0" y="0.0" width="567" height="43"/>
<autoresizingMask key="autoresizingMask"/>
<subviews>
<label opaque="NO" multipleTouchEnabled="YES" contentMode="left" text="Different color above and below zero" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" id="gBx-jT-eLY">
<rect key="frame" x="15" y="0.0" width="550" height="43.5"/>
<rect key="frame" x="15" y="0.0" width="550" height="43"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<fontDescription key="fontDescription" type="system" pointSize="16"/>
<color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
Expand All @@ -73,11 +73,11 @@
<rect key="frame" x="0.0" y="196" width="600" height="44"/>
<autoresizingMask key="autoresizingMask"/>
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" tableViewCell="zDM-kl-h6r" id="Dme-Ni-nuQ">
<rect key="frame" x="0.0" y="0.0" width="567" height="43.5"/>
<rect key="frame" x="0.0" y="0.0" width="567" height="43"/>
<autoresizingMask key="autoresizingMask"/>
<subviews>
<label opaque="NO" multipleTouchEnabled="YES" contentMode="left" text="Specifying x and y values" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" id="TdA-DG-rS3">
<rect key="frame" x="15" y="0.0" width="550" height="43.5"/>
<rect key="frame" x="15" y="0.0" width="550" height="43"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<fontDescription key="fontDescription" type="system" pointSize="16"/>
<color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
Expand All @@ -90,11 +90,11 @@
<rect key="frame" x="0.0" y="240" width="600" height="44"/>
<autoresizingMask key="autoresizingMask"/>
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" tableViewCell="D8X-Tk-VkX" id="7Xc-4n-JT9">
<rect key="frame" x="0.0" y="0.0" width="567" height="43.5"/>
<rect key="frame" x="0.0" y="0.0" width="567" height="43"/>
<autoresizingMask key="autoresizingMask"/>
<subviews>
<label opaque="NO" multipleTouchEnabled="YES" contentMode="left" text="Stock Chart" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" id="H34-jE-uY7">
<rect key="frame" x="15" y="0.0" width="550" height="43.5"/>
<rect key="frame" x="15" y="0.0" width="550" height="43"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<fontDescription key="fontDescription" type="system" pointSize="16"/>
<color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
Expand Down
3 changes: 3 additions & 0 deletions Example/SwiftChart/BasicChartViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,11 @@ class BasicChartViewController: UIViewController, ChartDelegate {
case 0:

// Simple chart
chart.animation.enabled = true

let series = ChartSeries([0, 6, 2, 8, 4, 7, 3, 10, 8])
series.color = ChartColors.greenColor()
series.points = true
chart.addSeries(series)


Expand Down
70 changes: 69 additions & 1 deletion Source/Chart.swift
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,12 @@ public class Chart: UIControl {
*/
@IBInspectable
public var lineWidth: CGFloat = 2

/**
Width of the chart's poinst.
*/
@IBInspectable
public var pointWidth: CGFloat = 2

/**
Delegate for listening to Chart touch events.
Expand Down Expand Up @@ -163,6 +169,12 @@ public class Chart: UIControl {
Alpha component for the area's color.
*/
public var areaAlphaComponent: CGFloat = 0.1

public struct ChartAnimation {
public var enabled: Bool = false
public var duration: CFTimeInterval = 1
}
public var animation: ChartAnimation = ChartAnimation()

// MARK: Private variables

Expand Down Expand Up @@ -291,6 +303,12 @@ public class Chart: UIControl {

if series.line {
drawLine(xValues: scaledXValues, yValues: scaledYValues, seriesIndex: index)
if series.points {
for i in 1..<scaledXValues.count {
drawPoints(xValues: scaledXValues, yValues: scaledYValues, seriesIndex: index, i: i)
}
}

}
if series.area {
drawArea(xValues: scaledXValues, yValues: scaledYValues, seriesIndex: index)
Expand Down Expand Up @@ -454,10 +472,60 @@ public class Chart: UIControl {
self.layer.addSublayer(lineLayer)

layerStore.append(lineLayer)

// animate line drawing
if animation.enabled {
let animateStrokeEnd = CABasicAnimation(keyPath: "strokeEnd")
animateStrokeEnd.duration = animation.duration
animateStrokeEnd.fromValue = 0
animateStrokeEnd.toValue = 1
lineLayer.addAnimation(animateStrokeEnd, forKey: "strokeEnd")
}

return lineLayer
}


private func drawPoints(xValues xValues: Array<Float>, yValues: Array<Float>, seriesIndex: Int, i: Int) -> CAShapeLayer {

let circleLayer = CAShapeLayer()
let circleRadius: CGFloat = 2.0

func circleFrame(x: CGFloat, y: CGFloat) -> CGRect {
var circleFrame = CGRect(x: x, y: y, width: 2*circleRadius, height: 2*circleRadius)
circleFrame.origin.x = x - circleRadius
circleFrame.origin.y = y - circleRadius
return circleFrame
}

func circlePath(i: Int) -> UIBezierPath {
let x = xValues[i]
let y = yValues[i]
return UIBezierPath(ovalInRect: circleFrame(CGFloat(x), y: CGFloat(y)))
}

circleLayer.frame = self.bounds
circleLayer.path = circlePath(i).CGPath

circleLayer.lineWidth = pointWidth
circleLayer.fillColor = series[seriesIndex].colors.above.CGColor
circleLayer.strokeColor = series[seriesIndex].colors.above.CGColor

self.layer.addSublayer(circleLayer)

layerStore.append(circleLayer)

// animate the opacity of the poinst
if animation.enabled {
let animateOpacity = CABasicAnimation(keyPath: "opacity")
animateOpacity.duration = animation.duration
animateOpacity.fromValue = 0
animateOpacity.toValue = 1
circleLayer.addAnimation(animateOpacity, forKey: "opacity")
}

return circleLayer
}

private func drawArea(xValues xValues: Array<Float>, yValues: Array<Float>, seriesIndex: Int) {
let isAboveXAxis = isVerticalSegmentAboveXAxis(yValues)
let area = CGPathCreateMutable()
Expand Down
2 changes: 2 additions & 0 deletions Source/ChartSeries.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public class ChartSeries {
public var data: Array<(x: Float, y: Float)>
public var area: Bool = false
public var line: Bool = true
public var points: Bool = false
public var color: UIColor = ChartColors.blueColor() {
didSet {
colors = (above: color, below: color)
Expand All @@ -38,3 +39,4 @@ public class ChartSeries {
self.data = data.map ({ (Float($0.x), Float($0.y))})
}
}