Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AutoScaling yAxis during panning / zooming #95

Merged
merged 6 commits into from
May 26, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 34 additions & 1 deletion Charts/Classes/Charts/BarLineChartViewBase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public class BarLineChartViewBase: ChartViewBase, UIGestureRecognizerDelegate
private var _pinchZoomEnabled = false
private var _doubleTapToZoomEnabled = true
private var _dragEnabled = true
private var _autoScaleMinMaxEnabled = false;

private var _scaleXEnabled = true
private var _scaleYEnabled = true
Expand Down Expand Up @@ -153,6 +154,12 @@ public class BarLineChartViewBase: ChartViewBase, UIGestureRecognizerDelegate
_leftYAxisRenderer?.renderAxisLine(context: context);
_rightYAxisRenderer?.renderAxisLine(context: context);

if (_autoScaleMinMaxEnabled)
{
calcMinMax();
calculateOffsets();
}

// make sure the graph values and grid cannot be drawn outside the content-rect
CGContextSaveGState(context);

Expand Down Expand Up @@ -256,6 +263,11 @@ public class BarLineChartViewBase: ChartViewBase, UIGestureRecognizerDelegate

internal override func calcMinMax()
{
if (_autoScaleMinMaxEnabled)
{
_data.calcMinMax(start: lowestVisibleXIndex, end: highestVisibleXIndex);
}

var minLeft = _data.getYMin(.Left);
var maxLeft = _data.getYMax(.Left);
var minRight = _data.getYMin(.Right);
Expand Down Expand Up @@ -864,7 +876,7 @@ public class BarLineChartViewBase: ChartViewBase, UIGestureRecognizerDelegate

/// Zooms in or out by the given scale factor. x and y are the coordinates
/// (in pixels) of the zoom center.
///
///
/// :param: scaleX if < 1f --> zoom out, if > 1f --> zoom in
/// :param: scaleY if < 1f --> zoom out, if > 1f --> zoom in
/// :param: x
Expand Down Expand Up @@ -1470,6 +1482,27 @@ public class BarLineChartViewBase: ChartViewBase, UIGestureRecognizerDelegate
{
return _leftAxis.isInverted || _rightAxis.isInverted;
}

/// flat that indicates if auto scaling on the y axis is enabled.
/// if yes, the y axis automatically adjusts to the min and max y values of the current x axis range
public var autoScaleMinMaxEnabled: Bool
{
get
{
return _autoScaleMinMaxEnabled;
}
set
{
if (_autoScaleMinMaxEnabled != newValue)
{
_autoScaleMinMaxEnabled = newValue;
}
}
}

/// returns true if autoScaleMinMax is enabled, false if no
/// :default: false
public var isAutoScaleMinMaxEnabled : Bool { return autoScaleMinMaxEnabled; }
}

/// Default formatter that calculates the position of the filled line.
Expand Down
30 changes: 22 additions & 8 deletions Charts/Classes/Data/CandleChartDataSet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,27 +45,41 @@ public class CandleChartDataSet: BarLineScatterCandleChartDataSet
super.init(yVals: yVals, label: label);
}

