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

Make the ChartXAxisRenderer more flexible: now possible to overwrite drawing the line or label of the ChartLimitLine #432

Merged
merged 5 commits into from
Oct 8, 2015
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
2 changes: 1 addition & 1 deletion Charts/Classes/Renderers/ChartAxisRendererBase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import UIKit

public class ChartAxisRendererBase: ChartRendererBase
{
internal var transformer: ChartTransformer!
public var transformer: ChartTransformer!

public override init()
{
Expand Down
146 changes: 77 additions & 69 deletions Charts/Classes/Renderers/ChartXAxisRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -237,8 +237,6 @@ public class ChartXAxisRenderer: ChartAxisRendererBase
CGContextRestoreGState(context)
}

private var _limitLineSegmentsBuffer = [CGPoint](count: 2, repeatedValue: CGPoint())

public override func renderLimitLines(context context: CGContext?)
{
var limitLines = _xAxis.limitLines
Expand All @@ -257,83 +255,93 @@ public class ChartXAxisRenderer: ChartAxisRendererBase
for (var i = 0; i < limitLines.count; i++)
{
let l = limitLines[i]

position.x = CGFloat(l.limit)
position.y = 0.0
position = CGPointApplyAffineTransform(position, trans)

_limitLineSegmentsBuffer[0].x = position.x
_limitLineSegmentsBuffer[0].y = viewPortHandler.contentTop
_limitLineSegmentsBuffer[1].x = position.x
_limitLineSegmentsBuffer[1].y = viewPortHandler.contentBottom
renderLimitLineLine(context: context, limitLine: l, position: position)
renderLimitLineLabel(context: context, limitLine: l, position: position)
}

CGContextRestoreGState(context)
}

private var _limitLineSegmentsBuffer = [CGPoint](count: 2, repeatedValue: CGPoint())

public func renderLimitLineLine(context context: CGContext?, limitLine: ChartLimitLine, position: CGPoint)
{
_limitLineSegmentsBuffer[0].x = position.x
_limitLineSegmentsBuffer[0].y = viewPortHandler.contentTop
_limitLineSegmentsBuffer[1].x = position.x
_limitLineSegmentsBuffer[1].y = viewPortHandler.contentBottom

CGContextSetStrokeColorWithColor(context, limitLine.lineColor.CGColor)
CGContextSetLineWidth(context, limitLine.lineWidth)
if (limitLine.lineDashLengths != nil)
{
CGContextSetLineDash(context, limitLine.lineDashPhase, limitLine.lineDashLengths!, limitLine.lineDashLengths!.count)
}
else
{
CGContextSetLineDash(context, 0.0, nil, 0)
}

CGContextStrokeLineSegments(context, _limitLineSegmentsBuffer, 2)
}

public func renderLimitLineLabel(context context: CGContext?, limitLine: ChartLimitLine, position: CGPoint, yOffset: CGFloat = 2.0)
{
let label = limitLine.label

// if drawing the limit-value label is enabled
if (label.characters.count > 0)
{
let labelLineHeight = limitLine.valueFont.lineHeight

let xOffset: CGFloat = limitLine.lineWidth

CGContextSetStrokeColorWithColor(context, l.lineColor.CGColor)
CGContextSetLineWidth(context, l.lineWidth)
if (l.lineDashLengths != nil)
if (limitLine.labelPosition == .RightTop)
{
CGContextSetLineDash(context, l.lineDashPhase, l.lineDashLengths!, l.lineDashLengths!.count)
ChartUtils.drawText(context: context,
text: label,
point: CGPoint(
x: position.x + xOffset,
y: viewPortHandler.contentTop + yOffset),
align: .Left,
attributes: [NSFontAttributeName: limitLine.valueFont, NSForegroundColorAttributeName: limitLine.valueTextColor])
}
else
else if (limitLine.labelPosition == .RightBottom)
{
CGContextSetLineDash(context, 0.0, nil, 0)
ChartUtils.drawText(context: context,
text: label,
point: CGPoint(
x: position.x + xOffset,
y: viewPortHandler.contentBottom - labelLineHeight - yOffset),
align: .Left,
attributes: [NSFontAttributeName: limitLine.valueFont, NSForegroundColorAttributeName: limitLine.valueTextColor])
}

CGContextStrokeLineSegments(context, _limitLineSegmentsBuffer, 2)

let label = l.label

// if drawing the limit-value label is enabled
if (label.characters.count > 0)
else if (limitLine.labelPosition == .LeftTop)
{
let labelLineHeight = l.valueFont.lineHeight

let add = CGFloat(4.0)
let xOffset: CGFloat = l.lineWidth
let yOffset: CGFloat = add / 2.0

if (l.labelPosition == .RightTop)
{
ChartUtils.drawText(context: context,
text: label,
point: CGPoint(
x: position.x + xOffset,
y: viewPortHandler.contentTop + yOffset),
align: .Left,
attributes: [NSFontAttributeName: l.valueFont, NSForegroundColorAttributeName: l.valueTextColor])
}
else if (l.labelPosition == .RightBottom)
{
ChartUtils.drawText(context: context,
text: label,
point: CGPoint(
x: position.x + xOffset,
y: viewPortHandler.contentBottom - labelLineHeight - yOffset),
align: .Left,
attributes: [NSFontAttributeName: l.valueFont, NSForegroundColorAttributeName: l.valueTextColor])
}
else if (l.labelPosition == .LeftTop)
{
ChartUtils.drawText(context: context,
text: label,
point: CGPoint(
x: position.x - xOffset,
y: viewPortHandler.contentTop + yOffset),
align: .Right,
attributes: [NSFontAttributeName: l.valueFont, NSForegroundColorAttributeName: l.valueTextColor])
}
else
{
ChartUtils.drawText(context: context,
text: label,
point: CGPoint(
x: position.x - xOffset,
y: viewPortHandler.contentBottom - labelLineHeight - yOffset),
align: .Right,
attributes: [NSFontAttributeName: l.valueFont, NSForegroundColorAttributeName: l.valueTextColor])
}
ChartUtils.drawText(context: context,
text: label,
point: CGPoint(
x: position.x - xOffset,
y: viewPortHandler.contentTop + yOffset),
align: .Right,
attributes: [NSFontAttributeName: limitLine.valueFont, NSForegroundColorAttributeName: limitLine.valueTextColor])
}
else
{
ChartUtils.drawText(context: context,
text: label,
point: CGPoint(
x: position.x - xOffset,
y: viewPortHandler.contentBottom - labelLineHeight - yOffset),
align: .Right,
attributes: [NSFontAttributeName: limitLine.valueFont, NSForegroundColorAttributeName: limitLine.valueTextColor])
}
}

CGContextRestoreGState(context)
}
}

}
4 changes: 2 additions & 2 deletions Charts/Classes/Utils/ChartUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import Foundation
import UIKit
import Darwin

internal class ChartUtils
public class ChartUtils
{
internal struct Math
{
Expand Down Expand Up @@ -118,7 +118,7 @@ internal class ChartUtils
)
}

internal class func drawText(context context: CGContext?, text: String, var point: CGPoint, align: NSTextAlignment, attributes: [String : AnyObject]?)
public class func drawText(context context: CGContext?, text: String, var point: CGPoint, align: NSTextAlignment, attributes: [String : AnyObject]?)
{
if (align == .Center)
{
Expand Down