From c3b7926b0591400ecc5fe9e6336212fd82f76be8 Mon Sep 17 00:00:00 2001 From: zntfdr Date: Sat, 2 Jan 2016 22:56:30 +0800 Subject: [PATCH 1/6] removed c-style for loops --- Charts/Classes/Renderers/ChartLegendRenderer.swift | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/Charts/Classes/Renderers/ChartLegendRenderer.swift b/Charts/Classes/Renderers/ChartLegendRenderer.swift index faf9f4b6ea..3d8fd0f04c 100755 --- a/Charts/Classes/Renderers/ChartLegendRenderer.swift +++ b/Charts/Classes/Renderers/ChartLegendRenderer.swift @@ -35,7 +35,7 @@ public class ChartLegendRenderer: ChartRendererBase var colors = [UIColor?]() // loop for building up the colors and labels used in the legend - for (var i = 0, count = data.dataSetCount; i < count; i++) + for i in 0.. Date: Sat, 2 Jan 2016 23:07:41 +0800 Subject: [PATCH 2/6] merged multiple switch-cases in one (while maintaining code readability) --- .../Renderers/ChartLegendRenderer.swift | 28 ++++++++++--------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/Charts/Classes/Renderers/ChartLegendRenderer.swift b/Charts/Classes/Renderers/ChartLegendRenderer.swift index 3d8fd0f04c..2e663e584f 100755 --- a/Charts/Classes/Renderers/ChartLegendRenderer.swift +++ b/Charts/Classes/Renderers/ChartLegendRenderer.swift @@ -137,12 +137,13 @@ public class ChartLegendRenderer: ChartRendererBase switch (legendPosition) { - case .BelowChartLeft: fallthrough - case .BelowChartRight: fallthrough - case .BelowChartCenter: fallthrough - case .AboveChartLeft: fallthrough - case .AboveChartRight: fallthrough - case .AboveChartCenter: + case + .BelowChartLeft, + .BelowChartRight, + .BelowChartCenter, + .AboveChartLeft, + .AboveChartRight, + .AboveChartCenter: let contentWidth: CGFloat = viewPortHandler.contentWidth @@ -253,13 +254,14 @@ public class ChartLegendRenderer: ChartRendererBase break - case .PiechartCenter: fallthrough - case .RightOfChart: fallthrough - case .RightOfChartCenter: fallthrough - case .RightOfChartInside: fallthrough - case .LeftOfChart: fallthrough - case .LeftOfChartCenter: fallthrough - case .LeftOfChartInside: + case + .PiechartCenter, + .RightOfChart, + .RightOfChartCenter, + .RightOfChartInside, + .LeftOfChart, + .LeftOfChartCenter, + .LeftOfChartInside: // contains the stacked legend size in pixels var stack = CGFloat(0.0) From f93c83c91dafcdb69fe593f9cdbce6b496be228b Mon Sep 17 00:00:00 2001 From: zntfdr Date: Sat, 2 Jan 2016 23:22:05 +0800 Subject: [PATCH 3/6] replaced if-chain with nicer switch conditional --- Charts/Classes/Renderers/ChartLegendRenderer.swift | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/Charts/Classes/Renderers/ChartLegendRenderer.swift b/Charts/Classes/Renderers/ChartLegendRenderer.swift index 2e663e584f..86fb6dd65a 100755 --- a/Charts/Classes/Renderers/ChartLegendRenderer.swift +++ b/Charts/Classes/Renderers/ChartLegendRenderer.swift @@ -296,19 +296,13 @@ public class ChartLegendRenderer: ChartRendererBase } } - if (legendPosition == .RightOfChart || - legendPosition == .LeftOfChart) + switch legendPosition { + case .RightOfChart, .LeftOfChart: posY = viewPortHandler.contentTop + yoffset - } - else if (legendPosition == .RightOfChartCenter || - legendPosition == .LeftOfChartCenter) - { + case .RightOfChartCenter, .LeftOfChartCenter: posY = viewPortHandler.chartHeight / 2.0 - _legend.neededHeight / 2.0 - } - else /*if (legend.position == .RightOfChartInside || - legend.position == .LeftOfChartInside)*/ - { + default: // case .RightOfChartInside, .LeftOfChartInside posY = viewPortHandler.contentTop + yoffset } } From 337fe67b001076c987eccde0e1da5d752cda8182 Mon Sep 17 00:00:00 2001 From: zntfdr Date: Sat, 2 Jan 2016 23:25:55 +0800 Subject: [PATCH 4/6] making good use of defer control statement --- Charts/Classes/Renderers/ChartLegendRenderer.swift | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Charts/Classes/Renderers/ChartLegendRenderer.swift b/Charts/Classes/Renderers/ChartLegendRenderer.swift index 86fb6dd65a..711c80f37a 100755 --- a/Charts/Classes/Renderers/ChartLegendRenderer.swift +++ b/Charts/Classes/Renderers/ChartLegendRenderer.swift @@ -387,6 +387,7 @@ public class ChartLegendRenderer: ChartRendererBase let formsize = legend.formSize CGContextSaveGState(context) + defer { CGContextRestoreGState(context) } switch (legend.form) { @@ -411,8 +412,6 @@ public class ChartLegendRenderer: ChartRendererBase break } - - CGContextRestoreGState(context) } /// Draws the provided label at the given position. From 5f1bbc9477109844a76dbfb8d066641f8cc3519c Mon Sep 17 00:00:00 2001 From: zntfdr Date: Sat, 2 Jan 2016 23:29:13 +0800 Subject: [PATCH 5/6] removed unnecessary break statements --- Charts/Classes/Renderers/ChartLegendRenderer.swift | 7 ------- 1 file changed, 7 deletions(-) diff --git a/Charts/Classes/Renderers/ChartLegendRenderer.swift b/Charts/Classes/Renderers/ChartLegendRenderer.swift index 711c80f37a..919ad29391 100755 --- a/Charts/Classes/Renderers/ChartLegendRenderer.swift +++ b/Charts/Classes/Renderers/ChartLegendRenderer.swift @@ -252,8 +252,6 @@ public class ChartLegendRenderer: ChartRendererBase } } - break - case .PiechartCenter, .RightOfChart, @@ -368,7 +366,6 @@ public class ChartLegendRenderer: ChartRendererBase } } - break } } @@ -394,11 +391,9 @@ public class ChartLegendRenderer: ChartRendererBase case .Circle: CGContextSetFillColorWithColor(context, formColor!.CGColor) CGContextFillEllipseInRect(context, CGRect(x: x, y: y - formsize / 2.0, width: formsize, height: formsize)) - break case .Square: CGContextSetFillColorWithColor(context, formColor!.CGColor) CGContextFillRect(context, CGRect(x: x, y: y - formsize / 2.0, width: formsize, height: formsize)) - break case .Line: CGContextSetLineWidth(context, legend.formLineWidth) @@ -409,8 +404,6 @@ public class ChartLegendRenderer: ChartRendererBase _formLineSegmentsBuffer[1].x = x + formsize _formLineSegmentsBuffer[1].y = y CGContextStrokeLineSegments(context, _formLineSegmentsBuffer, 2) - - break } } From 48c28c88a67f969cb9be6021c8aaaed2031fae4a Mon Sep 17 00:00:00 2001 From: zntfdr Date: Sat, 2 Jan 2016 23:32:49 +0800 Subject: [PATCH 6/6] making good use of guard control statement --- Charts/Classes/Renderers/ChartLegendRenderer.swift | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/Charts/Classes/Renderers/ChartLegendRenderer.swift b/Charts/Classes/Renderers/ChartLegendRenderer.swift index 919ad29391..d6b30b274d 100755 --- a/Charts/Classes/Renderers/ChartLegendRenderer.swift +++ b/Charts/Classes/Renderers/ChartLegendRenderer.swift @@ -374,10 +374,7 @@ public class ChartLegendRenderer: ChartRendererBase /// Draws the Legend-form at the given position with the color at the given index. internal func drawForm(context context: CGContext, x: CGFloat, y: CGFloat, colorIndex: Int, legend: ChartLegend) { - let formColor = legend.colors[colorIndex] - - if (formColor === nil || formColor == UIColor.clearColor()) - { + guard let formColor = legend.colors[colorIndex] where formColor != UIColor.clearColor() else { return } @@ -389,15 +386,15 @@ public class ChartLegendRenderer: ChartRendererBase switch (legend.form) { case .Circle: - CGContextSetFillColorWithColor(context, formColor!.CGColor) + CGContextSetFillColorWithColor(context, formColor.CGColor) CGContextFillEllipseInRect(context, CGRect(x: x, y: y - formsize / 2.0, width: formsize, height: formsize)) case .Square: - CGContextSetFillColorWithColor(context, formColor!.CGColor) + CGContextSetFillColorWithColor(context, formColor.CGColor) CGContextFillRect(context, CGRect(x: x, y: y - formsize / 2.0, width: formsize, height: formsize)) case .Line: CGContextSetLineWidth(context, legend.formLineWidth) - CGContextSetStrokeColorWithColor(context, formColor!.CGColor) + CGContextSetStrokeColorWithColor(context, formColor.CGColor) _formLineSegmentsBuffer[0].x = x _formLineSegmentsBuffer[0].y = y