Feature to invert the y-axis is now working.
diff --git a/MPChartExample/src/com/xxmassdeveloper/mpchartexample/InvertedLineChartActivity.java b/MPChartExample/src/com/xxmassdeveloper/mpchartexample/InvertedLineChartActivity.java
index 4860431..316ac72 100644
--- a/MPChartExample/src/com/xxmassdeveloper/mpchartexample/InvertedLineChartActivity.java
+++ b/MPChartExample/src/com/xxmassdeveloper/mpchartexample/InvertedLineChartActivity.java
@@ -71,7 +71,7 @@
mChart.setDescription("");
// invert the y-axis
-// mChart.setInvertYAxisEnabled(true);
+ mChart.setInvertYAxisEnabled(true);
// enable value highlighting
mChart.setHighlightEnabled(true);
diff --git a/MPChartExample/src/com/xxmassdeveloper/mpchartexample/notimportant/MainActivity.java b/MPChartExample/src/com/xxmassdeveloper/mpchartexample/notimportant/MainActivity.java
index 515dc0c..5d2037a 100644
--- a/MPChartExample/src/com/xxmassdeveloper/mpchartexample/notimportant/MainActivity.java
+++ b/MPChartExample/src/com/xxmassdeveloper/mpchartexample/notimportant/MainActivity.java
@@ -74,9 +74,9 @@
objects.add(new ContentItem(
"Multiple charts inside ListView",
"Demonstrates the usage of different chart types inside a ListView."));
-// objects.add(new ContentItem(
-// "Inverted Line Chart",
-// "Demonstrates the feature of inverting the y-axis."));
+ objects.add(new ContentItem(
+ "Inverted Line Chart",
+ "Demonstrates the feature of inverting the y-axis."));
MyAdapter adapter = new MyAdapter(this, objects);
diff --git a/MPChartLib/src/com/github/mikephil/charting/charts/BarLineChartBase.java b/MPChartLib/src/com/github/mikephil/charting/charts/BarLineChartBase.java
index 9b6ae8a..4cb2cea 100644
--- a/MPChartLib/src/com/github/mikephil/charting/charts/BarLineChartBase.java
+++ b/MPChartLib/src/com/github/mikephil/charting/charts/BarLineChartBase.java
@@ -38,6 +38,10 @@
* @author Philipp Jahoda
*/
public abstract class BarLineChartBase extends Chart {
+
+
+ /** if set to true, the y-axis is inverted and low values start at the top */
+ private boolean mInvertYAxis = false;
/** the maximum number of entried to which values will be drawn */
protected int mMaxVisibleCount = 100;
@@ -417,14 +421,14 @@
mMatrixValueToPx.set(val);
Matrix offset = new Matrix();
- offset.postTranslate(mOffsetLeft, getHeight() - mOffsetBottom);
-
-// if (!mInvertYAxis)
-// offset.postTranslate(mOffsetLeft, getHeight() - mOffsetBottom);
-// else {
-// offset.setTranslate(mOffsetLeft, 0);
-// offset.postScale(1.0f, -1.0f);
-// }
+ // offset.postTranslate(mOffsetLeft, getHeight() - mOffsetBottom);
+
+ if (!mInvertYAxis)
+ offset.postTranslate(mOffsetLeft, getHeight() - mOffsetBottom);
+ else {
+ offset.setTranslate(mOffsetLeft, -getOffsetTop());
+ offset.postScale(1.0f, -1.0f);
+ }
mMatrixOffset.set(offset);
}
@@ -546,18 +550,20 @@
PointD p1 = getValuesByTouchPoint(mContentRect.left, mContentRect.top);
PointD p2 = getValuesByTouchPoint(mContentRect.left, mContentRect.bottom);
- mYChartMin = (float) p2.y;
- mYChartMax = (float) p1.y;
+ // mYChartMin = (float) p2.y;
+ // mYChartMax = (float) p1.y;
-// if (!mInvertYAxis) {
-// mYChartMin = (float) p2.y;
-// mYChartMax = (float) p1.y;
-// } else {
-//
-// if(!mStartAtZero) mYChartMin = (float) Math.min(p1.y, p2.y);
-// else mYChartMin = 0;
-// mYChartMax = (float) Math.max(p1.y, p2.y);
-// }
+ if (!mInvertYAxis) {
+ mYChartMin = (float) p2.y;
+ mYChartMax = (float) p1.y;
+ } else {
+
+ if (!mStartAtZero)
+ mYChartMin = (float) Math.min(p1.y, p2.y);
+ else
+ mYChartMin = 0;
+ mYChartMax = (float) Math.max(p1.y, p2.y);
+ }
float yMin = mYChartMin;
float yMax = mYChartMax;
@@ -988,15 +994,24 @@
refreshTouch(save);
}
-// private boolean mInvertYAxis = false;
-//
-// public void setInvertYAxisEnabled(boolean enabled) {
-// mInvertYAxis = enabled;
-// }
-//
-// public boolean isInvertYAxisEnabled() {
-// return mInvertYAxis;
-// }
+ /**
+ * If this is set to true, the y-axis is inverted which means that low
+ * values are on top of the chart, high values on bottom.
+ *
+ * @param enabled
+ */
+ public void setInvertYAxisEnabled(boolean enabled) {
+ mInvertYAxis = enabled;
+ }
+
+ /**
+ * If this returns true, the y-axis is inverted.
+ *
+ * @return
+ */
+ public boolean isInvertYAxisEnabled() {
+ return mInvertYAxis;
+ }
/**
* Centers the viewport around the specified x-index and the specified
diff --git a/MPChartLib/src/com/github/mikephil/charting/listener/BarLineChartTouchListener.java b/MPChartLib/src/com/github/mikephil/charting/listener/BarLineChartTouchListener.java
index 15a3af0..117831e 100644
--- a/MPChartLib/src/com/github/mikephil/charting/listener/BarLineChartTouchListener.java
+++ b/MPChartLib/src/com/github/mikephil/charting/listener/BarLineChartTouchListener.java
@@ -205,14 +205,15 @@
mMatrix.set(mSavedMatrix);
PointF dragPoint = new PointF(event.getX(), event.getY());
- mMatrix.postTranslate(dragPoint.x - mTouchStartPoint.x, dragPoint.y
- - mTouchStartPoint.y);
-// mMatrix.set(mSavedMatrix);
-// PointF dragPoint = new PointF(event.getX(), event.getY());
-// mMatrix.postTranslate(dragPoint.x - mTouchStartPoint.x, -(dragPoint.y
-// - mTouchStartPoint.y));
-
+ // check if axis is inverted
+ if(!mChart.isInvertYAxisEnabled()) {
+ mMatrix.postTranslate(dragPoint.x - mTouchStartPoint.x, dragPoint.y
+ - mTouchStartPoint.y);
+ } else {
+ mMatrix.postTranslate(dragPoint.x - mTouchStartPoint.x, -(dragPoint.y
+ - mTouchStartPoint.y));
+ }
} else if (mTouchMode == X_ZOOM || mTouchMode == Y_ZOOM || mTouchMode == PINCH_ZOOM) {
@@ -222,13 +223,6 @@
if (totalDist > 10f) {
- float[] values = new float[9];
- mMatrix.getValues(values);
-
- // get the previous scale factors
- // float oldScaleX = values[Matrix.MSCALE_X];
- // float oldScaleY = values[Matrix.MSCALE_Y];
-
// get the translation
PointF t = getTrans(mTouchPointCenter.x, mTouchPointCenter.y);
@@ -368,7 +362,14 @@
public PointF getTrans(float x, float y) {
float xTrans = x - mChart.getOffsetLeft();
- float yTrans = -(mChart.getMeasuredHeight() - y - mChart.getOffsetBottom());
+ float yTrans = 0f;
+
+ // check if axis is inverted
+ if(!mChart.isInvertYAxisEnabled()) {
+ yTrans = -(mChart.getMeasuredHeight() - y - mChart.getOffsetBottom());
+ } else {
+ yTrans = -(y - mChart.getOffsetTop());
+ }
return new PointF(xTrans, yTrans);
}