Skip to content

Commit

Permalink
Merge pull request #694 from danielgindi/auto-scale-y-axis
Browse files Browse the repository at this point in the history
Auto scale y axis
  • Loading branch information
PhilJay committed May 31, 2015
2 parents 2ac47ea + 6606853 commit 9fdd30b
Show file tree
Hide file tree
Showing 28 changed files with 257 additions and 62 deletions.
4 changes: 4 additions & 0 deletions MPChartExample/res/menu/bar.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,9 @@
android:id="@+id/actionTogglePinch"
android:title="Toggle PinchZoom">
</item>
<item
android:id="@+id/actionToggleAutoScaleMinMax"
android:title="Toggle auto scale min/max">
</item>

</menu>
4 changes: 4 additions & 0 deletions MPChartExample/res/menu/bubble.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,9 @@
android:id="@+id/actionTogglePinch"
android:title="Toggle PinchZoom">
</item>
<item
android:id="@+id/actionToggleAutoScaleMinMax"
android:title="Toggle auto scale min/max">
</item>

</menu>
4 changes: 4 additions & 0 deletions MPChartExample/res/menu/candle.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,9 @@
android:id="@+id/actionTogglePinch"
android:title="Toggle PinchZoom">
</item>
<item
android:id="@+id/actionToggleAutoScaleMinMax"
android:title="Toggle auto scale min/max">
</item>

</menu>
4 changes: 4 additions & 0 deletions MPChartExample/res/menu/draw.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,8 @@
android:id="@+id/actionTogglePinch"
android:title="Toggle PinchZoom">
</item>
<item
android:id="@+id/actionToggleAutoScaleMinMax"
android:title="Toggle auto scale min/max">
</item>
</menu>
6 changes: 5 additions & 1 deletion MPChartExample/res/menu/line.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,12 @@
android:id="@+id/actionSave"
android:title="Save to SD-Card">
</item>
<item
<item
android:id="@+id/actionTogglePinch"
android:title="Toggle PinchZoom">
</item>
<item
android:id="@+id/actionToggleAutoScaleMinMax"
android:title="Toggle auto scale min/max">
</item>
</menu>
4 changes: 4 additions & 0 deletions MPChartExample/res/menu/scatter.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,9 @@
android:id="@+id/actionTogglePinch"
android:title="Toggle PinchZoom">
</item>
<item
android:id="@+id/actionToggleAutoScaleMinMax"
android:title="Toggle auto scale min/max">
</item>

