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

Displayed points for series in the graph. #15

Open
wants to merge 2 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
44 changes: 44 additions & 0 deletions Chart/Chart.swift
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,12 @@ class Chart: UIControl {
@IBInspectable
var lineWidth: CGFloat = 2

/**
Width of the chart's poinst.
*/
@IBInspectable
var pointWidth: CGFloat = 2

/**
Delegate for listening to Chart touch events.
*/
Expand Down Expand Up @@ -291,6 +297,12 @@ class Chart: UIControl {

if series.line {
drawLine(xValues: scaledXValues, yValues: scaledYValues, seriesIndex: index)
if series.points {
for i in 1..<scaledXValues.count {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I won't use another loop here because it is resource expensive. I think the right method for adding the markers is the drawLabelsAndGridOnXAxis, i.e. where it is drawing the vertical lines for the x-axis.

drawPoints(xValues: scaledXValues, yValues: scaledYValues, seriesIndex: index, i: i)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I understand it, drawPoints actually draws only one point for the coordinates at the i position. I'm confused by the plural in the method name. Why pass all the xValues and yValues as arguments?

}
}

}
if series.area {
drawArea(xValues: scaledXValues, yValues: scaledYValues, seriesIndex: index)
Expand Down Expand Up @@ -451,6 +463,38 @@ class Chart: UIControl {
return lineLayer
}

private func drawPoints(xValues xValues: Array<Float>, yValues: Array<Float>, seriesIndex: Int, i: Int) -> CAShapeLayer {

let circleLayer = CAShapeLayer()
let circleRadius: CGFloat = 2.0

func circleFrame(x: CGFloat, y: CGFloat) -> CGRect {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a good idea declaring a function inside a method which is looped over a relative big amount of values. I'd put the shape layer outside the class.

var circleFrame = CGRect(x: x, y: y, width: 2*circleRadius, height: 2*circleRadius)
circleFrame.origin.x = x - circleRadius
circleFrame.origin.y = y - circleRadius
return circleFrame
}

func circlePath(i: Int) -> UIBezierPath {
let x = xValues[i]
let y = yValues[i]
return UIBezierPath(ovalInRect: circleFrame(CGFloat(x), y: CGFloat(y)))
}

circleLayer.frame = self.bounds
circleLayer.path = circlePath(i).CGPath

circleLayer.lineWidth = pointWidth
circleLayer.fillColor = series[seriesIndex].colors.above.CGColor
circleLayer.strokeColor = series[seriesIndex].colors.above.CGColor
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Each chart series is using a (above, below) tuple (example), so I'm not sure – why did you choose to use the above color?


self.layer.addSublayer(circleLayer)

layerStore.append(circleLayer)

return circleLayer
}

private func drawArea(xValues xValues: Array<Float>, yValues: Array<Float>, seriesIndex: Int) {
let isAboveXAxis = isVerticalSegmentAboveXAxis(yValues)
let area = CGPathCreateMutable()
Expand Down
1 change: 1 addition & 0 deletions Chart/ChartSeries.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class ChartSeries {
var data: Array<(x: Float, y: Float)>
var area: Bool = false
var line: Bool = true
var points: Bool = false
var color: UIColor = ChartColors.blueColor() {
didSet {
colors = (above: color, below: color)
Expand Down