Skip to content

Commit

Permalink
autoScaleMinMax is working again (Fixes #1209)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielgindi committed Aug 15, 2016
1 parent d7769e6 commit ee4de2b
Show file tree
Hide file tree
Showing 11 changed files with 129 additions and 135 deletions.
5 changes: 0 additions & 5 deletions Charts/Classes/Charts/BarChartView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,6 @@ public class BarChartView: BarLineChartViewBase, BarChartDataProvider
guard let data = self.data as? BarChartData
else { return }

if self.autoScaleMinMaxEnabled
{
data.calcMinMax()
}

if fitBars
{
_xAxis.calculate(
Expand Down
42 changes: 22 additions & 20 deletions Charts/Classes/Charts/BarLineChartViewBase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ public class BarLineChartViewBase: ChartViewBase, BarLineScatterCandleBubbleChar

/// flag that indicates if auto scaling on the y axis is enabled
private var _autoScaleMinMaxEnabled = false
private var _autoScaleLastLowestVisibleX: Double?
private var _autoScaleLastHighestVisibleX: Double?

private var _pinchZoomEnabled = false
private var _doubleTapToZoomEnabled = true
Expand Down Expand Up @@ -189,20 +187,9 @@ public class BarLineChartViewBase: ChartViewBase, BarLineScatterCandleBubbleChar
_leftYAxisRenderer?.renderAxisLine(context: context)
_rightYAxisRenderer?.renderAxisLine(context: context)

if (_autoScaleMinMaxEnabled)
if _autoScaleMinMaxEnabled
{
let lowestVisibleX = self.lowestVisibleX,
highestVisibleX = self.highestVisibleX

if (_autoScaleLastLowestVisibleX == nil || _autoScaleLastLowestVisibleX != lowestVisibleX ||
_autoScaleLastHighestVisibleX == nil || _autoScaleLastHighestVisibleX != highestVisibleX)
{
calcMinMax()
calculateOffsets()

_autoScaleLastLowestVisibleX = lowestVisibleX
_autoScaleLastHighestVisibleX = highestVisibleX
}
autoScale()
}

// The renderers are responsible for clipping, to account for line-width center etc.
Expand Down Expand Up @@ -264,6 +251,26 @@ public class BarLineChartViewBase: ChartViewBase, BarLineScatterCandleBubbleChar
drawDescription(context: context)
}

private var _autoScaleLastLowestVisibleX: Double?
private var _autoScaleLastHighestVisibleX: Double?

/// Performs auto scaling of the axis by recalculating the minimum and maximum y-values based on the entries currently in view.
internal func autoScale()
{
guard let data = _data
else { return }

data.calcMinMaxY(fromX: self.lowestVisibleX, toX: self.highestVisibleX)

_xAxis.calculate(min: data.xMin, max: data.xMax)

// calculate axis range (min / max) according to provided data
_leftAxis.calculate(min: data.getYMin(.Left), max: data.getYMax(.Left))
_rightAxis.calculate(min: data.getYMin(.Right), max: data.getYMax(.Right))

calculateOffsets();
}

internal func prepareValuePxMatrix()
{
_rightAxisTransformer.prepareMatrixValuePx(chartXMin: _xAxis._axisMinimum, deltaX: CGFloat(xAxis.axisRange), deltaY: CGFloat(_rightAxis.axisRange), chartYMin: _rightAxis._axisMinimum)
Expand Down Expand Up @@ -305,11 +312,6 @@ public class BarLineChartViewBase: ChartViewBase, BarLineScatterCandleBubbleChar

internal override func calcMinMax()
{
if _autoScaleMinMaxEnabled
{
_data?.calcMinMax()
}

// calculate / set x-axis range
_xAxis.calculate(min: _data?.xMin ?? 0.0, max: _data?.xMax ?? 0.0)

Expand Down
5 changes: 5 additions & 0 deletions Charts/Classes/Data/Implementations/ChartBaseDataSet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ public class ChartBaseDataSet: NSObject, IChartDataSet
fatalError("calcMinMax is not implemented in ChartBaseDataSet")
}

public func calcMinMaxY(fromX fromX: Double, toX: Double)
{
fatalError("calcMinMaxY(fromX:, toX:) is not implemented in ChartBaseDataSet")
}

public var yMin: Double
{
fatalError("yMin is not implemented in ChartBaseDataSet")
Expand Down
56 changes: 19 additions & 37 deletions Charts/Classes/Data/Implementations/Standard/BarChartDataSet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -80,57 +80,39 @@ public class BarChartDataSet: BarLineScatterCandleBubbleChartDataSet, IBarChartD
}
}

public override func calcMinMax()
public override func calcMinMax(entry e: ChartDataEntry)
{
if _values.count == 0
{
return
}

_yMax = -DBL_MAX
_yMin = DBL_MAX
_xMax = -DBL_MAX
_xMin = DBL_MAX
guard let e = e as? BarChartDataEntry
else { return }

for e in _values as! [BarChartDataEntry]
if !e.y.isNaN
{
if !e.y.isNaN
if e.yValues == nil
{
if e.yValues == nil
if e.y < _yMin
{
if e.y < _yMin
{
_yMin = e.y
}

if e.y > _yMax
{
_yMax = e.y
}
_yMin = e.y
}
else

if e.y > _yMax
{
if -e.negativeSum < _yMin
{
_yMin = -e.negativeSum
}

if e.positiveSum > _yMax
{
_yMax = e.positiveSum
}
_yMax = e.y
}

if e.x < _xMin
}
else
{
if -e.negativeSum < _yMin
{
_xMin = e.x
_yMin = -e.negativeSum
}

if e.x > _xMax
if e.positiveSum > _yMax
{
_xMax = e.x
_yMax = e.positiveSum
}
}

calcMinMaxX(entry: e)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,30 +23,18 @@ public class BubbleChartDataSet: BarLineScatterCandleBubbleChartDataSet, IBubble
public var normalizeSizeEnabled: Bool = true
public var isNormalizeSizeEnabled: Bool { return normalizeSizeEnabled }

public override func calcMinMax()
public override func calcMinMax(entry e: ChartDataEntry)
{
if self.entryCount == 0
{
return
}

// need chart width to guess this properly
guard let e = e as? BubbleChartDataEntry
else { return }

super.calcMinMax(entry: e)

_yMax = -DBL_MAX
_yMin = DBL_MAX
_xMax = -DBL_MAX
_xMin = DBL_MAX
let size = e.size

for e in values as! [BubbleChartDataEntry]
if size > _maxSize
{
calcMinMax(entry: e)

let size = e.size

if size > _maxSize
{
_maxSize = size
}
_maxSize = size
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,42 +28,22 @@ public class CandleChartDataSet: LineScatterCandleRadarChartDataSet, ICandleChar

// MARK: - Data functions and accessors

public override func calcMinMax()
public override func calcMinMax(entry e: ChartDataEntry)
{
let yValCount = self.entryCount
guard let e = e as? CandleChartDataEntry
else { return }

if yValCount == 0
if (e.low < _yMin)
{
return
_yMin = e.low
}

_yMax = -DBL_MAX
_yMin = DBL_MAX
_xMax = -DBL_MAX
_xMin = DBL_MAX

for e in _values as! [CandleChartDataEntry]
if (e.high > _yMax)
{
if (e.low < _yMin)
{
_yMin = e.low
}

if (e.high > _yMax)
{
_yMax = e.high
}

if (e.x < _xMin)
{
_xMin = e.x
}

if (e.x > _xMax)
{
_xMax = e.x
}
_yMax = e.high
}

calcMinMaxX(entry: e)
}

// MARK: - Styling functions and accessors
Expand Down
19 changes: 16 additions & 3 deletions Charts/Classes/Data/Implementations/Standard/ChartData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,25 @@ public class ChartData: NSObject

internal func initialize(dataSets: [IChartDataSet])
{
calcMinMax()
notifyDataChanged()
}

/// Call this method to let the ChartData know that the underlying data has changed.
/// Calling this performs all necessary recalculations needed when the contained data has changed.
public func notifyDataChanged()
{
initialize(_dataSets)
calcMinMax()
}

public func calcMinMaxY(fromX fromX: Double, toX: Double)
{
for set in _dataSets
{
set.calcMinMaxY(fromX: fromX, toX: toX)
}

// apply the new data
calcMinMax()
}

/// calc minimum and maximum y value over all datasets
Expand Down Expand Up @@ -126,6 +137,7 @@ public class ChartData: NSObject
}
}

/// Adjusts the current minimum and maximum values based on the provided Entry object.
public func calcMinMax(entry e: ChartDataEntry, axis: YAxis.AxisDependency)
{
if _yMax < e.y
Expand Down Expand Up @@ -174,6 +186,7 @@ public class ChartData: NSObject
}
}

/// Adjusts the minimum and maximum values based on the given DataSet.
public func calcMinMax(dataSet d: IChartDataSet)
{
if _yMax < d.yMax
Expand Down Expand Up @@ -325,7 +338,7 @@ public class ChartData: NSObject
set
{
_dataSets = newValue
initialize(_dataSets)
notifyDataChanged()
}
}

Expand Down
48 changes: 40 additions & 8 deletions Charts/Classes/Data/Implementations/Standard/ChartDataSet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -109,19 +109,30 @@ public class ChartDataSet: ChartBaseDataSet
}
}

/// Updates the min and max x and y value of this DataSet based on the given Entry.
///
/// - parameter e:
internal func calcMinMax(entry e: ChartDataEntry)
public override func calcMinMaxY(fromX fromX: Double, toX: Double)
{
if e.y < _yMin
if _values.count == 0
{
_yMin = e.y
return
}
if e.y > _yMax

_yMax = -DBL_MAX
_yMin = DBL_MAX

let indexFrom = entryIndex(x: fromX, rounding: .Down)
let indexTo = entryIndex(x: toX, rounding: .Up)

if indexTo <= indexFrom { return }

for i in indexFrom..<indexTo
{
_yMax = e.y
// only recalculate y
calcMinMaxY(entry: _values[i])
}
}

public func calcMinMaxX(entry e: ChartDataEntry)
{
if e.x < _xMin
{
_xMin = e.x
Expand All @@ -132,6 +143,27 @@ public class ChartDataSet: ChartBaseDataSet
}
}

internal func calcMinMaxY(entry e: ChartDataEntry)
{
if e.y < _yMin
{
_yMin = e.y
}
if e.y > _yMax
{
_yMax = e.y
}
}

/// Updates the min and max x and y value of this DataSet based on the given Entry.
///
/// - parameter e:
internal func calcMinMax(entry e: ChartDataEntry)
{
calcMinMaxX(entry: e)
calcMinMaxY(entry: e)
}

/// - returns: The minimum y-value this DataSet holds
public override var yMin: Double { return _yMin }

Expand Down
Loading

0 comments on commit ee4de2b

Please sign in to comment.