Skip to content

Commit 73054be

Browse files
authored
Merge pull request PhilJay#3642 from pagrzybe/autoscale_restrictions
Option to set restrictions for Y axis autoscaling.
2 parents 0b6632f + 92c14db commit 73054be

File tree

1 file changed

+57
-2
lines changed
  • MPChartLib/src/main/java/com/github/mikephil/charting/components

1 file changed

+57
-2
lines changed

MPChartLib/src/main/java/com/github/mikephil/charting/components/YAxis.java

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,16 @@ public class YAxis extends AxisBase {
3838
*/
3939
protected boolean mDrawZeroLine = false;
4040

41+
/**
42+
* flag indicating that auto scale min restriction should be used
43+
*/
44+
private boolean mUseAutoScaleRestrictionMin = false;
45+
46+
/**
47+
* flag indicating that auto scale max restriction should be used
48+
*/
49+
private boolean mUseAutoScaleRestrictionMax = false;
50+
4151
/**
4252
* Color of the zero line
4353
*/
@@ -357,12 +367,57 @@ public boolean needsOffset() {
357367
return false;
358368
}
359369

370+
/**
371+
* Returns true if autoscale restriction for axis min value is enabled
372+
*/
373+
public boolean isUseAutoScaleMinRestriction( ) {
374+
return mUseAutoScaleRestrictionMin;
375+
}
376+
377+
/**
378+
* Sets autoscale restriction for axis min value as enabled/disabled
379+
*/
380+
public void setUseAutoScaleMinRestriction( boolean isEnabled ) {
381+
mUseAutoScaleRestrictionMin = isEnabled;
382+
}
383+
384+
/**
385+
* Returns true if autoscale restriction for axis max value is enabled
386+
*/
387+
public boolean isUseAutoScaleMaxRestriction() {
388+
return mUseAutoScaleRestrictionMax;
389+
}
390+
391+
/**
392+
* Sets autoscale restriction for axis max value as enabled/disabled
393+
*/
394+
public void setUseAutoScaleMaxRestriction( boolean isEnabled ) {
395+
mUseAutoScaleRestrictionMax = isEnabled;
396+
}
397+
398+
360399
@Override
361400
public void calculate(float dataMin, float dataMax) {
362401

402+
float min = dataMin;
403+
float max = dataMax;
404+
363405
// if custom, use value as is, else use data value
364-
float min = mCustomAxisMin ? mAxisMinimum : dataMin;
365-
float max = mCustomAxisMax ? mAxisMaximum : dataMax;
406+
if( mCustomAxisMin ) {
407+
if( mUseAutoScaleRestrictionMin ) {
408+
min = Math.min( dataMin, mAxisMinimum );
409+
} else {
410+
min = mAxisMinimum;
411+
}
412+
}
413+
414+
if( mCustomAxisMax ) {
415+
if( mUseAutoScaleRestrictionMax ) {
416+
max = Math.max( max, mAxisMaximum );
417+
} else {
418+
max = mAxisMaximum;
419+
}
420+
}
366421

367422
// temporary range (before calculations)
368423
float range = Math.abs(max - min);

0 commit comments

Comments
 (0)