From 53f176335bb1d8df211b30bc374a3a72a2759dab Mon Sep 17 00:00:00 2001 From: Daniel Cohen Gindi Date: Thu, 23 Jan 2020 12:32:32 +0200 Subject: [PATCH] labelXOffset = 10 is default for radar chart only + also consider other renderers Fixes https://github.com/danielgindi/Charts/pull/3406 --- Source/Charts/Charts/RadarChartView.swift | 1 + Source/Charts/Components/YAxis.swift | 2 +- Source/Charts/Renderers/YAxisRenderer.swift | 4 +++- .../Charts/Renderers/YAxisRendererHorizontalBarChart.swift | 7 ++++++- Source/Charts/Renderers/YAxisRendererRadarChart.swift | 2 +- 5 files changed, 12 insertions(+), 4 deletions(-) diff --git a/Source/Charts/Charts/RadarChartView.swift b/Source/Charts/Charts/RadarChartView.swift index 85892bff3f..6bcad33613 100644 --- a/Source/Charts/Charts/RadarChartView.swift +++ b/Source/Charts/Charts/RadarChartView.swift @@ -59,6 +59,7 @@ open class RadarChartView: PieRadarChartViewBase super.initialize() _yAxis = YAxis(position: .left) + _yAxis.labelXOffset = 10.0 renderer = RadarChartRenderer(chart: self, animator: _animator, viewPortHandler: _viewPortHandler) diff --git a/Source/Charts/Components/YAxis.swift b/Source/Charts/Components/YAxis.swift index f427810d9a..8f528615a4 100644 --- a/Source/Charts/Components/YAxis.swift +++ b/Source/Charts/Components/YAxis.swift @@ -81,7 +81,7 @@ open class YAxis: AxisBase @objc open var labelAlignment: NSTextAlignment = .left /// the horizontal offset of the y-label - @objc open var labelXOffset: CGFloat = 10.0 + @objc open var labelXOffset: CGFloat = 0.0 /// the side this axis object represents private var _axisDependency = AxisDependency.left diff --git a/Source/Charts/Renderers/YAxisRenderer.swift b/Source/Charts/Renderers/YAxisRenderer.swift index f91580ece7..80b492d5ee 100644 --- a/Source/Charts/Renderers/YAxisRenderer.swift +++ b/Source/Charts/Renderers/YAxisRenderer.swift @@ -142,6 +142,8 @@ open class YAxisRenderer: AxisRendererBase let from = yAxis.isDrawBottomYLabelEntryEnabled ? 0 : 1 let to = yAxis.isDrawTopYLabelEntryEnabled ? yAxis.entryCount : (yAxis.entryCount - 1) + let xOffset = yAxis.labelXOffset + for i in stride(from: from, to: to, by: 1) { let text = yAxis.getFormattedLabel(i) @@ -149,7 +151,7 @@ open class YAxisRenderer: AxisRendererBase ChartUtils.drawText( context: context, text: text, - point: CGPoint(x: fixedPosition, y: positions[i].y + offset), + point: CGPoint(x: fixedPosition + xOffset, y: positions[i].y + offset), align: textAlign, attributes: [.font: labelFont, .foregroundColor: labelTextColor] ) diff --git a/Source/Charts/Renderers/YAxisRendererHorizontalBarChart.swift b/Source/Charts/Renderers/YAxisRendererHorizontalBarChart.swift index e9c1af0e1c..aaf0cd2553 100644 --- a/Source/Charts/Renderers/YAxisRendererHorizontalBarChart.swift +++ b/Source/Charts/Renderers/YAxisRendererHorizontalBarChart.swift @@ -155,6 +155,8 @@ open class YAxisRendererHorizontalBarChart: YAxisRenderer let from = yAxis.isDrawBottomYLabelEntryEnabled ? 0 : 1 let to = yAxis.isDrawTopYLabelEntryEnabled ? yAxis.entryCount : (yAxis.entryCount - 1) + let xOffset = yAxis.labelXOffset + for i in stride(from: from, to: to, by: 1) { let text = yAxis.getFormattedLabel(i) @@ -162,7 +164,10 @@ open class YAxisRendererHorizontalBarChart: YAxisRenderer ChartUtils.drawText( context: context, text: text, - point: CGPoint(x: positions[i].x, y: fixedPosition - offset), + point: CGPoint( + x: positions[i].x, y: + fixedPosition - offset + xOffset + ), align: .center, attributes: [NSAttributedString.Key.font: labelFont, NSAttributedString.Key.foregroundColor: labelTextColor]) } diff --git a/Source/Charts/Renderers/YAxisRendererRadarChart.swift b/Source/Charts/Renderers/YAxisRendererRadarChart.swift index fe28b6dbfe..a5e44daa63 100644 --- a/Source/Charts/Renderers/YAxisRendererRadarChart.swift +++ b/Source/Charts/Renderers/YAxisRendererRadarChart.swift @@ -187,7 +187,7 @@ open class YAxisRendererRadarChart: YAxisRenderer let to = yAxis.isDrawTopYLabelEntryEnabled ? yAxis.entryCount : (yAxis.entryCount - 1) let alignment: NSTextAlignment = yAxis.labelAlignment - let xOffset: CGFloat = yAxis.labelXOffset + let xOffset = yAxis.labelXOffset for j in stride(from: from, to: to, by: 1) {