From aecd7f0d799d4fe3b752f1276b9d45c870fae9c2 Mon Sep 17 00:00:00 2001 From: Shant Tokatyan Date: Fri, 30 Nov 2018 17:34:15 -0800 Subject: [PATCH] Draws the line chart the same way regardless of the number of colors for the data set --- .../Charts/Renderers/LineChartRenderer.swift | 134 +++++------------- 1 file changed, 37 insertions(+), 97 deletions(-) diff --git a/Source/Charts/Renderers/LineChartRenderer.swift b/Source/Charts/Renderers/LineChartRenderer.swift index 47c6ea73b9..eee22200c2 100644 --- a/Source/Charts/Renderers/LineChartRenderer.swift +++ b/Source/Charts/Renderers/LineChartRenderer.swift @@ -316,123 +316,63 @@ open class LineChartRenderer: LineRadarRenderer context.saveGState() - // more than 1 color - if dataSet.colors.count > 1 - { if _lineSegments.count != pointsPerEntryPair { // Allocate once in correct size _lineSegments = [CGPoint](repeating: CGPoint(), count: pointsPerEntryPair) } - for j in stride(from: _xBounds.min, through: _xBounds.range + _xBounds.min, by: 1) + for j in stride(from: _xBounds.min, through: _xBounds.range + _xBounds.min, by: 1) + { + var e: ChartDataEntry! = dataSet.entryForIndex(j) + + if e == nil { continue } + + _lineSegments[0].x = CGFloat(e.x) + _lineSegments[0].y = CGFloat(e.y * phaseY) + + if j < _xBounds.max { - var e: ChartDataEntry! = dataSet.entryForIndex(j) - - if e == nil { continue } + e = dataSet.entryForIndex(j + 1) - _lineSegments[0].x = CGFloat(e.x) - _lineSegments[0].y = CGFloat(e.y * phaseY) + if e == nil { break } - if j < _xBounds.max + if isDrawSteppedEnabled { - e = dataSet.entryForIndex(j + 1) - - if e == nil { break } - - if isDrawSteppedEnabled - { - _lineSegments[1] = CGPoint(x: CGFloat(e.x), y: _lineSegments[0].y) - _lineSegments[2] = _lineSegments[1] - _lineSegments[3] = CGPoint(x: CGFloat(e.x), y: CGFloat(e.y * phaseY)) - } - else - { - _lineSegments[1] = CGPoint(x: CGFloat(e.x), y: CGFloat(e.y * phaseY)) - } + _lineSegments[1] = CGPoint(x: CGFloat(e.x), y: _lineSegments[0].y) + _lineSegments[2] = _lineSegments[1] + _lineSegments[3] = CGPoint(x: CGFloat(e.x), y: CGFloat(e.y * phaseY)) } else { - _lineSegments[1] = _lineSegments[0] + _lineSegments[1] = CGPoint(x: CGFloat(e.x), y: CGFloat(e.y * phaseY)) } + } + else + { + _lineSegments[1] = _lineSegments[0] + } - for i in 0..<_lineSegments.count - { - _lineSegments[i] = _lineSegments[i].applying(valueToPixelMatrix) - } - - if (!viewPortHandler.isInBoundsRight(_lineSegments[0].x)) - { - break - } - - // make sure the lines don't do shitty things outside bounds - if !viewPortHandler.isInBoundsLeft(_lineSegments[1].x) - || (!viewPortHandler.isInBoundsTop(_lineSegments[0].y) && !viewPortHandler.isInBoundsBottom(_lineSegments[1].y)) - { - continue - } - - // get the color that is set for this line-segment - context.setStrokeColor(dataSet.color(atIndex: j).cgColor) - context.strokeLineSegments(between: _lineSegments) + for i in 0..<_lineSegments.count + { + _lineSegments[i] = _lineSegments[i].applying(valueToPixelMatrix) } - } - else - { // only one color per dataset - var e1: ChartDataEntry! - var e2: ChartDataEntry! - - e1 = dataSet.entryForIndex(_xBounds.min) + if (!viewPortHandler.isInBoundsRight(_lineSegments[0].x)) + { + break + } - if e1 != nil + // make sure the lines don't do shitty things outside bounds + if !viewPortHandler.isInBoundsLeft(_lineSegments[1].x) + || (!viewPortHandler.isInBoundsTop(_lineSegments[0].y) && !viewPortHandler.isInBoundsBottom(_lineSegments[1].y)) { - context.beginPath() - var firstPoint = true - - for x in stride(from: _xBounds.min, through: _xBounds.range + _xBounds.min, by: 1) - { - e1 = dataSet.entryForIndex(x == 0 ? 0 : (x - 1)) - e2 = dataSet.entryForIndex(x) - - if e1 == nil || e2 == nil { continue } - - let pt = CGPoint( - x: CGFloat(e1.x), - y: CGFloat(e1.y * phaseY) - ).applying(valueToPixelMatrix) - - if firstPoint - { - context.move(to: pt) - firstPoint = false - } - else - { - context.addLine(to: pt) - } - - if isDrawSteppedEnabled - { - context.addLine(to: CGPoint( - x: CGFloat(e2.x), - y: CGFloat(e1.y * phaseY) - ).applying(valueToPixelMatrix)) - } - - context.addLine(to: CGPoint( - x: CGFloat(e2.x), - y: CGFloat(e2.y * phaseY) - ).applying(valueToPixelMatrix)) - } - - if !firstPoint - { - context.setStrokeColor(dataSet.color(atIndex: 0).cgColor) - context.strokePath() - } + continue } + + // get the color that is set for this line-segment + context.setStrokeColor(dataSet.color(atIndex: j).cgColor) + context.strokeLineSegments(between: _lineSegments) } context.restoreGState()