aboutsummaryrefslogtreecommitdiff
path: root/MPChartLib/src
diff options
context:
space:
mode:
authoralmic <mick.ashton@flare-esports.net>2018-11-07 12:41:53 -0700
committeralmic <mick.ashton@flare-esports.net>2018-11-07 12:41:53 -0700
commite5b66192e7b303d7d25fc172b1878c055b554047 (patch)
tree0347038c13cb3346196c96e67b9554bedf99398c /MPChartLib/src
parent608d9e29f59ebd63e234e13a4b4e1f3b3158e8c7 (diff)
downloadMPAndroidChart-e5b66192e7b303d7d25fc172b1878c055b554047.tar.gz
New ValueFormatter
I created a simplified value formatter class, which is an abstract class rather than an interface. The switch was chosen because the new format has all the methods predefined (something an interface wouldn't allow) meaning you can extend it and only change what you want. This also means that you only need one value formatting class for labels rather than two different classes, it just makes more sense. Please check the method signatures to learn how to use them, I'm sure you'll find this new format is much more customizable and faster to use. I've made the class abstract even though there are no abstract methods or fields, this is because it would certainly be a mistake to create a ValueFormatter and not override any methods. To convert existing code, just use 'extends' instead of 'implements' and change the names to 'ValueFormatter'. You'll need to change the methods you overwrite as well, just check the class and use the one you need.
Diffstat (limited to 'MPChartLib/src')
-rw-r--r--MPChartLib/src/main/java/com/github/mikephil/charting/charts/Chart.java5
-rw-r--r--MPChartLib/src/main/java/com/github/mikephil/charting/components/AxisBase.java11
-rw-r--r--MPChartLib/src/main/java/com/github/mikephil/charting/data/BaseDataSet.java8
-rw-r--r--MPChartLib/src/main/java/com/github/mikephil/charting/data/ChartData.java5
-rw-r--r--MPChartLib/src/main/java/com/github/mikephil/charting/formatter/DefaultAxisValueFormatter.java8
-rw-r--r--MPChartLib/src/main/java/com/github/mikephil/charting/formatter/DefaultValueFormatter.java8
-rw-r--r--MPChartLib/src/main/java/com/github/mikephil/charting/formatter/IAxisValueFormatter.java6
-rw-r--r--MPChartLib/src/main/java/com/github/mikephil/charting/formatter/IValueFormatter.java10
-rw-r--r--MPChartLib/src/main/java/com/github/mikephil/charting/formatter/IndexAxisValueFormatter.java12
-rw-r--r--MPChartLib/src/main/java/com/github/mikephil/charting/formatter/LargeValueFormatter.java16
-rw-r--r--MPChartLib/src/main/java/com/github/mikephil/charting/formatter/PercentFormatter.java39
-rw-r--r--MPChartLib/src/main/java/com/github/mikephil/charting/formatter/StackedValueFormatter.java24
-rw-r--r--MPChartLib/src/main/java/com/github/mikephil/charting/formatter/ValueFormatter.java137
-rw-r--r--MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/dataprovider/ChartInterface.java4
-rw-r--r--MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/datasets/IDataSet.java7
-rw-r--r--MPChartLib/src/main/java/com/github/mikephil/charting/renderer/BarChartRenderer.java25
-rw-r--r--MPChartLib/src/main/java/com/github/mikephil/charting/renderer/BubbleChartRenderer.java13
-rw-r--r--MPChartLib/src/main/java/com/github/mikephil/charting/renderer/CandleStickChartRenderer.java20
-rw-r--r--MPChartLib/src/main/java/com/github/mikephil/charting/renderer/CombinedChartRenderer.java7
-rw-r--r--MPChartLib/src/main/java/com/github/mikephil/charting/renderer/DataRenderer.java21
-rw-r--r--MPChartLib/src/main/java/com/github/mikephil/charting/renderer/HorizontalBarChartRenderer.java17
-rw-r--r--MPChartLib/src/main/java/com/github/mikephil/charting/renderer/LineChartRenderer.java13
-rw-r--r--MPChartLib/src/main/java/com/github/mikephil/charting/renderer/PieChartRenderer.java29
-rw-r--r--MPChartLib/src/main/java/com/github/mikephil/charting/renderer/RadarChartRenderer.java20
-rw-r--r--MPChartLib/src/main/java/com/github/mikephil/charting/renderer/ScatterChartRenderer.java19
-rw-r--r--MPChartLib/src/main/java/com/github/mikephil/charting/renderer/XAxisRenderer.java3
-rw-r--r--MPChartLib/src/main/java/com/github/mikephil/charting/renderer/XAxisRendererHorizontalBarChart.java7
-rw-r--r--MPChartLib/src/main/java/com/github/mikephil/charting/renderer/XAxisRendererRadarChart.java4
-rw-r--r--MPChartLib/src/main/java/com/github/mikephil/charting/utils/Utils.java24
-rw-r--r--MPChartLib/src/test/java/com/github/mikephil/charting/test/LargeValueFormatterTest.java50
-rw-r--r--MPChartLib/src/test/java/com/github/mikephil/charting/test/ObjectPoolTest.java2
31 files changed, 339 insertions, 235 deletions
diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/charts/Chart.java b/MPChartLib/src/main/java/com/github/mikephil/charting/charts/Chart.java
index 718d7e2a..5c82f9ab 100644
--- a/MPChartLib/src/main/java/com/github/mikephil/charting/charts/Chart.java
+++ b/MPChartLib/src/main/java/com/github/mikephil/charting/charts/Chart.java
@@ -1,4 +1,3 @@
-
package com.github.mikephil.charting.charts;
import android.animation.ValueAnimator;
@@ -35,7 +34,7 @@ import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.data.ChartData;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.formatter.DefaultValueFormatter;
-import com.github.mikephil.charting.formatter.IValueFormatter;
+import com.github.mikephil.charting.formatter.ValueFormatter;
import com.github.mikephil.charting.highlight.ChartHighlighter;
import com.github.mikephil.charting.highlight.Highlight;
import com.github.mikephil.charting.highlight.IHighlighter;
@@ -1015,7 +1014,7 @@ public abstract class Chart<T extends ChartData<? extends IDataSet<? extends Ent
*
* @return
*/
- public IValueFormatter getDefaultValueFormatter() {
+ public ValueFormatter getDefaultValueFormatter() {
return mDefaultValueFormatter;
}
diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/components/AxisBase.java b/MPChartLib/src/main/java/com/github/mikephil/charting/components/AxisBase.java
index 3c8028c2..c1f02828 100644
--- a/MPChartLib/src/main/java/com/github/mikephil/charting/components/AxisBase.java
+++ b/MPChartLib/src/main/java/com/github/mikephil/charting/components/AxisBase.java
@@ -1,4 +1,3 @@
-
package com.github.mikephil.charting.components;
import android.graphics.Color;
@@ -6,7 +5,7 @@ import android.graphics.DashPathEffect;
import android.util.Log;
import com.github.mikephil.charting.formatter.DefaultAxisValueFormatter;
-import com.github.mikephil.charting.formatter.IAxisValueFormatter;
+import com.github.mikephil.charting.formatter.ValueFormatter;
import com.github.mikephil.charting.utils.Utils;
import java.util.ArrayList;
@@ -22,7 +21,7 @@ public abstract class AxisBase extends ComponentBase {
/**
* custom formatter that is used instead of the auto-formatter if set
*/
- protected IAxisValueFormatter mAxisValueFormatter;
+ protected ValueFormatter mAxisValueFormatter;
private int mGridColor = Color.GRAY;
@@ -486,7 +485,7 @@ public abstract class AxisBase extends ComponentBase {
if (index < 0 || index >= mEntries.length)
return "";
else
- return getValueFormatter().getFormattedValue(mEntries[index], this);
+ return getValueFormatter().getAxisLabel(mEntries[index], this);
}
/**
@@ -498,7 +497,7 @@ public abstract class AxisBase extends ComponentBase {
*
* @param f
*/
- public void setValueFormatter(IAxisValueFormatter f) {
+ public void setValueFormatter(ValueFormatter f) {
if (f == null)
mAxisValueFormatter = new DefaultAxisValueFormatter(mDecimals);
@@ -511,7 +510,7 @@ public abstract class AxisBase extends ComponentBase {
*
* @return
*/
- public IAxisValueFormatter getValueFormatter() {
+ public ValueFormatter getValueFormatter() {
if (mAxisValueFormatter == null ||
(mAxisValueFormatter instanceof DefaultAxisValueFormatter &&
diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/data/BaseDataSet.java b/MPChartLib/src/main/java/com/github/mikephil/charting/data/BaseDataSet.java
index 7800986d..8ca3e68d 100644
--- a/MPChartLib/src/main/java/com/github/mikephil/charting/data/BaseDataSet.java
+++ b/MPChartLib/src/main/java/com/github/mikephil/charting/data/BaseDataSet.java
@@ -7,7 +7,7 @@ import android.graphics.Typeface;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.YAxis;
-import com.github.mikephil.charting.formatter.IValueFormatter;
+import com.github.mikephil.charting.formatter.ValueFormatter;
import com.github.mikephil.charting.interfaces.datasets.IDataSet;
import com.github.mikephil.charting.model.GradientColor;
import com.github.mikephil.charting.utils.ColorTemplate;
@@ -56,7 +56,7 @@ public abstract class BaseDataSet<T extends Entry> implements IDataSet<T> {
/**
* custom formatter that is used instead of the auto-formatter if set
*/
- protected transient IValueFormatter mValueFormatter;
+ protected transient ValueFormatter mValueFormatter;
/**
* the typeface used for the value text
@@ -313,7 +313,7 @@ public abstract class BaseDataSet<T extends Entry> implements IDataSet<T> {
}
@Override
- public void setValueFormatter(IValueFormatter f) {
+ public void setValueFormatter(ValueFormatter f) {
if (f == null)
return;
@@ -322,7 +322,7 @@ public abstract class BaseDataSet<T extends Entry> implements IDataSet<T> {
}
@Override
- public IValueFormatter getValueFormatter() {
+ public ValueFormatter getValueFormatter() {
if (needsFormatter())
return Utils.getDefaultValueFormatter();
return mValueFormatter;
diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/data/ChartData.java b/MPChartLib/src/main/java/com/github/mikephil/charting/data/ChartData.java
index 60d89f47..9bd46029 100644
--- a/MPChartLib/src/main/java/com/github/mikephil/charting/data/ChartData.java
+++ b/MPChartLib/src/main/java/com/github/mikephil/charting/data/ChartData.java
@@ -1,11 +1,10 @@
-
package com.github.mikephil.charting.data;
import android.graphics.Typeface;
import android.util.Log;
import com.github.mikephil.charting.components.YAxis.AxisDependency;
-import com.github.mikephil.charting.formatter.IValueFormatter;
+import com.github.mikephil.charting.formatter.ValueFormatter;
import com.github.mikephil.charting.highlight.Highlight;
import com.github.mikephil.charting.interfaces.datasets.IDataSet;
@@ -659,7 +658,7 @@ public abstract class ChartData<T extends IDataSet<? extends Entry>> {
*
* @param f
*/
- public void setValueFormatter(IValueFormatter f) {
+ public void setValueFormatter(ValueFormatter f) {
if (f == null)
return;
else {
diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/formatter/DefaultAxisValueFormatter.java b/MPChartLib/src/main/java/com/github/mikephil/charting/formatter/DefaultAxisValueFormatter.java
index 552c150e..c8834c3e 100644
--- a/MPChartLib/src/main/java/com/github/mikephil/charting/formatter/DefaultAxisValueFormatter.java
+++ b/MPChartLib/src/main/java/com/github/mikephil/charting/formatter/DefaultAxisValueFormatter.java
@@ -1,13 +1,11 @@
package com.github.mikephil.charting.formatter;
-import com.github.mikephil.charting.components.AxisBase;
-
import java.text.DecimalFormat;
/**
* Created by philipp on 02/06/16.
*/
-public class DefaultAxisValueFormatter implements IAxisValueFormatter
+public class DefaultAxisValueFormatter extends ValueFormatter
{
/**
@@ -18,7 +16,7 @@ public class DefaultAxisValueFormatter implements IAxisValueFormatter
/**
* the number of decimal digits this formatter uses
*/
- protected int digits = 0;
+ protected int digits;
/**
* Constructor that specifies to how many digits the value should be
@@ -40,7 +38,7 @@ public class DefaultAxisValueFormatter implements IAxisValueFormatter
}
@Override
- public String getFormattedValue(float value, AxisBase axis) {
+ public String getFormattedValue(float value) {
// avoid memory allocations here (for performance)
return mFormat.format(value);
}
diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/formatter/DefaultValueFormatter.java b/MPChartLib/src/main/java/com/github/mikephil/charting/formatter/DefaultValueFormatter.java
index e2fea4b0..40668b91 100644
--- a/MPChartLib/src/main/java/com/github/mikephil/charting/formatter/DefaultValueFormatter.java
+++ b/MPChartLib/src/main/java/com/github/mikephil/charting/formatter/DefaultValueFormatter.java
@@ -1,9 +1,5 @@
-
package com.github.mikephil.charting.formatter;
-import com.github.mikephil.charting.data.Entry;
-import com.github.mikephil.charting.utils.ViewPortHandler;
-
import java.text.DecimalFormat;
/**
@@ -12,7 +8,7 @@ import java.text.DecimalFormat;
*
* @author Philipp Jahoda
*/
-public class DefaultValueFormatter implements IValueFormatter
+public class DefaultValueFormatter extends ValueFormatter
{
/**
@@ -52,7 +48,7 @@ public class DefaultValueFormatter implements IValueFormatter
}
@Override
- public String getFormattedValue(float value, Entry entry, int dataSetIndex, ViewPortHandler viewPortHandler) {
+ public String getFormattedValue(float value) {
// put more logic here ...
// avoid memory allocations here (for performance reasons)
diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/formatter/IAxisValueFormatter.java b/MPChartLib/src/main/java/com/github/mikephil/charting/formatter/IAxisValueFormatter.java
index 51939b54..970ea6fc 100644
--- a/MPChartLib/src/main/java/com/github/mikephil/charting/formatter/IAxisValueFormatter.java
+++ b/MPChartLib/src/main/java/com/github/mikephil/charting/formatter/IAxisValueFormatter.java
@@ -6,7 +6,10 @@ import com.github.mikephil.charting.components.AxisBase;
* Created by Philipp Jahoda on 20/09/15.
* Custom formatter interface that allows formatting of
* axis labels before they are being drawn.
+ *
+ * @deprecated Extend {@link ValueFormatter} instead
*/
+@Deprecated
public interface IAxisValueFormatter
{
@@ -18,6 +21,9 @@ public interface IAxisValueFormatter
* @param value the value to be formatted
* @param axis the axis the value belongs to
* @return
+ *
+ * @deprecated Extend {@link ValueFormatter} and use {@link ValueFormatter#getAxisLabel(float, AxisBase)}
*/
+ @Deprecated
String getFormattedValue(float value, AxisBase axis);
}
diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/formatter/IValueFormatter.java b/MPChartLib/src/main/java/com/github/mikephil/charting/formatter/IValueFormatter.java
index 75d2363f..0dde7012 100644
--- a/MPChartLib/src/main/java/com/github/mikephil/charting/formatter/IValueFormatter.java
+++ b/MPChartLib/src/main/java/com/github/mikephil/charting/formatter/IValueFormatter.java
@@ -4,13 +4,12 @@ import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.utils.ViewPortHandler;
/**
- * Interface that allows custom formatting of all values inside the chart before they are
- * being drawn to the screen. Simply create your own formatting class and let
- * it implement IValueFormatter. Then override the getFormattedValue(...) method
- * and return whatever you want.
+ * Interface to format all values before they are drawn as labels.
*
* @author Philipp Jahoda
+ * @deprecated Extend {@link ValueFormatter} instead
*/
+@Deprecated
public interface IValueFormatter
{
@@ -24,6 +23,9 @@ public interface IValueFormatter
* @param dataSetIndex the index of the DataSet the entry in focus belongs to
* @param viewPortHandler provides information about the current chart state (scale, translation, ...)
* @return the formatted label ready for being drawn
+ *
+ * @deprecated Extend {@link ValueFormatter} and override an appropriate method
*/
+ @Deprecated
String getFormattedValue(float value, Entry entry, int dataSetIndex, ViewPortHandler viewPortHandler);
}
diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/formatter/IndexAxisValueFormatter.java b/MPChartLib/src/main/java/com/github/mikephil/charting/formatter/IndexAxisValueFormatter.java
index 07349a6a..7ab7bdbe 100644
--- a/MPChartLib/src/main/java/com/github/mikephil/charting/formatter/IndexAxisValueFormatter.java
+++ b/MPChartLib/src/main/java/com/github/mikephil/charting/formatter/IndexAxisValueFormatter.java
@@ -1,18 +1,11 @@
-
package com.github.mikephil.charting.formatter;
-import com.github.mikephil.charting.components.AxisBase;
-import com.github.mikephil.charting.data.Entry;
-import com.github.mikephil.charting.utils.ViewPortHandler;
-
-import java.text.DecimalFormat;
-import java.util.Arrays;
import java.util.Collection;
/**
* This formatter is used for passing an array of x-axis labels, on whole x steps.
*/
-public class IndexAxisValueFormatter implements IAxisValueFormatter
+public class IndexAxisValueFormatter extends ValueFormatter
{
private String[] mValues = new String[] {};
private int mValueCount = 0;
@@ -44,7 +37,8 @@ public class IndexAxisValueFormatter implements IAxisValueFormatter
setValues(values.toArray(new String[values.size()]));
}
- public String getFormattedValue(float value, AxisBase axis) {
+ @Override
+ public String getFormattedValue(float value) {
int index = Math.round(value);
if (index < 0 || index >= mValueCount || index != (int)value)
diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/formatter/LargeValueFormatter.java b/MPChartLib/src/main/java/com/github/mikephil/charting/formatter/LargeValueFormatter.java
index 211401ad..4870a4cf 100644
--- a/MPChartLib/src/main/java/com/github/mikephil/charting/formatter/LargeValueFormatter.java
+++ b/MPChartLib/src/main/java/com/github/mikephil/charting/formatter/LargeValueFormatter.java
@@ -1,10 +1,5 @@
-
package com.github.mikephil.charting.formatter;
-import com.github.mikephil.charting.components.AxisBase;
-import com.github.mikephil.charting.data.Entry;
-import com.github.mikephil.charting.utils.ViewPortHandler;
-
import java.text.DecimalFormat;
/**
@@ -17,7 +12,7 @@ import java.text.DecimalFormat;
* @author Philipp Jahoda
* @author Oleksandr Tyshkovets <olexandr.tyshkovets@gmail.com>
*/
-public class LargeValueFormatter implements IValueFormatter, IAxisValueFormatter
+public class LargeValueFormatter extends ValueFormatter
{
private String[] mSuffix = new String[]{
@@ -41,15 +36,8 @@ public class LargeValueFormatter implements IValueFormatter, IAxisValueFormatter
mText = appendix;
}
- // IValueFormatter
- @Override
- public String getFormattedValue(float value, Entry entry, int dataSetIndex, ViewPortHandler viewPortHandler) {
- return makePretty(value) + mText;
- }
-
- // IAxisValueFormatter
@Override
- public String getFormattedValue(float value, AxisBase axis) {
+ public String getFormattedValue(float value) {
return makePretty(value) + mText;
}
diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/formatter/PercentFormatter.java b/MPChartLib/src/main/java/com/github/mikephil/charting/formatter/PercentFormatter.java
index de8a1025..6bf1bd3c 100644
--- a/MPChartLib/src/main/java/com/github/mikephil/charting/formatter/PercentFormatter.java
+++ b/MPChartLib/src/main/java/com/github/mikephil/charting/formatter/PercentFormatter.java
@@ -1,9 +1,7 @@
-
package com.github.mikephil.charting.formatter;
-import com.github.mikephil.charting.components.AxisBase;
-import com.github.mikephil.charting.data.Entry;
-import com.github.mikephil.charting.utils.ViewPortHandler;
+import com.github.mikephil.charting.charts.PieChart;
+import com.github.mikephil.charting.data.PieEntry;
import java.text.DecimalFormat;
@@ -13,37 +11,36 @@ import java.text.DecimalFormat;
*
* @author Philipp Jahoda
*/
-public class PercentFormatter implements IValueFormatter, IAxisValueFormatter
+public class PercentFormatter extends ValueFormatter
{
- protected DecimalFormat mFormat;
+ public DecimalFormat mFormat;
+ private PieChart pieChart;
public PercentFormatter() {
mFormat = new DecimalFormat("###,###,##0.0");
}
- /**
- * Allow a custom decimalformat
- *
- * @param format
- */
- public PercentFormatter(DecimalFormat format) {
- this.mFormat = format;
+ // Can be used to remove percent signs if the chart isn't in percent mode
+ public PercentFormatter(PieChart pieChart) {
+ this();
+ this.pieChart = pieChart;
}
- // IValueFormatter
@Override
- public String getFormattedValue(float value, Entry entry, int dataSetIndex, ViewPortHandler viewPortHandler) {
+ public String getFormattedValue(float value) {
return mFormat.format(value) + " %";
}
- // IAxisValueFormatter
@Override
- public String getFormattedValue(float value, AxisBase axis) {
- return mFormat.format(value) + " %";
+ public String getPieLabel(float value, PieEntry pieEntry) {
+ if (pieChart != null && pieChart.isUsePercentValuesEnabled()) {
+ // Converted to percent
+ return getFormattedValue(value);
+ } else {
+ // raw value, skip percent sign
+ return mFormat.format(value);
+ }
}
- public int getDecimalDigits() {
- return 1;
- }
}
diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/formatter/StackedValueFormatter.java b/MPChartLib/src/main/java/com/github/mikephil/charting/formatter/StackedValueFormatter.java
index 0e835163..7c69dcf5 100644
--- a/MPChartLib/src/main/java/com/github/mikephil/charting/formatter/StackedValueFormatter.java
+++ b/MPChartLib/src/main/java/com/github/mikephil/charting/formatter/StackedValueFormatter.java
@@ -1,8 +1,6 @@
package com.github.mikephil.charting.formatter;
import com.github.mikephil.charting.data.BarEntry;
-import com.github.mikephil.charting.data.Entry;
-import com.github.mikephil.charting.utils.ViewPortHandler;
import java.text.DecimalFormat;
@@ -12,7 +10,7 @@ import java.text.DecimalFormat;
* A formatter specifically for stacked BarChart that allows to specify whether the all stack values
* or just the top value should be drawn.
*/
-public class StackedValueFormatter implements IValueFormatter
+public class StackedValueFormatter extends ValueFormatter
{
/**
@@ -23,7 +21,7 @@ public class StackedValueFormatter implements IValueFormatter
/**
* a string that should be appended behind the value
*/
- private String mAppendix;
+ private String mSuffix;
private DecimalFormat mFormat;
@@ -31,12 +29,12 @@ public class StackedValueFormatter implements IValueFormatter
* Constructor.
*
* @param drawWholeStack if true, all stack values of the stacked bar entry are drawn, else only top
- * @param appendix a string that should be appended behind the value
+ * @param suffix a string that should be appended behind the value
* @param decimals the number of decimal digits to use
*/
- public StackedValueFormatter(boolean drawWholeStack, String appendix, int decimals) {
+ public StackedValueFormatter(boolean drawWholeStack, String suffix, int decimals) {
this.mDrawWholeStack = drawWholeStack;
- this.mAppendix = appendix;
+ this.mSuffix = suffix;
StringBuffer b = new StringBuffer();
for (int i = 0; i < decimals; i++) {
@@ -49,12 +47,10 @@ public class StackedValueFormatter implements IValueFormatter
}
@Override
- public String getFormattedValue(float value, Entry entry, int dataSetIndex, ViewPortHandler viewPortHandler) {
+ public String getBarStackedLabel(float value, BarEntry entry) {
+ if (!mDrawWholeStack) {
- if (!mDrawWholeStack && entry instanceof BarEntry) {
-
- BarEntry barEntry = (BarEntry) entry;
- float[] vals = barEntry.getYVals();
+ float[] vals = entry.getYVals();
if (vals != null) {
@@ -62,7 +58,7 @@ public class StackedValueFormatter implements IValueFormatter
if (vals[vals.length - 1] == value) {
// return the "sum" across all stack values
- return mFormat.format(barEntry.getY()) + mAppendix;
+ return mFormat.format(entry.getY()) + mSuffix;
} else {
return ""; // return empty
}
@@ -70,6 +66,6 @@ public class StackedValueFormatter implements IValueFormatter
}
// return the "proposed" value
- return mFormat.format(value) + mAppendix;
+ return mFormat.format(value) + mSuffix;
}
}
diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/formatter/ValueFormatter.java b/MPChartLib/src/main/java/com/github/mikephil/charting/formatter/ValueFormatter.java
new file mode 100644
index 00000000..d2f53cb7
--- /dev/null
+++ b/MPChartLib/src/main/java/com/github/mikephil/charting/formatter/ValueFormatter.java
@@ -0,0 +1,137 @@
+package com.github.mikephil.charting.formatter;
+
+import com.github.mikephil.charting.components.AxisBase;
+import com.github.mikephil.charting.data.BarEntry;
+import com.github.mikephil.charting.data.BubbleEntry;
+import com.github.mikephil.charting.data.CandleEntry;
+import com.github.mikephil.charting.data.Entry;
+import com.github.mikephil.charting.data.PieEntry;
+import com.github.mikephil.charting.data.RadarEntry;
+import com.github.mikephil.charting.utils.ViewPortHandler;
+
+/**
+ * Class to format all values before they are drawn as labels.
+ */
+public abstract class ValueFormatter implements IAxisValueFormatter, IValueFormatter{
+
+ /**
+ * <b>DO NOT USE</b>, only for backwards compatibility and will be removed in future versions.
+ *
+ * @param value the value to be formatted
+ * @param axis the axis the value belongs to
+ * @return formatted string label
+ */
+ @Override
+ @Deprecated
+ public String getFormattedValue(float value, AxisBase axis) {
+ return getFormattedValue(value);
+ }
+
+ /**
+ * <b>DO NOT USE</b>, only for backwards compatibility and will be removed in future versions.
+ * @param value the value to be formatted
+ * @param entry the entry the value belongs to - in e.g. BarChart, this is of class BarEntry
+ * @param dataSetIndex the index of the DataSet the entry in focus belongs to
+ * @param viewPortHandler provides information about the current chart state (scale, translation, ...)
+ * @return formatted string label
+ */
+ @Override
+ @Deprecated
+ public String getFormattedValue(float value, Entry entry, int dataSetIndex, ViewPortHandler viewPortHandler) {
+ return getFormattedValue(value);
+ }
+
+ /**
+ * Called when drawing any label, used to change numbers into formatted strings.
+ *
+ * @param value float to be formatted
+ * @return formatted string label
+ */
+ public String getFormattedValue(float value) {
+ return String.valueOf(value);
+ }
+
+ /**
+ * Used to draw axis labels, calls {@link #getFormattedValue(float)} by default.
+ *
+ * @param value float to be formatted
+ * @param axis axis being labeled
+ * @return formatted string label
+ */
+ public String getAxisLabel(float value, AxisBase axis) {
+ return getFormattedValue(value);
+ }
+
+ /**
+ * Used to draw bar labels, calls {@link #getFormattedValue(float)} by default.
+ *
+ * @param barEntry bar being labeled
+ * @return formatted string label
+ */
+ public String getBarLabel(BarEntry barEntry) {
+ return getFormattedValue(barEntry.getY());
+ }
+
+ /**
+ * Used to draw stacked bar labels, calls {@link #getFormattedValue(float)} by default.
+ *
+ * @param value current value to be formatted
+ * @param stackedEntry stacked entry being labeled, contains all Y values
+ * @return formatted string label
+ */
+ public String getBarStackedLabel(float value, BarEntry stackedEntry) {
+ return getFormattedValue(value);
+ }
+
+ /**
+ * Used to draw line and scatter labels, calls {@link #getFormattedValue(float)} by default.
+ *
+ * @param entry point being labeled, contains X value
+ * @return formatted string label
+ */
+ public String getPointLabel(Entry entry) {
+ return getFormattedValue(entry.getY());
+ }
+
+ /**
+ * Used to draw pie value labels, calls {@link #getFormattedValue(float)} by default.
+ *
+ * @param value float to be formatted, may have been converted to percentage
+ * @param pieEntry slice being labeled, contains original, non-percentage Y value
+ * @return formatted string label
+ */
+ public String getPieLabel(float value, PieEntry pieEntry) {
+ return getFormattedValue(value);
+ }
+
+ /**
+ * Used to draw radar value labels, calls {@link #getFormattedValue(float)} by default.
+ *
+ * @param radarEntry entry being labeled
+ * @return formatted string label
+ */
+ public String getRadarLabel(RadarEntry radarEntry) {
+ return getFormattedValue(radarEntry.getY());
+ }
+
+ /**
+ * Used to draw bubble size labels, calls {@link #getFormattedValue(float)} by default.
+ *
+ * @param bubbleEntry bubble being labeled, also contains X and Y values
+ * @return formatted string label
+ */
+ public String getBubbleLabel(BubbleEntry bubbleEntry) {
+ return getFormattedValue(bubbleEntry.getSize());
+ }
+
+ /**
+ * Used to draw high labels, calls {@link #getFormattedValue(float)} by default.
+ *
+ * @param candleEntry candlestick being labeled
+ * @return formatted string label
+ */
+ public String getCandleLabel(CandleEntry candleEntry) {
+ return getFormattedValue(candleEntry.getHigh());
+ }
+
+}
diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/dataprovider/ChartInterface.java b/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/dataprovider/ChartInterface.java
index 219b46bd..182aa287 100644
--- a/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/dataprovider/ChartInterface.java
+++ b/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/dataprovider/ChartInterface.java
@@ -3,7 +3,7 @@ package com.github.mikephil.charting.interfaces.dataprovider;
import android.graphics.RectF;
import com.github.mikephil.charting.data.ChartData;
-import com.github.mikephil.charting.formatter.IValueFormatter;
+import com.github.mikephil.charting.formatter.ValueFormatter;
import com.github.mikephil.charting.utils.MPPointF;
/**
@@ -61,7 +61,7 @@ public interface ChartInterface {
RectF getContentRect();
- IValueFormatter getDefaultValueFormatter();
+ ValueFormatter getDefaultValueFormatter();
ChartData getData();
diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/datasets/IDataSet.java b/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/datasets/IDataSet.java
index f64db706..73a54470 100644
--- a/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/datasets/IDataSet.java
+++ b/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/datasets/IDataSet.java
@@ -1,14 +1,13 @@
package com.github.mikephil.charting.interfaces.datasets;
import android.graphics.DashPathEffect;
-import android.graphics.PointF;
import android.graphics.Typeface;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.DataSet;
import com.github.mikephil.charting.data.Entry;
-import com.github.mikephil.charting.formatter.IValueFormatter;
+import com.github.mikephil.charting.formatter.ValueFormatter;
import com.github.mikephil.charting.utils.MPPointF;
import com.github.mikephil.charting.model.GradientColor;
@@ -341,14 +340,14 @@ public interface IDataSet<T extends Entry> {
*
* @param f
*/
- void setValueFormatter(IValueFormatter f);
+ void setValueFormatter(ValueFormatter f);
/**
* Returns the formatter used for drawing the values inside the chart.
*
* @return
*/
- IValueFormatter getValueFormatter();
+ ValueFormatter getValueFormatter();
/**
* Returns true if the valueFormatter object of this DataSet is null.
diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/BarChartRenderer.java b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/BarChartRenderer.java
index d3f71af0..b5de65b0 100644
--- a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/BarChartRenderer.java
+++ b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/BarChartRenderer.java
@@ -1,4 +1,3 @@
-
package com.github.mikephil.charting.renderer;
import android.graphics.Canvas;
@@ -11,6 +10,7 @@ import com.github.mikephil.charting.animation.ChartAnimator;
import com.github.mikephil.charting.buffer.BarBuffer;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarEntry;
+import com.github.mikephil.charting.formatter.ValueFormatter;
import com.github.mikephil.charting.highlight.Highlight;
import com.github.mikephil.charting.highlight.Range;
import com.github.mikephil.charting.interfaces.dataprovider.BarDataProvider;
@@ -254,6 +254,8 @@ public class BarChartRenderer extends BarLineScatterCandleBubbleRenderer {
final float phaseY = mAnimator.getPhaseY();
+ ValueFormatter formatter = dataSet.getValueFormatter();
+
MPPointF iconsOffset = MPPointF.getInstance(dataSet.getIconsOffset());
iconsOffset.x = Utils.convertDpToPixel(iconsOffset.x);
iconsOffset.y = Utils.convertDpToPixel(iconsOffset.y);
@@ -276,8 +278,7 @@ public class BarChartRenderer extends BarLineScatterCandleBubbleRenderer {
float val = entry.getY();
if (dataSet.isDrawValuesEnabled()) {
- drawValue(c, dataSet.getValueFormatter(), val, entry, i, x,
- val >= 0 ?
+ drawValue(c, formatter.getBarLabel(entry), x, val >= 0 ?
(buffer.buffer[j + 1] + posOffset) :
(buffer.buffer[j + 3] + negOffset),
dataSet.getValueTextColor(j / 4));
@@ -335,8 +336,7 @@ public class BarChartRenderer extends BarLineScatterCandleBubbleRenderer {
continue;
if (dataSet.isDrawValuesEnabled()) {
- drawValue(c, dataSet.getValueFormatter(), entry.getY(), entry, i, x,
- buffer.buffer[bufferIndex + 1] +
+ drawValue(c, formatter.getBarLabel(entry), x, buffer.buffer[bufferIndex + 1] +
(entry.getY() >= 0 ? posOffset : negOffset),
color);
}
@@ -407,14 +407,7 @@ public class BarChartRenderer extends BarLineScatterCandleBubbleRenderer {
continue;
if (dataSet.isDrawValuesEnabled()) {
- drawValue(c,
- dataSet.getValueFormatter(),
- vals[k / 2],
- entry,
- i,
- x,
- y,
- color);
+ drawValue(c, formatter.getBarStackedLabel(val, entry), x, y, color);
}
if (entry.getIcon() != null && dataSet.isDrawIconsEnabled()) {
@@ -443,6 +436,12 @@ public class BarChartRenderer extends BarLineScatterCandleBubbleRenderer {
}
@Override
+ public void drawValue(Canvas c, String valueText, float x, float y, int color) {
+ mValuePaint.setColor(color);
+ c.drawText(valueText, x, y, mValuePaint);
+ }
+
+ @Override
public void drawHighlighted(Canvas c, Highlight[] indices) {
BarData barData = mChart.getBarData();
diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/BubbleChartRenderer.java b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/BubbleChartRenderer.java
index d53dcd47..57b81c1d 100644
--- a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/BubbleChartRenderer.java
+++ b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/BubbleChartRenderer.java
@@ -1,4 +1,3 @@
-
package com.github.mikephil.charting.renderer;
import android.graphics.Canvas;
@@ -9,6 +8,7 @@ import android.graphics.drawable.Drawable;
import com.github.mikephil.charting.animation.ChartAnimator;
import com.github.mikephil.charting.data.BubbleData;
import com.github.mikephil.charting.data.BubbleEntry;
+import com.github.mikephil.charting.formatter.ValueFormatter;
import com.github.mikephil.charting.highlight.Highlight;
import com.github.mikephil.charting.interfaces.dataprovider.BubbleDataProvider;
import com.github.mikephil.charting.interfaces.datasets.IBubbleDataSet;
@@ -150,6 +150,8 @@ public class BubbleChartRenderer extends BarLineScatterCandleBubbleRenderer {
final float alpha = phaseX == 1 ? phaseY : phaseX;
+ ValueFormatter formatter = dataSet.getValueFormatter();
+
MPPointF iconsOffset = MPPointF.getInstance(dataSet.getIconsOffset());
iconsOffset.x = Utils.convertDpToPixel(iconsOffset.x);
iconsOffset.y = Utils.convertDpToPixel(iconsOffset.y);
@@ -172,8 +174,7 @@ public class BubbleChartRenderer extends BarLineScatterCandleBubbleRenderer {
BubbleEntry entry = dataSet.getEntryForIndex(j / 2 + mXBounds.min);
if (dataSet.isDrawValuesEnabled()) {
- drawValue(c, dataSet.getValueFormatter(), entry.getSize(), entry, i, x,
- y + (0.5f * lineHeight), valueTextColor);
+ drawValue(c, formatter.getBubbleLabel(entry), x, y + (0.5f * lineHeight), valueTextColor);
}
if (entry.getIcon() != null && dataSet.isDrawIconsEnabled()) {
@@ -196,6 +197,12 @@ public class BubbleChartRenderer extends BarLineScatterCandleBubbleRenderer {
}
@Override
+ public void drawValue(Canvas c, String valueText, float x, float y, int color) {
+ mValuePaint.setColor(color);
+ c.drawText(valueText, x, y, mValuePaint);
+ }
+
+ @Override
public void drawExtras(Canvas c) {
}
diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/CandleStickChartRenderer.java b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/CandleStickChartRenderer.java
index 991b7021..027dda31 100644
--- a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/CandleStickChartRenderer.java
+++ b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/CandleStickChartRenderer.java
@@ -1,4 +1,3 @@
-
package com.github.mikephil.charting.renderer;
import android.graphics.Canvas;
@@ -8,6 +7,7 @@ import android.graphics.drawable.Drawable;
import com.github.mikephil.charting.animation.ChartAnimator;
import com.github.mikephil.charting.data.CandleData;
import com.github.mikephil.charting.data.CandleEntry;
+import com.github.mikephil.charting.formatter.ValueFormatter;
import com.github.mikephil.charting.highlight.Highlight;
import com.github.mikephil.charting.interfaces.dataprovider.CandleDataProvider;
import com.github.mikephil.charting.interfaces.datasets.ICandleDataSet;
@@ -279,6 +279,8 @@ public class CandleStickChartRenderer extends LineScatterCandleRadarRenderer {
float yOffset = Utils.convertDpToPixel(5f);
+ ValueFormatter formatter = dataSet.getValueFormatter();
+
MPPointF iconsOffset = MPPointF.getInstance(dataSet.getIconsOffset());
iconsOffset.x = Utils.convertDpToPixel(iconsOffset.x);
iconsOffset.y = Utils.convertDpToPixel(iconsOffset.y);
@@ -297,15 +299,7 @@ public class CandleStickChartRenderer extends LineScatterCandleRadarRenderer {
CandleEntry entry = dataSet.getEntryForIndex(j / 2 + mXBounds.min);
if (dataSet.isDrawValuesEnabled()) {
- drawValue(c,
- dataSet.getValueFormatter(),
- entry.getHigh(),
- entry,
- i,
- x,
- y - yOffset,
- dataSet
- .getValueTextColor(j / 2));
+ drawValue(c, formatter.getCandleLabel(entry), x, y - yOffset, dataSet.getValueTextColor(j / 2));
}
if (entry.getIcon() != null && dataSet.isDrawIconsEnabled()) {
@@ -328,6 +322,12 @@ public class CandleStickChartRenderer extends LineScatterCandleRadarRenderer {
}
@Override
+ public void drawValue(Canvas c, String valueText, float x, float y, int color) {
+ mValuePaint.setColor(color);
+ c.drawText(valueText, x, y, mValuePaint);
+ }
+
+ @Override
public void drawExtras(Canvas c) {
}
diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/CombinedChartRenderer.java b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/CombinedChartRenderer.java
index 6d0d4d3d..8f6be3c0 100644
--- a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/CombinedChartRenderer.java
+++ b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/CombinedChartRenderer.java
@@ -1,6 +1,7 @@
package com.github.mikephil.charting.renderer;
import android.graphics.Canvas;
+import android.util.Log;
import com.github.mikephil.charting.animation.ChartAnimator;
import com.github.mikephil.charting.charts.Chart;
@@ -9,7 +10,6 @@ import com.github.mikephil.charting.charts.CombinedChart.DrawOrder;
import com.github.mikephil.charting.data.ChartData;
import com.github.mikephil.charting.data.CombinedData;
import com.github.mikephil.charting.highlight.Highlight;
-import com.github.mikephil.charting.interfaces.dataprovider.BarLineScatterCandleBubbleDataProvider;
import com.github.mikephil.charting.utils.ViewPortHandler;
import java.lang.ref.WeakReference;
@@ -90,6 +90,11 @@ public class CombinedChartRenderer extends DataRenderer {
}
@Override
+ public void drawValue(Canvas c, String valueText, float x, float y, int color) {
+ Log.e("MPAndroidChart", "Erroneous call to drawValue() in CombinedChartRenderer!");
+ }
+
+ @Override
public void drawValues(Canvas c) {
for (DataRenderer renderer : mRenderers)
diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/DataRenderer.java b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/DataRenderer.java
index e8e5446f..da4a26ed 100644
--- a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/DataRenderer.java
+++ b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/DataRenderer.java
@@ -1,4 +1,3 @@
-
package com.github.mikephil.charting.renderer;
import android.graphics.Canvas;
@@ -6,15 +5,11 @@ import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Align;
import android.graphics.Paint.Style;
-import android.graphics.drawable.Drawable;
import com.github.mikephil.charting.animation.ChartAnimator;
-import com.github.mikephil.charting.data.Entry;
-import com.github.mikephil.charting.formatter.IValueFormatter;
import com.github.mikephil.charting.highlight.Highlight;
import com.github.mikephil.charting.interfaces.dataprovider.ChartInterface;
import com.github.mikephil.charting.interfaces.datasets.IDataSet;
-import com.github.mikephil.charting.utils.MPPointF;
import com.github.mikephil.charting.utils.Utils;
import com.github.mikephil.charting.utils.ViewPortHandler;
@@ -138,19 +133,13 @@ public abstract class DataRenderer extends Renderer {
/**
* Draws the value of the given entry by using the provided IValueFormatter.
*
- * @param c canvas
- * @param formatter formatter for custom value-formatting
- * @param value the value to be drawn
- * @param entry the entry the value belongs to
- * @param dataSetIndex the index of the DataSet the drawn Entry belongs to
- * @param x position
- * @param y position
+ * @param c canvas
+ * @param valueText label to draw
+ * @param x position
+ * @param y position
* @param color
*/
- public void drawValue(Canvas c, IValueFormatter formatter, float value, Entry entry, int dataSetIndex, float x, float y, int color) {
- mValuePaint.setColor(color);
- c.drawText(formatter.getFormattedValue(value, entry, dataSetIndex, mViewPortHandler), x, y, mValuePaint);
- }
+ public abstract void drawValue(Canvas c, String valueText, float x, float y, int color);
/**
* Draws any kind of additional information (e.g. line-circles).
diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/HorizontalBarChartRenderer.java b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/HorizontalBarChartRenderer.java
index a1e16508..7607abdd 100644
--- a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/HorizontalBarChartRenderer.java
+++ b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/HorizontalBarChartRenderer.java
@@ -1,4 +1,3 @@
-
package com.github.mikephil.charting.renderer;
import android.graphics.Canvas;
@@ -11,7 +10,7 @@ import com.github.mikephil.charting.buffer.BarBuffer;
import com.github.mikephil.charting.buffer.HorizontalBarBuffer;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarEntry;
-import com.github.mikephil.charting.formatter.IValueFormatter;
+import com.github.mikephil.charting.formatter.ValueFormatter;
import com.github.mikephil.charting.highlight.Highlight;
import com.github.mikephil.charting.interfaces.dataprovider.BarDataProvider;
import com.github.mikephil.charting.interfaces.dataprovider.ChartInterface;
@@ -167,7 +166,7 @@ public class HorizontalBarChartRenderer extends BarChartRenderer {
applyValueTextStyle(dataSet);
final float halfTextHeight = Utils.calcTextHeight(mValuePaint, "10") / 2f;
- IValueFormatter formatter = dataSet.getValueFormatter();
+ ValueFormatter formatter = dataSet.getValueFormatter();
// get the buffer
BarBuffer buffer = mBarBuffers[i];
@@ -196,7 +195,7 @@ public class HorizontalBarChartRenderer extends BarChartRenderer {
BarEntry entry = dataSet.getEntryForIndex(j / 4);
float val = entry.getY();
- String formattedValue = formatter.getFormattedValue(val, entry, i, mViewPortHandler);
+ String formattedValue = formatter.getBarLabel(entry);
// calculate the correct offset depending on the draw position of the value
float valueTextWidth = Utils.calcTextWidth(mValuePaint, formattedValue);
@@ -265,9 +264,7 @@ public class HorizontalBarChartRenderer extends BarChartRenderer {
if (!mViewPortHandler.isInBoundsBottom(buffer.buffer[bufferIndex + 1]))
continue;
- float val = entry.getY();
- String formattedValue = formatter.getFormattedValue(val,
- entry, i, mViewPortHandler);
+ String formattedValue = formatter.getBarLabel(entry);
// calculate the correct offset depending on the draw position of the value
float valueTextWidth = Utils.calcTextWidth(mValuePaint, formattedValue);
@@ -337,8 +334,7 @@ public class HorizontalBarChartRenderer extends BarChartRenderer {
for (int k = 0; k < transformed.length; k += 2) {
final float val = vals[k / 2];
- String formattedValue = formatter.getFormattedValue(val,
- entry, i, mViewPortHandler);
+ String formattedValue = formatter.getBarStackedLabel(val, entry);
// calculate the correct offset depending on the draw position of the value
float valueTextWidth = Utils.calcTextWidth(mValuePaint, formattedValue);
@@ -396,7 +392,8 @@ public class HorizontalBarChartRenderer extends BarChartRenderer {
}
}
- protected void drawValue(Canvas c, String valueText, float x, float y, int color) {
+ @Override
+ public void drawValue(Canvas c, String valueText, float x, float y, int color) {
mValuePaint.setColor(color);
c.drawText(valueText, x, y, mValuePaint);
}
diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/LineChartRenderer.java b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/LineChartRenderer.java
index 7beb6ca5..ead9d6d7 100644
--- a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/LineChartRenderer.java
+++ b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/LineChartRenderer.java
@@ -12,6 +12,7 @@ import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
+import com.github.mikephil.charting.formatter.ValueFormatter;
import com.github.mikephil.charting.highlight.Highlight;
import com.github.mikephil.charting.interfaces.dataprovider.LineDataProvider;
import com.github.mikephil.charting.interfaces.datasets.IDataSet;
@@ -294,7 +295,7 @@ public class LineChartRenderer extends LineRadarRenderer {
int entryCount = dataSet.getEntryCount();
- final boolean isDrawSteppedEnabled = dataSet.isDrawSteppedEnabled();
+ final boolean isDrawSteppedEnabled = dataSet.getMode() == LineDataSet.Mode.STEPPED;
final int pointsPerEntryPair = isDrawSteppedEnabled ? 4 : 2;
Transformer trans = mChart.getTransformer(dataSet.getAxisDependency());
@@ -547,6 +548,7 @@ public class LineChartRenderer extends LineRadarRenderer {
float[] positions = trans.generateTransformedValuesLine(dataSet, mAnimator.getPhaseX(), mAnimator
.getPhaseY(), mXBounds.min, mXBounds.max);
+ ValueFormatter formatter = dataSet.getValueFormatter();
MPPointF iconsOffset = MPPointF.getInstance(dataSet.getIconsOffset());
iconsOffset.x = Utils.convertDpToPixel(iconsOffset.x);
@@ -566,8 +568,7 @@ public class LineChartRenderer extends LineRadarRenderer {
Entry entry = dataSet.getEntryForIndex(j / 2 + mXBounds.min);
if (dataSet.isDrawValuesEnabled()) {
- drawValue(c, dataSet.getValueFormatter(), entry.getY(), entry, i, x,
- y - valOffset, dataSet.getValueTextColor(j / 2));
+ drawValue(c, formatter.getPointLabel(entry), x, y - valOffset, dataSet.getValueTextColor(j / 2));
}
if (entry.getIcon() != null && dataSet.isDrawIconsEnabled()) {
@@ -590,6 +591,12 @@ public class LineChartRenderer extends LineRadarRenderer {
}
@Override
+ public void drawValue(Canvas c, String valueText, float x, float y, int color) {
+ mValuePaint.setColor(color);
+ c.drawText(valueText, x, y, mValuePaint);
+ }
+
+ @Override
public void drawExtras(Canvas c) {
drawCircles(c);
}
diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/PieChartRenderer.java b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/PieChartRenderer.java
index 8c37a0b8..b14657ce 100644
--- a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/PieChartRenderer.java
+++ b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/PieChartRenderer.java
@@ -1,4 +1,3 @@
-
package com.github.mikephil.charting.renderer;
import android.graphics.Bitmap;
@@ -22,7 +21,7 @@ import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.PieData;
import com.github.mikephil.charting.data.PieDataSet;
import com.github.mikephil.charting.data.PieEntry;
-import com.github.mikephil.charting.formatter.IValueFormatter;
+import com.github.mikephil.charting.formatter.ValueFormatter;
import com.github.mikephil.charting.highlight.Highlight;
import com.github.mikephil.charting.interfaces.datasets.IPieDataSet;
import com.github.mikephil.charting.utils.ColorTemplate;
@@ -438,7 +437,7 @@ public class PieChartRenderer extends DataRenderer {
float lineHeight = Utils.calcTextHeight(mValuePaint, "Q")
+ Utils.convertDpToPixel(4f);
- IValueFormatter formatter = dataSet.getValueFormatter();
+ ValueFormatter formatter = dataSet.getValueFormatter();
int entryCount = dataSet.getEntryCount();
@@ -472,6 +471,7 @@ public class PieChartRenderer extends DataRenderer {
float value = mChart.isUsePercentValuesEnabled() ? entry.getY()
/ yValueSum * 100f : entry.getY();
+ String formattedValue = formatter.getPieLabel(value, entry);
final float sliceXBase = (float) Math.cos(transformedAngle * Utils.FDEG2RAD);
final float sliceYBase = (float) Math.sin(transformedAngle * Utils.FDEG2RAD);
@@ -550,14 +550,7 @@ public class PieChartRenderer extends DataRenderer {
// draw everything, depending on settings
if (drawXOutside && drawYOutside) {
- drawValue(c,
- formatter,
- value,
- entry,
- 0,
- labelPtx,
- labelPty,
- dataSet.getValueTextColor(j));
+ drawValue(c, formattedValue, labelPtx, labelPty, dataSet.getValueTextColor(j));
if (j < data.getEntryCount() && entry.getLabel() != null) {
drawEntryLabel(c, entry.getLabel(), labelPtx, labelPty + lineHeight);
@@ -569,8 +562,7 @@ public class PieChartRenderer extends DataRenderer {
}
} else if (drawYOutside) {
- drawValue(c, formatter, value, entry, 0, labelPtx, labelPty + lineHeight / 2.f, dataSet
- .getValueTextColor(j));
+ drawValue(c, formattedValue, labelPtx, labelPty + lineHeight / 2.f, dataSet.getValueTextColor(j));
}
}
@@ -584,7 +576,7 @@ public class PieChartRenderer extends DataRenderer {
// draw everything, depending on settings
if (drawXInside && drawYInside) {
- drawValue(c, formatter, value, entry, 0, x, y, dataSet.getValueTextColor(j));
+ drawValue(c, formattedValue, x, y, dataSet.getValueTextColor(j));
if (j < data.getEntryCount() && entry.getLabel() != null) {
drawEntryLabel(c, entry.getLabel(), x, y + lineHeight);
@@ -595,8 +587,7 @@ public class PieChartRenderer extends DataRenderer {
drawEntryLabel(c, entry.getLabel(), x, y + lineHeight / 2f);
}
} else if (drawYInside) {
-
- drawValue(c, formatter, value, entry, 0, x, y + lineHeight / 2f, dataSet.getValueTextColor(j));
+ drawValue(c, formattedValue, x, y + lineHeight / 2f, dataSet.getValueTextColor(j));
}
}
@@ -626,6 +617,12 @@ public class PieChartRenderer extends DataRenderer {
c.restore();
}
+ @Override
+ public void drawValue(Canvas c, String valueText, float x, float y, int color) {
+ mValuePaint.setColor(color);
+ c.drawText(valueText, x, y, mValuePaint);
+ }
+
/**
* Draws an entry label at the specified position.
*
diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/RadarChartRenderer.java b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/RadarChartRenderer.java
index dbf0e8f8..3f932f87 100644
--- a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/RadarChartRenderer.java
+++ b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/RadarChartRenderer.java
@@ -1,4 +1,3 @@
-
package com.github.mikephil.charting.renderer;
import android.graphics.Canvas;
@@ -11,6 +10,7 @@ import com.github.mikephil.charting.animation.ChartAnimator;
import com.github.mikephil.charting.charts.RadarChart;
import com.github.mikephil.charting.data.RadarData;
import com.github.mikephil.charting.data.RadarEntry;
+import com.github.mikephil.charting.formatter.ValueFormatter;
import com.github.mikephil.charting.highlight.Highlight;
import com.github.mikephil.charting.interfaces.datasets.IRadarDataSet;
import com.github.mikephil.charting.utils.ColorTemplate;
@@ -174,6 +174,8 @@ public class RadarChartRenderer extends LineRadarRenderer {
// apply the text-styling defined by the DataSet
applyValueTextStyle(dataSet);
+ ValueFormatter formatter = dataSet.getValueFormatter();
+
MPPointF iconsOffset = MPPointF.getInstance(dataSet.getIconsOffset());
iconsOffset.x = Utils.convertDpToPixel(iconsOffset.x);
iconsOffset.y = Utils.convertDpToPixel(iconsOffset.y);
@@ -189,15 +191,7 @@ public class RadarChartRenderer extends LineRadarRenderer {
pOut);
if (dataSet.isDrawValuesEnabled()) {
- drawValue(c,
- dataSet.getValueFormatter(),
- entry.getY(),
- entry,
- i,
- pOut.x,
- pOut.y - yoffset,
- dataSet.getValueTextColor
- (j));
+ drawValue(c, formatter.getRadarLabel(entry), pOut.x, pOut.y - yoffset, dataSet.getValueTextColor(j));
}
if (entry.getIcon() != null && dataSet.isDrawIconsEnabled()) {
@@ -232,6 +226,12 @@ public class RadarChartRenderer extends LineRadarRenderer {
}
@Override
+ public void drawValue(Canvas c, String valueText, float x, float y, int color) {
+ mValuePaint.setColor(color);
+ c.drawText(valueText, x, y, mValuePaint);
+ }
+
+ @Override
public void drawExtras(Canvas c) {
drawWeb(c);
}
diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/ScatterChartRenderer.java b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/ScatterChartRenderer.java
index ccd077e5..98dddb93 100644
--- a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/ScatterChartRenderer.java
+++ b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/ScatterChartRenderer.java
@@ -1,4 +1,3 @@
-
package com.github.mikephil.charting.renderer;
import android.graphics.Canvas;
@@ -8,6 +7,7 @@ import android.util.Log;
import com.github.mikephil.charting.animation.ChartAnimator;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.ScatterData;
+import com.github.mikephil.charting.formatter.ValueFormatter;
import com.github.mikephil.charting.highlight.Highlight;
import com.github.mikephil.charting.interfaces.dataprovider.ScatterDataProvider;
import com.github.mikephil.charting.interfaces.datasets.IScatterDataSet;
@@ -118,6 +118,8 @@ public class ScatterChartRenderer extends LineScatterCandleRadarRenderer {
float shapeSize = Utils.convertDpToPixel(dataSet.getScatterShapeSize());
+ ValueFormatter formatter = dataSet.getValueFormatter();
+
MPPointF iconsOffset = MPPointF.getInstance(dataSet.getIconsOffset());
iconsOffset.x = Utils.convertDpToPixel(iconsOffset.x);
iconsOffset.y = Utils.convertDpToPixel(iconsOffset.y);
@@ -135,14 +137,7 @@ public class ScatterChartRenderer extends LineScatterCandleRadarRenderer {
Entry entry = dataSet.getEntryForIndex(j / 2 + mXBounds.min);
if (dataSet.isDrawValuesEnabled()) {
- drawValue(c,
- dataSet.getValueFormatter(),
- entry.getY(),
- entry,
- i,
- positions[j],
- positions[j + 1] - shapeSize,
- dataSet.getValueTextColor(j / 2 + mXBounds.min));
+ drawValue(c, formatter.getPointLabel(entry), positions[j], positions[j + 1] - shapeSize, dataSet.getValueTextColor(j / 2 + mXBounds.min));
}
if (entry.getIcon() != null && dataSet.isDrawIconsEnabled()) {
@@ -165,6 +160,12 @@ public class ScatterChartRenderer extends LineScatterCandleRadarRenderer {
}
@Override
+ public void drawValue(Canvas c, String valueText, float x, float y, int color) {
+ mValuePaint.setColor(color);
+ c.drawText(valueText, x, y, mValuePaint);
+ }
+
+ @Override
public void drawExtras(Canvas c) {
}
diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/XAxisRenderer.java b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/XAxisRenderer.java
index 8adb56c7..046f3469 100644
--- a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/XAxisRenderer.java
+++ b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/XAxisRenderer.java
@@ -1,4 +1,3 @@
-
package com.github.mikephil.charting.renderer;
import android.graphics.Canvas;
@@ -202,7 +201,7 @@ public class XAxisRenderer extends AxisRenderer {
if (mViewPortHandler.isInBoundsX(x)) {
- String label = mXAxis.getValueFormatter().getFormattedValue(mXAxis.mEntries[i / 2], mXAxis);
+ String label = mXAxis.getValueFormatter().getAxisLabel(mXAxis.mEntries[i / 2], mXAxis);
if (mXAxis.isAvoidFirstLastClippingEnabled()) {
diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/XAxisRendererHorizontalBarChart.java b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/XAxisRendererHorizontalBarChart.java
index 86047cf1..9054dcb6 100644
--- a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/XAxisRendererHorizontalBarChart.java
+++ b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/XAxisRendererHorizontalBarChart.java
@@ -1,4 +1,3 @@
-
package com.github.mikephil.charting.renderer;
import android.graphics.Canvas;
@@ -57,10 +56,10 @@ public class XAxisRendererHorizontalBarChart extends XAxisRenderer {
computeAxisValues(min, max);
}
-
+
@Override
protected void computeSize() {
-
+
mAxisLabelPaint.setTypeface(mXAxis.getTypeface());
mAxisLabelPaint.setTextSize(mXAxis.getTextSize());
@@ -156,7 +155,7 @@ public class XAxisRendererHorizontalBarChart extends XAxisRenderer {
if (mViewPortHandler.isInBoundsY(y)) {
- String label = mXAxis.getValueFormatter().getFormattedValue(mXAxis.mEntries[i / 2], mXAxis);
+ String label = mXAxis.getValueFormatter().getAxisLabel(mXAxis.mEntries[i / 2], mXAxis);
drawLabel(c, label, pos, y, anchor, labelRotationAngleDegrees);
}
}
diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/XAxisRendererRadarChart.java b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/XAxisRendererRadarChart.java
index 956e8c7d..6d83cf59 100644
--- a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/XAxisRendererRadarChart.java
+++ b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/XAxisRendererRadarChart.java
@@ -1,8 +1,6 @@
-
package com.github.mikephil.charting.renderer;
import android.graphics.Canvas;
-import android.graphics.PointF;
import com.github.mikephil.charting.charts.RadarChart;
import com.github.mikephil.charting.components.XAxis;
@@ -43,7 +41,7 @@ public class XAxisRendererRadarChart extends XAxisRenderer {
MPPointF pOut = MPPointF.getInstance(0,0);
for (int i = 0; i < mChart.getData().getMaxEntryCountSet().getEntryCount(); i++) {
- String label = mXAxis.getValueFormatter().getFormattedValue(i, mXAxis);
+ String label = mXAxis.getValueFormatter().getAxisLabel(i, mXAxis);
float angle = (sliceangle * i + mChart.getRotationAngle()) % 360f;
diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/utils/Utils.java b/MPChartLib/src/main/java/com/github/mikephil/charting/utils/Utils.java
index c3026739..60ff6ba3 100644
--- a/MPChartLib/src/main/java/com/github/mikephil/charting/utils/Utils.java
+++ b/MPChartLib/src/main/java/com/github/mikephil/charting/utils/Utils.java
@@ -1,4 +1,3 @@
-
package com.github.mikephil.charting.utils;
import android.annotation.SuppressLint;
@@ -7,7 +6,6 @@ import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
-import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.text.Layout;
@@ -15,14 +13,13 @@ import android.text.StaticLayout;
import android.text.TextPaint;
import android.util.DisplayMetrics;
import android.util.Log;
-import android.util.SizeF;
import android.view.MotionEvent;
import android.view.VelocityTracker;
import android.view.View;
import android.view.ViewConfiguration;
import com.github.mikephil.charting.formatter.DefaultValueFormatter;
-import com.github.mikephil.charting.formatter.IValueFormatter;
+import com.github.mikephil.charting.formatter.ValueFormatter;
import java.util.List;
@@ -229,15 +226,14 @@ public abstract class Utils {
1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000
};
- private static IValueFormatter mDefaultValueFormatter = generateDefaultValueFormatter();
+ private static ValueFormatter mDefaultValueFormatter = generateDefaultValueFormatter();
- private static IValueFormatter generateDefaultValueFormatter() {
- final DefaultValueFormatter formatter = new DefaultValueFormatter(1);
- return formatter;
+ private static ValueFormatter generateDefaultValueFormatter() {
+ return new DefaultValueFormatter(1);
}
/// - returns: The default value formatter used for all chart components that needs a default
- public static IValueFormatter getDefaultValueFormatter()
+ public static ValueFormatter getDefaultValueFormatter()
{
return mDefaultValueFormatter;
}
@@ -353,11 +349,11 @@ public abstract class Utils {
* @return
*/
public static float roundToNextSignificant(double number) {
- if (Double.isInfinite(number) ||
- Double.isNaN(number) ||
+ if (Double.isInfinite(number) ||
+ Double.isNaN(number) ||
number == 0.0)
return 0;
-
+
final float d = (float) Math.ceil((float) Math.log10(number < 0 ? -number : number));
final int pw = 1 - (int) d;
final float magnitude = (float) Math.pow(10, pw);
@@ -375,10 +371,10 @@ public abstract class Utils {
public static int getDecimals(float number) {
float i = roundToNextSignificant(number);
-
+
if (Float.isInfinite(i))
return 0;
-
+
return (int) Math.ceil(-Math.log10(i)) + 2;
}
diff --git a/MPChartLib/src/test/java/com/github/mikephil/charting/test/LargeValueFormatterTest.java b/MPChartLib/src/test/java/com/github/mikephil/charting/test/LargeValueFormatterTest.java
index f1e1e027..fc7eb93e 100644
--- a/MPChartLib/src/test/java/com/github/mikephil/charting/test/LargeValueFormatterTest.java
+++ b/MPChartLib/src/test/java/com/github/mikephil/charting/test/LargeValueFormatterTest.java
@@ -16,80 +16,80 @@ public class LargeValueFormatterTest {
LargeValueFormatter formatter = new LargeValueFormatter();
- String result = formatter.getFormattedValue(5f, null);
+ String result = formatter.getFormattedValue(5f);
assertEquals("5", result);
- result = formatter.getFormattedValue(5.5f, null);
+ result = formatter.getFormattedValue(5.5f);
assertEquals("5.5", result);
- result = formatter.getFormattedValue(50f, null);
+ result = formatter.getFormattedValue(50f);
assertEquals("50", result);
- result = formatter.getFormattedValue(50.5f, null);
+ result = formatter.getFormattedValue(50.5f);
assertEquals("50.5", result);
- result = formatter.getFormattedValue(500f, null);
+ result = formatter.getFormattedValue(500f);
assertEquals("500", result);
- result = formatter.getFormattedValue(1100f, null);
+ result = formatter.getFormattedValue(1100f);
assertEquals("1.1k", result);
- result = formatter.getFormattedValue(10000f, null);
+ result = formatter.getFormattedValue(10000f);
assertEquals("10k", result);
- result = formatter.getFormattedValue(10500f, null);
+ result = formatter.getFormattedValue(10500f);
assertEquals("10.5k", result);
- result = formatter.getFormattedValue(100000f, null);
+ result = formatter.getFormattedValue(100000f);
assertEquals("100k", result);
- result = formatter.getFormattedValue(1000000f, null);
+ result = formatter.getFormattedValue(1000000f);
assertEquals("1m", result);
- result = formatter.getFormattedValue(1500000f, null);
+ result = formatter.getFormattedValue(1500000f);
assertEquals("1.5m", result);
- result = formatter.getFormattedValue(9500000f, null);
+ result = formatter.getFormattedValue(9500000f);
assertEquals("9.5m", result);
- result = formatter.getFormattedValue(22200000f, null);
+ result = formatter.getFormattedValue(22200000f);
assertEquals("22.2m", result);
- result = formatter.getFormattedValue(222000000f, null);
+ result = formatter.getFormattedValue(222000000f);
assertEquals("222m", result);
- result = formatter.getFormattedValue(1000000000f, null);
+ result = formatter.getFormattedValue(1000000000f);
assertEquals("1b", result);
- result = formatter.getFormattedValue(9900000000f, null);
+ result = formatter.getFormattedValue(9900000000f);
assertEquals("9.9b", result);
- result = formatter.getFormattedValue(99000000000f, null);
+ result = formatter.getFormattedValue(99000000000f);
assertEquals("99b", result);
- result = formatter.getFormattedValue(99500000000f, null);
+ result = formatter.getFormattedValue(99500000000f);
assertEquals("99.5b", result);
- result = formatter.getFormattedValue(999000000000f, null);
+ result = formatter.getFormattedValue(999000000000f);
assertEquals("999b", result);
- result = formatter.getFormattedValue(1000000000000f, null);
+ result = formatter.getFormattedValue(1000000000000f);
assertEquals("1t", result);
formatter.setSuffix(new String[]{"", "k", "m", "b", "t", "q"}); // quadrillion support
- result = formatter.getFormattedValue(1000000000000000f, null);
+ result = formatter.getFormattedValue(1000000000000000f);
assertEquals("1q", result);
- result = formatter.getFormattedValue(1100000000000000f, null);
+ result = formatter.getFormattedValue(1100000000000000f);
assertEquals("1.1q", result);
- result = formatter.getFormattedValue(10000000000000000f, null);
+ result = formatter.getFormattedValue(10000000000000000f);
assertEquals("10q", result);
- result = formatter.getFormattedValue(13300000000000000f, null);
+ result = formatter.getFormattedValue(13300000000000000f);
assertEquals("13.3q", result);
- result = formatter.getFormattedValue(100000000000000000f, null);
+ result = formatter.getFormattedValue(100000000000000000f);
assertEquals("100q", result);
}
}
diff --git a/MPChartLib/src/test/java/com/github/mikephil/charting/test/ObjectPoolTest.java b/MPChartLib/src/test/java/com/github/mikephil/charting/test/ObjectPoolTest.java
index e1dbe81b..44946cf4 100644
--- a/MPChartLib/src/test/java/com/github/mikephil/charting/test/ObjectPoolTest.java
+++ b/MPChartLib/src/test/java/com/github/mikephil/charting/test/ObjectPoolTest.java
@@ -2,7 +2,7 @@ package com.github.mikephil.charting.test;
import com.github.mikephil.charting.utils.ObjectPool;
-import junit.framework.Assert;
+import org.junit.Assert;
import org.junit.Test;