</menu>
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,11 @@ public boolean onOptionsItemSelected(MenuItem item) {
mChart.invalidate();
break;
}
case R.id.actionToggleAutoScaleMinMax: {
mChart.setAutoScaleMinMaxEnabled(!mChart.isAutoScaleMinMaxEnabled());
mChart.notifyDataSetChanged();
break;
}
case R.id.actionToggleHighlightArrow: {
if (mChart.isDrawHighlightArrowEnabled())
mChart.setDrawHighlightArrow(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,11 @@ public boolean onOptionsItemSelected(MenuItem item) {
mChart.invalidate();
break;
}
case R.id.actionToggleAutoScaleMinMax: {
mChart.setAutoScaleMinMaxEnabled(!mChart.isAutoScaleMinMaxEnabled());
mChart.notifyDataSetChanged();
break;
}
case R.id.actionToggleHighlightArrow: {
if (mChart.isDrawHighlightArrowEnabled())
mChart.setDrawHighlightArrow(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,11 @@ public boolean onOptionsItemSelected(MenuItem item) {
mChart.invalidate();
break;
}
case R.id.actionToggleAutoScaleMinMax: {
mChart.setAutoScaleMinMaxEnabled(!mChart.isAutoScaleMinMaxEnabled());
mChart.notifyDataSetChanged();
break;
}
case R.id.actionToggleHighlight: {
if (mChart.isHighlightEnabled())
mChart.setHighlightEnabled(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,11 @@ public boolean onOptionsItemSelected(MenuItem item) {
mChart.invalidate();
break;
}
case R.id.actionToggleAutoScaleMinMax: {
mChart.setAutoScaleMinMaxEnabled(!mChart.isAutoScaleMinMaxEnabled());
mChart.notifyDataSetChanged();
break;
}
case R.id.actionToggleHighlightArrow: {
if (mChart.isDrawHighlightArrowEnabled())
mChart.setDrawHighlightArrow(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,11 @@ public boolean onOptionsItemSelected(MenuItem item) {
mChart.invalidate();
break;
}
case R.id.actionToggleAutoScaleMinMax: {
mChart.setAutoScaleMinMaxEnabled(!mChart.isAutoScaleMinMaxEnabled());
mChart.notifyDataSetChanged();
break;
}
case R.id.actionToggleStartzero: {
mChart.getAxisLeft().setStartAtZero(!mChart.getAxisLeft().isStartAtZeroEnabled());
mChart.getAxisRight().setStartAtZero(!mChart.getAxisRight().isStartAtZeroEnabled());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,11 @@ public boolean onOptionsItemSelected(MenuItem item) {
mChart.invalidate();
break;
}
case R.id.actionToggleAutoScaleMinMax: {
mChart.setAutoScaleMinMaxEnabled(!mChart.isAutoScaleMinMaxEnabled());
mChart.notifyDataSetChanged();
break;
}
case R.id.actionToggleStartzero: {
mChart.getAxisLeft().setStartAtZero(!mChart.getAxisLeft().isStartAtZeroEnabled());
mChart.getAxisRight().setStartAtZero(!mChart.getAxisRight().isStartAtZeroEnabled());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,11 @@ public boolean onOptionsItemSelected(MenuItem item) {
mChart.invalidate();
break;
}
case R.id.actionToggleAutoScaleMinMax: {
mChart.setAutoScaleMinMaxEnabled(!mChart.isAutoScaleMinMaxEnabled());
mChart.notifyDataSetChanged();
break;
}
case R.id.animateX: {
mChart.animateX(3000);
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,11 @@ public boolean onOptionsItemSelected(MenuItem item) {
mChart.invalidate();
break;
}
case R.id.actionToggleAutoScaleMinMax: {
mChart.setAutoScaleMinMaxEnabled(!mChart.isAutoScaleMinMaxEnabled());
mChart.notifyDataSetChanged();
break;
}
case R.id.actionSave: {
// mChart.saveToGallery("title"+System.currentTimeMillis());
mChart.saveToPath("title" + System.currentTimeMillis(), "");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,11 @@ public boolean onOptionsItemSelected(MenuItem item) {
mChart.invalidate();
break;
}
case R.id.actionToggleAutoScaleMinMax: {
mChart.setAutoScaleMinMaxEnabled(!mChart.isAutoScaleMinMaxEnabled());
mChart.notifyDataSetChanged();
break;
}
case R.id.actionToggleHighlightArrow: {
if (mChart.isDrawHighlightArrowEnabled())
mChart.setDrawHighlightArrow(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,11 @@ public boolean onOptionsItemSelected(MenuItem item) {
mChart.invalidate();
break;
}
case R.id.actionToggleAutoScaleMinMax: {
mChart.setAutoScaleMinMaxEnabled(!mChart.isAutoScaleMinMaxEnabled());
mChart.notifyDataSetChanged();
break;
}
case R.id.actionToggleFilter: {

// the angle of filtering is 35°
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,11 @@ public boolean onOptionsItemSelected(MenuItem item) {
mChart.invalidate();
break;
}
case R.id.actionToggleAutoScaleMinMax: {
mChart.setAutoScaleMinMaxEnabled(!mChart.isAutoScaleMinMaxEnabled());
mChart.notifyDataSetChanged();
break;
}
case R.id.animateX: {
mChart.animateX(3000);
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,11 @@ public boolean onOptionsItemSelected(MenuItem item) {
mChart.invalidate();
break;
}
case R.id.actionToggleAutoScaleMinMax: {
mChart.setAutoScaleMinMaxEnabled(!mChart.isAutoScaleMinMaxEnabled());
mChart.notifyDataSetChanged();
break;
}
case R.id.animateX: {
mChart.animateX(3000);
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,11 @@ public boolean onOptionsItemSelected(MenuItem item) {
mChart.invalidate();
break;
}
case R.id.actionToggleAutoScaleMinMax: {
mChart.setAutoScaleMinMaxEnabled(!mChart.isAutoScaleMinMaxEnabled());
mChart.notifyDataSetChanged();
break;
}
case R.id.actionToggleHighlight: {
if (mChart.isHighlightEnabled())
mChart.setHighlightEnabled(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,11 @@ public boolean onOptionsItemSelected(MenuItem item) {
mChart.invalidate();
break;
}
case R.id.actionToggleAutoScaleMinMax: {
mChart.setAutoScaleMinMaxEnabled(!mChart.isAutoScaleMinMaxEnabled());
mChart.notifyDataSetChanged();
break;
}
case R.id.actionToggleStartzero: {
mChart.getAxisLeft().setStartAtZero(!mChart.getAxisLeft().isStartAtZeroEnabled());
mChart.getAxisRight().setStartAtZero(!mChart.getAxisRight().isStartAtZeroEnabled());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,11 @@ public boolean onOptionsItemSelected(MenuItem item) {
mChart.invalidate();
break;
}
case R.id.actionToggleAutoScaleMinMax: {
mChart.setAutoScaleMinMaxEnabled(!mChart.isAutoScaleMinMaxEnabled());
mChart.notifyDataSetChanged();
break;
}
case R.id.actionToggleHighlightArrow: {
if (mChart.isDrawHighlightArrowEnabled())
mChart.setDrawHighlightArrow(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ public abstract class BarLineChartBase<T extends BarLineScatterCandleData<? exte
/** the maximum number of entried to which values will be drawn */
protected int mMaxVisibleCount = 100;

/** flag that indicates if auto scaling on the y axis is enabled */
private boolean mAutoScaleMinMaxEnabled = false;
private Integer mAutoScaleLastLowestVisibleXIndex = null;
private Integer mAutoScaleLastHighestVisibleXIndex = null;

/**
* flag that indicates if pinch-zoom is enabled. if true, both x and y axis
* can be scaled with 2 fingers, if false, x and y axis can be scaled
Expand Down Expand Up @@ -192,6 +197,23 @@ protected void onDraw(Canvas canvas) {
mAxisRendererLeft.renderAxisLine(canvas);
mAxisRendererRight.renderAxisLine(canvas);

if (mAutoScaleMinMaxEnabled) {
final int lowestVisibleXIndex = getLowestVisibleXIndex();
final int highestVisibleXIndex = getHighestVisibleXIndex();

if (mAutoScaleLastLowestVisibleXIndex == null ||
mAutoScaleLastLowestVisibleXIndex != lowestVisibleXIndex ||
mAutoScaleLastHighestVisibleXIndex == null ||
mAutoScaleLastHighestVisibleXIndex != highestVisibleXIndex) {

calcMinMax();
calculateOffsets();

mAutoScaleLastLowestVisibleXIndex = lowestVisibleXIndex;
mAutoScaleLastHighestVisibleXIndex = highestVisibleXIndex;
}
}

// make sure the graph values and grid cannot be drawn outside the
// content-rect
int clipRestoreCount = canvas.save();
Expand Down Expand Up @@ -314,6 +336,9 @@ public void notifyDataSetChanged() {
@Override
protected void calcMinMax() {

if (mAutoScaleMinMaxEnabled)
mData.calcMinMax(getLowestVisibleXIndex(), getHighestVisibleXIndex());

float minLeft = mData.getYMin(AxisDependency.LEFT);
float maxLeft = mData.getYMax(AxisDependency.LEFT);
float minRight = mData.getYMin(AxisDependency.RIGHT);
Expand Down Expand Up @@ -1376,6 +1401,24 @@ public boolean isAnyAxisInverted() {
return false;
}

/**
* flag that indicates if auto scaling on the y axis is enabled.
* @param enabled if yes, the y axis automatically adjusts to the
* min and max y values of the current x axis range
* whenever the viewport changes
*/
public void setAutoScaleMinMaxEnabled(boolean enabled) {
mAutoScaleMinMaxEnabled = enabled;
}

/**
* @default false
* @return true if auto scaling on the y axis is enabled.
*/
public boolean isAutoScaleMinMaxEnabled() {
return mAutoScaleMinMaxEnabled;
}

@Override
public void setPaint(Paint p, int which) {
super.setPaint(p, which);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,30 @@ public void setColor(int color, int alpha) {
}

@Override
protected void calcMinMax() {

protected void calcMinMax(int start, int end) {
if (mYVals.size() == 0)
return;

final List<BubbleEntry> entries = getYVals();

int endValue;

if (end == 0)
endValue = mYVals.size() - 1;
else
endValue = end;

mLastStart = start;
mLastEnd = endValue;

mYMin = yMin(entries.get(start));
mYMax = yMax(entries.get(start));

// need chart width to guess this properly

for (BubbleEntry entry : entries) {
for (int i = start; i <= endValue; i++) {

final BubbleEntry entry = entries.get(i);

final float ymin = yMin(entry);
final float ymax = yMax(entry);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,19 +66,28 @@ public DataSet<CandleEntry> copy() {
}

@Override
protected void calcMinMax() {
protected void calcMinMax(int start, int end) {
// super.calcMinMax();

if (mYVals.size() == 0) {
if (mYVals.size() == 0)
return;
}

List<CandleEntry> entries = mYVals;

mYMin = entries.get(0).getLow();
mYMax = entries.get(0).getHigh();
int endValue;

if (end == 0)
endValue = mYVals.size() - 1;
else
endValue = end;

mLastStart = start;
mLastEnd = endValue;

mYMin = mYVals.get(start).getLow();
mYMax = mYVals.get(start).getHigh();

for (int i = 0; i < entries.size(); i++) {
for (int i = start + 1; i <= endValue; i++) {

CandleEntry e = entries.get(i);

Expand Down
Loading

0 comments on commit 9fdd30b

Please sign in to comment.