From 037b8304954caf8ece05808431f2d582314c3f63 Mon Sep 17 00:00:00 2001 From: Ieuan Tudur Peace Date: Wed, 15 Nov 2017 16:02:36 +0000 Subject: [PATCH 01/11] Show highlited line on touch --- Source/Chart.swift | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/Source/Chart.swift b/Source/Chart.swift index 763b830a1..a1adebd1b 100644 --- a/Source/Chart.swift +++ b/Source/Chart.swift @@ -186,8 +186,9 @@ open class Chart: UIControl { open var maxY: Float? /** - Color for the highlight line. - */ + Should show highlight line when touched. + */ + open var showHighlightLine: Bool = true open var highlightLineColor = UIColor.gray /** @@ -726,9 +727,11 @@ open class Chart: UIControl { delegate?.didFinishTouchingChart(self) return } - - drawHighlightLineFromLeftPosition(left) - + + if showHighlightLine == true { + drawHighlightLineFromLeftPosition(left) + } + if delegate == nil { return } From 5d1d49697e77eba6dcc7b8a84990412e3f772d2f Mon Sep 17 00:00:00 2001 From: Ieuan Tudur Peace Date: Wed, 15 Nov 2017 16:12:36 +0000 Subject: [PATCH 02/11] Add a left inset --- Source/Chart.swift | 48 +++++++++++++++++++++++++++++----------------- 1 file changed, 30 insertions(+), 18 deletions(-) diff --git a/Source/Chart.swift b/Source/Chart.swift index a1adebd1b..d7480aa3b 100644 --- a/Source/Chart.swift +++ b/Source/Chart.swift @@ -153,7 +153,12 @@ open class Chart: UIControl { Height of the area at the top of the chart, acting a padding to make place for the top y-axis label. */ open var topInset: CGFloat = 20 - + + /** + Height of the area at the left of the chart, acting a padding to make place for the left x-axis label. + */ + open var leftInset: CGFloat = 0 + /** Width of the chart's lines. */ @@ -303,8 +308,8 @@ open class Chart: UIControl { fileprivate func drawChart() { drawingHeight = bounds.height - bottomInset - topInset - drawingWidth = bounds.width - + drawingWidth = bounds.width - leftInset + let minMax = getMinMax() min = minMax.min max = minMax.max @@ -414,8 +419,9 @@ open class Chart: UIControl { } else { factor = width / (max.x - min.x) } - - let scaled = values.map { factor * ($0 - self.min.x) } + + let reversedValues = Array(values.reversed()) + let scaled = reversedValues.map {Float(self.leftInset) + width - factor * ($0 - self.min.x) } return scaled } @@ -522,30 +528,32 @@ open class Chart: UIControl { // horizontal axis at the bottom context.move(to: CGPoint(x: CGFloat(0), y: drawingHeight + topInset)) - context.addLine(to: CGPoint(x: CGFloat(drawingWidth), y: drawingHeight + topInset)) + context.addLine(to: CGPoint(x: CGFloat(drawingWidth + leftInset), y: drawingHeight + topInset)) context.strokePath() // horizontal axis at the top - context.move(to: CGPoint(x: CGFloat(0), y: CGFloat(0))) - context.addLine(to: CGPoint(x: CGFloat(drawingWidth), y: CGFloat(0))) + context.move(to: CGPoint(x: CGFloat(drawingWidth + leftInset), y: CGFloat(0))) + context.addLine(to: CGPoint(x: CGFloat(drawingWidth + leftInset), y: CGFloat(0))) context.strokePath() // horizontal axis when y = 0 if min.y < 0 && max.y > 0 { let y = CGFloat(getZeroValueOnYAxis(zeroLevel: 0)) context.move(to: CGPoint(x: CGFloat(0), y: y)) - context.addLine(to: CGPoint(x: CGFloat(drawingWidth), y: y)) + context.addLine(to: CGPoint(x: CGFloat(drawingWidth + leftInset), y: y)) context.strokePath() } - - // vertical axis on the left - context.move(to: CGPoint(x: CGFloat(0), y: CGFloat(0))) - context.addLine(to: CGPoint(x: CGFloat(0), y: drawingHeight + topInset)) - context.strokePath() - + + if leftInset < 20 { + // vertical axis on the left + context.move(to: CGPoint(x: CGFloat(0), y: CGFloat(0))) + context.addLine(to: CGPoint(x: CGFloat(0), y: drawingHeight + topInset)) + context.strokePath() + } + // vertical axis on the right - context.move(to: CGPoint(x: CGFloat(drawingWidth), y: CGFloat(0))) - context.addLine(to: CGPoint(x: CGFloat(drawingWidth), y: drawingHeight + topInset)) + context.move(to: CGPoint(x: CGFloat(drawingWidth + leftInset), y: CGFloat(0))) + context.addLine(to: CGPoint(x: CGFloat(drawingWidth + leftInset), y: drawingHeight + topInset)) context.strokePath() } @@ -632,7 +640,11 @@ open class Chart: UIControl { var labels: [Float] if yLabels == nil { - labels = [(min.y + max.y) / 2, max.y] + if leftInset < 20 { + labels = [(min.y + max.y) / 2, max.y] + } else { + labels = [min.y, (min.y + max.y) / 2, max.y] + } if yLabelsOnRightSide || min.y != 0 { labels.insert(min.y, at: 0) } From 45ee2420f48b0dd64294193f2c6345851e5f37df Mon Sep 17 00:00:00 2001 From: Ieuan Tudur Peace Date: Wed, 15 Nov 2017 16:22:41 +0000 Subject: [PATCH 03/11] Update readme --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 7e31c3bf9..24d9e841c 100644 --- a/README.md +++ b/README.md @@ -203,12 +203,14 @@ Some tips for debugging an hidden chart: * `minX` – minimum x-value. * `minY` – minimum y-value. * `topInset` – height of the area at the top of the chart, acting a padding to make place for the top y-axis label. +* `leftInset` – width of the area at the left of the chart, acting a padding to make place for the left x-axis label. * `xLabelsFormatter` – formats the labels on the x-axis. * `xLabelsOrientation` – sets the x-axis labels orientation to vertical or horizontal. * `xLabelsTextAlignment` – text-alignment for the x-labels. * `xLabelsSkipLast` (default `true`) - Skip the last x-label. Setting this to `false` will make the label overflow the frame width, so use carefully! * `yLabelsFormatter` – formats the labels on the y-axis. * `yLabelsOnRightSide` – place the y-labels on the right side. +* `showHighlightLine` – boolen to show or hide the highlight line on touch of the graph. #### Methods From 7c2a8e7888767ee36ab23fea3bea86e4b1024a6f Mon Sep 17 00:00:00 2001 From: Ieuan Tudur Peace Date: Wed, 15 Nov 2017 17:22:42 +0000 Subject: [PATCH 04/11] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 24d9e841c..32bcf680c 100644 --- a/README.md +++ b/README.md @@ -210,7 +210,7 @@ Some tips for debugging an hidden chart: * `xLabelsSkipLast` (default `true`) - Skip the last x-label. Setting this to `false` will make the label overflow the frame width, so use carefully! * `yLabelsFormatter` – formats the labels on the y-axis. * `yLabelsOnRightSide` – place the y-labels on the right side. -* `showHighlightLine` – boolen to show or hide the highlight line on touch of the graph. +* `showHighlightLine` (default `true`) – boolen to show or hide the highlight line on touch of the graph. #### Methods From cfeec12545481e74f5e2791774916f88b3d7cdfd Mon Sep 17 00:00:00 2001 From: Ieuan Tudur Peace Date: Thu, 16 Nov 2017 10:02:41 +0000 Subject: [PATCH 05/11] Add comment --- Source/Chart.swift | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Source/Chart.swift b/Source/Chart.swift index d7480aa3b..d1cf92234 100644 --- a/Source/Chart.swift +++ b/Source/Chart.swift @@ -194,6 +194,10 @@ open class Chart: UIControl { Should show highlight line when touched. */ open var showHighlightLine: Bool = true + + /** + Color for the highlight line + */ open var highlightLineColor = UIColor.gray /** From 2194e51ecbc5e99de610e08bdfd3251ec6e0e400 Mon Sep 17 00:00:00 2001 From: Ieuan Tudur Peace Date: Sat, 20 Jan 2018 16:46:11 +0000 Subject: [PATCH 06/11] Remove the magic number --- Source/Chart.swift | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/Source/Chart.swift b/Source/Chart.swift index d7480aa3b..1990e3617 100644 --- a/Source/Chart.swift +++ b/Source/Chart.swift @@ -544,18 +544,19 @@ open class Chart: UIControl { context.strokePath() } - if leftInset < 20 { + //Display vertical axis to the right if left inset + if leftInset == 0 { // vertical axis on the left context.move(to: CGPoint(x: CGFloat(0), y: CGFloat(0))) context.addLine(to: CGPoint(x: CGFloat(0), y: drawingHeight + topInset)) context.strokePath() + } else { + // vertical axis on the right + context.move(to: CGPoint(x: CGFloat(drawingWidth + leftInset), y: CGFloat(0))) + context.addLine(to: CGPoint(x: CGFloat(drawingWidth + leftInset), y: drawingHeight + topInset)) + context.strokePath() } - // vertical axis on the right - context.move(to: CGPoint(x: CGFloat(drawingWidth + leftInset), y: CGFloat(0))) - context.addLine(to: CGPoint(x: CGFloat(drawingWidth + leftInset), y: drawingHeight + topInset)) - context.strokePath() - } fileprivate func drawLabelsAndGridOnXAxis() { From 2a7d031e489dddf7a135686ade69b853574883fe Mon Sep 17 00:00:00 2001 From: Ieuan Tudur Peace Date: Sat, 20 Jan 2018 16:50:01 +0000 Subject: [PATCH 07/11] Correct comment --- Source/Chart.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Source/Chart.swift b/Source/Chart.swift index 1990e3617..7d7504237 100644 --- a/Source/Chart.swift +++ b/Source/Chart.swift @@ -155,7 +155,7 @@ open class Chart: UIControl { open var topInset: CGFloat = 20 /** - Height of the area at the left of the chart, acting a padding to make place for the left x-axis label. + Width of the area at the left of the chart, acting a padding to make place for the left x-axis label. */ open var leftInset: CGFloat = 0 From 9d5b47370b947382e7c81a3074f0db7b92ff3df0 Mon Sep 17 00:00:00 2001 From: Ieuan Tudur Peace Date: Sat, 20 Jan 2018 16:56:13 +0000 Subject: [PATCH 08/11] Remove showHighlightLine option --- Source/Chart.swift | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/Source/Chart.swift b/Source/Chart.swift index 7d7504237..569092b52 100644 --- a/Source/Chart.swift +++ b/Source/Chart.swift @@ -190,12 +190,6 @@ open class Chart: UIControl { */ open var maxY: Float? - /** - Should show highlight line when touched. - */ - open var showHighlightLine: Bool = true - open var highlightLineColor = UIColor.gray - /** Width for the highlight line. */ @@ -740,10 +734,8 @@ open class Chart: UIControl { delegate?.didFinishTouchingChart(self) return } - - if showHighlightLine == true { - drawHighlightLineFromLeftPosition(left) - } + + drawHighlightLineFromLeftPosition(left) if delegate == nil { return From 04a3c3564db6fe6653bf2f2815cee94ed4213409 Mon Sep 17 00:00:00 2001 From: Ieuan Tudur Peace Date: Sat, 20 Jan 2018 17:02:39 +0000 Subject: [PATCH 09/11] Update README.md --- README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/README.md b/README.md index 32bcf680c..f9fa8025a 100644 --- a/README.md +++ b/README.md @@ -210,7 +210,6 @@ Some tips for debugging an hidden chart: * `xLabelsSkipLast` (default `true`) - Skip the last x-label. Setting this to `false` will make the label overflow the frame width, so use carefully! * `yLabelsFormatter` – formats the labels on the y-axis. * `yLabelsOnRightSide` – place the y-labels on the right side. -* `showHighlightLine` (default `true`) – boolen to show or hide the highlight line on touch of the graph. #### Methods From 5fb6bcc3e08d0ad07a47e4b77e9a43cd0338e438 Mon Sep 17 00:00:00 2001 From: Ieuan Tudur Peace Date: Sat, 20 Jan 2018 17:11:02 +0000 Subject: [PATCH 10/11] Re add highlightLineColor Shouldn't of deleted it! --- Source/Chart.swift | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/Source/Chart.swift b/Source/Chart.swift index 569092b52..c910a45e3 100644 --- a/Source/Chart.swift +++ b/Source/Chart.swift @@ -184,7 +184,12 @@ open class Chart: UIControl { Custom maximum value for the x-axis. */ open var maxX: Float? - + + /** + Color for the highlight line. + */ + open var highlightLineColor = UIColor.gray + /** Custom maximum value for the y-axis. */ From af13a03d5e017bed8659470fc382e641cbb2127a Mon Sep 17 00:00:00 2001 From: Ieuan Tudur Peace Date: Sat, 20 Jan 2018 17:27:59 +0000 Subject: [PATCH 11/11] Move var highlightLineColor to below maxY --- Source/Chart.swift | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Source/Chart.swift b/Source/Chart.swift index c910a45e3..927cb1923 100644 --- a/Source/Chart.swift +++ b/Source/Chart.swift @@ -185,15 +185,15 @@ open class Chart: UIControl { */ open var maxX: Float? - /** - Color for the highlight line. - */ - open var highlightLineColor = UIColor.gray - /** Custom maximum value for the y-axis. */ open var maxY: Float? + + /** + Color for the highlight line. + */ + open var highlightLineColor = UIColor.gray /** Width for the highlight line.