Skip to content

Commit

Permalink
BarLineScatterCandleBubbleRenderer.XBounds conformance to RangeExpres…
Browse files Browse the repository at this point in the history
…sion and Sequence

Sequence conformance simplifies for-in loops
Looking forward to when data types conforming to Collection, RangeExpression conformance by XBounds allows for slicing of the collections further simplifying algorithms on data/datasets.
  • Loading branch information
jjatie committed Jan 26, 2019
1 parent f2795b9 commit 2a1ecb4
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 12 deletions.
38 changes: 36 additions & 2 deletions Source/Charts/Renderers/BarLineScatterCandleBubbleRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,46 @@ open class BarLineScatterCandleBubbleRenderer: DataRenderer
let low = chart.lowestVisibleX
let high = chart.highestVisibleX

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

self.min = entryFrom == nil ? 0 : dataSet.entryIndex(entry: entryFrom!)
self.max = entryTo == nil ? 0 : dataSet.entryIndex(entry: entryTo!)
range = Int(Double(self.max - self.min) * phaseX)
}
}
}

extension BarLineScatterCandleBubbleRenderer.XBounds: RangeExpression {
public func relative<C>(to collection: C) -> Swift.Range<Int>
where C : Collection, Bound == C.Index
{
return Swift.Range<Int>(min...min + range)
}

public func contains(_ element: Int) -> Bool {
return (min...min + range).contains(element)
}
}

extension BarLineScatterCandleBubbleRenderer.XBounds: Sequence {
public struct Iterator: IteratorProtocol {
private let bounds: BarLineScatterCandleBubbleRenderer.XBounds
private var value: Int

fileprivate init(bounds: BarLineScatterCandleBubbleRenderer.XBounds) {
self.bounds = bounds
self.value = bounds.min
}

public mutating func next() -> Int? {
guard value < bounds.max else { return nil }
value += 1
return value

This comment has been minimized.

Copy link
@liuxuan30

liuxuan30 Apr 11, 2019

Member

@jjatie I'm trying to fix line chart failure of drawing the first circle, it seems for j in Xbounds() j is getting 1 even bounds.min is 0. Is this a bug that we should return the current value and then +1?

This comment has been minimized.

Copy link
@liuxuan30

liuxuan30 Apr 11, 2019

Member

never mind. It has been fixed in #3891

}
}

public func makeIterator() -> Iterator {
return Iterator(bounds: self)
}
}
4 changes: 2 additions & 2 deletions Source/Charts/Renderers/BubbleChartRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ open class BubbleChartRenderer: BarLineScatterCandleBubbleRenderer
let maxBubbleHeight: CGFloat = abs(viewPortHandler.contentBottom - viewPortHandler.contentTop)
let referenceSize: CGFloat = min(maxBubbleHeight, maxBubbleWidth)

