Skip to content

Commit

Permalink
Merge pull request #1 from fumoboy007/master
Browse files Browse the repository at this point in the history
Fix bug in `XBounds` calculation of minimum/maximum visible entry index.
  • Loading branch information
JackMoseley2001 authored Jul 30, 2022
2 parents b38b8d4 + cfd7e8e commit 2643deb
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ open class ChartDataSet: ChartBaseDataSet
rounding: ChartDataSetRounding) -> Int
{
var closest = partitioningIndex { $0.x >= xValue }
guard closest < endIndex else { return rounding == .closest ? (endIndex-1) : -1 }
guard closest < endIndex else { return [.down, .closest].contains(rounding) ? (endIndex-1) : -1 }

var closestXValue = self[closest].x

Expand Down
12 changes: 9 additions & 3 deletions Source/Charts/Renderers/BarLineScatterCandleBubbleRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,15 @@ open class BarLineScatterCandleBubbleRenderer: NSObject, DataRenderer

let low = chart.lowestVisibleX
let high = chart.highestVisibleX

let entryFrom = dataSet.entryForXValue(low, closestToY: .nan, rounding: .down)
let entryTo = dataSet.entryForXValue(high, closestToY: .nan, rounding: .up)

// First, try to find entries on the boundary of or outside of the visible range. Then, if there are none, try to find entries
// inside of the visible range.
//
// We want to allow and prioritize entries outside of the visible range because renderers may draw graphics in between entries.
// For example, a zoomed-in line graph should still show a line connecting the entry outside of the visible range with the entry
// inside of the visible range even if the line is only partially shown.
let entryFrom = dataSet.entryForXValue(low, closestToY: .nan, rounding: .down) ?? dataSet.entryForXValue(low, closestToY: .nan, rounding: .up)
let entryTo = dataSet.entryForXValue(high, closestToY: .nan, rounding: .up) ?? dataSet.entryForXValue(high, closestToY: .nan, rounding: .down)

self.min = entryFrom == nil ? 0 : dataSet.entryIndex(entry: entryFrom!)
self.max = entryTo == nil ? 0 : dataSet.entryIndex(entry: entryTo!)
Expand Down

0 comments on commit 2643deb

Please sign in to comment.