Added IndexAxisValueFormatter, to allow for easy x-axis labels like MPAndroidChart 2.0

This commit is contained in:
Daniel Cohen Gindi 2016-11-16 11:13:29 +02:00
parent 0818d766db
commit 4cb83a7b91
17 changed files with 77 additions and 75 deletions

View file

@ -96,11 +96,6 @@ public class BarChartActivityMultiDataset extends DemoBase implements OnSeekBarC
public String getFormattedValue(float value, AxisBase axis) {
return String.valueOf((int) value);
}
@Override
public int getDecimalDigits() {
return 0;
}
});
YAxis leftAxis = mChart.getAxisLeft();

View file

@ -90,11 +90,6 @@ public class BarChartPositiveNegative extends DemoBase {
public String getFormattedValue(float value, AxisBase axis) {
return data.get(Math.min(Math.max((int) value, 0), data.size()-1)).xAxisValue;
}
@Override
public int getDecimalDigits() {
return 0;
}
});
setData(data);

View file

@ -84,11 +84,6 @@ public class CombinedChartActivity extends DemoBase {
public String getFormattedValue(float value, AxisBase axis) {
return mMonths[(int) value % mMonths.length];
}
@Override
public int getDecimalDigits() {
return 0;
}
});
CombinedData data = new CombinedData();

View file

@ -100,11 +100,6 @@ public class LineChartTime extends DemoBase implements OnSeekBarChangeListener {
long millis = TimeUnit.HOURS.toMillis((long) value);
return mFormat.format(new Date(millis));
}
@Override
public int getDecimalDigits() {
return 0;
}
});
YAxis leftAxis = mChart.getAxisLeft();

View file

@ -81,11 +81,6 @@ public class RadarChartActivitry extends DemoBase {
public String getFormattedValue(float value, AxisBase axis) {
return mActivities[(int) value % mActivities.length];
}
@Override
public int getDecimalDigits() {
return 0;
}
});
xAxis.setTextColor(Color.WHITE);

View file

@ -85,11 +85,6 @@ public class StackedBarActivityNegative extends DemoBase implements
public String getFormattedValue(float value, AxisBase axis) {
return format.format(value) + "-" + format.format(value + 10);
}
@Override
public int getDecimalDigits() {
return 0;
}
});
Legend l = mChart.getLegend();
@ -243,10 +238,5 @@ public class StackedBarActivityNegative extends DemoBase implements
public String getFormattedValue(float value, AxisBase axis) {
return mFormat.format(Math.abs(value)) + "m";
}
@Override
public int getDecimalDigits() {
return 0;
}
}
}

View file

@ -136,9 +136,4 @@ public class DayAxisValueFormatter implements IAxisValueFormatter
return 2020;
}
@Override
public int getDecimalDigits() {
return 0;
}
}

View file

@ -18,9 +18,4 @@ public class MyAxisValueFormatter implements IAxisValueFormatter
public String getFormattedValue(float value, AxisBase axis) {
return mFormat.format(value) + " $";
}
@Override
public int getDecimalDigits() {
return 1;
}
}

View file

@ -37,9 +37,4 @@ public class MyCustomXAxisValueFormatter implements IAxisValueFormatter
else
return mFormat.format(value);
}
@Override
public int getDecimalDigits() {
return 1;
}
}

View file

@ -24,9 +24,4 @@ public class YearXAxisFormatter implements IAxisValueFormatter
float percent = value / axis.mAxisRange;
return mMonths[(int) (mMonths.length * percent)];
}
@Override
public int getDecimalDigits() {
return 0;
}
}

View file

@ -90,11 +90,6 @@ public class RealmWikiExample extends RealmBaseActivity {
public String getFormattedValue(float value, AxisBase axis) {
return results.get((int) value).getPlayerName();
}
@Override
public int getDecimalDigits() {
return 0;
}
};
lineChart.getXAxis().setValueFormatter(formatter);

View file

@ -496,12 +496,10 @@ public abstract class AxisBase extends ComponentBase {
*/
public IAxisValueFormatter getValueFormatter() {
if (mAxisValueFormatter == null) {
if (mAxisValueFormatter == null ||
(mAxisValueFormatter instanceof DefaultAxisValueFormatter &&
((DefaultAxisValueFormatter)mAxisValueFormatter).getDecimalDigits() != mDecimals))
mAxisValueFormatter = new DefaultAxisValueFormatter(mDecimals);
} else if (mAxisValueFormatter.getDecimalDigits() != mDecimals && mAxisValueFormatter instanceof
DefaultAxisValueFormatter) {
mAxisValueFormatter = new DefaultAxisValueFormatter(mDecimals);
}
return mAxisValueFormatter;
}

View file

@ -45,7 +45,11 @@ public class DefaultAxisValueFormatter implements IAxisValueFormatter
return mFormat.format(value);
}
@Override
/**
* Returns the number of decimal digits this formatter uses or -1, if unspecified.
*
* @return
*/
public int getDecimalDigits() {
return digits;
}

View file

@ -20,11 +20,4 @@ public interface IAxisValueFormatter
* @return
*/
String getFormattedValue(float value, AxisBase axis);
/**
* Returns the number of decimal digits this formatter uses or -1, if unspecified.
*
* @return
*/
int getDecimalDigits();
}

View file

@ -0,0 +1,69 @@
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
{
private String[] mValues = new String[] {};
private int mValueCount = 0;
/**
* An empty constructor.
* Use `setValues` to set the axis labels.
*/
public IndexAxisValueFormatter() {
}
/**
* Constructor that specifies axis labels.
*
* @param values The values string array
*/
public IndexAxisValueFormatter(String[] values) {
if (values != null)
setValues(values);
}
/**
* Constructor that specifies axis labels.
*
* @param values The values string array
*/
public IndexAxisValueFormatter(Collection<String> values) {
if (values != null)
setValues(values.toArray(new String[values.size()]));
}
public String getFormattedValue(float value, AxisBase axis) {
int index = Math.round(value);
if (index < 0 || index >= mValueCount || index != (int)value)
return "";
return mValues[index];
}
public String[] getValues()
{
return mValues;
}
public void setValues(String[] values)
{
if (values == null)
values = new String[] {};
this.mValues = values;
this.mValueCount = values.length;
}
}

View file

@ -93,7 +93,6 @@ public class LargeValueFormatter implements IValueFormatter, IAxisValueFormatter
return r;
}
@Override
public int getDecimalDigits() {
return 0;
}

View file

@ -43,7 +43,6 @@ public class PercentFormatter implements IValueFormatter, IAxisValueFormatter
return mFormat.format(value) + " %";
}
@Override
public int getDecimalDigits() {
return 1;
}