Skip to content

Commit

Permalink
Introducing zero line which can be drawn regardless of grid lines
Browse files Browse the repository at this point in the history
  • Loading branch information
danielgindi committed Feb 11, 2016
1 parent 1690b34 commit 627118b
Show file tree
Hide file tree
Showing 7 changed files with 143 additions and 51 deletions.
17 changes: 17 additions & 0 deletions Charts/Classes/Components/ChartYAxis.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,23 @@ public class ChartYAxis: ChartAxisBase
/// if true, the set number of y-labels will be forced
public var forceLabelsEnabled = false

/// flag that indicates if the zero-line should be drawn regardless of other grid lines
public var drawZeroLineEnabled = true

/// Color of the zero line
public var zeroLineColor: UIColor? = UIColor.grayColor()

/// Width of the zero line
public var zeroLineWidth: CGFloat = 1.0

/// This is how much (in pixels) into the dash pattern are we starting from.
public var zeroLineDashPhase = CGFloat(0.0)

/// This is the actual dash pattern.
/// I.e. [2, 3] will paint [-- -- ]
/// [1, 3, 4, 2] will paint [- ---- - ---- ]
public var zeroLineDashLengths: [CGFloat]?

/// the formatter used to customly format the y-labels
public var valueFormatter: NSNumberFormatter?

Expand Down
103 changes: 77 additions & 26 deletions Charts/Classes/Renderers/ChartYAxisRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -280,46 +280,97 @@ public class ChartYAxisRenderer: ChartAxisRendererBase
{
guard let yAxis = yAxis else { return }

if (!yAxis.isDrawGridLinesEnabled || !yAxis.isEnabled)
if !yAxis.isEnabled
{
return
}

CGContextSaveGState(context)

if (!yAxis.gridAntialiasEnabled)
if yAxis.drawGridLinesEnabled
{
CGContextSetShouldAntialias(context, false)
CGContextSaveGState(context)

if (!yAxis.gridAntialiasEnabled)
{
CGContextSetShouldAntialias(context, false)
}

CGContextSetStrokeColorWithColor(context, yAxis.gridColor.CGColor)
CGContextSetLineWidth(context, yAxis.gridLineWidth)
if (yAxis.gridLineDashLengths != nil)
{
CGContextSetLineDash(context, yAxis.gridLineDashPhase, yAxis.gridLineDashLengths, yAxis.gridLineDashLengths.count)
}
else
{
CGContextSetLineDash(context, 0.0, nil, 0)
}

let valueToPixelMatrix = transformer.valueToPixelMatrix

var position = CGPoint(x: 0.0, y: 0.0)

// draw the horizontal grid
for (var i = 0, count = yAxis.entryCount; i < count; i++)
{
position.x = 0.0
position.y = CGFloat(yAxis.entries[i])
position = CGPointApplyAffineTransform(position, valueToPixelMatrix)

_gridLineBuffer[0].x = viewPortHandler.contentLeft
_gridLineBuffer[0].y = position.y
_gridLineBuffer[1].x = viewPortHandler.contentRight
_gridLineBuffer[1].y = position.y
CGContextStrokeLineSegments(context, _gridLineBuffer, 2)
}

CGContextRestoreGState(context)
}

CGContextSetStrokeColorWithColor(context, yAxis.gridColor.CGColor)
CGContextSetLineWidth(context, yAxis.gridLineWidth)
if (yAxis.gridLineDashLengths != nil)
if yAxis.drawZeroLineEnabled
{
CGContextSetLineDash(context, yAxis.gridLineDashPhase, yAxis.gridLineDashLengths, yAxis.gridLineDashLengths.count)
}
else
{
CGContextSetLineDash(context, 0.0, nil, 0)
// draw zero line

var position = CGPoint(x: 0.0, y: 0.0)
transformer.pointValueToPixel(&position)

drawZeroLine(context: context,
x1: viewPortHandler.contentLeft,
x2: viewPortHandler.contentRight,
y1: position.y,
y2: position.y);
}
}

