Skip to content

Commit

Permalink
Add value text rotation for other charts
Browse files Browse the repository at this point in the history
  • Loading branch information
Chinh Tran committed Feb 21, 2018
1 parent 5a1a46e commit 31f7ad5
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 13 deletions.
3 changes: 3 additions & 0 deletions Source/Charts/Renderers/BubbleChartRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,8 @@ open class BubbleChartRenderer: BarLineScatterCandleBubbleRenderer
let valueToPixelMatrix = trans.valueToPixelMatrix

let iconsOffset = dataSet.iconsOffset

let angleRadians = dataSet.valueRotationAngle.DEG2RAD

for j in _xBounds.min..._xBounds.range + _xBounds.min
{
Expand Down Expand Up @@ -182,6 +184,7 @@ open class BubbleChartRenderer: BarLineScatterCandleBubbleRenderer
at: CGPoint(x: pt.x,
y: pt.y - (0.5 * lineHeight)),
align: .center,
angleRadians: angleRadians,
attributes: [.font: valueFont,
.foregroundColor: valueTextColor])
}
Expand Down
3 changes: 3 additions & 0 deletions Source/Charts/Renderers/CandleStickChartRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,8 @@ open class CandleStickChartRenderer: LineScatterCandleRadarRenderer

let iconsOffset = dataSet.iconsOffset

let angleRadians = dataSet.valueRotationAngle.DEG2RAD

_xBounds.set(chart: dataProvider, dataSet: dataSet, animator: animator)

let lineHeight = valueFont.lineHeight
Expand Down Expand Up @@ -299,6 +301,7 @@ open class CandleStickChartRenderer: LineScatterCandleRadarRenderer
at: CGPoint(x: pt.x,
y: pt.y - yOffset),
align: .center,
angleRadians: angleRadians,
attributes: [.font: valueFont,
.foregroundColor: dataSet.valueTextColorAt(j)])
}
Expand Down
10 changes: 10 additions & 0 deletions Source/Charts/Renderers/PieChartRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,8 @@ open class PieChartRenderer: NSObject, DataRenderer

let iconsOffset = dataSet.iconsOffset

let angleRadians = dataSet.valueRotationAngle.DEG2RAD

let xValuePosition = dataSet.xValuePosition
let yValuePosition = dataSet.yValuePosition

Expand Down Expand Up @@ -429,6 +431,7 @@ open class PieChartRenderer: NSObject, DataRenderer
context.drawText(valueText,
at: labelPoint,
align: align,
angleRadians: angleRadians,
attributes: [.font: valueFont,
.foregroundColor: valueTextColor])

Expand All @@ -438,6 +441,7 @@ open class PieChartRenderer: NSObject, DataRenderer
at: CGPoint(x: labelPoint.x,
y: labelPoint.y + lineHeight),
align: align,
angleRadians: angleRadians,
attributes: [.font: entryLabelFont ?? valueFont,
.foregroundColor: entryLabelColor ?? valueTextColor])
}
Expand All @@ -450,6 +454,7 @@ open class PieChartRenderer: NSObject, DataRenderer
at: CGPoint(x: labelPoint.x,
y: labelPoint.y + lineHeight / 2.0),
align: align,
angleRadians: angleRadians,
attributes: [.font: entryLabelFont ?? valueFont,
.foregroundColor: entryLabelColor ?? valueTextColor])
}
Expand All @@ -460,6 +465,7 @@ open class PieChartRenderer: NSObject, DataRenderer
at: CGPoint(x: labelPoint.x,
y: labelPoint.y + lineHeight / 2.0),
align: align,
angleRadians: angleRadians,
attributes: [.font: valueFont,
.foregroundColor: valueTextColor])
}
Expand All @@ -476,13 +482,15 @@ open class PieChartRenderer: NSObject, DataRenderer
context.drawText(valueText,
at: CGPoint(x: x, y: y),
align: .center,
angleRadians: angleRadians,
attributes: [.font: valueFont, .foregroundColor: valueTextColor])

