-
-
Notifications
You must be signed in to change notification settings - Fork 6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added value text rotation #2200
Changes from 3 commits
ef21083
5a1a46e
31f7ad5
ea87d1f
66cd435
aadc3a8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -302,6 +302,9 @@ open class ChartBaseDataSet: NSObject, ChartDataSetProtocol | |
/// the font for the value-text labels | ||
open var valueFont: NSUIFont = NSUIFont.systemFont(ofSize: 7.0) | ||
|
||
/// the rotation angle for value-text labels | ||
open var valueRotationAngle: CGFloat = CGFloat(0.0) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
/// The form to draw for this dataset in the legend. | ||
open var form = Legend.Form.default | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -200,6 +200,9 @@ public protocol ChartDataSetProtocol | |
/// the font for the value-text labels | ||
var valueFont: NSUIFont { get set } | ||
|
||
/// the rotation angle for value-text labels | ||
var valueRotationAngle: CGFloat { get set } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
/// The form to draw for this dataset in the legend. | ||
/// | ||
/// Return `.Default` to use the default legend form. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -317,6 +317,8 @@ open class BarChartRenderer: BarLineScatterCandleBubbleRenderer | |
shouldDrawValues(forDataSet: dataSet) | ||
else { continue } | ||
|
||
let angleRadians = dataSet.valueRotationAngle.DEG2RAD | ||
|
||
let isInverted = dataProvider.isInverted(axis: dataSet.axisDependency) | ||
|
||
// calculate the correct offset depending on the draw position of the value | ||
|
@@ -376,7 +378,9 @@ open class BarChartRenderer: BarLineScatterCandleBubbleRenderer | |
: (rect.origin.y + rect.size.height + negOffset), | ||
font: valueFont, | ||
align: .center, | ||
color: dataSet.valueTextColorAt(j)) | ||
color: dataSet.valueTextColorAt(j), | ||
anchor: CGPoint(x: 0.5, y: 0.5), | ||
angleRadians: angleRadians) | ||
} | ||
|
||
if let icon = e.icon, dataSet.isDrawIconsEnabled | ||
|
@@ -393,6 +397,7 @@ open class BarChartRenderer: BarLineScatterCandleBubbleRenderer | |
atCenter: CGPoint(x: px, y: py), | ||
size: icon.size) | ||
} | ||
|
||
} | ||
} | ||
else | ||
|
@@ -469,7 +474,9 @@ open class BarChartRenderer: BarLineScatterCandleBubbleRenderer | |
yPos: y, | ||
font: valueFont, | ||
align: .center, | ||
color: dataSet.valueTextColorAt(index)) | ||
color: dataSet.valueTextColorAt(index), | ||
anchor: CGPoint(x: 0.5, y: 0.5), | ||
angleRadians: angleRadians) | ||
} | ||
|
||
if let icon = e.icon, dataSet.isDrawIconsEnabled | ||
|
@@ -501,7 +508,9 @@ open class BarChartRenderer: BarLineScatterCandleBubbleRenderer | |
(e.y >= 0 ? posOffset : negOffset), | ||
font: valueFont, | ||
align: .center, | ||
color: dataSet.valueTextColorAt(index)) | ||
color: dataSet.valueTextColorAt(index), | ||
anchor: CGPoint(x: 0.5, y: 0.5), | ||
angleRadians: angleRadians) | ||
} | ||
|
||
if let icon = e.icon, dataSet.isDrawIconsEnabled | ||
|
@@ -527,10 +536,20 @@ open class BarChartRenderer: BarLineScatterCandleBubbleRenderer | |
} | ||
|
||
/// Draws a value at the specified x and y position. | ||
@objc open func drawValue(context: CGContext, value: String, xPos: CGFloat, yPos: CGFloat, font: NSUIFont, align: NSTextAlignment, color: NSUIColor) | ||
@objc open func drawValue(context: CGContext, value: String, xPos: CGFloat, yPos: CGFloat, font: NSUIFont, align: NSTextAlignment, color: NSUIColor, anchor: CGPoint, angleRadians: CGFloat) | ||
{ | ||
context.drawText(value, at: CGPoint(x: xPos, y: yPos), align: align, attributes: [.font: font, .foregroundColor: color]) | ||
if (angleRadians == 0.0) | ||
{ | ||
context.drawText(value, at: CGPoint(x: xPos, y: yPos), align: align, attributes: [.font: font, .foregroundColor: color]) | ||
} | ||
else | ||
{ | ||
// align left to center text with rotation | ||
context.drawText(value, at: CGPoint(x: xPos, y: yPos), align: align, anchor: anchor, | ||
angleRadians: angleRadians, attributes: [.font: font, .foregroundColor: color]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no line break please |
||
} | ||
} | ||
|
||
|
||
open override func drawExtras(context: CGContext) | ||
{ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -137,27 +137,25 @@ 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]?) | ||
{ | ||
var point = point | ||
|
||
if align == .center | ||
let drawPoint = getDrawPoint(text: text, point: point, align: align, attributes: attributes) | ||
if (angleRadians == 0.0) | ||
{ | ||
point.x -= text.size(withAttributes: attributes).width / 2.0 | ||
NSUIGraphicsPushContext(self) | ||
|
||
(text as NSString).draw(at: drawPoint, withAttributes: attributes) | ||
|
||
NSUIGraphicsPopContext() | ||
} | ||
else if align == .right | ||
else | ||
{ | ||
point.x -= text.size(withAttributes: attributes).width | ||
drawText(text, at: drawPoint, anchor: anchor, angleRadians: angleRadians, attributes: attributes) | ||
} | ||
|
||
NSUIGraphicsPushContext(self) | ||
|
||
(text as NSString).draw(at: point, withAttributes: attributes) | ||
|
||
NSUIGraphicsPopContext() | ||
} | ||
|
||
open func drawText(_ text: String, at point: CGPoint, anchor: CGPoint, angleRadians: CGFloat, attributes: [NSAttributedStringKey : Any]?) | ||
open func drawText(_ text: String, at point: CGPoint, anchor: CGPoint = CGPoint(x: 0.5, y: 0.5), angleRadians: CGFloat, attributes: [NSAttributedStringKey : Any]?) | ||
{ | ||
var drawOffset = CGPoint() | ||
|
||
|
@@ -209,6 +207,21 @@ extension CGContext { | |
NSUIGraphicsPopContext() | ||
} | ||
|
||
func getDrawPoint(text: String, point: CGPoint, align: NSTextAlignment, attributes: [NSAttributedStringKey : Any]?) -> CGPoint | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. private func |
||
{ | ||
var point = point | ||
|
||
if align == .center | ||
{ | ||
point.x -= text.size(withAttributes: attributes).width / 2.0 | ||
} | ||
else if align == .right | ||
{ | ||
point.x -= text.size(withAttributes: attributes).width | ||
} | ||
return point | ||
} | ||
|
||
func drawMultilineText(_ text: String, at point: CGPoint, constrainedTo size: CGSize, anchor: CGPoint, knownTextSize: CGSize, angleRadians: CGFloat, attributes: [NSAttributedStringKey : Any]?) | ||
{ | ||
var rect = CGRect(origin: .zero, size: knownTextSize) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please change to "The rotation angle (in degrees) for value-text labels
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you change it in the protocol, you can delete the documentation comment here and Xcode will use the one from the protocol.