From 74be1bf61d50fb62c968b144236655e1b56754c0 Mon Sep 17 00:00:00 2001 From: Simu Voicu-Mircea Date: Wed, 28 Aug 2019 15:54:14 +0200 Subject: [PATCH 1/5] Added width parameter for lineSeries in order to be able to have different widths for different series --- Source/Chart.swift | 6 +++--- Source/ChartSeries.swift | 5 +++++ 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/Source/Chart.swift b/Source/Chart.swift index 0a9cc3897..9f1dac21c 100644 --- a/Source/Chart.swift +++ b/Source/Chart.swift @@ -342,7 +342,7 @@ open class Chart: UIControl { let scaledYValues = scaleValuesOnYAxis( segment.map { $0.y } ) if series.line { - drawLine(scaledXValues, yValues: scaledYValues, seriesIndex: index) + drawLine(scaledXValues, yValues: scaledYValues, seriesIndex: index, width: series.width) } if series.area { drawArea(scaledXValues, yValues: scaledYValues, seriesIndex: index) @@ -461,7 +461,7 @@ open class Chart: UIControl { // MARK: - Drawings - fileprivate func drawLine(_ xValues: [Double], yValues: [Double], seriesIndex: Int) { + fileprivate func drawLine(_ xValues: [Double], yValues: [Double], seriesIndex: Int, width: CGFloat?) { // YValues are "reverted" from top to bottom, so 'above' means <= level let isAboveZeroLine = yValues.max()! <= self.scaleValueOnYAxis(series[seriesIndex].colors.zeroLevel) let path = CGMutablePath() @@ -481,7 +481,7 @@ open class Chart: UIControl { lineLayer.strokeColor = series[seriesIndex].colors.below.cgColor } lineLayer.fillColor = nil - lineLayer.lineWidth = lineWidth + lineLayer.lineWidth = width ?? lineWidth lineLayer.lineJoin = CAShapeLayerLineJoin.bevel self.layer.addSublayer(lineLayer) diff --git a/Source/ChartSeries.swift b/Source/ChartSeries.swift index 46e6207bc..0f5832e4b 100644 --- a/Source/ChartSeries.swift +++ b/Source/ChartSeries.swift @@ -21,6 +21,11 @@ open class ChartSeries { */ open var line: Bool = true + /** + The width of the series line. Useful when different line widths are required + */ + open var width: CGFloat? + /** Draws an area below the series line. */ From 228938d695960e60dcb2df1d55fc687d5be7f4d6 Mon Sep 17 00:00:00 2001 From: Simu Voicu-Mircea Date: Fri, 30 Aug 2019 15:38:00 +0200 Subject: [PATCH 2/5] Added dashed pattern option to series --- Source/Chart.swift | 1 + Source/ChartSeries.swift | 6 ++++++ 2 files changed, 7 insertions(+) diff --git a/Source/Chart.swift b/Source/Chart.swift index 9f1dac21c..a64bf3ac2 100644 --- a/Source/Chart.swift +++ b/Source/Chart.swift @@ -474,6 +474,7 @@ open class Chart: UIControl { let lineLayer = CAShapeLayer() lineLayer.frame = self.bounds lineLayer.path = path + lineLayer.lineDashPattern = series[seriesIndex].dashed ? [7, 3] : nil if isAboveZeroLine { lineLayer.strokeColor = series[seriesIndex].colors.above.cgColor diff --git a/Source/ChartSeries.swift b/Source/ChartSeries.swift index 0f5832e4b..045e91932 100644 --- a/Source/ChartSeries.swift +++ b/Source/ChartSeries.swift @@ -21,6 +21,12 @@ open class ChartSeries { */ open var line: Bool = true + /** + Draws the line in a dashed pattern. + */ + + open var dashed: Bool = false + /** The width of the series line. Useful when different line widths are required */ From 70126b6dda6f2fefd55724ad864da31ef84df23b Mon Sep 17 00:00:00 2001 From: Simu Voicu-Mircea Date: Fri, 30 Aug 2019 16:09:05 +0200 Subject: [PATCH 3/5] Commented dashing code for Y axis grid --- Source/Chart.swift | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/Source/Chart.swift b/Source/Chart.swift index a64bf3ac2..0e932dd70 100644 --- a/Source/Chart.swift +++ b/Source/Chart.swift @@ -650,12 +650,12 @@ open class Chart: UIControl { context.move(to: CGPoint(x: CGFloat(0), y: y)) context.addLine(to: CGPoint(x: self.bounds.width, y: y)) - if labels[i] != 0 { - // Horizontal grid for 0 is not dashed - context.setLineDash(phase: CGFloat(0), lengths: [CGFloat(5)]) - } else { - context.setLineDash(phase: CGFloat(0), lengths: []) - } +// if labels[i] != 0 { +// // Horizontal grid for 0 is not dashed +// context.setLineDash(phase: CGFloat(0), lengths: [CGFloat(5)]) +// } else { +// context.setLineDash(phase: CGFloat(0), lengths: []) +// } context.strokePath() } From c0370462cf9b42f42d825bc7048e6dd79a8be232 Mon Sep 17 00:00:00 2001 From: Simu Voicu-Mircea Date: Fri, 30 Aug 2019 16:14:36 +0200 Subject: [PATCH 4/5] Reverted last change --- Source/Chart.swift | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/Source/Chart.swift b/Source/Chart.swift index 0e932dd70..a64bf3ac2 100644 --- a/Source/Chart.swift +++ b/Source/Chart.swift @@ -650,12 +650,12 @@ open class Chart: UIControl { context.move(to: CGPoint(x: CGFloat(0), y: y)) context.addLine(to: CGPoint(x: self.bounds.width, y: y)) -// if labels[i] != 0 { -// // Horizontal grid for 0 is not dashed -// context.setLineDash(phase: CGFloat(0), lengths: [CGFloat(5)]) -// } else { -// context.setLineDash(phase: CGFloat(0), lengths: []) -// } + if labels[i] != 0 { + // Horizontal grid for 0 is not dashed + context.setLineDash(phase: CGFloat(0), lengths: [CGFloat(5)]) + } else { + context.setLineDash(phase: CGFloat(0), lengths: []) + } context.strokePath() } From e2065f8ec39df3748bfdd2b39120663786f68e26 Mon Sep 17 00:00:00 2001 From: Simu Voicu-Mircea Date: Fri, 30 Aug 2019 16:45:08 +0200 Subject: [PATCH 5/5] Added option for dashed horizontal and vertical grid --- Source/Chart.swift | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/Source/Chart.swift b/Source/Chart.swift index a64bf3ac2..7ded1c708 100644 --- a/Source/Chart.swift +++ b/Source/Chart.swift @@ -144,11 +144,22 @@ open class Chart: UIControl { Enable the lines for the labels on the x-axis */ open var showXLabelsAndGrid: Bool = true + + /** + Show the lines for the labels on the x-axis with dashed pattern + */ + open var showXGridDashed: Bool = true + /** Enable the lines for the labels on the y-axis */ open var showYLabelsAndGrid: Bool = true + /** + Show the lines for the labels on the y-axis with dashed pattern + */ + open var showYGridDashed: Bool = true + /** Height of the area at the bottom of the chart, containing the labels for the x-axis. */ @@ -576,6 +587,12 @@ open class Chart: UIControl { if x != 0 && x != drawingWidth { context.move(to: CGPoint(x: x, y: CGFloat(0))) context.addLine(to: CGPoint(x: x, y: bounds.height)) + if labels[i] != 0 && showXGridDashed { + // Vertical grid for 0 is not dashed or when showXGrddDashed is false + context.setLineDash(phase: CGFloat(0), lengths: [CGFloat(5)]) + } else { + context.setLineDash(phase: CGFloat(0), lengths: []) + } context.strokePath() } @@ -650,8 +667,8 @@ open class Chart: UIControl { context.move(to: CGPoint(x: CGFloat(0), y: y)) context.addLine(to: CGPoint(x: self.bounds.width, y: y)) - if labels[i] != 0 { - // Horizontal grid for 0 is not dashed + if labels[i] != 0 && showYGridDashed { + // Horizontal grid for 0 is not dashed or when showYGrodDashed is false context.setLineDash(phase: CGFloat(0), lengths: [CGFloat(5)]) } else { context.setLineDash(phase: CGFloat(0), lengths: [])