internal override func calcMinMax()
internal override func calcMinMax(#start: Int, end: Int)
{
if (yVals.count == 0)
{
return;
}

var entries = yVals as! [CandleChartDataEntry];

_yMin = entries[0].low;
_yMax = entries[0].high;

for (var i = 0; i < entries.count; i++)

var endValue : Int;

if end == 0
{
endValue = entries.count - 1;
}
else
{
endValue = end;
}

_lastStart = start;
_lastEnd = end;

_yMin = entries[start].low;
_yMax = entries[start].high;

for (var i = 0; i <= endValue; i++)
{
var e = entries[i];

if (e.low < _yMin)
{
_yMin = e.low;
}

if (e.high > _yMax)
{
_yMax = e.high;
Expand Down
16 changes: 12 additions & 4 deletions Charts/Classes/Data/ChartData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ public class ChartData: NSObject
internal var _rightAxisMin = Float(0.0)
private var _yValueSum = Float(0.0)
private var _yValCount = Int(0)
internal var _lastStart = Int(0);
internal var _lastEnd = Int(0);

/// the average length (in characters) across all x-value strings
private var _xValAverageLength = Float(0.0)
Expand Down Expand Up @@ -69,7 +71,7 @@ public class ChartData: NSObject
{
checkIsLegal(dataSets);

calcMinMax();
calcMinMax(start: _lastStart, end: _lastEnd);
calcYValueSum();
calcYValueCount();

Expand Down Expand Up @@ -120,21 +122,27 @@ public class ChartData: NSObject
}

/// calc minimum and maximum y value over all datasets
internal func calcMinMax()
internal func calcMinMax(#start: Int, end: Int)
{

if (_dataSets == nil || _dataSets.count < 1)
{
_yMax = 0.0;
_yMin = 0.0;
}
else
{
_lastStart = start;
_lastEnd = end;

// calculate absolute min and max
_yMin = _dataSets[0].yMin;
_yMax = _dataSets[0].yMax;

for (var i = 0; i < _dataSets.count; i++)
{
_dataSets[i].calcMinMax(start: start, end: end);

if (_dataSets[i].yMin < _yMin)
{
_yMin = _dataSets[i].yMin;
Expand Down Expand Up @@ -552,7 +560,7 @@ public class ChartData: NSObject
_yValCount -= d.entryCount;
_yValueSum -= d.yValueSum;

calcMinMax();
calcMinMax(start: _lastStart, end: _lastEnd);

return true;
}
Expand Down Expand Up @@ -629,7 +637,7 @@ public class ChartData: NSObject
_yValCount -= 1;
_yValueSum -= val;

calcMinMax();
calcMinMax(start: _lastStart, end: _lastEnd);
}

return removed;
Expand Down
38 changes: 28 additions & 10 deletions Charts/Classes/Data/ChartDataSet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ public class ChartDataSet: NSObject
internal var _yMax = Float(0.0)
internal var _yMin = Float(0.0)
internal var _yValueSum = Float(0.0)
internal var _lastStart = 0;
internal var _lastEnd = 0;
public var label = "DataSet"
public var visible = true;
public var drawValuesEnabled = true;
Expand Down Expand Up @@ -58,7 +60,7 @@ public class ChartDataSet: NSObject
// default color
colors.append(UIColor(red: 140.0/255.0, green: 234.0/255.0, blue: 255.0/255.0, alpha: 1.0));

self.calcMinMax();
self.calcMinMax(start: _lastStart, end: _lastEnd);
self.calcYValueSum();
}

Expand All @@ -70,21 +72,35 @@ public class ChartDataSet: NSObject
/// Use this method to tell the data set that the underlying data has changed
public func notifyDataSetChanged()
{
calcMinMax();
calcMinMax(start: _lastStart, end: _lastEnd);
calcYValueSum();
}

internal func calcMinMax()
internal func calcMinMax(#start : Int, end: Int)
{
if _yVals!.count == 0
{
return;
}

_yMin = yVals[0].value;
_yMax = yVals[0].value;
var endValue : Int;

for var i = 0; i < _yVals.count; i++
if end == 0
{
endValue = _yVals.count - 1;
}
else
{
endValue = end;
}

_lastStart = start;
_lastEnd = endValue;

_yMin = yVals[start].value;
_yMax = yVals[start].value;

for var i = start; i <= endValue; i++
{
let e = _yVals[i];
if (e.value < _yMin)
Expand Down Expand Up @@ -291,7 +307,7 @@ public class ChartDataSet: NSObject
if (removed)
{
_yValueSum -= entry.value;
calcMinMax();
calcMinMax(start: _lastStart, end: _lastEnd);
}

return removed;
Expand All @@ -305,7 +321,7 @@ public class ChartDataSet: NSObject
var e = _yVals.removeAtIndex(index);

_yValueSum -= e.value;
calcMinMax();
calcMinMax(start: _lastStart, end: _lastEnd);

return true;
}
Expand Down Expand Up @@ -348,8 +364,8 @@ public class ChartDataSet: NSObject
return drawValuesEnabled;
}

/// Checks if this DataSet contains the specified Entry.
/// :returns: true if contains the entry, false if not.
/// Checks if this DataSet contains the specified Entry.
/// :returns: true if contains the entry, false if not.
public func contains(e: ChartDataEntry) -> Bool
{
for entry in _yVals
Expand All @@ -367,6 +383,8 @@ public class ChartDataSet: NSObject
public func clear()
{
_yVals.removeAll(keepCapacity: true);
_lastStart = 0;
_lastEnd = 0;
notifyDataSetChanged();
}

Expand Down
10 changes: 5 additions & 5 deletions Charts/Classes/Data/CombinedChartData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public class CombinedChartData: BarLineScatterCandleChartData

checkIsLegal(newValue.dataSets);

calcMinMax();
calcMinMax(start: _lastStart, end: _lastEnd);
calcYValueSum();
calcYValueCount();

Expand All @@ -71,7 +71,7 @@ public class CombinedChartData: BarLineScatterCandleChartData

checkIsLegal(newValue.dataSets);

calcMinMax();
calcMinMax(start: _lastStart, end: _lastEnd);
calcYValueSum();
calcYValueCount();

Expand All @@ -95,7 +95,7 @@ public class CombinedChartData: BarLineScatterCandleChartData

checkIsLegal(newValue.dataSets);

calcMinMax();
calcMinMax(start: _lastStart, end: _lastEnd);
calcYValueSum();
calcYValueCount();

Expand All @@ -119,7 +119,7 @@ public class CombinedChartData: BarLineScatterCandleChartData

checkIsLegal(newValue.dataSets);

calcMinMax();
calcMinMax(start: _lastStart, end: _lastEnd);
calcYValueSum();
calcYValueCount();

Expand All @@ -143,7 +143,7 @@ public class CombinedChartData: BarLineScatterCandleChartData

checkIsLegal(newValue.dataSets);

calcMinMax();
calcMinMax(start: _lastStart, end: _lastEnd);
calcYValueSum();
calcYValueCount();

Expand Down