|
19 | 19 | import android.graphics.Typeface; |
20 | 20 | import android.os.Environment; |
21 | 21 | import android.provider.MediaStore.Images; |
| 22 | +import android.text.TextUtils; |
22 | 23 | import android.util.AttributeSet; |
23 | 24 | import android.util.Log; |
24 | 25 | import android.view.MotionEvent; |
@@ -187,6 +188,8 @@ public abstract class Chart extends View { |
187 | 188 |
|
188 | 189 | /** listener that is called when a value on the chart is selected */ |
189 | 190 | protected OnChartValueSelectedListener mSelectionListener; |
| 191 | + private String mNoDataText = "No chart data available."; |
| 192 | + private String mNoDataTextDescription; |
190 | 193 |
|
191 | 194 | /** default constructor for initialization in code */ |
192 | 195 | public Chart(Context context) { |
@@ -310,6 +313,23 @@ public void setData(ChartData data) { |
310 | 313 | Log.i(LOG_TAG, "Data is set."); |
311 | 314 | } |
312 | 315 |
|
| 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 | + |
313 | 333 | /** |
314 | 334 | * Sets primitive data for the chart. Internally, this is converted into a |
315 | 335 | * ChartData object with one DataSet (type 0). If you have more specific |
@@ -380,7 +400,12 @@ protected void onDraw(Canvas canvas) { |
380 | 400 | if (mDataNotSet) { // check if there is data |
381 | 401 |
|
382 | 402 | // 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 | + } |
384 | 409 | return; |
385 | 410 | } |
386 | 411 |
|
|
0 commit comments