if j < data.entryCount && pe?.label != nil
{
context.drawText(pe!.label!,
at: CGPoint(x: x, y: y + lineHeight),
align: .center,
angleRadians: angleRadians,
attributes: [.font: entryLabelFont ?? valueFont,
.foregroundColor: entryLabelColor ?? valueTextColor])
}
Expand All @@ -494,6 +502,7 @@ open class PieChartRenderer: NSObject, DataRenderer
context.drawText(pe!.label!,
at: CGPoint(x: x, y: y + lineHeight / 2.0),
align: .center,
angleRadians: angleRadians,
attributes: [.font: entryLabelFont ?? valueFont,
.foregroundColor: entryLabelColor ?? valueTextColor])
}
Expand All @@ -503,6 +512,7 @@ open class PieChartRenderer: NSObject, DataRenderer
context.drawText(valueText,
at: CGPoint(x: x, y: y + lineHeight / 2.0),
align: .center,
angleRadians: angleRadians,
attributes: [.font: valueFont, .foregroundColor: valueTextColor])
}
}
Expand Down
3 changes: 3 additions & 0 deletions Source/Charts/Renderers/RadarChartRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,8 @@ open class RadarChartRenderer: LineRadarRenderer
continue
}

let angleRadians = dataSet.valueRotationAngle.DEG2RAD

let entryCount = dataSet.entryCount

let iconsOffset = dataSet.iconsOffset
Expand All @@ -183,6 +185,7 @@ open class RadarChartRenderer: LineRadarRenderer
viewPortHandler: viewPortHandler),
at: CGPoint(x: p.x, y: p.y - yoffset - valueFont.lineHeight),
align: .center,
angleRadians: angleRadians,
attributes: [.font: valueFont,
.foregroundColor: dataSet.valueTextColorAt(j)])
}
Expand Down
3 changes: 3 additions & 0 deletions Source/Charts/Renderers/ScatterChartRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ open class ScatterChartRenderer: LineScatterCandleRadarRenderer

let iconsOffset = dataSet.iconsOffset

let angleRadians = dataSet.valueRotationAngle.DEG2RAD

let shapeSize = dataSet.scatterShapeSize
let lineHeight = valueFont.lineHeight

Expand Down Expand Up @@ -169,6 +171,7 @@ open class ScatterChartRenderer: LineScatterCandleRadarRenderer
at: CGPoint(x: pt.x,
y: pt.y - shapeSize - lineHeight),
align: .center,
angleRadians: angleRadians,
attributes: [.font: valueFont,
.foregroundColor: dataSet.valueTextColorAt(j)]
)
Expand Down
26 changes: 13 additions & 13 deletions Source/Charts/Utils/ChartUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -137,22 +137,22 @@ extension CGContext {
NSUIGraphicsPopContext()
}

open func drawText(_ text: String, at point: CGPoint, align: NSTextAlignment, attributes: [NSAttributedStringKey : Any]?)
open func drawText(_ text: String, at point: CGPoint, align: NSTextAlignment, anchor: CGPoint = CGPoint(x: 0.5, y: 0.5), angleRadians: CGFloat = 0.0, attributes: [NSAttributedStringKey : Any]?)
{

let drawPoint = getDrawPoint(text: text, point: point, align: align, attributes: attributes)

NSUIGraphicsPushContext(self)

(text as NSString).draw(at: drawPoint, withAttributes: attributes)

NSUIGraphicsPopContext()
}

open func drawText(_ text: String, at point: CGPoint, align: NSTextAlignment, anchor: CGPoint = CGPoint(x: 0.5, y: 0.5), angleRadians: CGFloat, attributes: [NSAttributedStringKey : Any]?)
{
let drawPoint = getDrawPoint(text: text, point: point, align: align, attributes: attributes)
drawText(text, at: drawPoint, anchor: anchor, angleRadians: angleRadians, attributes: attributes)
if (angleRadians == 0.0)
{
NSUIGraphicsPushContext(self)

(text as NSString).draw(at: drawPoint, withAttributes: attributes)

NSUIGraphicsPopContext()
}
else
{
drawText(text, at: drawPoint, anchor: anchor, angleRadians: angleRadians, attributes: attributes)
}
}

open func drawText(_ text: String, at point: CGPoint, anchor: CGPoint = CGPoint(x: 0.5, y: 0.5), angleRadians: CGFloat, attributes: [NSAttributedStringKey : Any]?)
Expand Down

0 comments on commit 31f7ad5

Please sign in to comment.