diff --git a/MPChartLib/src/com/github/mikephil/charting/charts/BarLineChartBase.java b/MPChartLib/src/com/github/mikephil/charting/charts/BarLineChartBase.java index 4cb2cea9f..32bbb9ef5 100644 --- a/MPChartLib/src/com/github/mikephil/charting/charts/BarLineChartBase.java +++ b/MPChartLib/src/com/github/mikephil/charting/charts/BarLineChartBase.java @@ -655,24 +655,21 @@ private void drawXLabels(float yPos) { 0f, 0f }; - for (int i = 0; i < mCurrentData.getXValCount(); i++) { + for (int i = 0; i < mCurrentData.getXValCount(); i += mXLabels.mXAxisLabelModulus) { - if (i % mXLabels.mXAxisLabelModulus == 0) { + position[0] = i; - position[0] = i; + // center the text + if (mXLabels.isCenterXLabelsEnabled()) + position[0] += 0.5f; - // center the text - if (mXLabels.isCenterXLabelsEnabled()) - position[0] += 0.5f; + transformPointArray(position); - transformPointArray(position); + if (position[0] >= mOffsetLeft && position[0] <= getWidth() - mOffsetRight) { - if (position[0] >= mOffsetLeft && position[0] <= getWidth() - mOffsetRight) { - - mDrawCanvas.drawText(mCurrentData.getXVals().get(i), position[0], - yPos, - mXLabelPaint); - } + mDrawCanvas.drawText(mCurrentData.getXVals().get(i), position[0], + yPos, + mXLabelPaint); } } } @@ -848,19 +845,16 @@ protected void drawVerticalGrid() { 0f, 0f }; - for (int i = 0; i < mCurrentData.getXValCount(); i++) { - - if (i % mXLabels.mXAxisLabelModulus == 0) { + for (int i = 0; i < mCurrentData.getXValCount(); i += mXLabels.mXAxisLabelModulus) { - position[0] = i; + position[0] = i; - transformPointArray(position); + transformPointArray(position); - if (position[0] >= mOffsetLeft && position[0] <= getWidth()) { + if (position[0] >= mOffsetLeft && position[0] <= getWidth()) { - mDrawCanvas.drawLine(position[0], mOffsetTop, position[0], getHeight() - - mOffsetBottom, mGridPaint); - } + mDrawCanvas.drawLine(position[0], mOffsetTop, position[0], getHeight() + - mOffsetBottom, mGridPaint); } } } @@ -1517,6 +1511,11 @@ public Highlight getHighlightByTouchPoint(float x, float y) { } } + if(mDataNotSet) { + Log.i(LOG_TAG, "no data set"); + return null; + } + ArrayList valsAtIndex = getYValsAtIndex(xIndex); dataSetIndex = getClosestDataSetIndex(valsAtIndex, (float) yTouchVal); diff --git a/MPChartLib/src/com/github/mikephil/charting/charts/Chart.java b/MPChartLib/src/com/github/mikephil/charting/charts/Chart.java index a245d6b85..a1a067ead 100644 --- a/MPChartLib/src/com/github/mikephil/charting/charts/Chart.java +++ b/MPChartLib/src/com/github/mikephil/charting/charts/Chart.java @@ -19,6 +19,7 @@ import android.graphics.Typeface; import android.os.Environment; import android.provider.MediaStore.Images; +import android.text.TextUtils; import android.util.AttributeSet; import android.util.Log; import android.view.MotionEvent; @@ -193,6 +194,15 @@ public abstract class Chart extends View { /** listener that is called when a value on the chart is selected */ protected OnChartValueSelectedListener mSelectionListener; + /** text that is displayed when the chart is empty */ + private String mNoDataText = "No chart data available."; + + /** + * text that is displayed when the chart is empty that describes why the + * chart is empty + */ + private String mNoDataTextDescription; + /** default constructor for initialization in code */ public Chart(Context context) { super(context); @@ -340,6 +350,32 @@ public void setData(ChartData data) { // setData(data); // } + // /** + // * Sets primitive data for the chart. Internally, this is converted into a + // * ChartData object with one DataSet (type 0). If you have more specific + // * requirements for your data, use the setData(ChartData data) method and + // * create your own ChartData object with as many DataSets as you like. + // * + // * @param xVals + // * @param yVals + // */ + // public void setData(ArrayList xVals, ArrayList yVals) { + // + // ArrayList series = new ArrayList(); + // + // for (int i = 0; i < yVals.size(); i++) { + // series.add(new Entry(yVals.get(i), i)); + // } + // + // DataSet set = new DataSet(series, "DataSet"); + // ArrayList dataSets = new ArrayList(); + // dataSets.add(set); + // + // ChartData data = new ChartData(xVals, dataSets); + // + // setData(data); + // } + /** * does needed preparations for drawing */ @@ -384,7 +420,13 @@ protected void onDraw(Canvas canvas) { if (mDataNotSet) { // check if there is data // if no data, inform the user - canvas.drawText("No chart data available.", getWidth() / 2, getHeight() / 2, mInfoPaint); + canvas.drawText(mNoDataText, getWidth() / 2, getHeight() / 2, mInfoPaint); + + if (!TextUtils.isEmpty(mNoDataTextDescription)) { + float textOffset = -mInfoPaint.ascent() + mInfoPaint.descent(); + canvas.drawText(mNoDataTextDescription, getWidth() / 2, (getHeight() / 2) + + textOffset, mInfoPaint); + } return; } @@ -413,7 +455,7 @@ protected void prepareMatrix() { mMatrixValueToPx.postScale(scaleX, -scaleY); mMatrixOffset.reset(); - + mMatrixOffset.postTranslate(mOffsetLeft, getHeight() - mOffsetBottom); // mMatrixOffset.setTranslate(mOffsetLeft, 0); @@ -852,7 +894,7 @@ protected void drawDescription() { @Override public boolean onTouchEvent(MotionEvent event) { - if (mListener == null) + if (mListener == null || mDataNotSet) return false; // check if touch gestures are enabled @@ -1176,6 +1218,26 @@ public void setDescription(String desc) { this.mDescription = desc; } + /** + * Sets the text that informs the user that there is no data available with + * which to draw the chart. + * + * @param text + */ + public void setNoDataText(String text) { + mNoDataText = text; + } + + /** + * Sets descriptive text to explain to the user why there is no chart + * available Defaults to empty if not set + * + * @param text + */ + public void setNoDataTextDescription(String text) { + mNoDataTextDescription = text; + } + /** * 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 diff --git a/README.md b/README.md index e5c0d3273..22bada6af 100644 --- a/README.md +++ b/README.md @@ -7,14 +7,22 @@ Remember: *It's all about the looks.* The **experimental** branch might contain new features that are still buggy. It is recommended to be safe and only make use of the code on the **master** branch. -If you are having questions or problems, feel free to contact me, **create issues** for this project on GitHub or open questions on [**stackoverflow**](https://stackoverflow.com) with the `mpandroidchart` tag. - -Forks, pull-requests or any other forms of contribution are always welcome. +Forks, pull-requests or any other forms of contribution are **always welcome**. Demo ----- -For a brief overview of the most important features, please download the **PlayStore Demo** [**MPAndroidChart Example.apk**](https://play.google.com/store/apps/details?id=com.xxmassdeveloper.mpchartexample) and try it out. +For a brief overview of the most important features, please download the **PlayStore Demo** [**MPAndroidChart Example.apk**](https://play.google.com/store/apps/details?id=com.xxmassdeveloper.mpchartexample) and try it out. The corresponding code for the demo-application is also included in this repository inside the **MPChartExample folder**. + +Questions & Issues +----- + +If you are having questions or problems, feel free to contact me. Since I would very much like that other users of this library **can also benefit** from your question, I am asking you to contact me via e-mail **only as a last option**. Instead, you should: + + - Open questions on [**stackoverflow**](https://stackoverflow.com) with the `mpandroidchart` tag + - Create Issues here on GitHub + +Please let me know via e-mail that you have opened a stackoverflow question so that I can get to answering it right away. Thank you. Features =======