@@ -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}
0 commit comments