Skip to content

Commit

Permalink
Added feature to custom format all values in the chart with the Value…
Browse files Browse the repository at this point in the history
…Formatter interface. General bugfixes.
  • Loading branch information
PhilJay committed Sep 28, 2014
1 parent 23c1cf9 commit c471866
Show file tree
Hide file tree
Showing 14 changed files with 107 additions and 73 deletions.
4 changes: 2 additions & 2 deletions MPChartExample/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.xxmassdeveloper.mpchartexample"
android:versionCode="20"
android:versionName="1.6.1" >
android:versionCode="21"
android:versionName="1.6.2" >

<uses-sdk
android:minSdkVersion="8"
Expand Down
4 changes: 2 additions & 2 deletions MPChartExample/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ android {
applicationId 'com.xxmassdeveloper.mpchartexample'
minSdkVersion 16
targetSdkVersion 19
versionCode 20
versionName '1.6.1'
versionCode 21
versionName '1.6.2'

sourceSets {
main {
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@

package com.xxmassdeveloper.mpchartexample;

import com.github.mikephil.charting.utils.ValueFormatter;

import java.text.DecimalFormat;

public class MyValueFormatter implements ValueFormatter {

DecimalFormat mFormatter = new DecimalFormat("###,###,###");

@Override
public String getFormattedValue(float value) {
// do here whatever you want, avoid excessive calculations and memory
// allocations
return mFormatter.format(value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,10 @@ protected void onCreate(Bundle savedInstanceState) {
// drawn
mChart.setMaxVisibleValueCount(60);

DecimalFormat f = new DecimalFormat("###,###,###");
MyValueFormatter customFormatter = new MyValueFormatter();

// set a custom formatter for the values inside the chart
mChart.setValueFormatter(f);
mChart.setValueFormatter(customFormatter);

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

XLabels xLabels = mChart.getXLabels();
xLabels.setPosition(XLabelPosition.TOP);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -614,7 +614,7 @@ protected void drawValues() {

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

drawValue(mValueFormat.format(val), valuePoints[j],
drawValue(mValueFormatter.getFormattedValue(val), valuePoints[j],
valuePoints[j + 1] + offset);
}

Expand All @@ -639,7 +639,7 @@ protected void drawValues() {
// in between
if (vals == null) {

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

} else {
Expand All @@ -659,7 +659,7 @@ protected void drawValues() {

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

drawValue(mValueFormat.format(vals[k / 2]), valuePoints[j],
drawValue(mValueFormatter.getFormattedValue(vals[k / 2]), valuePoints[j],
transformed[k + 1] + offset);
}
}
Expand Down
70 changes: 44 additions & 26 deletions MPChartLib/src/com/github/mikephil/charting/charts/Chart.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import com.github.mikephil.charting.utils.MarkerView;
import com.github.mikephil.charting.utils.SelInfo;
import com.github.mikephil.charting.utils.Utils;
import com.github.mikephil.charting.utils.ValueFormatter;
import com.nineoldandroids.animation.ObjectAnimator;
import com.nineoldandroids.animation.ValueAnimator;
import com.nineoldandroids.animation.ValueAnimator.AnimatorUpdateListener;
Expand Down Expand Up @@ -70,8 +71,14 @@ public abstract class Chart<T extends ChartData<? extends DataSet<? extends Entr
*/
private int mBackgroundColor = Color.WHITE;

/** the decimalformat responsible for formatting the values in the chart */
protected DecimalFormat mValueFormat = null;
/** custom formatter that is used instead of the auto-formatter if set */
protected ValueFormatter mValueFormatter = null;

/**
* flag that indicates if the default formatter should be used or if a
* custom one is set
*/
private boolean mUseDefaultFormatter = true;

/** chart offset to the left */
protected float mOffsetLeft = 12;
Expand Down Expand Up @@ -327,7 +334,7 @@ protected void init() {
* @param data
*/
public void setData(T data) {

if (data == null || !data.isValid()) {
Log.e(LOG_TAG,
"Cannot set data for chart. Provided chart values are null or contain less than 1 entry.");
Expand Down Expand Up @@ -398,8 +405,8 @@ protected void calcMinMax(boolean fixedValues) {
*/
protected void calcFormats() {

// check if a custom formatter was set (then this flag is true)
if (!mUseCustomFormatter) {
// check if a custom formatter is set or not
if (mUseDefaultFormatter) {

float reference = 0f;

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

mValueFormat = new DecimalFormat("###,###,###,##0" + b.toString());
DecimalFormat formatter = new DecimalFormat("###,###,###,##0" + b.toString());
mValueFormatter = new DefaultValueFormatter(formatter);
}
}

Expand Down Expand Up @@ -1066,10 +1074,11 @@ protected void drawMarkers() {
continue;

float[] pos = getMarkerPosition(e, dataSetIndex);

// check bounds
if(pos[0] < mOffsetLeft || pos[0] > getWidth() - mOffsetRight
|| pos[1] < mOffsetTop || pos[1] > getHeight() - mOffsetBottom) continue;
if (pos[0] < mOffsetLeft || pos[0] > getWidth() - mOffsetRight
|| pos[1] < mOffsetTop || pos[1] > getHeight() - mOffsetBottom)
continue;

// callbacks to update the content
mMarkerView.refreshContent(e, dataSetIndex);
Expand Down Expand Up @@ -1768,32 +1777,30 @@ public void setDrawMarkerViews(boolean enabled) {
mDrawMarkerViews = enabled;
}

private boolean mUseCustomFormatter = false;

/**
* Sets the formatter to be used for drawing the values inside the chart. If
* no formatter is set, the chart will automatically create one. To
* re-enable auto formatting after setting a custom formatter, call
* setValueFormatter(null).
* no formatter is set, the chart will automatically determine a reasonable
* formatting (concerning decimals) for all the values that are drawn inside
* the chart. Set this to NULL to re-enable auto formatting.
*
* @param f
*/
public void setValueFormatter(DecimalFormat f) {
mValueFormat = f;
public void setValueFormatter(ValueFormatter f) {
mValueFormatter = f;

if (f != null)
mUseCustomFormatter = true;
if (f == null)
mUseDefaultFormatter = true;
else
mUseCustomFormatter = false;
mUseDefaultFormatter = false;
}

/**
* Returns the formatter used for drawing the values inside the chart.
*
* @return
*/
public DecimalFormat getValueFormatter() {
return mValueFormat;
public ValueFormatter getValueFormatter() {
return mValueFormatter;
}

/**
Expand Down Expand Up @@ -2132,15 +2139,12 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}

/** flag indicating if the matrix has alerady been prepared */
private boolean mMatrixOnLayoutPrepared = false;

@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);

prepareContentRect();

//
// prepareContentRect();
// Log.i(LOG_TAG,
Expand All @@ -2155,6 +2159,20 @@ protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
}

private class DefaultValueFormatter implements ValueFormatter {

private DecimalFormat mFormat;

public DefaultValueFormatter(DecimalFormat f) {
mFormat = f;
}

@Override
public String getFormattedValue(float value) {
return mFormat.format(value);
}
}

// @Override
// protected void onAttachedToWindow() {
// super.onAttachedToWindow();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -309,12 +309,12 @@ protected void drawValues() {

if (mDrawUnitInChart) {

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

mDrawCanvas.drawText(mValueFormat.format(val), positions[j],
mDrawCanvas.drawText(mValueFormatter.getFormattedValue(val), positions[j],
positions[j + 1] - valOffset,
mValuePaint);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -463,9 +463,9 @@ protected void drawValues() {
float value = entries.get(j).getVal();

if (mUsePercentValues)
val = mValueFormat.format(Math.abs(getPercentOfTotal(value))) + " %";
val = mValueFormatter.getFormattedValue(Math.abs(getPercentOfTotal(value))) + " %";
else
val = mValueFormat.format(value);
val = mValueFormatter.getFormattedValue(value);

if (mDrawUnitInChart)
val = val + mUnit;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -492,10 +492,10 @@ protected void drawValues() {
PointF p = getPosition(c, e.getVal() * factor, sliceangle * j + mRotationAngle);

if (mDrawUnitInChart)
mDrawCanvas.drawText(mValueFormat.format(e.getVal()) + mUnit,
mDrawCanvas.drawText(mValueFormatter.getFormattedValue(e.getVal()) + mUnit,
p.x, p.y - yoffset, mValuePaint);
else
mDrawCanvas.drawText(mValueFormat.format(e.getVal()),
mDrawCanvas.drawText(mValueFormatter.getFormattedValue(e.getVal()),
p.x, p.y - yoffset, mValuePaint);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,11 @@ protected void drawValues() {

if (mDrawUnitInChart) {

mDrawCanvas.drawText(mValueFormat.format(val) + mUnit, positions[j],
mDrawCanvas.drawText(mValueFormatter.getFormattedValue(val) + mUnit, positions[j],
positions[j + 1] - shapeSize, mValuePaint);
} else {

mDrawCanvas.drawText(mValueFormat.format(val), positions[j],
mDrawCanvas.drawText(mValueFormatter.getFormattedValue(val), positions[j],
positions[j + 1] - shapeSize,
mValuePaint);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@

package com.github.mikephil.charting.utils;

/**
* Interface that allows custom formatting of all values and value-labels
* displayed inside the chart. Simply create your own formatting class and let
* it implement ValueFormatter. Then override the getFormattedLabel(...) method
* and return whatever you want.
*
* @author Philipp Jahoda
*/
public interface ValueFormatter {

/**
* Called when a value (from labels, or inside the chart) is formatted
* before being drawn. For performance reasons, avoid excessive calculations
* and memory allocations inside this method.
*
* @param value the value to be formatted
* @return the formatted label ready for being drawn
*/
public String getFormattedValue(float value);
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public class YLabels extends LabelBase {
protected boolean mShowOnlyMinMax = false;

/** the formatter used to customly format the y-labels */
private YLabelFormatter mFormatter = null;
private ValueFormatter mFormatter = null;

/** the position of the y-labels relative to the chart */
private YLabelPosition mPosition = YLabelPosition.LEFT;
Expand Down Expand Up @@ -151,7 +151,7 @@ public boolean isSeparateThousandsEnabled() {
*
* @return
*/
public YLabelFormatter getFormatter() {
public ValueFormatter getFormatter() {
return mFormatter;
}

Expand All @@ -160,7 +160,7 @@ public YLabelFormatter getFormatter() {
*
* @param f
*/
public void setFormatter(YLabelFormatter f) {
public void setFormatter(ValueFormatter f) {
this.mFormatter = f;
}

Expand Down Expand Up @@ -202,7 +202,7 @@ public String getFormattedLabel(int index) {
text = Utils.formatNumber(mEntries[index], mDecimals,
isSeparateThousandsEnabled());
else
text = getFormatter().getFormattedLabel(mEntries[index]);
text = getFormatter().getFormattedValue(mEntries[index]);

return text;
}
Expand Down

0 comments on commit c471866

Please sign in to comment.