Skip to content

Commit

Permalink
Merge pull request #432 from pajai/master
Browse files Browse the repository at this point in the history
Make the ChartXAxisRenderer more flexible: now possible to overwrite drawing the line or label of the ChartLimitLine
  • Loading branch information
danielgindi committed Oct 8, 2015
2 parents d4482ca + d6687bc commit 10fbd96
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 72 deletions.
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

0 comments on commit 10fbd96

Please sign in to comment.