From 9b588799509f5b39739360930b555df1ebdae6c3 Mon Sep 17 00:00:00 2001 From: pryaniq Date: Thu, 12 Nov 2015 12:02:52 +0300 Subject: [PATCH 1/5] Displayed points for series in the graph. How use: yourSeriesName1.points = true yourSeriesName1.points = false chart.poinstWidth = 2.0 # Conflicts: # Source/Chart.swift # Source/ChartSeries.swift --- Source/Chart.swift | 48 ++++++++++++++++++++++++++++++++++++++-- Source/ChartSeries.swift | 2 ++ 2 files changed, 48 insertions(+), 2 deletions(-) diff --git a/Source/Chart.swift b/Source/Chart.swift index 0a81b5110..59dd28a4d 100644 --- a/Source/Chart.swift +++ b/Source/Chart.swift @@ -122,7 +122,13 @@ public class Chart: UIControl { Width of the chart's lines. */ @IBInspectable - public var lineWidth: CGFloat = 2 + var lineWidth: CGFloat = 2 + + /** + Width of the chart's poinst. + */ + @IBInspectable + var pointWidth: CGFloat = 2 /** Delegate for listening to Chart touch events. @@ -291,6 +297,12 @@ public class Chart: UIControl { if series.line { drawLine(xValues: scaledXValues, yValues: scaledYValues, seriesIndex: index) + if series.points { + for i in 1.., yValues: Array, 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) + + return circleLayer + } + private func drawArea(xValues xValues: Array, yValues: Array, seriesIndex: Int) { let isAboveXAxis = isVerticalSegmentAboveXAxis(yValues) let area = CGPathCreateMutable() diff --git a/Source/ChartSeries.swift b/Source/ChartSeries.swift index 001b48e73..1c8d59a7d 100644 --- a/Source/ChartSeries.swift +++ b/Source/ChartSeries.swift @@ -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 + var points: Bool = true public var color: UIColor = ChartColors.blueColor() { didSet { colors = (above: color, below: color) @@ -38,3 +39,4 @@ public class ChartSeries { self.data = data.map ({ (Float($0.x), Float($0.y))}) } } + From dba49767890150a8643960e630ed3c8d3470eb9b Mon Sep 17 00:00:00 2001 From: Daniel Friedrich Date: Fri, 15 Jan 2016 12:27:45 +0100 Subject: [PATCH 2/5] make points property public --- Source/ChartSeries.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Source/ChartSeries.swift b/Source/ChartSeries.swift index 1c8d59a7d..5ca03ab25 100644 --- a/Source/ChartSeries.swift +++ b/Source/ChartSeries.swift @@ -14,7 +14,7 @@ public class ChartSeries { public var data: Array<(x: Float, y: Float)> public var area: Bool = false public var line: Bool = true - var points: Bool = true + public var points: Bool = false public var color: UIColor = ChartColors.blueColor() { didSet { colors = (above: color, below: color) From 202c6f89b4c89fed6ff5b9bba89ef100eb66031d Mon Sep 17 00:00:00 2001 From: Daniel Friedrich Date: Fri, 15 Jan 2016 12:28:28 +0100 Subject: [PATCH 3/5] add animation of lines and points --- Source/Chart.swift | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/Source/Chart.swift b/Source/Chart.swift index 59dd28a4d..81fae38e3 100644 --- a/Source/Chart.swift +++ b/Source/Chart.swift @@ -122,13 +122,13 @@ public class Chart: UIControl { Width of the chart's lines. */ @IBInspectable - var lineWidth: CGFloat = 2 + public var lineWidth: CGFloat = 2 /** Width of the chart's poinst. */ @IBInspectable - var pointWidth: CGFloat = 2 + public var pointWidth: CGFloat = 2 /** Delegate for listening to Chart touch events. @@ -169,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 = true + public var duration: CFTimeInterval = 1 + } + public var animation: ChartAnimation = ChartAnimation() // MARK: Private variables @@ -466,6 +472,15 @@ 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 } @@ -499,6 +514,15 @@ public class Chart: UIControl { 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 } From af14e3c1ad7b6416bc3306ab6bf059f74a45386a Mon Sep 17 00:00:00 2001 From: Daniel Friedrich Date: Fri, 15 Jan 2016 12:28:51 +0100 Subject: [PATCH 4/5] use animation in basic example --- Example/SwiftChart/Base.lproj/Main.storyboard | 22 +++++++++---------- .../SwiftChart/BasicChartViewController.swift | 3 +++ 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/Example/SwiftChart/Base.lproj/Main.storyboard b/Example/SwiftChart/Base.lproj/Main.storyboard index 7966db591..3af82243e 100644 --- a/Example/SwiftChart/Base.lproj/Main.storyboard +++ b/Example/SwiftChart/Base.lproj/Main.storyboard @@ -22,11 +22,11 @@ - + -