Skip to content

Commit e5b6619

Browse files
committed
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.
1 parent 608d9e2 commit e5b6619

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+408
-338
lines changed

.gitignore

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
bin/
1313
gen/
1414
generated/
15-
docs/
1615
finalOutput/
1716

1817
build.xml
@@ -23,6 +22,8 @@ local.properties
2322
# Eclipse project files
2423
.classpath
2524
.project
25+
.settings/
26+
.vscode/
2627

2728
# Proguard folder generated by Eclipse
2829
proguard/
@@ -31,7 +32,8 @@ proguard/
3132
*.iml
3233
*.ipr
3334
*.iws
34-
.idea/
35+
/.idea/*
36+
!/.idea/runConfigurations
3537

3638
.directory
3739

MPChartExample/src/main/java/com/xxmassdeveloper/mpchartexample/BarChartActivity.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
package com.xxmassdeveloper.mpchartexample;
32

43
import android.Manifest;
@@ -28,15 +27,15 @@
2827
import com.github.mikephil.charting.data.BarDataSet;
2928
import com.github.mikephil.charting.data.BarEntry;
3029
import com.github.mikephil.charting.data.Entry;
31-
import com.github.mikephil.charting.formatter.IAxisValueFormatter;
30+
import com.github.mikephil.charting.formatter.ValueFormatter;
3231
import com.github.mikephil.charting.highlight.Highlight;
3332
import com.github.mikephil.charting.interfaces.datasets.IBarDataSet;
3433
import com.github.mikephil.charting.interfaces.datasets.IDataSet;
3534
import com.github.mikephil.charting.listener.OnChartValueSelectedListener;
3635
import com.github.mikephil.charting.model.GradientColor;
3736
import com.github.mikephil.charting.utils.MPPointF;
3837
import com.xxmassdeveloper.mpchartexample.custom.DayAxisValueFormatter;
39-
import com.xxmassdeveloper.mpchartexample.custom.MyAxisValueFormatter;
38+
import com.xxmassdeveloper.mpchartexample.custom.MyValueFormatter;
4039
import com.xxmassdeveloper.mpchartexample.custom.XYMarkerView;
4140
import com.xxmassdeveloper.mpchartexample.notimportant.DemoBase;
4241

@@ -86,7 +85,7 @@ protected void onCreate(Bundle savedInstanceState) {
8685
chart.setDrawGridBackground(false);
8786
// chart.setDrawYLabels(false);
8887

89-
IAxisValueFormatter xAxisFormatter = new DayAxisValueFormatter(chart);
88+
ValueFormatter xAxisFormatter = new DayAxisValueFormatter(chart);
9089

9190
XAxis xAxis = chart.getXAxis();
9291
xAxis.setPosition(XAxisPosition.BOTTOM);
@@ -96,7 +95,7 @@ protected void onCreate(Bundle savedInstanceState) {
9695
xAxis.setLabelCount(7);
9796
xAxis.setValueFormatter(xAxisFormatter);
9897

99-
IAxisValueFormatter custom = new MyAxisValueFormatter();
98+
ValueFormatter custom = new MyValueFormatter("$");
10099

101100
YAxis leftAxis = chart.getAxisLeft();
102101
leftAxis.setTypeface(tfLight);

MPChartExample/src/main/java/com/xxmassdeveloper/mpchartexample/BarChartActivityMultiDataset.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
package com.xxmassdeveloper.mpchartexample;
32

43
import android.Manifest;
@@ -17,16 +16,15 @@
1716
import android.widget.TextView;
1817

1918
import com.github.mikephil.charting.charts.BarChart;
20-
import com.github.mikephil.charting.components.AxisBase;
2119
import com.github.mikephil.charting.components.Legend;
2220
import com.github.mikephil.charting.components.XAxis;
2321
import com.github.mikephil.charting.components.YAxis;
2422
import com.github.mikephil.charting.data.BarData;
2523
import com.github.mikephil.charting.data.BarDataSet;
2624
import com.github.mikephil.charting.data.BarEntry;
2725
import com.github.mikephil.charting.data.Entry;
28-
import com.github.mikephil.charting.formatter.IAxisValueFormatter;
2926
import com.github.mikephil.charting.formatter.LargeValueFormatter;
27+
import com.github.mikephil.charting.formatter.ValueFormatter;
3028
import com.github.mikephil.charting.highlight.Highlight;
3129
import com.github.mikephil.charting.interfaces.datasets.IBarDataSet;
3230
import com.github.mikephil.charting.listener.OnChartValueSelectedListener;
@@ -100,9 +98,9 @@ protected void onCreate(Bundle savedInstanceState) {
10098
xAxis.setTypeface(tfLight);
10199
xAxis.setGranularity(1f);
102100
xAxis.setCenterAxisLabels(true);
103-
xAxis.setValueFormatter(new IAxisValueFormatter() {
101+
xAxis.setValueFormatter(new ValueFormatter() {
104102
@Override
105-
public String getFormattedValue(float value, AxisBase axis) {
103+
public String getFormattedValue(float value) {
106104
return String.valueOf((int) value);
107105
}
108106
});

MPChartExample/src/main/java/com/xxmassdeveloper/mpchartexample/BarChartPositiveNegative.java

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
package com.xxmassdeveloper.mpchartexample;
32

43
import android.content.Intent;
@@ -10,17 +9,13 @@
109
import android.view.WindowManager;
1110

1211
import com.github.mikephil.charting.charts.BarChart;
13-
import com.github.mikephil.charting.components.AxisBase;
1412
import com.github.mikephil.charting.components.XAxis;
1513
import com.github.mikephil.charting.components.XAxis.XAxisPosition;
1614
import com.github.mikephil.charting.components.YAxis;
1715
import com.github.mikephil.charting.data.BarData;
1816
import com.github.mikephil.charting.data.BarDataSet;
1917
import com.github.mikephil.charting.data.BarEntry;
20-
import com.github.mikephil.charting.data.Entry;
21-
import com.github.mikephil.charting.formatter.IAxisValueFormatter;
22-
import com.github.mikephil.charting.formatter.IValueFormatter;
23-
import com.github.mikephil.charting.utils.ViewPortHandler;
18+
import com.github.mikephil.charting.formatter.ValueFormatter;
2419
import com.xxmassdeveloper.mpchartexample.notimportant.DemoBase;
2520

2621
import java.text.DecimalFormat;
@@ -88,9 +83,9 @@ protected void onCreate(Bundle savedInstanceState) {
8883
data.add(new Data(3f, -442.3f, "01-01"));
8984
data.add(new Data(4f, -2280.1f, "01-02"));
9085

91-
xAxis.setValueFormatter(new IAxisValueFormatter() {
86+
xAxis.setValueFormatter(new ValueFormatter() {
9287
@Override
93-
public String getFormattedValue(float value, AxisBase axis) {
88+
public String getFormattedValue(float value) {
9489
return data.get(Math.min(Math.max((int) value, 0), data.size()-1)).xAxisValue;
9590
}
9691
});
@@ -135,7 +130,7 @@ private void setData(List<Data> dataList) {
135130
BarData data = new BarData(set);
136131
data.setValueTextSize(13f);
137132
data.setValueTypeface(tfRegular);
138-
data.setValueFormatter(new ValueFormatter());
133+
data.setValueFormatter(new Formatter());
139134
data.setBarWidth(0.8f);
140135

141136
chart.setData(data);
@@ -159,17 +154,17 @@ private class Data {
159154
}
160155
}
161156

162-
private class ValueFormatter implements IValueFormatter
157+
private class Formatter extends ValueFormatter
163158
{
164159

165160
private final DecimalFormat mFormat;
166161

167-
ValueFormatter() {
162+
Formatter() {
168163
mFormat = new DecimalFormat("######.0");
169164
}
170165

171166
@Override
172-
public String getFormattedValue(float value, Entry entry, int dataSetIndex, ViewPortHandler viewPortHandler) {
167+
public String getFormattedValue(float value) {
173168
return mFormat.format(value);
174169
}
175170
}

MPChartExample/src/main/java/com/xxmassdeveloper/mpchartexample/CombinedChartActivity.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
package com.xxmassdeveloper.mpchartexample;
32

43
import android.content.Intent;
@@ -11,7 +10,6 @@
1110

1211
import com.github.mikephil.charting.charts.CombinedChart;
1312
import com.github.mikephil.charting.charts.CombinedChart.DrawOrder;
14-
import com.github.mikephil.charting.components.AxisBase;
1513
import com.github.mikephil.charting.components.Legend;
1614
import com.github.mikephil.charting.components.XAxis;
1715
import com.github.mikephil.charting.components.XAxis.XAxisPosition;
@@ -31,7 +29,7 @@
3129
import com.github.mikephil.charting.data.LineDataSet;
3230
import com.github.mikephil.charting.data.ScatterData;
3331
import com.github.mikephil.charting.data.ScatterDataSet;
34-
import com.github.mikephil.charting.formatter.IAxisValueFormatter;
32+
import com.github.mikephil.charting.formatter.ValueFormatter;
3533
import com.github.mikephil.charting.interfaces.datasets.IDataSet;
3634
import com.github.mikephil.charting.utils.ColorTemplate;
3735
import com.xxmassdeveloper.mpchartexample.notimportant.DemoBase;
@@ -83,9 +81,9 @@ protected void onCreate(Bundle savedInstanceState) {
8381
xAxis.setPosition(XAxisPosition.BOTH_SIDED);
8482
xAxis.setAxisMinimum(0f);
8583
xAxis.setGranularity(1f);
86-
xAxis.setValueFormatter(new IAxisValueFormatter() {
84+
xAxis.setValueFormatter(new ValueFormatter() {
8785
@Override
88-
public String getFormattedValue(float value, AxisBase axis) {
86+
public String getFormattedValue(float value) {
8987
return months[(int) value % months.length];
9088
}
9189
});

MPChartExample/src/main/java/com/xxmassdeveloper/mpchartexample/LineChartTime.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
package com.xxmassdeveloper.mpchartexample;
32

43
import android.Manifest;
@@ -16,15 +15,14 @@
1615
import android.widget.TextView;
1716

1817
import com.github.mikephil.charting.charts.LineChart;
19-
import com.github.mikephil.charting.components.AxisBase;
2018
import com.github.mikephil.charting.components.Legend;
2119
import com.github.mikephil.charting.components.XAxis;
2220
import com.github.mikephil.charting.components.YAxis;
2321
import com.github.mikephil.charting.components.YAxis.AxisDependency;
2422
import com.github.mikephil.charting.data.Entry;
2523
import com.github.mikephil.charting.data.LineData;
2624
import com.github.mikephil.charting.data.LineDataSet;
27-
import com.github.mikephil.charting.formatter.IAxisValueFormatter;
25+
import com.github.mikephil.charting.formatter.ValueFormatter;
2826
import com.github.mikephil.charting.interfaces.datasets.ILineDataSet;
2927
import com.github.mikephil.charting.utils.ColorTemplate;
3028
import com.xxmassdeveloper.mpchartexample.notimportant.DemoBase;
@@ -92,12 +90,12 @@ protected void onCreate(Bundle savedInstanceState) {
9290
xAxis.setTextColor(Color.rgb(255, 192, 56));
9391
xAxis.setCenterAxisLabels(true);
9492
xAxis.setGranularity(1f); // one hour
95-
xAxis.setValueFormatter(new IAxisValueFormatter() {
93+
xAxis.setValueFormatter(new ValueFormatter() {
9694

9795
private final SimpleDateFormat mFormat = new SimpleDateFormat("dd MMM HH:mm", Locale.ENGLISH);
9896

9997
@Override
100-
public String getFormattedValue(float value, AxisBase axis) {
98+
public String getFormattedValue(float value) {
10199

102100
long millis = TimeUnit.HOURS.toMillis((long) value);
103101
return mFormat.format(new Date(millis));

MPChartExample/src/main/java/com/xxmassdeveloper/mpchartexample/PieChartActivity.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ private void setData(int count, float range) {
160160
//dataSet.setSelectionShift(0f);
161161

162162
PieData data = new PieData(dataSet);
163-
data.setValueFormatter(new PercentFormatter());
163+
data.setValueFormatter(new PercentFormatter(chart));
164164
data.setValueTextSize(11f);
165165
data.setValueTextColor(Color.WHITE);
166166
data.setValueTypeface(tfLight);

MPChartExample/src/main/java/com/xxmassdeveloper/mpchartexample/RadarChartActivity.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
package com.xxmassdeveloper.mpchartexample;
32

43
import android.Manifest;
@@ -14,15 +13,14 @@
1413

1514
import com.github.mikephil.charting.animation.Easing;
1615
import com.github.mikephil.charting.charts.RadarChart;
17-
import com.github.mikephil.charting.components.AxisBase;
1816
import com.github.mikephil.charting.components.Legend;
1917
import com.github.mikephil.charting.components.MarkerView;
2018
import com.github.mikephil.charting.components.XAxis;
2119
import com.github.mikephil.charting.components.YAxis;
2220
import com.github.mikephil.charting.data.RadarData;
2321
import com.github.mikephil.charting.data.RadarDataSet;
2422
import com.github.mikephil.charting.data.RadarEntry;
25-
import com.github.mikephil.charting.formatter.IAxisValueFormatter;
23+
import com.github.mikephil.charting.formatter.ValueFormatter;
2624
import com.github.mikephil.charting.interfaces.datasets.IDataSet;
2725
import com.github.mikephil.charting.interfaces.datasets.IRadarDataSet;
2826
import com.xxmassdeveloper.mpchartexample.custom.RadarMarkerView;
@@ -69,12 +67,12 @@ protected void onCreate(Bundle savedInstanceState) {
6967
xAxis.setTextSize(9f);
7068
xAxis.setYOffset(0f);
7169
xAxis.setXOffset(0f);
72-
xAxis.setValueFormatter(new IAxisValueFormatter() {
70+
xAxis.setValueFormatter(new ValueFormatter() {
7371

7472
private final String[] mActivities = new String[]{"Burger", "Steak", "Salad", "Pasta", "Pizza"};
7573

7674
@Override
77-
public String getFormattedValue(float value, AxisBase axis) {
75+
public String getFormattedValue(float value) {
7876
return mActivities[(int) value % mActivities.length];
7977
}
8078
});

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@
2424
import com.github.mikephil.charting.data.BarDataSet;
2525
import com.github.mikephil.charting.data.BarEntry;
2626
import com.github.mikephil.charting.data.Entry;
27+
import com.github.mikephil.charting.formatter.StackedValueFormatter;
2728
import com.github.mikephil.charting.highlight.Highlight;
2829
import com.github.mikephil.charting.interfaces.datasets.IBarDataSet;
2930
import com.github.mikephil.charting.listener.OnChartValueSelectedListener;
3031
import com.github.mikephil.charting.utils.ColorTemplate;
31-
import com.xxmassdeveloper.mpchartexample.custom.MyAxisValueFormatter;
3232
import com.xxmassdeveloper.mpchartexample.custom.MyValueFormatter;
3333
import com.xxmassdeveloper.mpchartexample.notimportant.DemoBase;
3434

@@ -78,7 +78,7 @@ protected void onCreate(Bundle savedInstanceState) {
7878

7979
// change the position of the y-labels
8080
YAxis leftAxis = chart.getAxisLeft();
81-
leftAxis.setValueFormatter(new MyAxisValueFormatter());
81+
leftAxis.setValueFormatter(new MyValueFormatter("K"));
8282
leftAxis.setAxisMinimum(0f); // this replaces setStartAtZero(true)
8383
chart.getAxisRight().setEnabled(false);
8484

@@ -142,7 +142,7 @@ public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
142142
dataSets.add(set1);
143143

144144
BarData data = new BarData(dataSets);
145-
data.setValueFormatter(new MyValueFormatter());
145+
data.setValueFormatter(new StackedValueFormatter(false, "", 1));
146146
data.setValueTextColor(Color.WHITE);
147147

148148
chart.setData(data);

MPChartExample/src/main/java/com/xxmassdeveloper/mpchartexample/StackedBarActivityNegative.java

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
package com.xxmassdeveloper.mpchartexample;
32

43
import android.Manifest;
@@ -14,7 +13,6 @@
1413
import android.view.WindowManager;
1514

1615
import com.github.mikephil.charting.charts.HorizontalBarChart;
17-
import com.github.mikephil.charting.components.AxisBase;
1816
import com.github.mikephil.charting.components.Legend;
1917
import com.github.mikephil.charting.components.XAxis;
2018
import com.github.mikephil.charting.components.XAxis.XAxisPosition;
@@ -23,12 +21,10 @@
2321
import com.github.mikephil.charting.data.BarDataSet;
2422
import com.github.mikephil.charting.data.BarEntry;
2523
import com.github.mikephil.charting.data.Entry;
26-
import com.github.mikephil.charting.formatter.IValueFormatter;
27-
import com.github.mikephil.charting.formatter.IAxisValueFormatter;
24+
import com.github.mikephil.charting.formatter.ValueFormatter;
2825
import com.github.mikephil.charting.highlight.Highlight;
2926
import com.github.mikephil.charting.interfaces.datasets.IBarDataSet;
3027
import com.github.mikephil.charting.listener.OnChartValueSelectedListener;
31-
import com.github.mikephil.charting.utils.ViewPortHandler;
3228
import com.xxmassdeveloper.mpchartexample.notimportant.DemoBase;
3329

3430
import java.text.DecimalFormat;
@@ -80,12 +76,12 @@ protected void onCreate(Bundle savedInstanceState) {
8076
xAxis.setCenterAxisLabels(true);
8177
xAxis.setLabelCount(12);
8278
xAxis.setGranularity(10f);
83-
xAxis.setValueFormatter(new IAxisValueFormatter() {
79+
xAxis.setValueFormatter(new ValueFormatter() {
8480

8581
private final DecimalFormat format = new DecimalFormat("###");
8682

8783
@Override
88-
public String getFormattedValue(float value, AxisBase axis) {
84+
public String getFormattedValue(float value) {
8985
return format.format(value) + "-" + format.format(value + 10);
9086
}
9187
});
@@ -242,23 +238,16 @@ public void onNothingSelected() {
242238
Log.i("NOTING SELECTED", "");
243239
}
244240

245-
private class CustomFormatter implements IValueFormatter, IAxisValueFormatter {
241+
private class CustomFormatter extends ValueFormatter {
246242

247243
private final DecimalFormat mFormat;
248244

249245
CustomFormatter() {
250246
mFormat = new DecimalFormat("###");
251247
}
252248

253-
// data
254-
@Override
255-
public String getFormattedValue(float value, Entry entry, int dataSetIndex, ViewPortHandler viewPortHandler) {
256-
return mFormat.format(Math.abs(value)) + "m";
257-
}
258-
259-
// YAxis
260249
@Override
261-
public String getFormattedValue(float value, AxisBase axis) {
250+
public String getFormattedValue(float value) {
262251
return mFormat.format(Math.abs(value)) + "m";
263252
}
264253
}

0 commit comments

Comments
 (0)