Skip to content
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

[Feature] added ability to rotate ChartLimitLine label #5085

Merged
merged 6 commits into from
Aug 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Source/Charts/Components/ChartLimitLine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ open class ChartLimitLine: ComponentBase
@objc open var drawLabelEnabled = true
@objc open var label = ""
@objc open var labelPosition = LabelPosition.rightTop
@objc open var labelRotationAngle = CGFloat(0.0)

public override init()
{
Expand Down
39 changes: 22 additions & 17 deletions Source/Charts/Renderers/XAxisRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -449,40 +449,45 @@ open class XAxisRenderer: NSObject, AxisRenderer
// if drawing the limit-value label is enabled
guard limitLine.drawLabelEnabled, !label.isEmpty else { return }

let labelLineHeight = limitLine.valueFont.lineHeight
let labelLineSize = label.size(withAttributes: [.font: limitLine.valueFont])
let labelLineRotatedSize = labelLineSize.rotatedBy(degrees: limitLine.labelRotationAngle)
let labelLineRotatedWidth = labelLineRotatedSize.width
let labelLineRotatedHeight = labelLineRotatedSize.height

let xOffset: CGFloat = limitLine.lineWidth + limitLine.xOffset
let labelRotationAngleRadians = limitLine.labelRotationAngle.DEG2RAD

let align: TextAlignment
let point: CGPoint
let anchor = CGPoint(x: 0.0, y: 0.0)
liuxuan30 marked this conversation as resolved.
Show resolved Hide resolved

switch limitLine.labelPosition
{
switch limitLine.labelPosition {
case .rightTop:
align = .left
point = CGPoint(x: position.x + xOffset,
y: viewPortHandler.contentTop + yOffset)
point = CGPoint(
x: position.x + xOffset,
y: viewPortHandler.contentTop + yOffset)

case .rightBottom:
align = .left
point = CGPoint(x: position.x + xOffset,
y: viewPortHandler.contentBottom - labelLineHeight - yOffset)
y: viewPortHandler.contentBottom - labelLineRotatedHeight - yOffset)

case .leftTop:
align = .right
point = CGPoint(x: position.x - xOffset,
point = CGPoint(x: position.x - labelLineRotatedWidth - xOffset,
y: viewPortHandler.contentTop + yOffset)

case .leftBottom:
align = .right
point = CGPoint(x: position.x - xOffset,
y: viewPortHandler.contentBottom - labelLineHeight - yOffset)
point = CGPoint(x: position.x - labelLineRotatedWidth - xOffset,
y: viewPortHandler.contentBottom - labelLineRotatedHeight - yOffset)
}

let attributes: [NSAttributedString.Key : Any] = [
.font: limitLine.valueFont,
.foregroundColor: limitLine.valueTextColor
]

context.drawText(label,
at: point,
align: align,
attributes: [.font: limitLine.valueFont,
.foregroundColor: limitLine.valueTextColor])
anchor: anchor,
angleRadians: labelRotationAngleRadians,
attributes: attributes)
}
}
35 changes: 21 additions & 14 deletions Source/Charts/Renderers/XAxisRendererHorizontalBarChart.swift
Original file line number Diff line number Diff line change
Expand Up @@ -249,41 +249,48 @@ open class XAxisRendererHorizontalBarChart: XAxisRenderer
// if drawing the limit-value label is enabled
if l.drawLabelEnabled, !label.isEmpty
{
let labelLineHeight = l.valueFont.lineHeight


let labelLineSize = label.size(withAttributes: [.font: l.valueFont])
let labelLineRotatedSize = labelLineSize.rotatedBy(degrees: l.labelRotationAngle)
let labelLineRotatedWidth = labelLineRotatedSize.width
let labelLineRotatedHeight = labelLineRotatedSize.height

let xOffset = 4.0 + l.xOffset
let yOffset = l.lineWidth + labelLineHeight + l.yOffset
let yOffset = l.lineWidth + labelLineRotatedHeight + l.yOffset
let labelRotationAngleRadians = l.labelRotationAngle.DEG2RAD

let align: TextAlignment
let point: CGPoint
let anchor = CGPoint(x: 0.0, y: 0.0)

switch l.labelPosition
{
case .rightTop:
align = .right
point = CGPoint(x: viewPortHandler.contentRight - xOffset,
point = CGPoint(x: viewPortHandler.contentRight - labelLineRotatedWidth - xOffset,
y: position.y - yOffset)

case .rightBottom:
align = .right
point = CGPoint(x: viewPortHandler.contentRight - xOffset,
y: position.y + yOffset - labelLineHeight)
point = CGPoint(x: viewPortHandler.contentRight - labelLineRotatedWidth - xOffset,
y: position.y - labelLineRotatedHeight + yOffset)

case .leftTop:
align = .left
point = CGPoint(x: viewPortHandler.contentLeft + xOffset,
y: position.y - yOffset)

case .leftBottom:
align = .left
point = CGPoint(x: viewPortHandler.contentLeft + xOffset,
y: position.y + yOffset - labelLineHeight)
y: position.y - labelLineRotatedHeight + yOffset)
}

let attributes: [NSAttributedString.Key : Any] = [
.font: l.valueFont,
.foregroundColor: l.valueTextColor
]

context.drawText(label,
at: point,
align: align,
attributes: [.font: l.valueFont, .foregroundColor: l.valueTextColor])
anchor: anchor,
angleRadians: labelRotationAngleRadians,
attributes: attributes)
}
}
}
Expand Down
34 changes: 20 additions & 14 deletions Source/Charts/Renderers/YAxisRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -295,41 +295,47 @@ open class YAxisRenderer: NSObject, AxisRenderer
// if drawing the limit-value label is enabled
guard l.drawLabelEnabled, !label.isEmpty else { continue }

