Skip to content

Commit c471866

Browse files
committed
Added feature to custom format all values in the chart with the ValueFormatter interface. General bugfixes.
1 parent 23c1cf9 commit c471866

File tree

14 files changed

+107
-73
lines changed

14 files changed

+107
-73
lines changed

MPChartExample/AndroidManifest.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
33
package="com.xxmassdeveloper.mpchartexample"
4-
android:versionCode="20"
5-
android:versionName="1.6.1" >
4+
android:versionCode="21"
5+
android:versionName="1.6.2" >
66

77
<uses-sdk
88
android:minSdkVersion="8"

MPChartExample/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ android {
77
applicationId 'com.xxmassdeveloper.mpchartexample'
88
minSdkVersion 16
99
targetSdkVersion 19
10-
versionCode 20
11-
versionName '1.6.1'
10+
versionCode 21
11+
versionName '1.6.2'
1212

1313
sourceSets {
1414
main {
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
2+
package com.xxmassdeveloper.mpchartexample;
3+
4+
import com.github.mikephil.charting.utils.ValueFormatter;
5+
6+
import java.text.DecimalFormat;
7+
8+
public class MyValueFormatter implements ValueFormatter {
9+
10+
DecimalFormat mFormatter = new DecimalFormat("###,###,###");
11+
12+
@Override
13+
public String getFormattedValue(float value) {
14+
// do here whatever you want, avoid excessive calculations and memory
15+
// allocations
16+
return mFormatter.format(value);
17+
}
18+
}

MPChartExample/src/com/xxmassdeveloper/mpchartexample/StackedBarActivity.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,10 @@ protected void onCreate(Bundle savedInstanceState) {
6666
// drawn
6767
mChart.setMaxVisibleValueCount(60);
6868

69-
DecimalFormat f = new DecimalFormat("###,###,###");
69+
MyValueFormatter customFormatter = new MyValueFormatter();
7070

7171
// set a custom formatter for the values inside the chart
72-
mChart.setValueFormatter(f);
72+
mChart.setValueFormatter(customFormatter);
7373

7474
// if false values are only drawn for the stack sum, else each value is drawn
7575
mChart.setDrawValuesForWholeStack(true);
@@ -85,6 +85,7 @@ protected void onCreate(Bundle savedInstanceState) {
8585
YLabels yLabels = mChart.getYLabels();
8686
yLabels.setPosition(YLabelPosition.BOTH_SIDED);
8787
yLabels.setLabelCount(5);
88+
yLabels.setFormatter(customFormatter);
8889

8990
XLabels xLabels = mChart.getXLabels();
9091
xLabels.setPosition(XLabelPosition.TOP);

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -614,7 +614,7 @@ protected void drawValues() {
614614

615615
float val = entries.get(j / 2).getVal();
616616

617-
drawValue(mValueFormat.format(val), valuePoints[j],
617+
drawValue(mValueFormatter.getFormattedValue(val), valuePoints[j],
618618
valuePoints[j + 1] + offset);
619619
}
620620

@@ -639,7 +639,7 @@ protected void drawValues() {
639639
// in between
640640
if (vals == null) {
641641

642-
drawValue(mValueFormat.format(e.getVal()), valuePoints[j],
642+
drawValue(mValueFormatter.getFormattedValue(e.getVal()), valuePoints[j],
643643
valuePoints[j + 1] + offset);
644644

645645
} else {
@@ -659,7 +659,7 @@ protected void drawValues() {
659659

660660
for (int k = 0; k < transformed.length; k += 2) {
661661

662-
drawValue(mValueFormat.format(vals[k / 2]), valuePoints[j],
662+
drawValue(mValueFormatter.getFormattedValue(vals[k / 2]), valuePoints[j],
663663
transformed[k + 1] + offset);
664664
}
665665
}

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

Lines changed: 44 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import com.github.mikephil.charting.utils.MarkerView;
3535
import com.github.mikephil.charting.utils.SelInfo;
3636
import com.github.mikephil.charting.utils.Utils;
37+
import com.github.mikephil.charting.utils.ValueFormatter;
3738
import com.nineoldandroids.animation.ObjectAnimator;
3839
import com.nineoldandroids.animation.ValueAnimator;
3940
import com.nineoldandroids.animation.ValueAnimator.AnimatorUpdateListener;
@@ -70,8 +71,14 @@ public abstract class Chart<T extends ChartData<? extends DataSet<? extends Entr
7071
*/
7172
private int mBackgroundColor = Color.WHITE;
7273

73-
/** the decimalformat responsible for formatting the values in the chart */
74-
protected DecimalFormat mValueFormat = null;
74+
/** custom formatter that is used instead of the auto-formatter if set */
75+
protected ValueFormatter mValueFormatter = null;
76+
77+
/**
78+
* flag that indicates if the default formatter should be used or if a
79+
* custom one is set
80+
*/
81+
private boolean mUseDefaultFormatter = true;
7582

7683
/** chart offset to the left */
7784
protected float mOffsetLeft = 12;
@@ -327,7 +334,7 @@ protected void init() {
327334
* @param data
328335
*/
329336
public void setData(T data) {
330-
337+
331338
if (data == null || !data.isValid()) {
332339
Log.e(LOG_TAG,
333340
"Cannot set data for chart. Provided chart values are null or contain less than 1 entry.");
@@ -398,8 +405,8 @@ protected void calcMinMax(boolean fixedValues) {
398405
*/
399406
protected void calcFormats() {
400407

401-
// check if a custom formatter was set (then this flag is true)
402-
if (!mUseCustomFormatter) {
408+
// check if a custom formatter is set or not
409+
if (mUseDefaultFormatter) {
403410

404411
float reference = 0f;
405412

@@ -419,7 +426,8 @@ protected void calcFormats() {
419426
b.append("0");
420427
}
421428

422-
mValueFormat = new DecimalFormat("###,###,###,##0" + b.toString());
429+
DecimalFormat formatter = new DecimalFormat("###,###,###,##0" + b.toString());
430+
mValueFormatter = new DefaultValueFormatter(formatter);
423431
}
424432
}
425433

@@ -1066,10 +1074,11 @@ protected void drawMarkers() {
10661074
continue;
10671075

10681076
float[] pos = getMarkerPosition(e, dataSetIndex);
1069-
1077+
10701078
// check bounds
1071-
if(pos[0] < mOffsetLeft || pos[0] > getWidth() - mOffsetRight
1072-
|| pos[1] < mOffsetTop || pos[1] > getHeight() - mOffsetBottom) continue;
1079+
if (pos[0] < mOffsetLeft || pos[0] > getWidth() - mOffsetRight
1080+
|| pos[1] < mOffsetTop || pos[1] > getHeight() - mOffsetBottom)
1081+
continue;
10731082

10741083
// callbacks to update the content
10751084
mMarkerView.refreshContent(e, dataSetIndex);
@@ -1768,32 +1777,30 @@ public void setDrawMarkerViews(boolean enabled) {
17681777
mDrawMarkerViews = enabled;
17691778
}
17701779

1771-
private boolean mUseCustomFormatter = false;
1772-
17731780
/**
17741781
* Sets the formatter to be used for drawing the values inside the chart. If
1775-
* no formatter is set, the chart will automatically create one. To
1776-
* re-enable auto formatting after setting a custom formatter, call
1777-
* setValueFormatter(null).
1782+
* no formatter is set, the chart will automatically determine a reasonable
1783+
* formatting (concerning decimals) for all the values that are drawn inside
1784+
* the chart. Set this to NULL to re-enable auto formatting.
17781785
*
17791786
* @param f
17801787
*/
1781-
public void setValueFormatter(DecimalFormat f) {
1782-
mValueFormat = f;
1788+
public void setValueFormatter(ValueFormatter f) {
1789+
mValueFormatter = f;
17831790

1784-
if (f != null)
1785-
mUseCustomFormatter = true;
1791+
if (f == null)
1792+
mUseDefaultFormatter = true;
17861793
else
1787-
mUseCustomFormatter = false;
1794+
mUseDefaultFormatter = false;
17881795
}
17891796

17901797
/**
17911798
* Returns the formatter used for drawing the values inside the chart.
17921799
*
17931800
* @return
17941801
*/
1795-
public DecimalFormat getValueFormatter() {
1796-
return mValueFormat;
1802+
public ValueFormatter getValueFormatter() {
1803+
return mValueFormatter;
17971804
}
17981805

17991806
/**
@@ -2132,15 +2139,12 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
21322139
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
21332140
}
21342141

2135-
/** flag indicating if the matrix has alerady been prepared */
2136-
private boolean mMatrixOnLayoutPrepared = false;
2137-
21382142
@Override
21392143
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
21402144
super.onLayout(changed, left, top, right, bottom);
2141-
2145+
21422146
prepareContentRect();
2143-
2147+
21442148
//
21452149
// prepareContentRect();
21462150
// Log.i(LOG_TAG,
@@ -2155,6 +2159,20 @@ protected void onSizeChanged(int w, int h, int oldw, int oldh) {
21552159
super.onSizeChanged(w, h, oldw, oldh);
21562160
}
21572161

2162+
private class DefaultValueFormatter implements ValueFormatter {
2163+
2164+
private DecimalFormat mFormat;
2165+
2166+
public DefaultValueFormatter(DecimalFormat f) {
2167+
mFormat = f;
2168+
}
2169+
2170+
@Override
2171+
public String getFormattedValue(float value) {
2172+
return mFormat.format(value);
2173+
}
2174+
}
2175+
21582176
// @Override
21592177
// protected void onAttachedToWindow() {
21602178
// super.onAttachedToWindow();

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -309,12 +309,12 @@ protected void drawValues() {
309309

310310
if (mDrawUnitInChart) {
311311

312-
mDrawCanvas.drawText(mValueFormat.format(val) + mUnit, positions[j],
312+
mDrawCanvas.drawText(mValueFormatter.getFormattedValue(val) + mUnit, positions[j],
313313
positions[j + 1]
314314
- valOffset, mValuePaint);
315315
} else {
316316

317-
mDrawCanvas.drawText(mValueFormat.format(val), positions[j],
317+
mDrawCanvas.drawText(mValueFormatter.getFormattedValue(val), positions[j],
318318
positions[j + 1] - valOffset,
319319
mValuePaint);
320320
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -463,9 +463,9 @@ protected void drawValues() {
463463
float value = entries.get(j).getVal();
464464

465465
if (mUsePercentValues)
466-
val = mValueFormat.format(Math.abs(getPercentOfTotal(value))) + " %";
466+
val = mValueFormatter.getFormattedValue(Math.abs(getPercentOfTotal(value))) + " %";
467467
else
468-
val = mValueFormat.format(value);
468+
val = mValueFormatter.getFormattedValue(value);
469469

470470
if (mDrawUnitInChart)
471471
val = val + mUnit;

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -492,10 +492,10 @@ protected void drawValues() {
492492
PointF p = getPosition(c, e.getVal() * factor, sliceangle * j + mRotationAngle);
493493

494494
if (mDrawUnitInChart)
495-
mDrawCanvas.drawText(mValueFormat.format(e.getVal()) + mUnit,
495+
mDrawCanvas.drawText(mValueFormatter.getFormattedValue(e.getVal()) + mUnit,
496496
p.x, p.y - yoffset, mValuePaint);
497497
else
498-
mDrawCanvas.drawText(mValueFormat.format(e.getVal()),
498+
mDrawCanvas.drawText(mValueFormatter.getFormattedValue(e.getVal()),
499499
p.x, p.y - yoffset, mValuePaint);
500500
}
501501
}

0 commit comments

Comments
 (0)