Bugfix in setOffsets(...) method.
diff --git a/MPChartExample/src/com/xxmassdeveloper/mpchartexample/LineChartActivity1.java b/MPChartExample/src/com/xxmassdeveloper/mpchartexample/LineChartActivity1.java
index 79611ae..f204ea9 100644
--- a/MPChartExample/src/com/xxmassdeveloper/mpchartexample/LineChartActivity1.java
+++ b/MPChartExample/src/com/xxmassdeveloper/mpchartexample/LineChartActivity1.java
@@ -118,7 +118,6 @@
// add data
setData(45, 100);
-
mChart.animateX(2500);
// // restrain the maximum scale-out factor
diff --git a/MPChartExample/src/com/xxmassdeveloper/mpchartexample/MyMarkerView.java b/MPChartExample/src/com/xxmassdeveloper/mpchartexample/MyMarkerView.java
index e1b1828..6b4f17a 100644
--- a/MPChartExample/src/com/xxmassdeveloper/mpchartexample/MyMarkerView.java
+++ b/MPChartExample/src/com/xxmassdeveloper/mpchartexample/MyMarkerView.java
@@ -25,7 +25,7 @@
}
// callbacks everytime the MarkerView is redrawn, can be used to update the
- // content
+ // content (user-interface)
@Override
public void refreshContent(Entry e, int dataSetIndex) {
diff --git a/MPChartLib/src/com/github/mikephil/charting/charts/Chart.java b/MPChartLib/src/com/github/mikephil/charting/charts/Chart.java
index 848b3f3..4823ecd 100644
--- a/MPChartLib/src/com/github/mikephil/charting/charts/Chart.java
+++ b/MPChartLib/src/com/github/mikephil/charting/charts/Chart.java
@@ -22,6 +22,7 @@
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
+
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.ChartData;
@@ -42,6 +43,7 @@
import com.nineoldandroids.animation.ObjectAnimator;
import com.nineoldandroids.animation.ValueAnimator;
import com.nineoldandroids.animation.ValueAnimator.AnimatorUpdateListener;
+
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
@@ -1454,6 +1456,7 @@
*
* @return
*/
+ @Override
public PointF getCenterOffsets() {
return new PointF(mContentRect.centerX(), mContentRect.centerY());
}
@@ -1517,8 +1520,9 @@
/**
* Sets the offsets from the border of the view to the actual chart in every
- * direction manually. Provide density pixels -> they are then rendered to
- * pixels inside the chart
+ * direction manually. This method needs to be recalled everytime a new data
+ * object is set for the chart. Provide density pixels -> they are then
+ * rendered to pixels inside the chart.
*
* @param left
* @param right
@@ -1531,6 +1535,10 @@
mOffsetLeft = Utils.convertDpToPixel(left);
mOffsetRight = Utils.convertDpToPixel(right);
mOffsetTop = Utils.convertDpToPixel(top);
+
+ mTrans.prepareMatrixValuePx(this);
+ mTrans.prepareMatrixOffset(this);
+ prepareContentRect();
}
@Override
@@ -2261,6 +2269,11 @@
return this;
}
+ @Override
+ public PointF getCenterOfView() {
+ return getCenter();
+ }
+
// @Override
// protected void onAttachedToWindow() {
// super.onAttachedToWindow();
diff --git a/MPChartLib/src/com/github/mikephil/charting/charts/HorizontalBarChart.java b/MPChartLib/src/com/github/mikephil/charting/charts/HorizontalBarChart.java
index 742866a..9bcd7ba 100644
--- a/MPChartLib/src/com/github/mikephil/charting/charts/HorizontalBarChart.java
+++ b/MPChartLib/src/com/github/mikephil/charting/charts/HorizontalBarChart.java
@@ -1,3 +1,4 @@
+
package com.github.mikephil.charting.charts;
import android.content.Context;
@@ -6,181 +7,188 @@
import android.graphics.Rect;
import android.util.AttributeSet;
-import com.github.mikephil.charting.renderer.ColumnToBarTransformer;
+import com.github.mikephil.charting.renderer.HorizontalBarChartTransformer;
import com.github.mikephil.charting.utils.Utils;
import com.github.mikephil.charting.utils.YLabels;
+/**
+ * BarChart with horizontal bar orientation. In this implementation, x- and
+ * y-axis are switched.
+ */
public class HorizontalBarChart extends BarChart
{
- public HorizontalBarChart(Context context)
- {
- super(context);
- }
+ public HorizontalBarChart(Context context)
+ {
+ super(context);
+ }
- public HorizontalBarChart(Context context, AttributeSet attrs)
- {
- super(context, attrs);
- }
+ public HorizontalBarChart(Context context, AttributeSet attrs)
+ {
+ super(context, attrs);
+ }
- public HorizontalBarChart(Context context, AttributeSet attrs,
- int defStyle)
- {
- super(context, attrs, defStyle);
- }
+ public HorizontalBarChart(Context context, AttributeSet attrs,
+ int defStyle)
+ {
+ super(context, attrs, defStyle);
+ }
- @Override
- protected void init()
- {
- super.init();
- setDrawXLabels(false);
- setDrawYLabels(true);
- mTrans = new ColumnToBarTransformer();
- mValuePaint.setTextAlign(Paint.Align.LEFT);
- }
+ @Override
+ protected void init() {
+ super.init();
- /**
- * Prepares a bar for drawing on the specified x-index and y-position. Also
- * prepares the shadow-bar if enabled.
- *
- * @param x the x-position
- * @param y the y-position
- * @param barspace the space between bars
- */
- @Override
- protected void prepareBar(float x, float y, float barspace) {
+ setDrawXLabels(false);
+ setDrawYLabels(true);
+ mTrans = new HorizontalBarChartTransformer();
+ mValuePaint.setTextAlign(Paint.Align.LEFT);
+ }
- super.prepareBar(x, y, barspace);
- float spaceHalf = barspace / 2f;
+ /**
+ * Prepares a bar for drawing on the specified x-index and y-position. Also
+ * prepares the shadow-bar if enabled.
+ *
+ * @param x the x-position
+ * @param y the y-position
+ * @param barspace the space between bars
+ */
+// @Override
+// protected void prepareBar(float x, float y, float barspace) {
+// super.prepareBar(x, y, barspace);
+//
+// float spaceHalf = barspace / 2f;
+//
+// float top = x + spaceHalf;
+// float bottom = x + 1f - spaceHalf;
+//
+// mBarRect.set(0, top, y, bottom);
+//
+// mTrans.rectValueToPixel(mBarRect, mPhaseY);
+//
+// // if a shadow is drawn, prepare it too
+// if (mDrawBarShadow) {
+// mBarShadow.set(mBarRect.left, mOffsetTop, mBarRect.right, getHeight() - mOffsetBottom);
+// }
+// }
- float top = x + spaceHalf;
- float bottom = x + 1f - spaceHalf;
+// @Override
+// protected void calcModulus() {
+//
+// float[] values = new float[9];
+// mTrans.getTouchMatrix().getValues(values);
+//
+// mXLabels.mYAxisLabelModulus = (int) Math
+// .ceil((mData.getXValCount() * mXLabels.mLabelHeight)
+// / (mContentRect.height() * values[Matrix.MSCALE_Y]));
+// }
+//
+// @Override
+// protected void drawXLabels(float yPos)
+// {
+// if (!mDrawXLabels)
+// return;
+//
+// mXLabelPaint.setTextAlign(Paint.Align.RIGHT);
+// // pre allocate to save performance (dont allocate in loop)
+// float[] position = new float[] {
+// 0f, 0f
+// };
+//
+// float offset = Utils.calcTextHeight(mXLabelPaint, mData.getXVals().get(0)) / 2f;
+//
+// int step = mData.getDataSetCount();
+//
+// for (int i = 0; i < mData.getXValCount(); i += mXLabels.mYAxisLabelModulus) {
+//
+// position[1] = i * step + i * mData.getGroupSpace()
+// + mData.getGroupSpace() / 2f;
+//
+// // center the text
+// if (mXLabels.isCenterXLabelsEnabled())
+// position[1] += (step / 2f);
+//
+// mTrans.pointValuesToPixel(position);
+//
+// if (position[1] >= mOffsetTop && position[1] <= getHeight() - mOffsetBottom)
+// {
+// String label = mData.getXVals().get(i);
+//
+// mDrawCanvas.drawText(label, mOffsetLeft - 10, position[1] + offset, mXLabelPaint);
+// }
+// }
+// }
- mBarRect.set(0, top, y, bottom);
+ private static String TALL_VALUE = "100%";
- mTrans.rectValueToPixel(mBarRect, mPhaseY);
+ @Override
+ protected float getPositiveYOffset(boolean drawAboveValueBar)
+ {
+ Rect bounds = new Rect();
+ mValuePaint.getTextBounds(TALL_VALUE, 0, TALL_VALUE.length(), bounds);
- // if a shadow is drawn, prepare it too
- if (mDrawBarShadow) {
- mBarShadow.set(mBarRect.left, mOffsetTop, mBarRect.right, getHeight() - mOffsetBottom);
- }
- }
-
- @Override
- protected void calcModulus() {
-
- float[] values = new float[9];
- mTrans.getTouchMatrix().getValues(values);
+ return bounds.height() / 2;
+ }
- mXLabels.mYAxisLabelModulus = (int) Math
- .ceil((mData.getXValCount() * mXLabels.mLabelHeight)
- / (mContentRect.height() * values[Matrix.MSCALE_Y]));
- }
+ @Override
+ protected float getNegativeYOffset(boolean drawAboveValueBar)
+ {
+ return getPositiveYOffset(drawAboveValueBar);
+ }
- @Override
- protected void drawXLabels(float yPos)
- {
- if (!mDrawXLabels) return;
+// /**
+// * Sets up the y-axis labels. Computes the desired number of labels between
+// * the two given extremes. Unlike the papareXLabels() method, this method
+// * needs to be called upon every refresh of the view.
+// *
+// * @return
+// */
+// @Override
+// protected void prepareYLabels()
+// {
+// mYLabels = new XLabelsAsYLabels();
+// // super.prepareYLabels();
+//
+// }
- mXLabelPaint.setTextAlign(Paint.Align.RIGHT);
- // pre allocate to save performance (dont allocate in loop)
- float[] position = new float[] {
- 0f, 0f
- };
-
- float offset = Utils.calcTextHeight(mXLabelPaint, mData.getXVals().get(0)) / 2f;
+ private class XLabelsAsYLabels extends YLabels
+ {
+ /**
+ * Returns the longest formatted label (in terms of characters) the
+ * y-labels contain.
+ *
+ * @return
+ */
+ @Override
+ public String getLongestLabel()
+ {
+ String longest = "";
- int step = mData.getDataSetCount();
+ for (int i = 0; i < mData.getXValCount(); i++)
+ {
+ String text = mData.getXVals().get(i);
- for (int i = 0; i < mData.getXValCount(); i += mXLabels.mYAxisLabelModulus) {
+ if (longest.length() < text.length())
+ longest = text;
+ }
- position[1] = i * step + i * mData.getGroupSpace()
- + mData.getGroupSpace() / 2f;
+ return longest;
+ }
- // center the text
- if (mXLabels.isCenterXLabelsEnabled())
- position[1] += (step / 2f);
+ /**
+ * Returns the formatted y-label at the specified index. This will
+ * either use the auto-formatter or the custom formatter (if one is
+ * set).
+ *
+ * @param index
+ * @return
+ */
+ @Override
+ public String getFormattedLabel(int index)
+ {
+ super.getFormattedLabel(index);
+ if (index < 0)
+ return "";
- mTrans.pointValuesToPixel(position);
-
- if (position[1] >= mOffsetTop && position[1] <= getHeight() - mOffsetBottom)
- {
- String label = mData.getXVals().get(i);
-
- mDrawCanvas.drawText(label, mOffsetLeft-10, position[1] + offset, mXLabelPaint);
- }
- }
- }
-
- private static String TALL_VALUE = "100%";
- @Override
- protected float getPositiveYOffset(boolean drawAboveValueBar)
- {
- Rect bounds = new Rect();
- mValuePaint.getTextBounds(TALL_VALUE,0,TALL_VALUE.length(),bounds);
-
- return bounds.height()/2;
- }
-
- @Override
- protected float getNegativeYOffset(boolean drawAboveValueBar)
- {
- return getPositiveYOffset(drawAboveValueBar);
- }
-
- /**
- * Sets up the y-axis labels. Computes the desired number of labels between
- * the two given extremes. Unlike the papareXLabels() method, this method
- * needs to be called upon every refresh of the view.
- *
- * @return
- */
- @Override
- protected void prepareYLabels()
- {
- mYLabels = new XLabelsAsYLabels();
-// super.prepareYLabels();
-
- }
-
- private class XLabelsAsYLabels extends YLabels
- {
- /**
- * Returns the longest formatted label (in terms of characters) the y-labels
- * contain.
- *
- * @return
- */
- @Override
- public String getLongestLabel()
- {
- String longest = "";
-
- for (int i = 0; i < mData.getXValCount(); i ++)
- {
- String text = mData.getXVals().get(i);
-
- if (longest.length() < text.length())
- longest = text;
- }
-
- return longest;
- }
-
- /**
- * Returns the formatted y-label at the specified index. This will either
- * use the auto-formatter or the custom formatter (if one is set).
- *
- * @param index
- * @return
- */
- @Override
- public String getFormattedLabel(int index)
- {
- super.getFormattedLabel(index);
- if (index < 0)
- return "";
-
- return mData.getXVals().get(index);
- }
- }
+ return mData.getXVals().get(index);
+ }
+ }
}
diff --git a/MPChartLib/src/com/github/mikephil/charting/interfaces/ChartInterface.java b/MPChartLib/src/com/github/mikephil/charting/interfaces/ChartInterface.java
index b19b89b..e243e30 100644
--- a/MPChartLib/src/com/github/mikephil/charting/interfaces/ChartInterface.java
+++ b/MPChartLib/src/com/github/mikephil/charting/interfaces/ChartInterface.java
@@ -1,6 +1,7 @@
package com.github.mikephil.charting.interfaces;
+import android.graphics.PointF;
import android.graphics.RectF;
import android.view.View;
@@ -32,6 +33,10 @@
public int getHeight();
+ public PointF getCenterOfView();
+
+ public PointF getCenterOffsets();
+
public RectF getContentRect();
public View getChartView();
diff --git a/MPChartLib/src/com/github/mikephil/charting/listener/BarLineChartTouchListener.java b/MPChartLib/src/com/github/mikephil/charting/listener/BarLineChartTouchListener.java
index 8f86b2c..93b3fe5 100644
--- a/MPChartLib/src/com/github/mikephil/charting/listener/BarLineChartTouchListener.java
+++ b/MPChartLib/src/com/github/mikephil/charting/listener/BarLineChartTouchListener.java
@@ -424,7 +424,6 @@
OnChartGestureListener l = mChart.getOnChartGestureListener();
if (l != null) {
-
l.onChartSingleTapped(e);
}
diff --git a/MPChartLib/src/com/github/mikephil/charting/renderer/ColumnToBarTransformer.java b/MPChartLib/src/com/github/mikephil/charting/renderer/ColumnToBarTransformer.java
deleted file mode 100644
index d4540b8..0000000
--- a/MPChartLib/src/com/github/mikephil/charting/renderer/ColumnToBarTransformer.java
+++ /dev/null
@@ -1,65 +0,0 @@
-package com.github.mikephil.charting.renderer;
-
-import com.github.mikephil.charting.data.BarData;
-import com.github.mikephil.charting.data.Entry;
-import com.github.mikephil.charting.interfaces.ChartInterface;
-
-import java.util.ArrayList;
-
-public class ColumnToBarTransformer extends Transformer
-{
- /**
- * Prepares the matrix that transforms values to pixels.
- *
- * @param chart
- */
- @Override
- public void prepareMatrixValuePx(ChartInterface chart)
- {
- float scaleX = (chart.getWidth() - chart.getOffsetRight() - chart.getOffsetLeft()) / chart.getDeltaY();
- float scaleY = (chart.getHeight() - chart.getOffsetTop() - chart.getOffsetBottom()) / chart.getDeltaX();
-
- // setup all matrices
- mMatrixValueToPx.reset();
- mMatrixValueToPx.postTranslate(0, -chart.getYChartMin());
- mMatrixValueToPx.postScale(scaleX, -scaleY);
- }
-
-
- /**
- * Transforms an arraylist of Entry into a float array containing the x and
- * y values transformed with all matrices for the BARCHART.
- *
- * @param entries
- * @param dataSet the dataset index
- * @return
- */
- public float[] generateTransformedValuesBarChart(ArrayList<? extends Entry> entries,
- int dataSet, BarData bd, float phaseY) {
-
- float[] valuePoints = new float[entries.size() * 2];
-
- int setCount = bd.getDataSetCount();
- float space = bd.getGroupSpace();
-
- for (int j = 0; j < valuePoints.length; j += 2) {
-
- int index = j/2;
- Entry e = entries.get(index);
-
- //TODO: Need to find a better value than 1, should be dependant on yMax, I think
- float x = e.getVal() + 1;
- // calculate the y-position, depending on datasetcount
- float y = e.getXIndex() + (index * (setCount - 1)) + dataSet + 0.5f + space * index
- + space / 2f;
-
- valuePoints[j] = x;
- valuePoints[j + 1] = y;
- }
-
- pointValuesToPixel(valuePoints);
-
- return valuePoints;
- }
-
-}
diff --git a/MPChartLib/src/com/github/mikephil/charting/renderer/HorizontalBarChartTransformer.java b/MPChartLib/src/com/github/mikephil/charting/renderer/HorizontalBarChartTransformer.java
new file mode 100644
index 0000000..f1e7450
--- /dev/null
+++ b/MPChartLib/src/com/github/mikephil/charting/renderer/HorizontalBarChartTransformer.java
@@ -0,0 +1,60 @@
+package com.github.mikephil.charting.renderer;
+
+import com.github.mikephil.charting.interfaces.ChartInterface;
+
+public class HorizontalBarChartTransformer extends Transformer {
+
+ /**
+ * Prepares the matrix that transforms values to pixels.
+ *
+ * @param chart
+ */
+ @Override
+ public void prepareMatrixValuePx(ChartInterface chart) {
+ float scaleX = (chart.getWidth() - chart.getOffsetRight() - chart.getOffsetLeft()) / chart.getDeltaY();
+ float scaleY = (chart.getHeight() - chart.getOffsetTop() - chart.getOffsetBottom()) / chart.getDeltaX();
+
+ // setup all matrices
+ mMatrixValueToPx.reset();
+ mMatrixValueToPx.postTranslate(0, -chart.getYChartMin());
+ mMatrixValueToPx.postScale(scaleX, -scaleY);
+ mMatrixValueToPx.postRotate(90, chart.getHeight() / 2, chart.getWidth() / 2);
+ }
+
+// /**
+// * Transforms an arraylist of Entry into a float array containing the x and
+// * y values transformed with all matrices for the BARCHART.
+// *
+// * @param entries
+// * @param dataSet the dataset index
+// * @return
+// */
+// public float[] generateTransformedValuesBarChart(ArrayList<? extends Entry> entries,
+// int dataSet, BarData bd, float phaseY) {
+//
+// float[] valuePoints = new float[entries.size() * 2];
+//
+// int setCount = bd.getDataSetCount();
+// float space = bd.getGroupSpace();
+//
+// for (int j = 0; j < valuePoints.length; j += 2) {
+//
+// int index = j/2;
+// Entry e = entries.get(index);
+//
+// //TODO: Need to find a better value than 1, should be dependant on yMax, I think
+// float x = e.getVal() + 1;
+// // calculate the y-position, depending on datasetcount
+// float y = e.getXIndex() + (index * (setCount - 1)) + dataSet + 0.5f + space * index
+// + space / 2f;
+//
+// valuePoints[j] = x;
+// valuePoints[j + 1] = y;
+// }
+//
+// pointValuesToPixel(valuePoints);
+//
+// return valuePoints;
+// }
+
+}
diff --git a/MPChartLib/src/com/github/mikephil/charting/renderer/Transformer.java b/MPChartLib/src/com/github/mikephil/charting/renderer/Transformer.java
index cbcd0c3..2112a26 100644
--- a/MPChartLib/src/com/github/mikephil/charting/renderer/Transformer.java
+++ b/MPChartLib/src/com/github/mikephil/charting/renderer/Transformer.java
@@ -56,7 +56,8 @@
}
/**
- * Prepares the matrix that transforms values to pixels.
+ * Prepares the matrix that transforms values to pixels. Calculates the
+ * scale factors from the charts size and offsets.
*
* @param chart
*/
@@ -74,6 +75,20 @@
}
/**
+ * Prepares the transformation matrix with the specified scales.
+ *
+ * @param chart
+ * @param scaleX
+ * @param scaleY
+ */
+ public void prepareMatrixValuePx(ChartInterface chart, float scaleX, float scaleY) {
+
+ mMatrixValueToPx.reset();
+ mMatrixValueToPx.postTranslate(0, -chart.getYChartMin());
+ mMatrixValueToPx.postScale(scaleX, -scaleY);
+ }
+
+ /**
* Prepares the matrix that contains all offsets.
*
* @param chart