Skip to content

Commit 34dafbc

Browse files
authored
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
1 parent 53f7c3a commit 34dafbc

File tree

9 files changed

+79
-92
lines changed

9 files changed

+79
-92
lines changed

ChartsDemo-iOS/Objective-C/Demos/LineChart1ViewController.m

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ - (void)viewDidLoad
6161
ChartLimitLine *llXAxis = [[ChartLimitLine alloc] initWithLimit:10.0 label:@"Index 10"];
6262
llXAxis.lineWidth = 4.0;
6363
llXAxis.lineDashLengths = @[@(10.f), @(10.f), @(0.f)];
64-
llXAxis.labelPosition = ChartLimitLabelPositionRightBottom;
64+
llXAxis.labelPosition = ChartLimitLabelPositionBottomRight;
6565
llXAxis.valueFont = [UIFont systemFontOfSize:10.f];
6666

6767
//[_chartView.xAxis addLimitLine:llXAxis];
@@ -72,13 +72,13 @@ - (void)viewDidLoad
7272
ChartLimitLine *ll1 = [[ChartLimitLine alloc] initWithLimit:150.0 label:@"Upper Limit"];
7373
ll1.lineWidth = 4.0;
7474
ll1.lineDashLengths = @[@5.f, @5.f];
75-
ll1.labelPosition = ChartLimitLabelPositionRightTop;
75+
ll1.labelPosition = ChartLimitLabelPositionTopRight;
7676
ll1.valueFont = [UIFont systemFontOfSize:10.0];
7777

7878
ChartLimitLine *ll2 = [[ChartLimitLine alloc] initWithLimit:-30.0 label:@"Lower Limit"];
7979
ll2.lineWidth = 4.0;
8080
ll2.lineDashLengths = @[@5.f, @5.f];
81-
ll2.labelPosition = ChartLimitLabelPositionRightBottom;
81+
ll2.labelPosition = ChartLimitLabelPositionBottomRight;
8282
ll2.valueFont = [UIFont systemFontOfSize:10.0];
8383

8484
ChartYAxis *leftAxis = _chartView.leftAxis;

