Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added width parameter for lineSeries #118

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 23 additions & 5 deletions Source/Chart.swift
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,22 @@ open class Chart: UIControl {
Enable the lines for the labels on the x-axis
*/
open var showXLabelsAndGrid: Bool = true

/**
Show the lines for the labels on the x-axis with dashed pattern
*/
open var showXGridDashed: Bool = true

/**
Enable the lines for the labels on the y-axis
*/
open var showYLabelsAndGrid: Bool = true

/**
Show the lines for the labels on the y-axis with dashed pattern
*/
open var showYGridDashed: Bool = true

/**
Height of the area at the bottom of the chart, containing the labels for the x-axis.
*/
Expand Down Expand Up @@ -342,7 +353,7 @@ open class Chart: UIControl {
let scaledYValues = scaleValuesOnYAxis( segment.map { $0.y } )

if series.line {
drawLine(scaledXValues, yValues: scaledYValues, seriesIndex: index)
drawLine(scaledXValues, yValues: scaledYValues, seriesIndex: index, width: series.width)
}
if series.area {
drawArea(scaledXValues, yValues: scaledYValues, seriesIndex: index)
Expand Down Expand Up @@ -461,7 +472,7 @@ open class Chart: UIControl {

// MARK: - Drawings

fileprivate func drawLine(_ xValues: [Double], yValues: [Double], seriesIndex: Int) {
fileprivate func drawLine(_ xValues: [Double], yValues: [Double], seriesIndex: Int, width: CGFloat?) {
// YValues are "reverted" from top to bottom, so 'above' means <= level
let isAboveZeroLine = yValues.max()! <= self.scaleValueOnYAxis(series[seriesIndex].colors.zeroLevel)
let path = CGMutablePath()
Expand All @@ -474,14 +485,15 @@ open class Chart: UIControl {
let lineLayer = CAShapeLayer()
lineLayer.frame = self.bounds
lineLayer.path = path
lineLayer.lineDashPattern = series[seriesIndex].dashed ? [7, 3] : nil

if isAboveZeroLine {
lineLayer.strokeColor = series[seriesIndex].colors.above.cgColor
} else {
lineLayer.strokeColor = series[seriesIndex].colors.below.cgColor
}
lineLayer.fillColor = nil
lineLayer.lineWidth = lineWidth
lineLayer.lineWidth = width ?? lineWidth
lineLayer.lineJoin = CAShapeLayerLineJoin.bevel

self.layer.addSublayer(lineLayer)
Expand Down Expand Up @@ -575,6 +587,12 @@ open class Chart: UIControl {
if x != 0 && x != drawingWidth {
context.move(to: CGPoint(x: x, y: CGFloat(0)))
context.addLine(to: CGPoint(x: x, y: bounds.height))
if labels[i] != 0 && showXGridDashed {
// Vertical grid for 0 is not dashed or when showXGrddDashed is false
context.setLineDash(phase: CGFloat(0), lengths: [CGFloat(5)])
} else {
context.setLineDash(phase: CGFloat(0), lengths: [])
}
context.strokePath()
}

Expand Down Expand Up @@ -649,8 +667,8 @@ open class Chart: UIControl {

context.move(to: CGPoint(x: CGFloat(0), y: y))
context.addLine(to: CGPoint(x: self.bounds.width, y: y))
if labels[i] != 0 {
// Horizontal grid for 0 is not dashed
if labels[i] != 0 && showYGridDashed {
// Horizontal grid for 0 is not dashed or when showYGrodDashed is false
context.setLineDash(phase: CGFloat(0), lengths: [CGFloat(5)])
} else {
context.setLineDash(phase: CGFloat(0), lengths: [])
Expand Down
11 changes: 11 additions & 0 deletions Source/ChartSeries.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,17 @@ open class ChartSeries {
*/
open var line: Bool = true

/**
Draws the line in a dashed pattern.
*/

open var dashed: Bool = false

/**
The width of the series line. Useful when different line widths are required
*/
open var width: CGFloat?

/**
Draws an area below the series line.
*/
Expand Down