From 92f0dda0eb1074a168f6d851dab969a8e8d2e411 Mon Sep 17 00:00:00 2001 From: Regno Date: Mon, 4 Jan 2016 17:07:03 +0500 Subject: [PATCH 1/6] Add drawGradientEnable parameter --- Charts/Classes/Data/LineChartDataSet.swift | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Charts/Classes/Data/LineChartDataSet.swift b/Charts/Classes/Data/LineChartDataSet.swift index eb308c4406..aa293f4903 100644 --- a/Charts/Classes/Data/LineChartDataSet.swift +++ b/Charts/Classes/Data/LineChartDataSet.swift @@ -35,6 +35,9 @@ public class LineChartDataSet: LineRadarChartDataSet /// if true, cubic lines are drawn instead of linear public var drawCubicEnabled = false + /// if true, gradient colors are drawn + public var drawGradientEnabled = false + public var drawCircleHoleEnabled = true public required init() @@ -103,6 +106,8 @@ public class LineChartDataSet: LineRadarChartDataSet public var isDrawCubicEnabled: Bool { return drawCubicEnabled; } + public var isdrawGradientEnabled: Bool { return drawGradientEnabled; } + public var isDrawCircleHoleEnabled: Bool { return drawCircleHoleEnabled; } /// Sets a custom FillFormatter to the chart that handles the position of the filled-line for each DataSet. Set this to null to use the default logic. @@ -137,6 +142,7 @@ public class LineChartDataSet: LineRadarChartDataSet copy.lineDashLengths = lineDashLengths copy.drawCirclesEnabled = drawCirclesEnabled copy.drawCubicEnabled = drawCubicEnabled + copy.drawGradientEnabled = drawGradientEnabled return copy } } From dfb1b851f45122cee416fea880b9fb47b783ee56 Mon Sep 17 00:00:00 2001 From: Regno Date: Mon, 4 Jan 2016 17:38:39 +0500 Subject: [PATCH 2/6] Add gragientPosition parameter --- Charts/Classes/Data/LineChartDataSet.swift | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Charts/Classes/Data/LineChartDataSet.swift b/Charts/Classes/Data/LineChartDataSet.swift index aa293f4903..cdee7d82ae 100644 --- a/Charts/Classes/Data/LineChartDataSet.swift +++ b/Charts/Classes/Data/LineChartDataSet.swift @@ -26,6 +26,8 @@ public class LineChartDataSet: LineRadarChartDataSet public var lineDashPhase = CGFloat(0.0) public var lineDashLengths: [CGFloat]! + public var gradientPositions = [CGFloat]() + /// formatter for customizing the position of the fill-line private var _fillFormatter: ChartFillFormatter = BarLineChartFillFormatter() From 4a31bf1bdd3514455786bb13446de3cd3918f5b7 Mon Sep 17 00:00:00 2001 From: Regno Date: Mon, 4 Jan 2016 17:59:54 +0500 Subject: [PATCH 3/6] Add property's functions --- Charts/Classes/Data/LineChartDataSet.swift | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/Charts/Classes/Data/LineChartDataSet.swift b/Charts/Classes/Data/LineChartDataSet.swift index cdee7d82ae..4ba6d966cb 100644 --- a/Charts/Classes/Data/LineChartDataSet.swift +++ b/Charts/Classes/Data/LineChartDataSet.swift @@ -108,7 +108,7 @@ public class LineChartDataSet: LineRadarChartDataSet public var isDrawCubicEnabled: Bool { return drawCubicEnabled; } - public var isdrawGradientEnabled: Bool { return drawGradientEnabled; } + public var isDrawGradientEnabled: Bool { return drawGradientEnabled; } public var isDrawCircleHoleEnabled: Bool { return drawCircleHoleEnabled; } @@ -132,6 +132,25 @@ public class LineChartDataSet: LineRadarChartDataSet } } + public func resetGradientPositions() + { + gradientPositions.removeAll(keepCapacity: false) + } + + public func addGradientPositions(position: CGFloat) + { + gradientPositions.append(position) + } + + public func gradientPositionAt(var index: Int) -> CGFloat + { + if (index < 0) + { + index = 0 + } + return gradientPositions[index % gradientPositions.count] + } + // MARK: NSCopying public override func copyWithZone(zone: NSZone) -> AnyObject From daee3d4fa512de6afd61026951885960496b11e2 Mon Sep 17 00:00:00 2001 From: Regno Date: Mon, 4 Jan 2016 18:02:52 +0500 Subject: [PATCH 4/6] Add drawCubicGradient function --- .../Classes/Renderers/LineChartRenderer.swift | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/Charts/Classes/Renderers/LineChartRenderer.swift b/Charts/Classes/Renderers/LineChartRenderer.swift index d3da538684..b11a6e82c6 100644 --- a/Charts/Classes/Renderers/LineChartRenderer.swift +++ b/Charts/Classes/Renderers/LineChartRenderer.swift @@ -188,6 +188,11 @@ public class LineChartRenderer: LineScatterCandleRadarChartRenderer drawCubicFill(context: context, dataSet: dataSet, spline: cubicPath, matrix: valueToPixelMatrix, from: minx, to: size) } + if (dataSet.isDrawGradientEnabled) + { + drawCubicGradient(context: context, dataSet: dataSet, spline: cubicPath, matrix: valueToPixelMatrix) + } + CGContextBeginPath(context) CGContextAddPath(context, cubicPath) CGContextSetStrokeColorWithColor(context, drawingColor.CGColor) @@ -227,6 +232,47 @@ public class LineChartRenderer: LineScatterCandleRadarChartRenderer CGContextRestoreGState(context) } + internal func drawCubicGradient(context context: CGContext, dataSet: LineChartDataSet, spline: CGMutablePath, matrix: CGAffineTransform) + { + CGContextSaveGState(context) + let strokedArc = CGPathCreateCopyByStrokingPath(spline, nil, dataSet.lineWidth, .Butt, .Miter, 10) + CGContextAddPath(context, strokedArc) + CGContextDrawPath(context, .Fill) + var gradientColors : [CGFloat] = [] + for (var i = 0; i < dataSet.colors.count; i++) { + let cColor = dataSet.colorAt(i) + var cRed : CGFloat = 0 + var cGreen : CGFloat = 0 + var cBlue : CGFloat = 0 + var cAlpha : CGFloat = 0 + if cColor.getRed(&cRed, green: &cGreen, blue: &cBlue, alpha: &cAlpha) { + gradientColors.append(cRed) + gradientColors.append(cGreen) + gradientColors.append(cBlue) + gradientColors.append(cAlpha) + } + } + var baseSpace = CGColorSpaceCreateDeviceRGB() + + var locations : [CGFloat] = [] + for (var i = 0; i < dataSet.gradientPositions.count; i++) { + locations.append((dataSet.gradientPositionAt(i) - dataSet.gradientPositionAt(0)) / (dataSet.gradientPositionAt(dataSet.gradientPositions.count - 1) - dataSet.gradientPositionAt(0) )) + } + var gradient = CGGradientCreateWithColorComponents(baseSpace, gradientColors, locations, locations.count) + baseSpace = nil + CGContextBeginPath(context) + CGContextAddPath(context, strokedArc) + CGContextClip(context) + var startPoint = CGPointMake(0, dataSet.gradientPositionAt(0)) + var endPoint = CGPointMake(0, dataSet.gradientPositionAt(dataSet.gradientPositions.count - 1)) + + startPoint = CGPointApplyAffineTransform(startPoint, matrix) + endPoint = CGPointApplyAffineTransform(endPoint, matrix) + CGContextDrawLinearGradient(context, gradient, startPoint, endPoint, CGGradientDrawingOptions(rawValue: 0)) + gradient = nil + CGContextRestoreGState(context) + } + private var _lineSegments = [CGPoint](count: 2, repeatedValue: CGPoint()) internal func drawLinear(context context: CGContext, dataSet: LineChartDataSet, entries: [ChartDataEntry]) From 9a510fc77f0784f29e6b455032dcbfa7425da402 Mon Sep 17 00:00:00 2001 From: Regno Date: Tue, 5 Jan 2016 14:39:29 +0500 Subject: [PATCH 5/6] Add drawGradientLine function --- .../Classes/Renderers/LineChartRenderer.swift | 154 ++++++++++++------ 1 file changed, 105 insertions(+), 49 deletions(-) diff --git a/Charts/Classes/Renderers/LineChartRenderer.swift b/Charts/Classes/Renderers/LineChartRenderer.swift index b11a6e82c6..dbaa964a9c 100644 --- a/Charts/Classes/Renderers/LineChartRenderer.swift +++ b/Charts/Classes/Renderers/LineChartRenderer.swift @@ -190,14 +190,13 @@ public class LineChartRenderer: LineScatterCandleRadarChartRenderer if (dataSet.isDrawGradientEnabled) { - drawCubicGradient(context: context, dataSet: dataSet, spline: cubicPath, matrix: valueToPixelMatrix) + drawGradientLine(context: context, dataSet: dataSet, spline: cubicPath, matrix: valueToPixelMatrix) + } else { + CGContextBeginPath(context) + CGContextAddPath(context, cubicPath) + CGContextSetStrokeColorWithColor(context, drawingColor.CGColor) + CGContextStrokePath(context) } - - CGContextBeginPath(context) - CGContextAddPath(context, cubicPath) - CGContextSetStrokeColorWithColor(context, drawingColor.CGColor) - CGContextStrokePath(context) - CGContextRestoreGState(context) } @@ -232,47 +231,6 @@ public class LineChartRenderer: LineScatterCandleRadarChartRenderer CGContextRestoreGState(context) } - internal func drawCubicGradient(context context: CGContext, dataSet: LineChartDataSet, spline: CGMutablePath, matrix: CGAffineTransform) - { - CGContextSaveGState(context) - let strokedArc = CGPathCreateCopyByStrokingPath(spline, nil, dataSet.lineWidth, .Butt, .Miter, 10) - CGContextAddPath(context, strokedArc) - CGContextDrawPath(context, .Fill) - var gradientColors : [CGFloat] = [] - for (var i = 0; i < dataSet.colors.count; i++) { - let cColor = dataSet.colorAt(i) - var cRed : CGFloat = 0 - var cGreen : CGFloat = 0 - var cBlue : CGFloat = 0 - var cAlpha : CGFloat = 0 - if cColor.getRed(&cRed, green: &cGreen, blue: &cBlue, alpha: &cAlpha) { - gradientColors.append(cRed) - gradientColors.append(cGreen) - gradientColors.append(cBlue) - gradientColors.append(cAlpha) - } - } - var baseSpace = CGColorSpaceCreateDeviceRGB() - - var locations : [CGFloat] = [] - for (var i = 0; i < dataSet.gradientPositions.count; i++) { - locations.append((dataSet.gradientPositionAt(i) - dataSet.gradientPositionAt(0)) / (dataSet.gradientPositionAt(dataSet.gradientPositions.count - 1) - dataSet.gradientPositionAt(0) )) - } - var gradient = CGGradientCreateWithColorComponents(baseSpace, gradientColors, locations, locations.count) - baseSpace = nil - CGContextBeginPath(context) - CGContextAddPath(context, strokedArc) - CGContextClip(context) - var startPoint = CGPointMake(0, dataSet.gradientPositionAt(0)) - var endPoint = CGPointMake(0, dataSet.gradientPositionAt(dataSet.gradientPositions.count - 1)) - - startPoint = CGPointApplyAffineTransform(startPoint, matrix) - endPoint = CGPointApplyAffineTransform(endPoint, matrix) - CGContextDrawLinearGradient(context, gradient, startPoint, endPoint, CGGradientDrawingOptions(rawValue: 0)) - gradient = nil - CGContextRestoreGState(context) - } - private var _lineSegments = [CGPoint](count: 2, repeatedValue: CGPoint()) internal func drawLinear(context context: CGContext, dataSet: LineChartDataSet, entries: [ChartDataEntry]) @@ -379,7 +337,16 @@ public class LineChartRenderer: LineScatterCandleRadarChartRenderer { drawLinearFill(context: context, dataSet: dataSet, entries: entries, minx: minx, maxx: maxx, trans: trans) } - } + if (dataSet.isDrawGradientEnabled) { + let path = generatePath( + entries, + fillMin: dataSet.fillFormatter?.getFillLinePosition(dataSet: dataSet, dataProvider: dataProvider!) ?? 0.0, + from: minx, + to: maxx, + matrix: trans.valueToPixelMatrix) + + drawGradientLine(context: context, dataSet: dataSet, spline: path, matrix: valueToPixelMatrix)} + } internal func drawLinearFill(context context: CGContext, dataSet: LineChartDataSet, entries: [ChartDataEntry], minx: Int, maxx: Int, trans: ChartTransformer) { @@ -430,6 +397,24 @@ public class LineChartRenderer: LineScatterCandleRadarChartRenderer return filled } + /// Generates the path that is used for gradient drawing. + private func generatePath(entries: [ChartDataEntry], fillMin: CGFloat, from: Int, to: Int, var matrix: CGAffineTransform) -> CGPath + { + let phaseX = _animator.phaseX + let phaseY = _animator.phaseY + + let filled = CGPathCreateMutable() + CGPathMoveToPoint(filled, &matrix, CGFloat(entries[from].xIndex), CGFloat(entries[from].value) * phaseY) + + // create a new path + for (var x = from + 1, count = Int(ceil(CGFloat(to - from) * phaseX + CGFloat(from))); x < count; x++) + { + let e = entries[x] + CGPathAddLineToPoint(filled, &matrix, CGFloat(e.xIndex), CGFloat(e.value) * phaseY) + } + return filled + } + public override func drawValues(context context: CGContext) { guard let dataProvider = dataProvider, lineData = dataProvider.lineData else { return } @@ -642,4 +627,75 @@ public class LineChartRenderer: LineScatterCandleRadarChartRenderer CGContextRestoreGState(context) } + + internal func drawGradientLine(context context: CGContext, dataSet: LineChartDataSet, spline: CGPath, matrix: CGAffineTransform) + { + CGContextSaveGState(context) + let gradientPath = CGPathCreateCopyByStrokingPath(spline, nil, dataSet.lineWidth, .Butt, .Miter, 10) + CGContextAddPath(context, gradientPath) + CGContextDrawPath(context, .Fill) + + let boundingBox = CGPathGetBoundingBox(gradientPath); + let gradientStart = CGPointMake(0, CGRectGetMaxY(boundingBox)); + let gradientEnd = CGPointMake(0, CGRectGetMinY(boundingBox)); + var gradientLocations : [CGFloat] = [] + var gradientColors : [CGFloat] = [] + var cRed : CGFloat = 0 + var cGreen : CGFloat = 0 + var cBlue : CGFloat = 0 + var cAlpha : CGFloat = 0 + + //Set lower bound color + gradientLocations.append(0) + var cColor = dataSet.colorAt(0) + if cColor.getRed(&cRed, green: &cGreen, blue: &cBlue, alpha: &cAlpha) { + gradientColors += [cRed, cGreen, cBlue, cAlpha] + } + + //Set middle colors + for (var i = 0; i < dataSet.gradientPositions.count; i++) { + var positionLocation = CGPointMake(0, dataSet.gradientPositionAt(i)) + positionLocation = CGPointApplyAffineTransform(positionLocation, matrix) + let normPositionLocation = (positionLocation.y - gradientStart.y) / (gradientEnd.y - gradientStart.y) + if (normPositionLocation < 0) { + gradientLocations.append(0) + } else if (normPositionLocation > 1) { + gradientLocations.append(1) + } else { + gradientLocations.append(normPositionLocation) + } + } + for (var i = 0; i < dataSet.colors.count; i++) { + cColor = dataSet.colorAt(i) + if cColor.getRed(&cRed, green: &cGreen, blue: &cBlue, alpha: &cAlpha) { + gradientColors += [cRed, cGreen, cBlue, cAlpha] + } + } + + //Set upper bound color + gradientLocations.append(1) + cColor = dataSet.colorAt(dataSet.colors.count - 1) + if cColor.getRed(&cRed, green: &cGreen, blue: &cBlue, alpha: &cAlpha) { + gradientColors += [cRed, cGreen, cBlue, cAlpha] + } + + //Define gradient + var baseSpace = CGColorSpaceCreateDeviceRGB() + var gradient : CGGradient? + if (dataSet.gradientPositions.count > 1) { + gradient = CGGradientCreateWithColorComponents(baseSpace, gradientColors, gradientLocations, gradientColors.count / 4) + } else { + gradient = CGGradientCreateWithColorComponents(baseSpace, gradientColors, nil, gradientColors.count / 4) + } + baseSpace = nil + + //Draw gradient path + CGContextBeginPath(context) + CGContextAddPath(context, gradientPath) + CGContextClip(context) + CGContextDrawLinearGradient(context, gradient, gradientStart, gradientEnd, CGGradientDrawingOptions(rawValue: 0)) + gradient = nil + CGContextRestoreGState(context) + } + } \ No newline at end of file From 25bd262fbb1dfb72e288438a3d6e9c0794e32acb Mon Sep 17 00:00:00 2001 From: Regno Date: Sat, 23 Jan 2016 15:49:01 +0500 Subject: [PATCH 6/6] Rebased to fix conflicts with master. --- .../Realm/RealmLineDataSet.swift | 9 ++++++ .../Standard/LineChartDataSet.swift | 28 ++++++------------- .../Data/Interfaces/ILineChartDataSet.swift | 9 ++++++ .../Classes/Renderers/LineChartRenderer.swift | 27 ++++++++++-------- 4 files changed, 43 insertions(+), 30 deletions(-) diff --git a/Charts/Classes/Data/Implementations/Realm/RealmLineDataSet.swift b/Charts/Classes/Data/Implementations/Realm/RealmLineDataSet.swift index 04835b1e5a..344d93e3b3 100644 --- a/Charts/Classes/Data/Implementations/Realm/RealmLineDataSet.swift +++ b/Charts/Classes/Data/Implementations/Realm/RealmLineDataSet.swift @@ -59,6 +59,12 @@ public class RealmLineDataSet: RealmLineRadarDataSet, ILineChartDataSet /// - returns: true if drawing cubic lines is enabled, false if not. public var isDrawCubicEnabled: Bool { return drawCubicEnabled } + /// If true, gradient lines are drawn instead of solid + public var drawGradientEnabled = false + + /// - returns: true if drawing gradeint lines is enabled, false if not. + public var isDrawGradientEnabled: Bool { return drawGradientEnabled } + /// The radius of the drawn circles. public var circleRadius = CGFloat(8.0) @@ -114,6 +120,9 @@ public class RealmLineDataSet: RealmLineRadarDataSet, ILineChartDataSet /// [1, 3, 4, 2] will paint [- ---- - ---- ] public var lineDashLengths: [CGFloat]? + /// This is the points where gradient should change color + public var gradientPositions: [CGFloat]? + /// formatter for customizing the position of the fill-line private var _fillFormatter: ChartFillFormatter = BarLineChartFillFormatter() diff --git a/Charts/Classes/Data/Implementations/Standard/LineChartDataSet.swift b/Charts/Classes/Data/Implementations/Standard/LineChartDataSet.swift index 58d0d497fb..c3a1df2179 100644 --- a/Charts/Classes/Data/Implementations/Standard/LineChartDataSet.swift +++ b/Charts/Classes/Data/Implementations/Standard/LineChartDataSet.swift @@ -70,6 +70,12 @@ public class LineChartDataSet: LineRadarChartDataSet, ILineChartDataSet /// - returns: true if drawing cubic lines is enabled, false if not. public var isDrawCubicEnabled: Bool { return drawCubicEnabled } + /// If true, gradient lines are drawn instead of solid + public var drawGradientEnabled = false + + /// - returns: true if drawing gradeint lines is enabled, false if not. + public var isDrawGradientEnabled: Bool { return drawGradientEnabled } + /// The radius of the drawn circles. public var circleRadius = CGFloat(8.0) @@ -125,6 +131,9 @@ public class LineChartDataSet: LineRadarChartDataSet, ILineChartDataSet /// [1, 3, 4, 2] will paint [- ---- - ---- ] public var lineDashLengths: [CGFloat]? + /// This is the points where gradient should change color + public var gradientPositions: [CGFloat]? + /// formatter for customizing the position of the fill-line private var _fillFormatter: ChartFillFormatter = BarLineChartFillFormatter() @@ -149,25 +158,6 @@ public class LineChartDataSet: LineRadarChartDataSet, ILineChartDataSet } } - public func resetGradientPositions() - { - gradientPositions.removeAll(keepCapacity: false) - } - - public func addGradientPositions(position: CGFloat) - { - gradientPositions.append(position) - } - - public func gradientPositionAt(var index: Int) -> CGFloat - { - if (index < 0) - { - index = 0 - } - return gradientPositions[index % gradientPositions.count] - } - // MARK: NSCopying public override func copyWithZone(zone: NSZone) -> AnyObject diff --git a/Charts/Classes/Data/Interfaces/ILineChartDataSet.swift b/Charts/Classes/Data/Interfaces/ILineChartDataSet.swift index f6fb8312f1..e0243a9318 100644 --- a/Charts/Classes/Data/Interfaces/ILineChartDataSet.swift +++ b/Charts/Classes/Data/Interfaces/ILineChartDataSet.swift @@ -31,6 +31,12 @@ public protocol ILineChartDataSet: ILineRadarChartDataSet /// - returns: true if drawing cubic lines is enabled, false if not. var isDrawCubicEnabled: Bool { get } + /// If true, gradient lines are drawn instead of solid + var drawGradientEnabled: Bool { get set } + + /// - returns: true if drawing gradeint lines is enabled, false if not. + var isDrawGradientEnabled: Bool { get } + /// The radius of the drawn circles. var circleRadius: CGFloat { get set } @@ -70,6 +76,9 @@ public protocol ILineChartDataSet: ILineRadarChartDataSet /// [1, 3, 4, 2] will paint [- ---- - ---- ] var lineDashLengths: [CGFloat]? { get set } + /// This is the points where gradient should change color + var gradientPositions: [CGFloat]? { get set } + /// Sets a custom FillFormatter to the chart that handles the position of the filled-line for each DataSet. Set this to null to use the default logic. var fillFormatter: ChartFillFormatter? { get set } } diff --git a/Charts/Classes/Renderers/LineChartRenderer.swift b/Charts/Classes/Renderers/LineChartRenderer.swift index 7ca903531a..35ab72437c 100644 --- a/Charts/Classes/Renderers/LineChartRenderer.swift +++ b/Charts/Classes/Renderers/LineChartRenderer.swift @@ -358,7 +358,7 @@ public class LineChartRenderer: LineScatterCandleRadarChartRenderer } if (dataSet.isDrawGradientEnabled) { let path = generatePath( - entries, + dataSet: dataSet, fillMin: dataSet.fillFormatter?.getFillLinePosition(dataSet: dataSet, dataProvider: dataProvider!) ?? 0.0, from: minx, to: maxx, @@ -428,21 +428,26 @@ public class LineChartRenderer: LineScatterCandleRadarChartRenderer } /// Generates the path that is used for gradient drawing. - private func generatePath(entries: [ChartDataEntry], fillMin: CGFloat, from: Int, to: Int, var matrix: CGAffineTransform) -> CGPath + private func generatePath(dataSet dataSet: ILineChartDataSet, fillMin: CGFloat, from: Int, to: Int, var matrix: CGAffineTransform) -> CGPath { let phaseX = _animator.phaseX let phaseY = _animator.phaseY - let filled = CGPathCreateMutable() - CGPathMoveToPoint(filled, &matrix, CGFloat(entries[from].xIndex), CGFloat(entries[from].value) * phaseY) + var e: ChartDataEntry! + let generatedPath = CGPathCreateMutable() + e = dataSet.entryForIndex(from) + if e != nil + { + CGPathMoveToPoint(generatedPath, &matrix, CGFloat(e.xIndex), CGFloat(e.value) * phaseY) + } // create a new path for (var x = from + 1, count = Int(ceil(CGFloat(to - from) * phaseX + CGFloat(from))); x < count; x++) { - let e = entries[x] - CGPathAddLineToPoint(filled, &matrix, CGFloat(e.xIndex), CGFloat(e.value) * phaseY) + guard let e = dataSet.entryForIndex(x) else { continue } + CGPathAddLineToPoint(generatedPath, &matrix, CGFloat(e.xIndex), CGFloat(e.value) * phaseY) } - return filled + return generatedPath } public override func drawValues(context context: CGContext) @@ -668,7 +673,7 @@ public class LineChartRenderer: LineScatterCandleRadarChartRenderer CGContextRestoreGState(context) } - internal func drawGradientLine(context context: CGContext, dataSet: LineChartDataSet, spline: CGPath, matrix: CGAffineTransform) + internal func drawGradientLine(context context: CGContext, dataSet: ILineChartDataSet, spline: CGPath, matrix: CGAffineTransform) { CGContextSaveGState(context) let gradientPath = CGPathCreateCopyByStrokingPath(spline, nil, dataSet.lineWidth, .Butt, .Miter, 10) @@ -693,8 +698,8 @@ public class LineChartRenderer: LineScatterCandleRadarChartRenderer } //Set middle colors - for (var i = 0; i < dataSet.gradientPositions.count; i++) { - var positionLocation = CGPointMake(0, dataSet.gradientPositionAt(i)) + for (var i = 0; i < dataSet.gradientPositions!.count; i++) { + var positionLocation = CGPointMake(0, dataSet.gradientPositions![i]) positionLocation = CGPointApplyAffineTransform(positionLocation, matrix) let normPositionLocation = (positionLocation.y - gradientStart.y) / (gradientEnd.y - gradientStart.y) if (normPositionLocation < 0) { @@ -722,7 +727,7 @@ public class LineChartRenderer: LineScatterCandleRadarChartRenderer //Define gradient var baseSpace = CGColorSpaceCreateDeviceRGB() var gradient : CGGradient? - if (dataSet.gradientPositions.count > 1) { + if (dataSet.gradientPositions!.count > 1) { gradient = CGGradientCreateWithColorComponents(baseSpace, gradientColors, gradientLocations, gradientColors.count / 4) } else { gradient = CGGradientCreateWithColorComponents(baseSpace, gradientColors, nil, gradientColors.count / 4)