Skip to content

Commit

Permalink
Normally we should iterate only until (and excluding) endIndex of a R…
Browse files Browse the repository at this point in the history
…ange.

Updated code related to activePointsInterval that did otherwise.
This was mainly to fix startAnimations which were iterating from
startIndex to endIndex and thus iterating once even on empty range 0..<0.

Also, since activePointsInterval.endIndex can’t be bigger than 
data.count we can simplify some checks.
  • Loading branch information
Andris committed Aug 3, 2016
1 parent 5b03c2a commit 11ee073
Showing 1 changed file with 7 additions and 12 deletions.
19 changes: 7 additions & 12 deletions Classes/ScrollableGraphView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -647,12 +647,12 @@ import UIKit
let actualMin = clamp(min - numberOfPointsOffscreen, min: minPossible, max: maxPossible)
let actualMax = clamp(max + numberOfPointsOffscreen, min: minPossible, max: maxPossible)

return actualMin ..< actualMax
return actualMin ... actualMax
}

private func calculateRangeForActivePointsInterval(interval: Range<Int>) -> (min: Double, max: Double) {

let dataForActivePoints = data[interval.startIndex...interval.endIndex]
let dataForActivePoints = data[interval]

// We don't have any active points, return defaults.
if(dataForActivePoints.count == 0) {
Expand Down Expand Up @@ -761,8 +761,6 @@ import UIKit

currentLinePath.removeAllPoints()

let numberOfPoints = min(data.count, activePointsInterval.endIndex)

let pathSegmentAdder = lineStyle == .Straight ? addStraightLineSegment : addCurvedLineSegment

zeroYPosition = calculatePosition(0, value: self.range.min).y
Expand All @@ -787,7 +785,7 @@ import UIKit
}

// Connect each point on the graph with a segment.
for i in activePointsInterval.startIndex ..< numberOfPoints {
for i in activePointsInterval.startIndex ..< activePointsInterval.endIndex - 1 {

let startPoint = graphPoints[i].location
let endPoint = graphPoints[i+1].location
Expand All @@ -798,7 +796,7 @@ import UIKit
// Connect the line to the ending edge if we are filling it.
if(shouldFill) {
// Add a line from the last data point to the base of the graph.
let lastDataPoint = graphPoints[activePointsInterval.endIndex]
let lastDataPoint = graphPoints[activePointsInterval.endIndex - 1]

let viewportRightZero = CGPoint(x: lastDataPoint.x + (rightmostPointPadding), y: zeroYPosition)
let rightFarEdgeTop = CGPoint(x: lastDataPoint.x + (rightmostPointPadding + viewportWidth), y: zeroYPosition)
Expand Down Expand Up @@ -981,7 +979,7 @@ import UIKit
// For any visible points, kickoff the animation to their new position after the axis' min/max has changed.
//let numberOfPointsToAnimate = pointsToAnimate.endIndex - pointsToAnimate.startIndex
var index = 0
for i in pointsToAnimate.startIndex ... pointsToAnimate.endIndex {
for i in pointsToAnimate {
let newPosition = calculatePosition(i, value: data[i])
let point = graphPoints[i]
animatePoint(point, toPosition: newPosition, withDelay: Double(index) * stagger)
Expand Down Expand Up @@ -1294,9 +1292,7 @@ private class BarDrawingLayer: ScrollableGraphViewDrawingLayer {
return barPath
}

let numberOfPoints = min(data.count, activePointsInterval.endIndex)

for i in activePointsInterval.startIndex ... numberOfPoints {
for i in activePointsInterval {

var location = CGPointZero

Expand Down Expand Up @@ -1380,9 +1376,8 @@ private class DataPointDrawingLayer: ScrollableGraphViewDrawingLayer {
}

let pointPathCreator = getPointPathCreator()
let numberOfPoints = min(data.count, activePointsInterval.endIndex)

for i in activePointsInterval.startIndex ... numberOfPoints {
for i in activePointsInterval {

var location = CGPointZero

Expand Down

0 comments on commit 11ee073

Please sign in to comment.