From 024c7da2b58754e1ea25fbb9577da42296b1aa70 Mon Sep 17 00:00:00 2001 From: Jacob Christie Date: Sat, 16 Feb 2019 15:21:34 -0400 Subject: [PATCH 1/5] Align `ChartLimit.LabelPosition` naming with `UIRectCorner` --- .../Demos/LineChart1ViewController.swift | 6 +- .../Charts/Charts/PieRadarChartViewBase.swift | 2 +- Source/Charts/Components/ChartLimitLine.swift | 10 +- .../Charts/Renderers/LineChartRenderer.swift | 2 +- Source/Charts/Renderers/XAxisRenderer.swift | 115 ++++++++---------- .../XAxisRendererHorizontalBarChart.swift | 6 +- Source/Charts/Renderers/YAxisRenderer.swift | 6 +- .../YAxisRendererHorizontalBarChart.swift | 6 +- 8 files changed, 72 insertions(+), 81 deletions(-) mode change 100755 => 100644 Source/Charts/Charts/PieRadarChartViewBase.swift diff --git a/ChartsDemo-iOS/Swift/Demos/LineChart1ViewController.swift b/ChartsDemo-iOS/Swift/Demos/LineChart1ViewController.swift index 39b0e0f30a..38e51f79d9 100644 --- a/ChartsDemo-iOS/Swift/Demos/LineChart1ViewController.swift +++ b/ChartsDemo-iOS/Swift/Demos/LineChart1ViewController.swift @@ -49,7 +49,7 @@ class LineChart1ViewController: DemoBaseViewController { let llXAxis = ChartLimitLine(limit: 10, label: "Index 10") llXAxis.lineWidth = 4 llXAxis.lineDashLengths = [10, 10, 0] - llXAxis.labelPosition = .rightBottom + llXAxis.labelPosition = .bottomRight llXAxis.valueFont = .systemFont(ofSize: 10) chartView.xAxis.gridLineDashLengths = [10, 10] @@ -58,13 +58,13 @@ class LineChart1ViewController: DemoBaseViewController { let ll1 = ChartLimitLine(limit: 150, label: "Upper Limit") ll1.lineWidth = 4 ll1.lineDashLengths = [5, 5] - ll1.labelPosition = .rightTop + ll1.labelPosition = .topRight ll1.valueFont = .systemFont(ofSize: 10) let ll2 = ChartLimitLine(limit: -30, label: "Lower Limit") ll2.lineWidth = 4 ll2.lineDashLengths = [5,5] - ll2.labelPosition = .rightBottom + ll2.labelPosition = .bottomRight ll2.valueFont = .systemFont(ofSize: 10) let leftAxis = chartView.leftAxis diff --git a/Source/Charts/Charts/PieRadarChartViewBase.swift b/Source/Charts/Charts/PieRadarChartViewBase.swift old mode 100755 new mode 100644 index 5153609335..784f080f1c --- a/Source/Charts/Charts/PieRadarChartViewBase.swift +++ b/Source/Charts/Charts/PieRadarChartViewBase.swift @@ -680,7 +680,7 @@ open class PieRadarChartViewBase: ChartViewBase } velocitySamples.append(currentSample) } - + private func calculateVelocity() -> CGFloat { guard var firstSample = velocitySamples.first, diff --git a/Source/Charts/Components/ChartLimitLine.swift b/Source/Charts/Components/ChartLimitLine.swift index e6682e3a33..41a2fe07b9 100644 --- a/Source/Charts/Components/ChartLimitLine.swift +++ b/Source/Charts/Components/ChartLimitLine.swift @@ -20,10 +20,10 @@ open class ChartLimitLine: ComponentBase @objc(ChartLimitLabelPosition) public enum LabelPosition: Int { - case leftTop - case leftBottom - case rightTop - case rightBottom + case topLeft + case topRight + case bottomLeft + case bottomRight } /// limit / maximum (the y-value or xIndex) @@ -39,7 +39,7 @@ open class ChartLimitLine: ComponentBase @objc open var drawLabelEnabled = true @objc open var label = "" - @objc open var labelPosition = LabelPosition.rightTop + @objc open var labelPosition = LabelPosition.topRight public override init() { diff --git a/Source/Charts/Renderers/LineChartRenderer.swift b/Source/Charts/Renderers/LineChartRenderer.swift index ec66ccd504..da9239c546 100644 --- a/Source/Charts/Renderers/LineChartRenderer.swift +++ b/Source/Charts/Renderers/LineChartRenderer.swift @@ -478,7 +478,7 @@ open class LineChartRenderer: LineRadarRenderer } _xBounds.set(chart: dataProvider, dataSet: dataSet, animator: animator) - + for j in _xBounds { guard let e = dataSet.entryForIndex(j) else { break } diff --git a/Source/Charts/Renderers/XAxisRenderer.swift b/Source/Charts/Renderers/XAxisRenderer.swift index a9732e07ab..2dc74683c0 100644 --- a/Source/Charts/Renderers/XAxisRenderer.swift +++ b/Source/Charts/Renderers/XAxisRenderer.swift @@ -12,6 +12,10 @@ import Foundation import CoreGraphics +#if !os(OSX) +import UIKit +#endif + @objc(ChartXAxisRenderer) open class XAxisRenderer: AxisRendererBase { @@ -334,29 +338,16 @@ open class XAxisRenderer: AxisRendererBase { guard let xAxis = self.axis as? XAxis, - let transformer = self.transformer + let transformer = self.transformer, + !xAxis.limitLines.isEmpty else { return } - var limitLines = xAxis.limitLines - - if limitLines.count == 0 - { - return - } - let trans = transformer.valueToPixelMatrix var position = CGPoint(x: 0.0, y: 0.0) - for i in 0 ..< limitLines.count + for l in xAxis.limitLines where l.isEnabled { - let l = limitLines[i] - - if !l.isEnabled - { - continue - } - context.saveGState() defer { context.restoreGState() } @@ -399,55 +390,55 @@ open class XAxisRenderer: AxisRendererBase { let label = limitLine.label - - // if drawing the limit-value label is enabled - if limitLine.drawLabelEnabled && label.count > 0 - { + guard limitLine.drawLabelEnabled, !label.isEmpty else { + return + } + let labelLineHeight = limitLine.valueFont.lineHeight let xOffset: CGFloat = limitLine.lineWidth + limitLine.xOffset - - if limitLine.labelPosition == .rightTop - { - ChartUtils.drawText(context: context, - text: label, - point: CGPoint( - x: position.x + xOffset, - y: viewPortHandler.contentTop + yOffset), - align: .left, - attributes: [NSAttributedString.Key.font: limitLine.valueFont, NSAttributedString.Key.foregroundColor: limitLine.valueTextColor]) - } - else if limitLine.labelPosition == .rightBottom - { - ChartUtils.drawText(context: context, - text: label, - point: CGPoint( - x: position.x + xOffset, - y: viewPortHandler.contentBottom - labelLineHeight - yOffset), - align: .left, - attributes: [NSAttributedString.Key.font: limitLine.valueFont, NSAttributedString.Key.foregroundColor: limitLine.valueTextColor]) - } - else if limitLine.labelPosition == .leftTop - { - ChartUtils.drawText(context: context, - text: label, - point: CGPoint( - x: position.x - xOffset, - y: viewPortHandler.contentTop + yOffset), - align: .right, - attributes: [NSAttributedString.Key.font: limitLine.valueFont, NSAttributedString.Key.foregroundColor: limitLine.valueTextColor]) - } - else - { - ChartUtils.drawText(context: context, - text: label, - point: CGPoint( - x: position.x - xOffset, - y: viewPortHandler.contentBottom - labelLineHeight - yOffset), - align: .right, - attributes: [NSAttributedString.Key.font: limitLine.valueFont, NSAttributedString.Key.foregroundColor: limitLine.valueTextColor]) + let attributes: [NSAttributedString.Key : Any] = [ + .font : limitLine.valueFont, + .foregroundColor : limitLine.valueTextColor + ] + + let (point, align): (CGPoint, NSTextAlignment) + switch limitLine.labelPosition { + case .topRight: + point = CGPoint( + x: position.x + xOffset, + y: viewPortHandler.contentTop + yOffset + ) + align = .left + + case .bottomRight: + point = CGPoint( + x: position.x + xOffset, + y: viewPortHandler.contentBottom - labelLineHeight - yOffset + ) + align = .left + + case .topLeft: + point = CGPoint( + x: position.x - xOffset, + y: viewPortHandler.contentTop + yOffset + ) + align = .right + + case .bottomLeft: + point = CGPoint( + x: position.x - xOffset, + y: viewPortHandler.contentBottom - labelLineHeight - yOffset + ) + align = .right } - } - } + ChartUtils.drawText( + context: context, + text: label, + point: point, + align: align, + attributes: attributes + ) + } } diff --git a/Source/Charts/Renderers/XAxisRendererHorizontalBarChart.swift b/Source/Charts/Renderers/XAxisRendererHorizontalBarChart.swift index 9b75d9ef74..ce777746a0 100644 --- a/Source/Charts/Renderers/XAxisRendererHorizontalBarChart.swift +++ b/Source/Charts/Renderers/XAxisRendererHorizontalBarChart.swift @@ -310,7 +310,7 @@ open class XAxisRendererHorizontalBarChart: XAxisRenderer let xOffset: CGFloat = 4.0 + l.xOffset let yOffset: CGFloat = l.lineWidth + labelLineHeight + l.yOffset - if l.labelPosition == .rightTop + if l.labelPosition == .topRight { ChartUtils.drawText(context: context, text: label, @@ -320,7 +320,7 @@ open class XAxisRendererHorizontalBarChart: XAxisRenderer align: .right, attributes: [NSAttributedString.Key.font: l.valueFont, NSAttributedString.Key.foregroundColor: l.valueTextColor]) } - else if l.labelPosition == .rightBottom + else if l.labelPosition == .bottomRight { ChartUtils.drawText(context: context, text: label, @@ -330,7 +330,7 @@ open class XAxisRendererHorizontalBarChart: XAxisRenderer align: .right, attributes: [NSAttributedString.Key.font: l.valueFont, NSAttributedString.Key.foregroundColor: l.valueTextColor]) } - else if l.labelPosition == .leftTop + else if l.labelPosition == .topLeft { ChartUtils.drawText(context: context, text: label, diff --git a/Source/Charts/Renderers/YAxisRenderer.swift b/Source/Charts/Renderers/YAxisRenderer.swift index a6f75b62f0..03a60f8fb2 100644 --- a/Source/Charts/Renderers/YAxisRenderer.swift +++ b/Source/Charts/Renderers/YAxisRenderer.swift @@ -340,7 +340,7 @@ open class YAxisRenderer: AxisRendererBase let xOffset: CGFloat = 4.0 + l.xOffset let yOffset: CGFloat = l.lineWidth + labelLineHeight + l.yOffset - if l.labelPosition == .rightTop + if l.labelPosition == .topRight { ChartUtils.drawText(context: context, text: label, @@ -350,7 +350,7 @@ open class YAxisRenderer: AxisRendererBase align: .right, attributes: [NSAttributedString.Key.font: l.valueFont, NSAttributedString.Key.foregroundColor: l.valueTextColor]) } - else if l.labelPosition == .rightBottom + else if l.labelPosition == .bottomRight { ChartUtils.drawText(context: context, text: label, @@ -360,7 +360,7 @@ open class YAxisRenderer: AxisRendererBase align: .right, attributes: [NSAttributedString.Key.font: l.valueFont, NSAttributedString.Key.foregroundColor: l.valueTextColor]) } - else if l.labelPosition == .leftTop + else if l.labelPosition == .topLeft { ChartUtils.drawText(context: context, text: label, diff --git a/Source/Charts/Renderers/YAxisRendererHorizontalBarChart.swift b/Source/Charts/Renderers/YAxisRendererHorizontalBarChart.swift index 247cd091bf..e9c1af0e1c 100644 --- a/Source/Charts/Renderers/YAxisRendererHorizontalBarChart.swift +++ b/Source/Charts/Renderers/YAxisRendererHorizontalBarChart.swift @@ -315,7 +315,7 @@ open class YAxisRendererHorizontalBarChart: YAxisRenderer let xOffset: CGFloat = l.lineWidth + l.xOffset let yOffset: CGFloat = 2.0 + l.yOffset - if l.labelPosition == .rightTop + if l.labelPosition == .topRight { ChartUtils.drawText(context: context, text: label, @@ -325,7 +325,7 @@ open class YAxisRendererHorizontalBarChart: YAxisRenderer align: .left, attributes: [NSAttributedString.Key.font: l.valueFont, NSAttributedString.Key.foregroundColor: l.valueTextColor]) } - else if l.labelPosition == .rightBottom + else if l.labelPosition == .bottomRight { ChartUtils.drawText(context: context, text: label, @@ -335,7 +335,7 @@ open class YAxisRendererHorizontalBarChart: YAxisRenderer align: .left, attributes: [NSAttributedString.Key.font: l.valueFont, NSAttributedString.Key.foregroundColor: l.valueTextColor]) } - else if l.labelPosition == .leftTop + else if l.labelPosition == .topLeft { ChartUtils.drawText(context: context, text: label, From e55e75954c8d2bbc1e4a0b2ba42589520e0d5ecd Mon Sep 17 00:00:00 2001 From: Jacob Christie Date: Sun, 17 Feb 2019 12:53:41 -0400 Subject: [PATCH 2/5] Fixed Demos --- ChartsDemo-iOS/Objective-C/Demos/LineChart1ViewController.m | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/ChartsDemo-iOS/Objective-C/Demos/LineChart1ViewController.m b/ChartsDemo-iOS/Objective-C/Demos/LineChart1ViewController.m index 66d62a37d9..40297febdb 100644 --- a/ChartsDemo-iOS/Objective-C/Demos/LineChart1ViewController.m +++ b/ChartsDemo-iOS/Objective-C/Demos/LineChart1ViewController.m @@ -61,7 +61,7 @@ - (void)viewDidLoad ChartLimitLine *llXAxis = [[ChartLimitLine alloc] initWithLimit:10.0 label:@"Index 10"]; llXAxis.lineWidth = 4.0; llXAxis.lineDashLengths = @[@(10.f), @(10.f), @(0.f)]; - llXAxis.labelPosition = ChartLimitLabelPositionRightBottom; + llXAxis.labelPosition = ChartLimitLabelPositionBottomRight; llXAxis.valueFont = [UIFont systemFontOfSize:10.f]; //[_chartView.xAxis addLimitLine:llXAxis]; @@ -72,13 +72,13 @@ - (void)viewDidLoad ChartLimitLine *ll1 = [[ChartLimitLine alloc] initWithLimit:150.0 label:@"Upper Limit"]; ll1.lineWidth = 4.0; ll1.lineDashLengths = @[@5.f, @5.f]; - ll1.labelPosition = ChartLimitLabelPositionRightTop; + ll1.labelPosition = ChartLimitLabelPositionTopRight; ll1.valueFont = [UIFont systemFontOfSize:10.0]; ChartLimitLine *ll2 = [[ChartLimitLine alloc] initWithLimit:-30.0 label:@"Lower Limit"]; ll2.lineWidth = 4.0; ll2.lineDashLengths = @[@5.f, @5.f]; - ll2.labelPosition = ChartLimitLabelPositionRightBottom; + ll2.labelPosition = ChartLimitLabelPositionBottomRight; ll2.valueFont = [UIFont systemFontOfSize:10.0]; ChartYAxis *leftAxis = _chartView.leftAxis; From 5da58c0feef5846a8513f3a266b62eb2633b94c1 Mon Sep 17 00:00:00 2001 From: Xuan Liu Date: Mon, 4 Mar 2019 09:41:53 +0800 Subject: [PATCH 3/5] fix indent after replacing if with guard --- Source/Charts/Renderers/XAxisRenderer.swift | 84 ++++++++++----------- 1 file changed, 41 insertions(+), 43 deletions(-) diff --git a/Source/Charts/Renderers/XAxisRenderer.swift b/Source/Charts/Renderers/XAxisRenderer.swift index 2dc74683c0..0858f77e49 100644 --- a/Source/Charts/Renderers/XAxisRenderer.swift +++ b/Source/Charts/Renderers/XAxisRenderer.swift @@ -390,55 +390,53 @@ open class XAxisRenderer: AxisRendererBase { let label = limitLine.label - guard limitLine.drawLabelEnabled, !label.isEmpty else { - return - } + guard limitLine.drawLabelEnabled, !label.isEmpty else { return } - let labelLineHeight = limitLine.valueFont.lineHeight - - let xOffset: CGFloat = limitLine.lineWidth + limitLine.xOffset - let attributes: [NSAttributedString.Key : Any] = [ - .font : limitLine.valueFont, - .foregroundColor : limitLine.valueTextColor - ] + let labelLineHeight = limitLine.valueFont.lineHeight - let (point, align): (CGPoint, NSTextAlignment) - switch limitLine.labelPosition { - case .topRight: - point = CGPoint( - x: position.x + xOffset, - y: viewPortHandler.contentTop + yOffset - ) - align = .left + let xOffset: CGFloat = limitLine.lineWidth + limitLine.xOffset + let attributes: [NSAttributedString.Key : Any] = [ + .font : limitLine.valueFont, + .foregroundColor : limitLine.valueTextColor + ] - case .bottomRight: - point = CGPoint( - x: position.x + xOffset, - y: viewPortHandler.contentBottom - labelLineHeight - yOffset - ) - align = .left + let (point, align): (CGPoint, NSTextAlignment) + switch limitLine.labelPosition { + case .topRight: + point = CGPoint( + x: position.x + xOffset, + y: viewPortHandler.contentTop + yOffset + ) + align = .left - case .topLeft: - point = CGPoint( - x: position.x - xOffset, - y: viewPortHandler.contentTop + yOffset - ) - align = .right + case .bottomRight: + point = CGPoint( + x: position.x + xOffset, + y: viewPortHandler.contentBottom - labelLineHeight - yOffset + ) + align = .left - case .bottomLeft: - point = CGPoint( - x: position.x - xOffset, - y: viewPortHandler.contentBottom - labelLineHeight - yOffset - ) - align = .right - } + case .topLeft: + point = CGPoint( + x: position.x - xOffset, + y: viewPortHandler.contentTop + yOffset + ) + align = .right - ChartUtils.drawText( - context: context, - text: label, - point: point, - align: align, - attributes: attributes + case .bottomLeft: + point = CGPoint( + x: position.x - xOffset, + y: viewPortHandler.contentBottom - labelLineHeight - yOffset ) + align = .right + } + + ChartUtils.drawText( + context: context, + text: label, + point: point, + align: align, + attributes: attributes + ) } } From 958b92608f039e2c44ffc8cad141917543a744f8 Mon Sep 17 00:00:00 2001 From: Jacob Christie Date: Mon, 4 Mar 2019 20:45:48 -0400 Subject: [PATCH 4/5] reverted mistaken changes --- Source/Charts/Charts/PieRadarChartViewBase.swift | 4 ++-- Source/Charts/Renderers/XAxisRenderer.swift | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Source/Charts/Charts/PieRadarChartViewBase.swift b/Source/Charts/Charts/PieRadarChartViewBase.swift index 784f080f1c..bbdc8d826f 100644 --- a/Source/Charts/Charts/PieRadarChartViewBase.swift +++ b/Source/Charts/Charts/PieRadarChartViewBase.swift @@ -714,12 +714,12 @@ open class PieRadarChartViewBase: ChartViewBase { lastSample.angle += 360.0 } - + // The velocity let velocity = abs((lastSample.angle - firstSample.angle) / timeDelta) return isClockwise ? velocity : -velocity } - + /// sets the starting angle of the rotation, this is only used by the touch listener, x and y is the touch position private func setGestureStartAngle(x: CGFloat, y: CGFloat) { diff --git a/Source/Charts/Renderers/XAxisRenderer.swift b/Source/Charts/Renderers/XAxisRenderer.swift index 0858f77e49..e7026dbea6 100644 --- a/Source/Charts/Renderers/XAxisRenderer.swift +++ b/Source/Charts/Renderers/XAxisRenderer.swift @@ -13,7 +13,7 @@ import Foundation import CoreGraphics #if !os(OSX) -import UIKit + import UIKit #endif @objc(ChartXAxisRenderer) From 7b7d78ef89c2526a0e843505c8c19f75f672490c Mon Sep 17 00:00:00 2001 From: Jacob Christie Date: Tue, 5 Mar 2019 18:52:51 -0400 Subject: [PATCH 5/5] Removed unused #if statements --- Source/Charts/Renderers/XAxisRenderer.swift | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/Source/Charts/Renderers/XAxisRenderer.swift b/Source/Charts/Renderers/XAxisRenderer.swift index e7026dbea6..6ea1ba771d 100644 --- a/Source/Charts/Renderers/XAxisRenderer.swift +++ b/Source/Charts/Renderers/XAxisRenderer.swift @@ -12,10 +12,6 @@ import Foundation import CoreGraphics -#if !os(OSX) - import UIKit -#endif - @objc(ChartXAxisRenderer) open class XAxisRenderer: AxisRendererBase { @@ -175,9 +171,11 @@ open class XAxisRenderer: AxisRendererBase let paraStyle = NSParagraphStyle.default.mutableCopy() as! NSMutableParagraphStyle paraStyle.alignment = .center - let labelAttrs: [NSAttributedString.Key : Any] = [NSAttributedString.Key.font: xAxis.labelFont, - NSAttributedString.Key.foregroundColor: xAxis.labelTextColor, - NSAttributedString.Key.paragraphStyle: paraStyle] + let labelAttrs: [NSAttributedString.Key : Any] = [ + .font: xAxis.labelFont, + .foregroundColor: xAxis.labelTextColor, + .paragraphStyle: paraStyle + ] let labelRotationAngleRadians = xAxis.labelRotationAngle.DEG2RAD let centeringEnabled = xAxis.isCenterAxisLabelsEnabled