/// Draws the zero line at the specified position.
public func drawZeroLine(
context context: CGContext,
x1: CGFloat,
x2: CGFloat,
y1: CGFloat,
y2: CGFloat)
{
guard let
yAxis = yAxis,
zeroLineColor = yAxis.zeroLineColor
else { return }

let valueToPixelMatrix = transformer.valueToPixelMatrix
CGContextSaveGState(context)

var position = CGPoint(x: 0.0, y: 0.0)
CGContextSetStrokeColorWithColor(context, zeroLineColor.CGColor)
CGContextSetLineWidth(context, yAxis.zeroLineWidth)

// draw the horizontal grid
for (var i = 0, count = yAxis.entryCount; i < count; i++)
if (yAxis.zeroLineDashLengths != nil)
{
position.x = 0.0
position.y = CGFloat(yAxis.entries[i])
position = CGPointApplyAffineTransform(position, valueToPixelMatrix)

_gridLineBuffer[0].x = viewPortHandler.contentLeft
_gridLineBuffer[0].y = position.y
_gridLineBuffer[1].x = viewPortHandler.contentRight
_gridLineBuffer[1].y = position.y
CGContextStrokeLineSegments(context, _gridLineBuffer, 2)
CGContextSetLineDash(context, yAxis.zeroLineDashPhase, yAxis.zeroLineDashLengths!, yAxis.zeroLineDashLengths!.count)
}
else
{
CGContextSetLineDash(context, 0.0, nil, 0)
}

CGContextMoveToPoint(context, x1, y1)
CGContextAddLineToPoint(context, x2, y2)
CGContextDrawPath(context, CGPathDrawingMode.Stroke)

CGContextRestoreGState(context)
}
Expand Down
65 changes: 41 additions & 24 deletions Charts/Classes/Renderers/ChartYAxisRendererHorizontalBarChart.swift
Original file line number Diff line number Diff line change
Expand Up @@ -175,41 +175,58 @@ public class ChartYAxisRendererHorizontalBarChart: ChartYAxisRenderer
{
guard let yAxis = yAxis else { return }

if (!yAxis.isEnabled || !yAxis.isDrawGridLinesEnabled)
if !yAxis.isEnabled
{
return
}

CGContextSaveGState(context)

// pre alloc
var position = CGPoint()

CGContextSetStrokeColorWithColor(context, yAxis.gridColor.CGColor)
CGContextSetLineWidth(context, yAxis.gridLineWidth)
if (yAxis.gridLineDashLengths != nil)
if yAxis.isDrawGridLinesEnabled
{
CGContextSetLineDash(context, yAxis.gridLineDashPhase, yAxis.gridLineDashLengths, yAxis.gridLineDashLengths.count)
}
else
{
CGContextSetLineDash(context, 0.0, nil, 0)
CGContextSaveGState(context)

// pre alloc
var position = CGPoint()

CGContextSetStrokeColorWithColor(context, yAxis.gridColor.CGColor)
CGContextSetLineWidth(context, yAxis.gridLineWidth)
if (yAxis.gridLineDashLengths != nil)
{
CGContextSetLineDash(context, yAxis.gridLineDashPhase, yAxis.gridLineDashLengths, yAxis.gridLineDashLengths.count)
}
else
{
CGContextSetLineDash(context, 0.0, nil, 0)
}

// draw the horizontal grid
for (var i = 0; i < yAxis.entryCount; i++)
{
position.x = CGFloat(yAxis.entries[i])
position.y = 0.0
transformer.pointValueToPixel(&position)

CGContextBeginPath(context)
CGContextMoveToPoint(context, position.x, viewPortHandler.contentTop)
CGContextAddLineToPoint(context, position.x, viewPortHandler.contentBottom)
CGContextStrokePath(context)
}

CGContextRestoreGState(context)
}

// draw the horizontal grid
for (var i = 0; i < yAxis.entryCount; i++)
if yAxis.drawZeroLineEnabled
{
position.x = CGFloat(yAxis.entries[i])
position.y = 0.0
// draw zero line

var position = CGPoint(x: 0.0, y: 0.0)
transformer.pointValueToPixel(&position)

CGContextBeginPath(context)
CGContextMoveToPoint(context, position.x, viewPortHandler.contentTop)
CGContextAddLineToPoint(context, position.x, viewPortHandler.contentBottom)
CGContextStrokePath(context)
drawZeroLine(context: context,
x1: position.x,
x2: position.x,
y1: viewPortHandler.contentTop,
y2: viewPortHandler.contentBottom);
}

CGContextRestoreGState(context)
}

private var _limitLineSegmentsBuffer = [CGPoint](count: 2, repeatedValue: CGPoint())
Expand Down
1 change: 1 addition & 0 deletions ChartsDemo/Classes/Demos/LineChart1ViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ - (void)viewDidLoad
leftAxis.customAxisMin = -50.0;
leftAxis.startAtZeroEnabled = NO;
leftAxis.gridLineDashLengths = @[@5.f, @5.f];
leftAxis.drawZeroLineEnabled = NO;
leftAxis.drawLimitLinesBehindDataEnabled = YES;

_chartView.rightAxis.enabled = NO;
Expand Down
1 change: 1 addition & 0 deletions ChartsDemo/Classes/Demos/LineChart2ViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ - (void)viewDidLoad
leftAxis.labelTextColor = [UIColor colorWithRed:51/255.f green:181/255.f blue:229/255.f alpha:1.f];
leftAxis.customAxisMax = 200.0;
leftAxis.drawGridLinesEnabled = YES;
leftAxis.drawZeroLineEnabled = NO;

ChartYAxis *rightAxis = _chartView.rightAxis;
rightAxis.labelTextColor = UIColor.redColor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ - (void)viewDidLoad
_chartView.rightAxis.startAtZeroEnabled = NO;
_chartView.rightAxis.customAxisMax = 25.0;
_chartView.rightAxis.customAxisMin = -25.0;
_chartView.rightAxis.drawGridLinesEnabled = NO;
_chartView.rightAxis.drawZeroLineEnabled = YES;
_chartView.rightAxis.labelCount = 7;
_chartView.rightAxis.valueFormatter = customFormatter;
_chartView.rightAxis.labelFont = [UIFont systemFontOfSize:9.f];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,10 @@ - (void)viewDidLoad
leftAxis.spaceBottom = 0.25f;
leftAxis.drawAxisLineEnabled = NO;
leftAxis.drawGridLinesEnabled = NO;

leftAxis.drawZeroLineEnabled = YES;
leftAxis.zeroLineColor = UIColor.grayColor;
leftAxis.zeroLineWidth = 0.7f;

_chartView.rightAxis.enabled = NO;
_chartView.legend.enabled = NO;

Expand Down

0 comments on commit 627118b

Please sign in to comment.