ChartsDemo-iOS/Swift/Demos/LineChart1ViewController.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class LineChart1ViewController: DemoBaseViewController {
4949
let llXAxis = ChartLimitLine(limit: 10, label: "Index 10")
5050
llXAxis.lineWidth = 4
5151
llXAxis.lineDashLengths = [10, 10, 0]
52-
llXAxis.labelPosition = .rightBottom
52+
llXAxis.labelPosition = .bottomRight
5353
llXAxis.valueFont = .systemFont(ofSize: 10)
5454

5555
chartView.xAxis.gridLineDashLengths = [10, 10]
@@ -58,13 +58,13 @@ class LineChart1ViewController: DemoBaseViewController {
5858
let ll1 = ChartLimitLine(limit: 150, label: "Upper Limit")
5959
ll1.lineWidth = 4
6060
ll1.lineDashLengths = [5, 5]
61-
ll1.labelPosition = .rightTop
61+
ll1.labelPosition = .topRight
6262
ll1.valueFont = .systemFont(ofSize: 10)
6363

6464
let ll2 = ChartLimitLine(limit: -30, label: "Lower Limit")
6565
ll2.lineWidth = 4
6666
ll2.lineDashLengths = [5,5]
67-
ll2.labelPosition = .rightBottom
67+
ll2.labelPosition = .bottomRight
6868
ll2.valueFont = .systemFont(ofSize: 10)
6969

7070
let leftAxis = chartView.leftAxis

Source/Charts/Charts/PieRadarChartViewBase.swift

100755100644
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -680,7 +680,7 @@ open class PieRadarChartViewBase: ChartViewBase
680680
}
681681
velocitySamples.append(currentSample)
682682
}
683-
683+
684684
private func calculateVelocity() -> CGFloat
685685
{
686686
guard var firstSample = velocitySamples.first,
@@ -714,12 +714,12 @@ open class PieRadarChartViewBase: ChartViewBase
714714
{
715715
lastSample.angle += 360.0
716716
}
717-
717+
718718
// The velocity
719719
let velocity = abs((lastSample.angle - firstSample.angle) / timeDelta)
720720
return isClockwise ? velocity : -velocity
721721
}
722-
722+
723723
/// sets the starting angle of the rotation, this is only used by the touch listener, x and y is the touch position
724724
private func setGestureStartAngle(x: CGFloat, y: CGFloat)
725725
{

Source/Charts/Components/ChartLimitLine.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ open class ChartLimitLine: ComponentBase
2020
@objc(ChartLimitLabelPosition)
2121
public enum LabelPosition: Int
2222
{
23-
case leftTop
24-
case leftBottom
25-
case rightTop
26-
case rightBottom
23+
case topLeft
24+
case topRight
25+
case bottomLeft
26+
case bottomRight
2727
}
2828

2929
/// limit / maximum (the y-value or xIndex)
@@ -39,7 +39,7 @@ open class ChartLimitLine: ComponentBase
3939

4040
@objc open var drawLabelEnabled = true
4141
@objc open var label = ""
42-
@objc open var labelPosition = LabelPosition.rightTop
42+
@objc open var labelPosition = LabelPosition.topRight
4343

4444
public override init()
4545
{

Source/Charts/Renderers/LineChartRenderer.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,7 @@ open class LineChartRenderer: LineRadarRenderer
478478
}
479479

480480
_xBounds.set(chart: dataProvider, dataSet: dataSet, animator: animator)
481-
481+
482482
for j in _xBounds
483483
{
484484
guard let e = dataSet.entryForIndex(j) else { break }

Source/Charts/Renderers/XAxisRenderer.swift

Lines changed: 55 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -171,9 +171,11 @@ open class XAxisRenderer: AxisRendererBase
171171
let paraStyle = NSParagraphStyle.default.mutableCopy() as! NSMutableParagraphStyle
172172
paraStyle.alignment = .center
173173

174-
let labelAttrs: [NSAttributedString.Key : Any] = [NSAttributedString.Key.font: xAxis.labelFont,
175-
NSAttributedString.Key.foregroundColor: xAxis.labelTextColor,
176-
NSAttributedString.Key.paragraphStyle: paraStyle]
174+
let labelAttrs: [NSAttributedString.Key : Any] = [
175+
.font: xAxis.labelFont,
176+
.foregroundColor: xAxis.labelTextColor,
177+
.paragraphStyle: paraStyle
178+
]
177179
let labelRotationAngleRadians = xAxis.labelRotationAngle.DEG2RAD
178180

179181
let centeringEnabled = xAxis.isCenterAxisLabelsEnabled
@@ -334,29 +336,16 @@ open class XAxisRenderer: AxisRendererBase
334336
{
335337
guard
336338
let xAxis = self.axis as? XAxis,
337-
let transformer = self.transformer
339+
let transformer = self.transformer,
340+
!xAxis.limitLines.isEmpty
338341
else { return }
339342

340-
var limitLines = xAxis.limitLines
341-
342-
if limitLines.count == 0
343-
{
344-
return
345-
}
346-
347343
let trans = transformer.valueToPixelMatrix
348344

349345
var position = CGPoint(x: 0.0, y: 0.0)
350346

351-
for i in 0 ..< limitLines.count
347+
for l in xAxis.limitLines where l.isEnabled
352348
{
353-
let l = limitLines[i]
354-
355-
if !l.isEnabled
356-
{
357-
continue
358-
}
359-
360349
context.saveGState()
361350
defer { context.restoreGState() }
362351

@@ -399,55 +388,53 @@ open class XAxisRenderer: AxisRendererBase
399388
{
400389

401390
let label = limitLine.label
402-
403-
// if drawing the limit-value label is enabled
404-
if limitLine.drawLabelEnabled && label.count > 0
405-
{
406-
let labelLineHeight = limitLine.valueFont.lineHeight
407-
408-
let xOffset: CGFloat = limitLine.lineWidth + limitLine.xOffset
409-
410-
if limitLine.labelPosition == .rightTop
411-
{
412-
ChartUtils.drawText(context: context,
413-
text: label,
414-
point: CGPoint(
415-
x: position.x + xOffset,
416-
y: viewPortHandler.contentTop + yOffset),
417-
align: .left,
418-
attributes: [NSAttributedString.Key.font: limitLine.valueFont, NSAttributedString.Key.foregroundColor: limitLine.valueTextColor])
419-
}
420-
else if limitLine.labelPosition == .rightBottom
421-
{
422-
ChartUtils.drawText(context: context,
423-
text: label,
424-
point: CGPoint(
425-
x: position.x + xOffset,
426-
y: viewPortHandler.contentBottom - labelLineHeight - yOffset),
427-
align: .left,
428-
attributes: [NSAttributedString.Key.font: limitLine.valueFont, NSAttributedString.Key.foregroundColor: limitLine.valueTextColor])
429-
}
430-
else if limitLine.labelPosition == .leftTop
431-
{
432-
ChartUtils.drawText(context: context,
433-
text: label,
434-
point: CGPoint(
435-
x: position.x - xOffset,
436-
y: viewPortHandler.contentTop + yOffset),
437-
align: .right,
438-
attributes: [NSAttributedString.Key.font: limitLine.valueFont, NSAttributedString.Key.foregroundColor: limitLine.valueTextColor])
439-
}
440-
else
441-
{
442-
ChartUtils.drawText(context: context,
443-
text: label,
444-
point: CGPoint(
445-
x: position.x - xOffset,
446-
y: viewPortHandler.contentBottom - labelLineHeight - yOffset),
447-
align: .right,
448-
attributes: [NSAttributedString.Key.font: limitLine.valueFont, NSAttributedString.Key.foregroundColor: limitLine.valueTextColor])
449-
}
391+
guard limitLine.drawLabelEnabled, !label.isEmpty else { return }
392+
393+
let labelLineHeight = limitLine.valueFont.lineHeight
394+
395+
let xOffset: CGFloat = limitLine.lineWidth + limitLine.xOffset
396+
let attributes: [NSAttributedString.Key : Any] = [
397+
.font : limitLine.valueFont,
398+
.foregroundColor : limitLine.valueTextColor
399+
]
400+
401+
let (point, align): (CGPoint, NSTextAlignment)
402+
switch limitLine.labelPosition {
403+
case .topRight:
404+
point = CGPoint(
405+
x: position.x + xOffset,
406+
y: viewPortHandler.contentTop + yOffset
407+
)
408+
align = .left
409+
410+
case .bottomRight:
411+
point = CGPoint(
412+
x: position.x + xOffset,
413+
y: viewPortHandler.contentBottom - labelLineHeight - yOffset
414+
)
415+
align = .left
416+
417+
case .topLeft:
418+
point = CGPoint(
419+
x: position.x - xOffset,
420+
y: viewPortHandler.contentTop + yOffset
421+
)
422+
align = .right
423+
424+
case .bottomLeft:
425+
point = CGPoint(
426+
x: position.x - xOffset,
427+
y: viewPortHandler.contentBottom - labelLineHeight - yOffset
428+
)
429+
align = .right
450430
}
451-
}
452431

432+
ChartUtils.drawText(
433+
context: context,
434+
text: label,
435+
point: point,
436+
align: align,
437+
attributes: attributes
438+
)
439+
}
453440
}

Source/Charts/Renderers/XAxisRendererHorizontalBarChart.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ open class XAxisRendererHorizontalBarChart: XAxisRenderer
310310
let xOffset: CGFloat = 4.0 + l.xOffset
311311
let yOffset: CGFloat = l.lineWidth + labelLineHeight + l.yOffset
312312

313-
if l.labelPosition == .rightTop
313+
if l.labelPosition == .topRight
314314
{
315315
ChartUtils.drawText(context: context,
316316
text: label,
@@ -320,7 +320,7 @@ open class XAxisRendererHorizontalBarChart: XAxisRenderer
320320
align: .right,
321321
attributes: [NSAttributedString.Key.font: l.valueFont, NSAttributedString.Key.foregroundColor: l.valueTextColor])
322322
}
323-
else if l.labelPosition == .rightBottom
323+
else if l.labelPosition == .bottomRight
324324
{
325325
ChartUtils.drawText(context: context,
326326
text: label,
@@ -330,7 +330,7 @@ open class XAxisRendererHorizontalBarChart: XAxisRenderer
330330
align: .right,
331331
attributes: [NSAttributedString.Key.font: l.valueFont, NSAttributedString.Key.foregroundColor: l.valueTextColor])
332332
}
333-
else if l.labelPosition == .leftTop
333+
else if l.labelPosition == .topLeft
334334
{
335335
ChartUtils.drawText(context: context,
336336
text: label,

Source/Charts/Renderers/YAxisRenderer.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ open class YAxisRenderer: AxisRendererBase
338338
let xOffset: CGFloat = 4.0 + l.xOffset
339339
let yOffset: CGFloat = l.lineWidth + labelLineHeight + l.yOffset
340340

341-
if l.labelPosition == .rightTop
341+
if l.labelPosition == .topRight
342342
{
343343
ChartUtils.drawText(context: context,
344344
text: label,
@@ -348,7 +348,7 @@ open class YAxisRenderer: AxisRendererBase
348348
align: .right,
349349
attributes: [NSAttributedString.Key.font: l.valueFont, NSAttributedString.Key.foregroundColor: l.valueTextColor])
350350
}
351-
else if l.labelPosition == .rightBottom
351+
else if l.labelPosition == .bottomRight
352352
{
353353
ChartUtils.drawText(context: context,
354354
text: label,
@@ -358,7 +358,7 @@ open class YAxisRenderer: AxisRendererBase
358358
align: .right,
359359
attributes: [NSAttributedString.Key.font: l.valueFont, NSAttributedString.Key.foregroundColor: l.valueTextColor])
360360
}
361-
else if l.labelPosition == .leftTop
361+
else if l.labelPosition == .topLeft
362362
{
363363
ChartUtils.drawText(context: context,
364364
text: label,

Source/Charts/Renderers/YAxisRendererHorizontalBarChart.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ open class YAxisRendererHorizontalBarChart: YAxisRenderer
315315
let xOffset: CGFloat = l.lineWidth + l.xOffset
316316
let yOffset: CGFloat = 2.0 + l.yOffset
317317

318-
if l.labelPosition == .rightTop
318+
if l.labelPosition == .topRight
319319
{
320320
ChartUtils.drawText(context: context,
321321
text: label,
@@ -325,7 +325,7 @@ open class YAxisRendererHorizontalBarChart: YAxisRenderer
325325
align: .left,
326326
attributes: [NSAttributedString.Key.font: l.valueFont, NSAttributedString.Key.foregroundColor: l.valueTextColor])
327327
}
328-
else if l.labelPosition == .rightBottom
328+
else if l.labelPosition == .bottomRight
329329
{
330330
ChartUtils.drawText(context: context,
331331
text: label,
@@ -335,7 +335,7 @@ open class YAxisRendererHorizontalBarChart: YAxisRenderer
335335
align: .left,
336336
attributes: [NSAttributedString.Key.font: l.valueFont, NSAttributedString.Key.foregroundColor: l.valueTextColor])
337337
}
338-
else if l.labelPosition == .leftTop
338+
else if l.labelPosition == .topLeft
339339
{
340340
ChartUtils.drawText(context: context,
341341
text: label,

0 commit comments

Comments
 (0)