private fun highlightGraphEvent(xTapCoordinate: Int, yTapCoordinate: Int) { var closestEntry: Entry? = null var minDistance: Int? = null // Filter for BG's, loop through, checking whether 'Tap' coordinates are within range of each BG lineData.getDataSetByLabel("BG", true).let { for (index in 0 until it.entryCount) { val entry = it.getEntryForIndex(index) // Calculate the distance from the BG distanceFromBG(entry, xTapCoordinate, yTapCoordinate)?.let { calcDistFromBG -> // Update minDistance if minDistace == null or calculated distance is less than minDistance?.let { currentMinDist -> if (calcDistFromBG < currentMinDist) { minDistance = calcDistFromBG closestEntry = entry.data as Bg } } ?: calcDistFromBG } } }