Skip to content

Commit

Permalink
Merge pull request #243 from SvenMuc/master
Browse files Browse the repository at this point in the history
Enhanced label positioning at limit lines
  • Loading branch information
danielgindi committed Aug 11, 2015
2 parents 55ba14a + bbfa8d2 commit 26c1449
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 23 deletions.
8 changes: 5 additions & 3 deletions Charts/Classes/Components/ChartLimitLine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ public class ChartLimitLine: ChartComponentBase
@objc
public enum ChartLimitLabelPosition: Int
{
case Left
case Right
case LeftTop
case LeftBottom
case RightTop
case RightBottom
}

/// limit / maximum (the y-value or xIndex)
Expand All @@ -36,7 +38,7 @@ public class ChartLimitLine: ChartComponentBase
public var valueTextColor = UIColor.blackColor()
public var valueFont = UIFont.systemFontOfSize(13.0)
public var label = ""
public var labelPosition = ChartLimitLabelPosition.Right
public var labelPosition = ChartLimitLabelPosition.RightTop

public override init()
{
Expand Down
28 changes: 24 additions & 4 deletions Charts/Classes/Renderers/ChartXAxisRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -285,26 +285,46 @@ public class ChartXAxisRenderer: ChartAxisRendererBase
var xOffset: CGFloat = l.lineWidth
var yOffset: CGFloat = add / 2.0

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -251,25 +251,45 @@ public class ChartXAxisRendererHorizontalBarChart: ChartXAxisRendererBarChart

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

if (l.labelPosition == .Right)
if (l.labelPosition == .RightTop)
{
ChartUtils.drawText(context: context,
text: label,
point: CGPoint(
x: viewPortHandler.contentRight - xOffset,
y: position.y - yOffset - labelLineHeight),
y: position.y - yOffset),
align: .Right,
attributes: [NSFontAttributeName: l.valueFont, NSForegroundColorAttributeName: l.valueTextColor])
}
else if (l.labelPosition == .RightBottom)
{
ChartUtils.drawText(context: context,
text: label,
point: CGPoint(
x: viewPortHandler.contentRight - xOffset,
y: position.y + yOffset - labelLineHeight),
align: .Right,
attributes: [NSFontAttributeName: l.valueFont, NSForegroundColorAttributeName: l.valueTextColor])
}
else if (l.labelPosition == .LeftTop)
{
ChartUtils.drawText(context: context,
text: label,
point: CGPoint(
x: viewPortHandler.contentLeft + xOffset,
y: position.y - yOffset),
align: .Left,
attributes: [NSFontAttributeName: l.valueFont, NSForegroundColorAttributeName: l.valueTextColor])
}
else
{
ChartUtils.drawText(context: context,
text: label,
point: CGPoint(
x: viewPortHandler.contentLeft + xOffset,
y: position.y - yOffset - labelLineHeight),
y: position.y + yOffset - labelLineHeight),
align: .Left,
attributes: [NSFontAttributeName: l.valueFont, NSForegroundColorAttributeName: l.valueTextColor])
}
Expand Down
28 changes: 24 additions & 4 deletions Charts/Classes/Renderers/ChartYAxisRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -357,25 +357,45 @@ public class ChartYAxisRenderer: ChartAxisRendererBase

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

if (l.labelPosition == .Right)
if (l.labelPosition == .RightTop)
{
ChartUtils.drawText(context: context,
text: label,
point: CGPoint(
x: viewPortHandler.contentRight - xOffset,
y: position.y - yOffset - labelLineHeight),
y: position.y - yOffset),
align: .Right,
attributes: [NSFontAttributeName: l.valueFont, NSForegroundColorAttributeName: l.valueTextColor])
}
else if (l.labelPosition == .RightBottom)
{
ChartUtils.drawText(context: context,
text: label,
point: CGPoint(
x: viewPortHandler.contentRight - xOffset,
y: position.y + yOffset - labelLineHeight),
align: .Right,
attributes: [NSFontAttributeName: l.valueFont, NSForegroundColorAttributeName: l.valueTextColor])
}
else if (l.labelPosition == .LeftTop)
{
ChartUtils.drawText(context: context,
text: label,
point: CGPoint(
x: viewPortHandler.contentLeft + xOffset,
y: position.y - yOffset),
align: .Left,
attributes: [NSFontAttributeName: l.valueFont, NSForegroundColorAttributeName: l.valueTextColor])
}
else
{
ChartUtils.drawText(context: context,
text: label,
point: CGPoint(
x: viewPortHandler.contentLeft + xOffset,
y: position.y - yOffset - labelLineHeight),
y: position.y + yOffset - labelLineHeight),
align: .Left,
attributes: [NSFontAttributeName: l.valueFont, NSForegroundColorAttributeName: l.valueTextColor])
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -255,27 +255,47 @@ public class ChartYAxisRendererHorizontalBarChart: ChartYAxisRenderer
let add = CGFloat(4.0)
var xOffset: CGFloat = l.lineWidth
var yOffset: CGFloat = add / 2.0

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

Expand Down
6 changes: 3 additions & 3 deletions ChartsDemo/Classes/Demos/LineChart1ViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -61,21 +61,21 @@ - (void)viewDidLoad
ChartLimitLine *llXAxis = [[ChartLimitLine alloc] initWithLimit:10.0 label:@"Index 10"];
llXAxis.lineWidth = 4.0;
llXAxis.lineDashLengths = @[@(10.f), @(10.f), @(0.f)];
llXAxis.labelPosition = ChartLimitLabelPositionRight;
llXAxis.labelPosition = ChartLimitLabelPositionRightBottom;
llXAxis.valueFont = [UIFont systemFontOfSize:10.f];

[_chartView.xAxis addLimitLine:llXAxis];

ChartLimitLine *ll1 = [[ChartLimitLine alloc] initWithLimit:130.0 label:@"Upper Limit"];
ll1.lineWidth = 4.0;
ll1.lineDashLengths = @[@5.f, @5.f];
ll1.labelPosition = ChartLimitLabelPositionRight;
ll1.labelPosition = ChartLimitLabelPositionRightTop;
ll1.valueFont = [UIFont systemFontOfSize:10.0];

ChartLimitLine *ll2 = [[ChartLimitLine alloc] initWithLimit:-30.0 label:@"Lower Limit"];
ll2.lineWidth = 4.0;
ll2.lineDashLengths = @[@5.f, @5.f];
ll2.labelPosition = ChartLimitLabelPositionRight;
ll2.labelPosition = ChartLimitLabelPositionRightBottom;
ll2.valueFont = [UIFont systemFontOfSize:10.0];

ChartYAxis *leftAxis = _chartView.leftAxis;
Expand Down

0 comments on commit 26c1449

Please sign in to comment.