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

Added Option to Place Limit Line Label Beneath Line #1983

Closed
wants to merge 5 commits into from
Closed
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
4 changes: 2 additions & 2 deletions Charts.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -1185,7 +1185,7 @@
MACOSX_DEPLOYMENT_TARGET = 10.11;
MTL_ENABLE_DEBUG_INFO = YES;
ONLY_ACTIVE_ARCH = YES;
SDKROOT = macosx;
SDKROOT = iphoneos;
SUPPORTED_PLATFORMS = "iphonesimulator iphoneos appletvsimulator appletvos macosx";
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 3.0;
Expand Down Expand Up @@ -1234,7 +1234,7 @@
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
MACOSX_DEPLOYMENT_TARGET = 10.11;
MTL_ENABLE_DEBUG_INFO = NO;
SDKROOT = macosx;
SDKROOT = iphoneos;
SUPPORTED_PLATFORMS = "iphonesimulator iphoneos appletvsimulator appletvos macosx";
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
SWIFT_VERSION = 3.0;
Expand Down
1 change: 1 addition & 0 deletions Source/Charts/Components/ChartLimitLine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ open class ChartLimitLine: ComponentBase
case leftBottom
case rightTop
case rightBottom
case centerBottom
}

/// limit / maximum (the y-value or xIndex)
Expand Down
35 changes: 32 additions & 3 deletions Source/Charts/Renderers/BarChartRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,24 @@ open class BarChartRenderer: BarLineScatterCandleBubbleRenderer
}
}

open func calcWidth(span: CGFloat) -> CGFloat
{
var width = CGFloat(60.0)
if (span < 240) { //z0
width = 60;
} else if (span < 1200) { //z1
width = 240;
} else if (span < 10000) { //z2
width = 960;
} else if (span < 80000) { //z3
width = 4800*3;
} else { //z4
width = 15360*2;
}
return width;
}


fileprivate func prepareBuffer(dataSet: IBarChartDataSet, index: Int)
{
guard
Expand Down Expand Up @@ -101,6 +119,10 @@ open class BarChartRenderer: BarLineScatterCandleBubbleRenderer
{
let left = CGFloat(x - barWidthHalf)
let right = CGFloat(x + barWidthHalf)

let span = right - left;
let width = calcWidth(span: span)

var top = isInverted
? (y <= 0.0 ? CGFloat(y) : 0)
: (y >= 0.0 ? CGFloat(y) : 0)
Expand All @@ -119,7 +141,7 @@ open class BarChartRenderer: BarLineScatterCandleBubbleRenderer
}

barRect.origin.x = left
barRect.size.width = right - left
barRect.size.width = width;//right - left
barRect.origin.y = top
barRect.size.height = bottom - top

Expand Down Expand Up @@ -164,8 +186,11 @@ open class BarChartRenderer: BarLineScatterCandleBubbleRenderer
top *= CGFloat(phaseY)
bottom *= CGFloat(phaseY)

let span = right - left;
let width = calcWidth(span: span)

barRect.origin.x = left
barRect.size.width = right - left
barRect.size.width = width//right - left
barRect.origin.y = top
barRect.size.height = bottom - top

Expand Down Expand Up @@ -339,7 +364,11 @@ open class BarChartRenderer: BarLineScatterCandleBubbleRenderer

rect.origin.x = CGFloat(left)
rect.origin.y = CGFloat(top)
rect.size.width = CGFloat(right - left)

let span = right - left;
let width = calcWidth(span: CGFloat(span))

rect.size.width = CGFloat(width) //right - left
rect.size.height = CGFloat(bottom - top)

trans.rectValueToPixel(&rect, phaseY: animator?.phaseY ?? 1.0)
Expand Down
17 changes: 16 additions & 1 deletion Source/Charts/Renderers/XAxisRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,12 @@ open class XAxisRenderer: AxisRendererBase

context.beginPath()
context.move(to: CGPoint(x: position.x, y: viewPortHandler.contentTop))
context.addLine(to: CGPoint(x: position.x, y: viewPortHandler.contentBottom))

if limitLine.labelPosition == .centerBottom {
context.addLine(to: CGPoint(x: position.x, y: viewPortHandler.contentBottom-limitLine.valueFont.lineHeight))
} else {
context.addLine(to: CGPoint(x: position.x, y: viewPortHandler.contentBottom))
}

context.setStrokeColor(limitLine.lineColor.cgColor)
context.setLineWidth(limitLine.lineWidth)
Expand Down Expand Up @@ -457,6 +462,16 @@ open class XAxisRenderer: AxisRendererBase
align: .left,
attributes: [NSFontAttributeName: limitLine.valueFont, NSForegroundColorAttributeName: limitLine.valueTextColor])
}
else if limitLine.labelPosition == .centerBottom
{
ChartUtils.drawText(context: context,
text: label,
point: CGPoint(
x: position.x,
y: viewPortHandler.contentBottom - labelLineHeight),
align: .center,
attributes: [NSFontAttributeName: limitLine.valueFont, NSForegroundColorAttributeName: limitLine.valueTextColor])
}
else if limitLine.labelPosition == .leftTop
{
ChartUtils.drawText(context: context,
Expand Down