             let circleHoleRadius = circleHoleDiameter / 2.0
             let isDrawCircleHoleEnabled = dataSet.isDrawCircleHoleEnabled
             
-            var optinalFromIndex: Int?
-            for x in (self.minX...self.maxX) {
-                if let entry = dataSet.entryForXIndex(x) {
-                    optinalFromIndex = dataSet.entryIndex(entry: entry)
-                    break
-                }
-            }
-            guard let fromIndex = optinalFromIndex else { return }
-            let toIndex = dataSet.entryIndex(xIndex: self.maxX)
+            guard let (fromIndex, toIndex) = getFirstAndLastIndex(dataSet) else { return }
+
             
             let diff = (fromIndex == toIndex) ? 1 : 0
             let minx = max(fromIndex - diff, 0)

                
                let entryCount = dataSet.entryCount
                
                var optinalFromIndex: Int?
                for x in (self.minX...self.maxX) {
                    if let entry = dataSet.entryForXIndex(x) {
                        optinalFromIndex = dataSet.entryIndex(entry: entry)
                        break
                    }
                }
                
                guard let fromIndex = optinalFromIndex else { return }
                let toIndex = dataSet.entryIndex(xIndex: self.maxX)
                
                let diff = (fromIndex == toIndex) ? 1 : 0
                let minx = max(fromIndex - diff, 0)

        CGContextRestoreGState(context)
    }
    
    
    // returns: The first visible non-nil-entry (first) and the one before the next nil-entry (last)
    internal func getFirstAndLastIndex(dataSet: ILineChartDataSet) -> (first: Int, last: Int)? {
        var fromEnty: ChartDataEntry?
        var toEntry: ChartDataEntry?
        for x in (self.minX...self.maxX) {