for j in stride(from: _xBounds.min, through: _xBounds.range + _xBounds.min, by: 1)
for j in _xBounds
{
guard let entry = dataSet.entryForIndex(j) as? BubbleChartDataEntry else { continue }

Expand Down Expand Up @@ -185,7 +185,7 @@ open class BubbleChartRenderer: BarLineScatterCandleBubbleRenderer

let iconsOffset = dataSet.iconsOffset

for j in _xBounds.min..._xBounds.range + _xBounds.min
for j in _xBounds
{
guard let e = dataSet.entryForIndex(j) as? BubbleChartDataEntry else { break }

Expand Down
4 changes: 2 additions & 2 deletions Source/Charts/Renderers/CandleStickChartRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ open class CandleStickChartRenderer: LineScatterCandleRadarRenderer

context.setLineWidth(dataSet.shadowWidth)

for j in stride(from: _xBounds.min, through: _xBounds.range + _xBounds.min, by: 1)
for j in _xBounds
{
// get the entry
guard let e = dataSet.entryForIndex(j) as? CandleChartDataEntry else { continue }
Expand Down Expand Up @@ -313,7 +313,7 @@ open class CandleStickChartRenderer: LineScatterCandleRadarRenderer
let lineHeight = valueFont.lineHeight
let yOffset: CGFloat = lineHeight + 5.0

for j in stride(from: _xBounds.min, through: _xBounds.range + _xBounds.min, by: 1)
for j in _xBounds
{
guard let e = dataSet.entryForIndex(j) as? CandleChartDataEntry else { break }

Expand Down
10 changes: 5 additions & 5 deletions Source/Charts/Renderers/LineChartRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ open class LineChartRenderer: LineRadarRenderer
// let the spline start
cubicPath.move(to: CGPoint(x: CGFloat(cur.x), y: CGFloat(cur.y * phaseY)), transform: valueToPixelMatrix)

for j in stride(from: (_xBounds.min + 1), through: _xBounds.range + _xBounds.min, by: 1)
for j in _xBounds.dropFirst()
{
prev = cur
cur = dataSet.entryForIndex(j)
Expand Down Expand Up @@ -325,7 +325,7 @@ open class LineChartRenderer: LineRadarRenderer
_lineSegments = [CGPoint](repeating: CGPoint(), count: pointsPerEntryPair)
}

for j in stride(from: _xBounds.min, through: _xBounds.range + _xBounds.min, by: 1)
for j in _xBounds

This comment has been minimized.

Copy link
@liuxuan30

liuxuan30 Apr 11, 2019

Member

the old stripe is tricky, it starts from _xBounds.min, and through _xBounds.range + _xBounds.min, but _xBounds didn't have that range. Thew new bound iterator() is buggy. It cannot fit both from 0 and through _xBounds.range + _xBounds.min

This comment has been minimized.

Copy link
@liuxuan30

liuxuan30 Apr 11, 2019

Member

one more question, I didn't find the PR number of this commit. anything wrong? Is this committed into master by mistake?

This comment has been minimized.

Copy link
@liuxuan30

liuxuan30 Apr 11, 2019

Member

never mind, it has been fixed in #3891. turned out the swift-5 PR didn't have latest master

{
var e: ChartDataEntry! = dataSet.entryForIndex(j)

Expand Down Expand Up @@ -391,7 +391,7 @@ open class LineChartRenderer: LineRadarRenderer
context.beginPath()
var firstPoint = true

for x in stride(from: _xBounds.min, through: _xBounds.range + _xBounds.min, by: 1)
for x in _xBounds
{
e1 = dataSet.entryForIndex(x == 0 ? 0 : (x - 1))
e2 = dataSet.entryForIndex(x)
Expand Down Expand Up @@ -544,7 +544,7 @@ open class LineChartRenderer: LineRadarRenderer

_xBounds.set(chart: dataProvider, dataSet: dataSet, animator: animator)

for j in stride(from: _xBounds.min, through: min(_xBounds.min + _xBounds.range, _xBounds.max), by: 1)

This comment has been minimized.

Copy link
@jjatie

jjatie Mar 9, 2021

Author Collaborator

Why does this use min(min + range, max)

for j in _xBounds
{
guard let e = dataSet.entryForIndex(j) else { break }

Expand Down Expand Up @@ -649,7 +649,7 @@ open class LineChartRenderer: LineRadarRenderer
(dataSet.circleHoleColor == nil ||
dataSet.circleHoleColor == NSUIColor.clear)

for j in stride(from: _xBounds.min, through: _xBounds.range + _xBounds.min, by: 1)
for j in _xBounds
{
guard let e = dataSet.entryForIndex(j) else { break }

Expand Down
2 changes: 1 addition & 1 deletion Source/Charts/Renderers/ScatterChartRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ open class ScatterChartRenderer: LineScatterCandleRadarRenderer

_xBounds.set(chart: dataProvider, dataSet: dataSet, animator: animator)

for j in stride(from: _xBounds.min, through: _xBounds.range + _xBounds.min, by: 1)
for j in _xBounds
{
guard let e = dataSet.entryForIndex(j) else { break }

Expand Down

1 comment on commit 2a1ecb4

@liuxuan30
Copy link
Member

@liuxuan30 liuxuan30 commented on 2a1ecb4 Aug 7, 2019

Choose a reason for hiding this comment

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

@jjatie this commit seems buggy. Recently there are issues about the xBounds, though seems not sure, but I found the line chart x animation is broken since this commit:

  1. the label is always on the screen during animation;
  2. horizontal bezier animation is broken

I created #4093 for tracking

Please sign in to comment.