Skip to content

Commit 13c693e

Browse files
committed
added ability to customise 'no data available' text and added an optional description
1 parent fedb234 commit 13c693e

1 file changed

Lines changed: 26 additions & 1 deletion

File tree

  • MPChartLib/src/com/github/mikephil/charting/charts

MPChartLib/src/com/github/mikephil/charting/charts/Chart.java

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import android.graphics.Typeface;
2020
import android.os.Environment;
2121
import android.provider.MediaStore.Images;
22+
import android.text.TextUtils;
2223
import android.util.AttributeSet;
2324
import android.util.Log;
2425
import android.view.MotionEvent;
@@ -187,6 +188,8 @@ public abstract class Chart extends View {
187188

188189
/** listener that is called when a value on the chart is selected */
189190
protected OnChartValueSelectedListener mSelectionListener;
191+
private String mNoDataText = "No chart data available.";
192+
private String mNoDataTextDescription;
190193

191194
/** default constructor for initialization in code */
192195
public Chart(Context context) {
@@ -310,6 +313,23 @@ public void setData(ChartData data) {
310313
Log.i(LOG_TAG, "Data is set.");
311314
}
312315

316+
/**
317+
* Informs the user that there is no data available with which to draw the chart
318+
* @param text
319+
*/
320+
public void setNoDataText(String text) {
321+
mNoDataText = text;
322+
}
323+
324+
/**
325+
* Sets descriptive text to explain to the user why there is no chart available
326+
* Defaults to empty if not set
327+
* @param text
328+
*/
329+
public void setNoDataTextDescription(String text) {
330+
mNoDataTextDescription = text;
331+
}
332+
313333
/**
314334
* Sets primitive data for the chart. Internally, this is converted into a
315335
* ChartData object with one DataSet (type 0). If you have more specific
@@ -380,7 +400,12 @@ protected void onDraw(Canvas canvas) {
380400
if (mDataNotSet) { // check if there is data
381401

382402
// if no data, inform the user
383-
canvas.drawText("No chart data available.", getWidth() / 2, getHeight() / 2, mInfoPaint);
403+
canvas.drawText(mNoDataText, getWidth() / 2, getHeight() / 2, mInfoPaint);
404+
405+
if(!TextUtils.isEmpty(mNoDataTextDescription)) {
406+
float textOffset = -mInfoPaint.ascent() + mInfoPaint.descent();
407+
canvas.drawText(mNoDataTextDescription, getWidth() / 2, (getHeight() / 2) + textOffset, mInfoPaint);
408+
}
384409
return;
385410
}
386411

0 commit comments

Comments
 (0)