Skip to content

Commit

Permalink
Merge pull request #6 from FreddyZeng/feature/zengfanrong
Browse files Browse the repository at this point in the history
Y轴线条 增加延长 接口
  • Loading branch information
FreddyZeng committed Dec 15, 2017
2 parents 0fa16c9 + 4fb2f60 commit 821120e
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 11 deletions.
5 changes: 5 additions & 0 deletions Source/Charts/Components/YAxis.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ open class YAxis: AxisBase
/// flag that indicates if the axis is inverted or not
@objc open var inverted = false

/// axis Y轴水平线,左边长线
@objc open var lineFirstSpace: CGFloat = 0.0;
/// axis Y轴水平线,右边长线
@objc open var lineLastSpace: CGFloat = 0.0;

/// flag that indicates if the zero-line should be drawn regardless of other grid lines
@objc open var drawZeroLineEnabled = false

Expand Down
43 changes: 32 additions & 11 deletions Source/Charts/Renderers/YAxisRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,9 @@ open class YAxisRenderer: AxisRendererBase
yAxis = self.axis as? YAxis
else { return }

let leftSpace: CGFloat = yAxis.lineFirstSpace
let rightSpace: CGFloat = yAxis.lineLastSpace

if !yAxis.isEnabled
{
return
Expand Down Expand Up @@ -199,12 +202,30 @@ open class YAxisRenderer: AxisRendererBase
}
}

@objc open var lineFirstSpace : CGFloat {
guard let
yAxis = self.axis as? YAxis
else { return 0.0 }
return yAxis.lineFirstSpace
}

@objc open var lineLastSpace : CGFloat {
guard let
yAxis = self.axis as? YAxis
else { return 0.0 }
return yAxis.lineLastSpace
}

@objc open var gridClippingRect: CGRect
{
var contentRect = viewPortHandler.contentRect
let dy = self.axis?.gridLineWidth ?? 0.0
let leftSpace: CGFloat = lineFirstSpace
let rightSpace: CGFloat = lineLastSpace
contentRect.origin.y -= dy / 2.0
contentRect.size.height += dy
contentRect.origin.x -= leftSpace
contentRect.size.width += (leftSpace + rightSpace)
return contentRect
}

Expand All @@ -213,8 +234,8 @@ open class YAxisRenderer: AxisRendererBase
position: CGPoint)
{
context.beginPath()
context.move(to: CGPoint(x: viewPortHandler.contentLeft, y: position.y))
context.addLine(to: CGPoint(x: viewPortHandler.contentRight, y: position.y))
context.move(to: CGPoint(x: viewPortHandler.contentLeft - lineFirstSpace, y: position.y))
context.addLine(to: CGPoint(x: viewPortHandler.contentRight + lineFirstSpace + lineLastSpace, y: position.y))
context.strokePath()
}

Expand Down Expand Up @@ -271,8 +292,8 @@ open class YAxisRenderer: AxisRendererBase
context.setLineDash(phase: 0.0, lengths: [])
}

context.move(to: CGPoint(x: viewPortHandler.contentLeft, y: pos.y))
context.addLine(to: CGPoint(x: viewPortHandler.contentRight, y: pos.y))
context.move(to: CGPoint(x: viewPortHandler.contentLeft - lineFirstSpace, y: pos.y))
context.addLine(to: CGPoint(x: viewPortHandler.contentRight + lineFirstSpace + lineLastSpace, y: pos.y))
context.drawPath(using: CGPathDrawingMode.stroke)
}

Expand All @@ -299,7 +320,7 @@ open class YAxisRenderer: AxisRendererBase
let trans = transformer.valueToPixelMatrix

var position = CGPoint(x: 0.0, y: 0.0)
var bottomSpacePosition = CGPoint(x: viewPortHandler.contentLeft, y: viewPortHandler.contentHeight)// 上次渲染的line位置
var bottomSpacePosition = CGPoint(x: viewPortHandler.contentLeft - lineFirstSpace, y: viewPortHandler.contentHeight)// 上次渲染的line位置

for i in 0 ..< limitLines.count
{
Expand All @@ -323,8 +344,8 @@ open class YAxisRenderer: AxisRendererBase
position = position.applying(trans)

context.beginPath()
context.move(to: CGPoint(x: viewPortHandler.contentLeft, y: position.y))
context.addLine(to: CGPoint(x: viewPortHandler.contentRight, y: position.y))
context.move(to: CGPoint(x: viewPortHandler.contentLeft - lineFirstSpace, y: position.y))
context.addLine(to: CGPoint(x: viewPortHandler.contentRight + lineFirstSpace + lineLastSpace, y: position.y))

context.setStrokeColor(l.lineColor.cgColor)
context.setLineWidth(l.lineWidth)
Expand All @@ -344,10 +365,10 @@ open class YAxisRenderer: AxisRendererBase
context.setFillColor(l.lineBottomSpaceColor.cgColor)
context.setStrokeColor(NSUIColor.clear.cgColor)

context.move(to: CGPoint(x: viewPortHandler.contentLeft, y: position.y))
context.addLine(to: CGPoint(x: viewPortHandler.contentRight, y: position.y))
context.addLine(to: CGPoint(x: viewPortHandler.contentRight, y: bottomSpacePosition.y))
context.addLine(to: CGPoint(x: viewPortHandler.contentLeft, y: bottomSpacePosition.y))
context.move(to: CGPoint(x: viewPortHandler.contentLeft - lineFirstSpace, y: position.y))
context.addLine(to: CGPoint(x: viewPortHandler.contentRight + lineLastSpace + lineFirstSpace, y: position.y))
context.addLine(to: CGPoint(x: viewPortHandler.contentRight + lineLastSpace + lineFirstSpace, y: bottomSpacePosition.y))
context.addLine(to: CGPoint(x: viewPortHandler.contentLeft - lineFirstSpace, y: bottomSpacePosition.y))
bottomSpacePosition = CGPoint(x: position.x, y: position.y - l.lineWidth/2.0)

context.fillPath()
Expand Down

0 comments on commit 821120e

Please sign in to comment.