let labelLineHeight = l.valueFont.lineHeight

let labelLineSize = label.size(withAttributes: [.font: l.valueFont])
let labelLineRotatedSize = labelLineSize.rotatedBy(degrees: l.labelRotationAngle)
let labelLineRotatedWidth = labelLineRotatedSize.width
let labelLineRotatedHeight = labelLineRotatedSize.height

let xOffset = 4.0 + l.xOffset
let yOffset = l.lineWidth + labelLineHeight + l.yOffset
let yOffset = l.lineWidth + labelLineRotatedHeight + l.yOffset
let labelRotationAngleRadians = l.labelRotationAngle.DEG2RAD

let align: TextAlignment
let point: CGPoint
let anchor = CGPoint(x: 0.0, y: 0.0)

switch l.labelPosition
{
case .rightTop:
align = .right
point = CGPoint(x: viewPortHandler.contentRight - xOffset,
point = CGPoint(x: viewPortHandler.contentRight - labelLineRotatedWidth - xOffset,
y: position.y - yOffset)

case .rightBottom:
align = .right
point = CGPoint(x: viewPortHandler.contentRight - xOffset,
y: position.y + yOffset - labelLineHeight)
point = CGPoint(x: viewPortHandler.contentRight - labelLineRotatedWidth - xOffset,
y: position.y - labelLineRotatedHeight + yOffset)

case .leftTop:
align = .left
point = CGPoint(x: viewPortHandler.contentLeft + xOffset,
y: position.y - yOffset)

case .leftBottom:
align = .left
point = CGPoint(x: viewPortHandler.contentLeft + xOffset,
y: position.y + yOffset - labelLineHeight)
y: position.y - labelLineRotatedHeight + yOffset)
}

let attributes: [NSAttributedString.Key : Any] = [
.font: l.valueFont,
.foregroundColor: l.valueTextColor
]

context.drawText(label,
at: point,
align: align,
attributes: [.font: l.valueFont, .foregroundColor: l.valueTextColor])
anchor: anchor,
angleRadians: labelRotationAngleRadians,
attributes: attributes)
}
}

