Skip to content

Commit

Permalink
Fixed text positioning when rotated
Browse files Browse the repository at this point in the history
  • Loading branch information
Chinh Tran committed Feb 28, 2017
1 parent 9758375 commit 1cf837c
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 19 deletions.
20 changes: 6 additions & 14 deletions Source/Charts/Renderers/BarChartRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ open class BarChartRenderer: BarLineScatterCandleBubbleRenderer
{
guard let dataSet = dataSets[dataSetIndex] as? IBarChartDataSet else { continue }

let valueRotationAngle = dataSet.valueRotationAngle
let angleRadians = dataSet.valueRotationAngle * ChartUtils.Math.FDEG2RAD

if !shouldDrawValues(forDataSet: dataSet)
{
Expand Down Expand Up @@ -444,7 +444,7 @@ open class BarChartRenderer: BarLineScatterCandleBubbleRenderer
font: valueFont,
align: .center,
color: dataSet.valueTextColorAt(j),
rotationAngle: valueRotationAngle
angleRadians: angleRadians
)
}

Expand Down Expand Up @@ -512,7 +512,7 @@ open class BarChartRenderer: BarLineScatterCandleBubbleRenderer
font: valueFont,
align: .center,
color: dataSet.valueTextColorAt(index),
rotationAngle: valueRotationAngle
angleRadians: angleRadians
)

}
Expand Down Expand Up @@ -600,7 +600,7 @@ open class BarChartRenderer: BarLineScatterCandleBubbleRenderer
font: valueFont,
align: .center,
color: dataSet.valueTextColorAt(index),
rotationAngle: valueRotationAngle
angleRadians: angleRadians
)
}

Expand All @@ -624,24 +624,16 @@ open class BarChartRenderer: BarLineScatterCandleBubbleRenderer
}

/// Draws a value at the specified x and y position.
open func drawValue(context: CGContext, value: String, xPos: CGFloat, yPos: CGFloat, font: NSUIFont, align: NSTextAlignment, color: NSUIColor, rotationAngle: CGFloat)
open func drawValue(context: CGContext, value: String, xPos: CGFloat, yPos: CGFloat, font: NSUIFont, align: NSTextAlignment, color: NSUIColor, angleRadians: CGFloat)
{
if (rotationAngle != 0.0)
{
let angleRadians = rotationAngle * ChartUtils.Math.FDEG2RAD
ChartUtils.drawText(
context: context,
text: value,
point: CGPoint(x: xPos, y: yPos),
align: .left,
align: .center,
attributes: [NSFontAttributeName: font, NSForegroundColorAttributeName: color],
anchor: CGPoint(x: 0.5, y: 0.5),
angleRadians: angleRadians)
}
else
{
ChartUtils.drawText(context: context, text: value, point: CGPoint(x: xPos, y: yPos), align: align, attributes: [NSFontAttributeName: font, NSForegroundColorAttributeName: color])
}
}


Expand Down
10 changes: 5 additions & 5 deletions Source/Charts/Renderers/HorizontalBarChartRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -327,8 +327,8 @@ open class HorizontalBarChartRenderer: BarChartRenderer
{
guard let dataSet = dataSets[dataSetIndex] as? IBarChartDataSet else { continue }

let valueRotationAngle = dataSet.valueRotationAngle

let angleRadians = dataSet.valueRotationAngle * ChartUtils.Math.FDEG2RAD
if !shouldDrawValues(forDataSet: dataSet) || !(dataSet.isDrawIconsEnabled && dataSet.isVisible)

{
Expand Down Expand Up @@ -405,7 +405,7 @@ open class HorizontalBarChartRenderer: BarChartRenderer
font: valueFont,
align: textAlign,
color: dataSet.valueTextColorAt(j),
rotationAngle: valueRotationAngle)
angleRadians: angleRadians)
}

if let icon = e.icon, dataSet.isDrawIconsEnabled
Expand Down Expand Up @@ -487,7 +487,7 @@ open class HorizontalBarChartRenderer: BarChartRenderer
font: valueFont,
align: textAlign,
color: dataSet.valueTextColorAt(index),
rotationAngle: valueRotationAngle)
angleRadians: angleRadians)

}

Expand Down Expand Up @@ -591,7 +591,7 @@ open class HorizontalBarChartRenderer: BarChartRenderer
font: valueFont,
align: textAlign,
color: dataSet.valueTextColorAt(index),
rotationAngle: valueRotationAngle)
angleRadians: angleRadians)

}

Expand Down
6 changes: 6 additions & 0 deletions Source/Charts/Utils/ChartUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,12 @@ open class ChartUtils
{
var point = point

var align = align

if angleRadians != 0.0 {
align = .left
}

if align == .center
{
point.x -= text.size(attributes: attributes).width / 2.0
Expand Down

0 comments on commit 1cf837c

Please sign in to comment.