Improve & test LargeValueFormatter
This commit is contained in:
parent
92504fa81d
commit
f69ae3f76b
6 changed files with 105 additions and 15 deletions
|
@ -247,6 +247,7 @@ public class BarChartActivityMultiDataset extends DemoBase implements OnSeekBarC
|
|||
mChart.getBarData().groupBars(startYear, groupSpace, barSpace);
|
||||
mChart.getXAxis().setAxisMinValue(startYear);
|
||||
mChart.getXAxis().setAxisMaxValue(mChart.getBarData().getIntervalWidth(groupSpace, barSpace) * mSeekBarX.getProgress() + startYear);
|
||||
mChart.notifyDataSetChanged();
|
||||
mChart.invalidate();
|
||||
}
|
||||
|
||||
|
|
|
@ -22,12 +22,12 @@ public class LargeValueFormatter implements ValueFormatter, AxisValueFormatter {
|
|||
private static String[] SUFFIX = new String[]{
|
||||
"", "k", "m", "b", "t"
|
||||
};
|
||||
private static final int MAX_LENGTH = 4;
|
||||
private static final int MAX_LENGTH = 5;
|
||||
private DecimalFormat mFormat;
|
||||
private String mText = "";
|
||||
|
||||
public LargeValueFormatter() {
|
||||
mFormat = new DecimalFormat("###E0");
|
||||
mFormat = new DecimalFormat("###E00");
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -68,9 +68,7 @@ public class LargeValueFormatter implements ValueFormatter, AxisValueFormatter {
|
|||
* @param suff new suffix
|
||||
*/
|
||||
public void setSuffix(String[] suff) {
|
||||
if (suff.length == 5) {
|
||||
SUFFIX = suff;
|
||||
}
|
||||
SUFFIX = suff;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -81,7 +79,11 @@ public class LargeValueFormatter implements ValueFormatter, AxisValueFormatter {
|
|||
|
||||
String r = mFormat.format(number);
|
||||
|
||||
r = r.replaceAll("E[0-9]", SUFFIX[Character.getNumericValue(r.charAt(r.length() - 1)) / 3]);
|
||||
int numericValue1 = Character.getNumericValue(r.charAt(r.length() - 1));
|
||||
int numericValue2 = Character.getNumericValue(r.charAt(r.length() - 2));
|
||||
int combined = Integer.valueOf(numericValue2 + "" + numericValue1);
|
||||
|
||||
r = r.replaceAll("E[0-9][0-9]", SUFFIX[combined / 3]);
|
||||
|
||||
while (r.length() > MAX_LENGTH || r.matches("[0-9]+\\.[a-z]")) {
|
||||
r = r.substring(0, r.length() - 2) + r.substring(r.length() - 1);
|
||||
|
|
|
@ -102,7 +102,6 @@ public class ViewPortHandler {
|
|||
mChartWidth = width;
|
||||
|
||||
restrainViewPort(offsetLeft, offsetTop, offsetRight, offsetBottom);
|
||||
|
||||
}
|
||||
|
||||
public boolean hasChartDimens() {
|
||||
|
|
|
@ -1,17 +1,11 @@
|
|||
package com.github.mikephil.charting.test;
|
||||
|
||||
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.data.ScatterDataSet;
|
||||
import com.github.mikephil.charting.renderer.AxisRenderer;
|
||||
import com.github.mikephil.charting.renderer.YAxisRenderer;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static junit.framework.Assert.assertEquals;
|
||||
|
||||
/**
|
||||
|
|
|
@ -3,14 +3,13 @@ package com.github.mikephil.charting.test;
|
|||
import com.github.mikephil.charting.data.DataSet;
|
||||
import com.github.mikephil.charting.data.Entry;
|
||||
import com.github.mikephil.charting.data.ScatterDataSet;
|
||||
import com.github.mikephil.charting.interfaces.datasets.IDataSet;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static junit.framework.Assert.*;
|
||||
import static junit.framework.Assert.assertEquals;
|
||||
|
||||
/**
|
||||
* Created by philipp on 31/05/16.
|
||||
|
|
|
@ -0,0 +1,95 @@
|
|||
package com.github.mikephil.charting.test;
|
||||
|
||||
import com.github.mikephil.charting.formatter.LargeValueFormatter;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static junit.framework.Assert.assertEquals;
|
||||
|
||||
/**
|
||||
* Created by philipp on 06/06/16.
|
||||
*/
|
||||
public class LargeValueFormatterTest {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
LargeValueFormatter formatter = new LargeValueFormatter();
|
||||
|
||||
String result = formatter.getFormattedValue(5f, null);
|
||||
assertEquals("5", result);
|
||||
|
||||
result = formatter.getFormattedValue(5.5f, null);
|
||||
assertEquals("5.5", result);
|
||||
|
||||
result = formatter.getFormattedValue(50f, null);
|
||||
assertEquals("50", result);
|
||||
|
||||
result = formatter.getFormattedValue(50.5f, null);
|
||||
assertEquals("50.5", result);
|
||||
|
||||
result = formatter.getFormattedValue(500f, null);
|
||||
assertEquals("500", result);
|
||||
|
||||
result = formatter.getFormattedValue(1100f, null);
|
||||
assertEquals("1.1k", result);
|
||||
|
||||
result = formatter.getFormattedValue(10000f, null);
|
||||
assertEquals("10k", result);
|
||||
|
||||
result = formatter.getFormattedValue(10500f, null);
|
||||
assertEquals("10.5k", result);
|
||||
|
||||
result = formatter.getFormattedValue(100000f, null);
|
||||
assertEquals("100k", result);
|
||||
|
||||
result = formatter.getFormattedValue(1000000f, null);
|
||||
assertEquals("1m", result);
|
||||
|
||||
result = formatter.getFormattedValue(1500000f, null);
|
||||
assertEquals("1.5m", result);
|
||||
|
||||
result = formatter.getFormattedValue(9500000f, null);
|
||||
assertEquals("9.5m", result);
|
||||
|
||||
result = formatter.getFormattedValue(22200000f, null);
|
||||
assertEquals("22.2m", result);
|
||||
|
||||
result = formatter.getFormattedValue(222000000f, null);
|
||||
assertEquals("222m", result);
|
||||
|
||||
result = formatter.getFormattedValue(1000000000f, null);
|
||||
assertEquals("1b", result);
|
||||
|
||||
result = formatter.getFormattedValue(9900000000f, null);
|
||||
assertEquals("9.9b", result);
|
||||
|
||||
result = formatter.getFormattedValue(99000000000f, null);
|
||||
assertEquals("99b", result);
|
||||
|
||||
result = formatter.getFormattedValue(99500000000f, null);
|
||||
assertEquals("99.5b", result);
|
||||
|
||||
result = formatter.getFormattedValue(999000000000f, null);
|
||||
assertEquals("999b", result);
|
||||
|
||||
result = formatter.getFormattedValue(1000000000000f, null);
|
||||
assertEquals("1t", result);
|
||||
|
||||
formatter.setSuffix(new String[]{"", "k", "m", "b", "t", "q"}); // quadrillion support
|
||||
result = formatter.getFormattedValue(1000000000000000f, null);
|
||||
assertEquals("1q", result);
|
||||
|
||||
result = formatter.getFormattedValue(1100000000000000f, null);
|
||||
assertEquals("1.1q", result);
|
||||
|
||||
result = formatter.getFormattedValue(10000000000000000f, null);
|
||||
assertEquals("10q", result);
|
||||
|
||||
result = formatter.getFormattedValue(13300000000000000f, null);
|
||||
assertEquals("13.3q", result);
|
||||
|
||||
result = formatter.getFormattedValue(100000000000000000f, null);
|
||||
assertEquals("100q", result);
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue