Skip to content

Commit

Permalink
Add half line width to clipping rect of grid/limit lines (Fixes #1204)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielgindi committed Aug 14, 2016
1 parent a8cf54a commit c05fbd8
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 27 deletions.
13 changes: 4 additions & 9 deletions Charts/Classes/Charts/BarLineChartViewBase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -205,10 +205,7 @@ public class BarLineChartViewBase: ChartViewBase, BarLineScatterCandleBubbleChar
}
}

// make sure the graph values and grid cannot be drawn outside the content-rect
CGContextSaveGState(context)
CGContextClipToRect(context, _viewPortHandler.contentRect)

// The renderers are responsible for clipping, to account for line-width center etc.
_xAxisRenderer?.renderGridLines(context: context)
_leftYAxisRenderer?.renderGridLines(context: context)
_rightYAxisRenderer?.renderGridLines(context: context)
Expand All @@ -226,6 +223,9 @@ public class BarLineChartViewBase: ChartViewBase, BarLineScatterCandleBubbleChar
_rightYAxisRenderer?.renderLimitLines(context: context)
}

// make sure the data cannot be drawn outside the content-rect
CGContextSaveGState(context)
CGContextClipToRect(context, _viewPortHandler.contentRect)
renderer?.drawData(context: context)

// if highlighting is enabled
Expand All @@ -238,9 +238,6 @@ public class BarLineChartViewBase: ChartViewBase, BarLineScatterCandleBubbleChar

renderer!.drawExtras(context: context)

CGContextSaveGState(context)
CGContextClipToRect(context, _viewPortHandler.contentRect)

if !_xAxis.isDrawLimitLinesBehindDataEnabled
{
_xAxisRenderer?.renderLimitLines(context: context)
Expand All @@ -254,8 +251,6 @@ public class BarLineChartViewBase: ChartViewBase, BarLineScatterCandleBubbleChar
_rightYAxisRenderer?.renderLimitLines(context: context)
}

CGContextRestoreGState(context)

_xAxisRenderer.renderAxisLabels(context: context)
_leftYAxisRenderer.renderAxisLabels(context: context)
_rightYAxisRenderer.renderAxisLabels(context: context)
Expand Down
24 changes: 17 additions & 7 deletions Charts/Classes/Renderers/XAxisRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,8 @@ public class XAxisRenderer: AxisRendererBase
}

CGContextSaveGState(context)
defer { CGContextRestoreGState(context) }
CGContextClipToRect(context, self.gridClippingRect)

CGContextSetShouldAntialias(context, xAxis.gridAntialiasEnabled)
CGContextSetStrokeColorWithColor(context, xAxis.gridColor.CGColor)
Expand Down Expand Up @@ -322,8 +324,13 @@ public class XAxisRenderer: AxisRendererBase

drawGridLine(context: context, x: position.x, y: position.y)
}

CGContextRestoreGState(context)
}

public var gridClippingRect: CGRect
{
var contentRect = viewPortHandler?.contentRect ?? CGRectZero
contentRect.insetInPlace(dx: -(self.axis?.gridLineWidth ?? 0.0) / 2.0, dy: 0.0)
return contentRect
}

public func drawGridLine(context context: CGContext, x: CGFloat, y: CGFloat)
Expand All @@ -347,6 +354,7 @@ public class XAxisRenderer: AxisRendererBase
{
guard let
xAxis = self.axis as? ChartXAxis,
viewPortHandler = self.viewPortHandler,
transformer = self.transformer
else { return }

Expand All @@ -357,8 +365,6 @@ public class XAxisRenderer: AxisRendererBase
return
}

CGContextSaveGState(context)

let trans = transformer.valueToPixelMatrix

var position = CGPoint(x: 0.0, y: 0.0)
Expand All @@ -371,16 +377,20 @@ public class XAxisRenderer: AxisRendererBase
{
continue
}


CGContextSaveGState(context)
defer { CGContextRestoreGState(context) }
var clippingRect = viewPortHandler.contentRect
clippingRect.insetInPlace(dx: -l.lineWidth / 2.0, dy: 0.0)
CGContextClipToRect(context, clippingRect)

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

renderLimitLineLine(context: context, limitLine: l, position: position)
renderLimitLineLabel(context: context, limitLine: l, position: position, yOffset: 2.0 + l.yOffset)
}

CGContextRestoreGState(context)
}

private var _limitLineSegmentsBuffer = [CGPoint](count: 2, repeatedValue: CGPoint())
Expand Down
17 changes: 13 additions & 4 deletions Charts/Classes/Renderers/XAxisRendererHorizontalBarChart.swift
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,13 @@ public class XAxisRendererHorizontalBarChart: XAxisRenderer
angleRadians: angleRadians)
}

public override var gridClippingRect: CGRect
{
var contentRect = viewPortHandler?.contentRect ?? CGRectZero
contentRect.insetInPlace(dx: 0.0, dy: -(self.axis?.gridLineWidth ?? 0.0) / 2.0)
return contentRect
}

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

public override func drawGridLine(context context: CGContext, x: CGFloat, y: CGFloat)
Expand Down Expand Up @@ -275,8 +282,6 @@ public class XAxisRendererHorizontalBarChart: XAxisRenderer
return
}

CGContextSaveGState(context)

let trans = transformer.valueToPixelMatrix

var position = CGPoint(x: 0.0, y: 0.0)
Expand All @@ -289,6 +294,12 @@ public class XAxisRendererHorizontalBarChart: XAxisRenderer
{
continue
}

CGContextSaveGState(context)
defer { CGContextRestoreGState(context) }
var clippingRect = viewPortHandler.contentRect
clippingRect.insetInPlace(dx: 0.0, dy: -l.lineWidth / 2.0)
CGContextClipToRect(context, clippingRect)

position.x = 0.0
position.y = CGFloat(l.limit)
Expand Down Expand Up @@ -364,7 +375,5 @@ public class XAxisRendererHorizontalBarChart: XAxisRenderer
}
}
}

CGContextRestoreGState(context)
}
}
23 changes: 19 additions & 4 deletions Charts/Classes/Renderers/YAxisRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,8 @@ public class YAxisRenderer: AxisRendererBase
let positions = transformedPositions()

CGContextSaveGState(context)
defer { CGContextRestoreGState(context) }
CGContextClipToRect(context, self.gridClippingRect)

CGContextSetShouldAntialias(context, yAxis.gridAntialiasEnabled)
CGContextSetStrokeColorWithColor(context, yAxis.gridColor.CGColor)
Expand All @@ -199,8 +201,6 @@ public class YAxisRenderer: AxisRendererBase
{
drawGridLine(context: context, position: positions[i])
}

CGContextRestoreGState(context)
}

if yAxis.drawZeroLineEnabled
Expand All @@ -210,6 +210,13 @@ public class YAxisRenderer: AxisRendererBase
}
}

public var gridClippingRect: CGRect
{
var contentRect = viewPortHandler?.contentRect ?? CGRectZero
contentRect.insetInPlace(dx: 0.0, dy: -(self.axis?.gridLineWidth ?? 0.0) / 2.0)
return contentRect
}

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

public func drawGridLine(
Expand Down Expand Up @@ -261,6 +268,10 @@ public class YAxisRenderer: AxisRendererBase
else { return }

CGContextSaveGState(context)
defer { CGContextRestoreGState(context) }
var clippingRect = viewPortHandler.contentRect
clippingRect.insetInPlace(dx: 0.0, dy: yAxis.zeroLineWidth / 2.0)
CGContextClipToRect(context, clippingRect)

CGContextSetStrokeColorWithColor(context, zeroLineColor.CGColor)
CGContextSetLineWidth(context, yAxis.zeroLineWidth)
Expand All @@ -279,8 +290,6 @@ public class YAxisRenderer: AxisRendererBase
CGContextMoveToPoint(context, viewPortHandler.contentLeft, pos.y - 1.0)
CGContextAddLineToPoint(context, viewPortHandler.contentRight, pos.y - 1.0)
CGContextDrawPath(context, CGPathDrawingMode.Stroke)

CGContextRestoreGState(context)
}

private var _limitLineSegmentsBuffer = [CGPoint](count: 2, repeatedValue: CGPoint())
Expand Down Expand Up @@ -315,6 +324,12 @@ public class YAxisRenderer: AxisRendererBase
continue
}

CGContextSaveGState(context)
defer { CGContextRestoreGState(context) }
var clippingRect = viewPortHandler.contentRect
clippingRect.insetInPlace(dx: 0.0, dy: -l.lineWidth / 2.0)
CGContextClipToRect(context, clippingRect)

position.x = 0.0
position.y = CGFloat(l.limit)
position = CGPointApplyAffineTransform(position, trans)
Expand Down
19 changes: 17 additions & 2 deletions Charts/Classes/Renderers/YAxisRendererHorizontalBarChart.swift
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,13 @@ public class YAxisRendererHorizontalBarChart: YAxisRenderer
}
}

public override var gridClippingRect: CGRect
{
var contentRect = viewPortHandler?.contentRect ?? CGRectZero
contentRect.insetInPlace(dx: -(self.axis?.gridLineWidth ?? 0.0) / 2.0, dy: 0.0)
return contentRect
}

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

public override func drawGridLine(
Expand Down Expand Up @@ -239,6 +246,10 @@ public class YAxisRendererHorizontalBarChart: YAxisRenderer
else { return }

CGContextSaveGState(context)
defer { CGContextRestoreGState(context) }
var clippingRect = viewPortHandler.contentRect
clippingRect.insetInPlace(dx: yAxis.zeroLineWidth / 2.0, dy: 0.0)
CGContextClipToRect(context, clippingRect)

CGContextSetStrokeColorWithColor(context, zeroLineColor.CGColor)
CGContextSetLineWidth(context, yAxis.zeroLineWidth)
Expand All @@ -257,8 +268,6 @@ public class YAxisRendererHorizontalBarChart: YAxisRenderer
CGContextMoveToPoint(context, pos.x - 1.0, viewPortHandler.contentTop)
CGContextAddLineToPoint(context, pos.x - 1.0, viewPortHandler.contentBottom)
CGContextDrawPath(context, CGPathDrawingMode.Stroke)

CGContextRestoreGState(context)
}

private var _limitLineSegmentsBuffer = [CGPoint](count: 2, repeatedValue: CGPoint())
Expand Down Expand Up @@ -293,6 +302,12 @@ public class YAxisRendererHorizontalBarChart: YAxisRenderer
continue
}

CGContextSaveGState(context)
defer { CGContextRestoreGState(context) }
var clippingRect = viewPortHandler.contentRect
clippingRect.insetInPlace(dx: -l.lineWidth / 2.0, dy: 0.0)
CGContextClipToRect(context, clippingRect)

position.x = CGFloat(l.limit)
position.y = 0.0
position = CGPointApplyAffineTransform(position, trans)
Expand Down
2 changes: 1 addition & 1 deletion ChartsDemo/Classes/Demos/LineChart2ViewController.xib
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="10117" systemVersion="15F34" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="10117" systemVersion="15G31" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES">
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="10085"/>
Expand Down

0 comments on commit c05fbd8

Please sign in to comment.