From 34dafbc6aaac4316656748f1a864426ac42e3d2d Mon Sep 17 00:00:00 2001 From: jjatie Date: Sat, 9 Mar 2019 12:11:08 -0400 Subject: [PATCH] Align `ChartLimit.LabelPosition` naming with `UIRectCorner` (#3846) * Align `ChartLimit.LabelPosition` naming with `UIRectCorner` * Fixed Demos * fix indent after replacing if with guard * reverted mistaken changes * Removed unused #if statements --- .../Demos/LineChart1ViewController.m | 6 +- .../Demos/LineChart1ViewController.swift | 6 +- .../Charts/Charts/PieRadarChartViewBase.swift | 6 +- Source/Charts/Components/ChartLimitLine.swift | 10 +- .../Charts/Renderers/LineChartRenderer.swift | 2 +- Source/Charts/Renderers/XAxisRenderer.swift | 123 ++++++++---------- .../XAxisRendererHorizontalBarChart.swift | 6 +- Source/Charts/Renderers/YAxisRenderer.swift | 6 +- .../YAxisRendererHorizontalBarChart.swift | 6 +- 9 files changed, 79 insertions(+), 92 deletions(-) mode change 100755 => 100644 Source/Charts/Charts/PieRadarChartViewBase.swift 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; 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..bbdc8d826f --- 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, @@ -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/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..6ea1ba771d 100644 --- a/Source/Charts/Renderers/XAxisRenderer.swift +++ b/Source/Charts/Renderers/XAxisRenderer.swift @@ -171,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 @@ -334,29 +336,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 +388,53 @@ open class XAxisRenderer: AxisRendererBase { let label = limitLine.label - - // if drawing the limit-value label is enabled - if limitLine.drawLabelEnabled && label.count > 0 - { - 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]) - } + 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 (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 0fa2fbeb7f..4fb3dd2ef2 100644 --- a/Source/Charts/Renderers/YAxisRenderer.swift +++ b/Source/Charts/Renderers/YAxisRenderer.swift @@ -338,7 +338,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, @@ -348,7 +348,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, @@ -358,7 +358,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,