Expand Down
32 changes: 19 additions & 13 deletions Source/Charts/Renderers/YAxisRendererHorizontalBarChart.swift
Original file line number Diff line number Diff line change
Expand Up @@ -253,41 +253,47 @@ open class YAxisRendererHorizontalBarChart: YAxisRenderer
// if drawing the limit-value label is enabled
if l.drawLabelEnabled, !label.isEmpty
{
let labelLineHeight = l.valueFont.lineHeight

let labelLineSize = label.size(withAttributes: [.font: l.valueFont])
let labelLineRotatedSize = labelLineSize.rotatedBy(degrees: l.labelRotationAngle)
let labelLineRotatedWidth = labelLineRotatedSize.width
let labelLineRotatedHeight = labelLineRotatedSize.height

let xOffset = l.lineWidth + l.xOffset
let yOffset = 2.0 + l.yOffset
let labelRotationAngleRadians = l.labelRotationAngle.DEG2RAD

let align: TextAlignment
let point: CGPoint
let anchor = CGPoint(x: 0.0, y: 0.0)

switch l.labelPosition
{
case .rightTop:
align = .left
point = CGPoint(x: position.x + xOffset,
y: viewPortHandler.contentTop + yOffset)

case .rightBottom:
align = .left
point = CGPoint(x: position.x + xOffset,
y: viewPortHandler.contentBottom - labelLineHeight - yOffset)
y: viewPortHandler.contentBottom - labelLineRotatedHeight - yOffset)

case .leftTop:
align = .right
point = CGPoint(x: position.x - xOffset,
point = CGPoint(x: position.x - labelLineRotatedWidth - xOffset,
y: viewPortHandler.contentTop + yOffset)

case .leftBottom:
align = .right
point = CGPoint(x: position.x - xOffset,
y: viewPortHandler.contentBottom - labelLineHeight - yOffset)
point = CGPoint(x: position.x - labelLineRotatedWidth - xOffset,
y: viewPortHandler.contentBottom - labelLineRotatedHeight - yOffset)
}

let attributes: [NSAttributedString.Key : Any] = [
.font: l.valueFont,
.foregroundColor: l.valueTextColor
]

context.drawText(label,
at: point,
align: align,
attributes: [.font: l.valueFont, .foregroundColor: l.valueTextColor])
anchor: anchor,
angleRadians: labelRotationAngleRadians,
attributes: attributes)
}
}
}
Expand Down
37 changes: 37 additions & 0 deletions Tests/ChartsTests/LineChartTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,41 @@ class LineChartTests: XCTestCase {
dataSet.drawIconsEnabled = true
assertChartSnapshot(matching: chart)
}

func testLimitLineLabelRotatedBy180() {
addLimitLineWithRotatedAngle(labelPosition: .rightBottom, labelRotationAngle: 180)
assertChartSnapshot(matching: chart)
}

func testLimitLineLabelRotatedBy120() {
addLimitLineWithRotatedAngle(labelPosition: .rightTop, labelRotationAngle: 120)
assertChartSnapshot(matching: chart)
}

func testLimitLineLabelRotatedBy90() {
addLimitLineWithRotatedAngle(labelPosition: .leftTop, labelRotationAngle: 90)
assertChartSnapshot(matching: chart)
}

func testLimitLineLabelRotatedBy45() {
addLimitLineWithRotatedAngle(labelPosition: .leftBottom, labelRotationAngle: 45)
assertChartSnapshot(matching: chart)
}

func testLimitLineLabelRotatedBy30() {
addLimitLineWithRotatedAngle(labelPosition: .leftBottom, labelRotationAngle: 30)
assertChartSnapshot(matching: chart)
}

func testLimitLineLabelDefaultRotation() {
addLimitLineWithRotatedAngle(labelPosition: .rightTop, labelRotationAngle: 0)
assertChartSnapshot(matching: chart)
}

private func addLimitLineWithRotatedAngle(labelPosition: ChartLimitLine.LabelPosition, labelRotationAngle: CGFloat) {
let limitline = ChartLimitLine(limit: 50, label: "Limit Line")
limitline.labelPosition = labelPosition
limitline.labelRotationAngle = labelRotationAngle
chart.leftAxis.addLimitLine(limitline)
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.