Skip to content

Commit 5a15715

Browse files
committed
New formatters for YAxis and XAxis with greater customizeability.
1 parent 94dae2e commit 5a15715

Some content is hidden

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

46 files changed

+355
-202
lines changed

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

+3-4
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@
3333
import com.github.mikephil.charting.data.filter.Approximator.ApproximatorType;
3434
import com.github.mikephil.charting.listener.OnChartValueSelectedListener;
3535
import com.github.mikephil.charting.highlight.Highlight;
36-
import com.github.mikephil.charting.utils.ValueFormatter;
37-
import com.xxmassdeveloper.mpchartexample.custom.MyValueFormatter;
36+
import com.github.mikephil.charting.formatter.YAxisValueFormatter;
37+
import com.xxmassdeveloper.mpchartexample.custom.MyYAxisValueFormatter;
3838
import com.xxmassdeveloper.mpchartexample.notimportant.DemoBase;
3939

4040
import java.util.ArrayList;
@@ -92,7 +92,7 @@ protected void onCreate(Bundle savedInstanceState) {
9292
xAxis.setDrawGridLines(false);
9393
xAxis.setSpaceBetweenLabels(2);
9494

95-
ValueFormatter custom = new MyValueFormatter();
95+
YAxisValueFormatter custom = new MyYAxisValueFormatter();
9696

9797
YAxis leftAxis = mChart.getAxisLeft();
9898
leftAxis.setTypeface(mTf);
@@ -267,7 +267,6 @@ private void setData(int count, float range) {
267267
dataSets.add(set1);
268268

269269
BarData data = new BarData(xVals, dataSets);
270-
// data.setValueFormatter(new MyValueFormatter());
271270
data.setValueTextSize(10f);
272271
data.setValueTypeface(mTf);
273272

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
import com.github.mikephil.charting.data.Entry;
2525
import com.github.mikephil.charting.listener.OnChartValueSelectedListener;
2626
import com.github.mikephil.charting.highlight.Highlight;
27-
import com.github.mikephil.charting.utils.LargeValueFormatter;
27+
import com.github.mikephil.charting.formatter.LargeValueFormatter;
2828
import com.xxmassdeveloper.mpchartexample.custom.MyMarkerView;
2929
import com.xxmassdeveloper.mpchartexample.notimportant.DemoBase;
3030

MPChartExample/src/com/xxmassdeveloper/mpchartexample/LineChartActivity1.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ protected void onCreate(Bundle savedInstanceState) {
103103
llXAxis.setTextSize(10f);
104104

105105
XAxis xAxis = mChart.getXAxis();
106-
//xAxis.setXValueFormatter(new MyCustomXValueFormatter());
106+
//xAxis.setValueFormatter(new MyCustomXAxisValueFormatter());
107107
//xAxis.addLimitLine(llXAxis); // add x-axis limit line
108108

109109
LimitLine ll1 = new LimitLine(130f, "Upper Limit");
@@ -133,8 +133,8 @@ protected void onCreate(Bundle savedInstanceState) {
133133

134134
mChart.getAxisRight().setEnabled(false);
135135

136-
mChart.getViewPortHandler().setMaximumScaleY(2f);
137-
mChart.getViewPortHandler().setMaximumScaleX(2f);
136+
//mChart.getViewPortHandler().setMaximumScaleY(2f);
137+
//mChart.getViewPortHandler().setMaximumScaleX(2f);
138138

139139
// add data
140140
setData(45, 100);

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import com.github.mikephil.charting.listener.OnChartValueSelectedListener;
2424
import com.github.mikephil.charting.utils.ColorTemplate;
2525
import com.github.mikephil.charting.highlight.Highlight;
26-
import com.github.mikephil.charting.utils.PercentFormatter;
26+
import com.github.mikephil.charting.formatter.PercentFormatter;
2727
import com.xxmassdeveloper.mpchartexample.notimportant.DemoBase;
2828

2929
import java.util.ArrayList;

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import com.github.mikephil.charting.listener.OnChartValueSelectedListener;
3030
import com.github.mikephil.charting.utils.ColorTemplate;
3131
import com.xxmassdeveloper.mpchartexample.custom.MyValueFormatter;
32+
import com.xxmassdeveloper.mpchartexample.custom.MyYAxisValueFormatter;
3233
import com.xxmassdeveloper.mpchartexample.notimportant.DemoBase;
3334

3435
public class StackedBarActivity extends DemoBase implements OnSeekBarChangeListener, OnChartValueSelectedListener {
@@ -71,7 +72,7 @@ protected void onCreate(Bundle savedInstanceState) {
7172

7273
// change the position of the y-labels
7374
YAxis yLabels = mChart.getAxisLeft();
74-
yLabels.setValueFormatter(new MyValueFormatter());
75+
yLabels.setValueFormatter(new MyYAxisValueFormatter());
7576
mChart.getAxisRight().setEnabled(false);
7677

7778
XAxis xLabels = mChart.getXAxis();

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

+12-3
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@
2424
import com.github.mikephil.charting.data.filter.Approximator.ApproximatorType;
2525
import com.github.mikephil.charting.listener.OnChartValueSelectedListener;
2626
import com.github.mikephil.charting.highlight.Highlight;
27-
import com.github.mikephil.charting.utils.ValueFormatter;
27+
import com.github.mikephil.charting.formatter.ValueFormatter;
28+
import com.github.mikephil.charting.utils.ViewPortHandler;
29+
import com.github.mikephil.charting.formatter.YAxisValueFormatter;
2830
import com.xxmassdeveloper.mpchartexample.notimportant.DemoBase;
2931

3032
import java.text.DecimalFormat;
@@ -211,16 +213,23 @@ public void onNothingSelected() {
211213

212214
}
213215

214-
private class CustomFormatter implements ValueFormatter {
216+
private class CustomFormatter implements ValueFormatter, YAxisValueFormatter {
215217

216218
private DecimalFormat mFormat;
217219

218220
public CustomFormatter() {
219221
mFormat = new DecimalFormat("###");
220222
}
221223

224+
// data
222225
@Override
223-
public String getFormattedValue(float value) {
226+
public String getFormattedValue(float value, Entry entry, int dataSetIndex, ViewPortHandler viewPortHandler) {
227+
return mFormat.format(Math.abs(value)) + "m";
228+
}
229+
230+
// YAxis
231+
@Override
232+
public String getFormattedValue(float value, YAxis yAxis) {
224233
return mFormat.format(Math.abs(value)) + "m";
225234
}
226235
}

MPChartExample/src/com/xxmassdeveloper/mpchartexample/custom/MyCustomXValueFormatter.java renamed to MPChartExample/src/com/xxmassdeveloper/mpchartexample/custom/MyCustomXAxisValueFormatter.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
package com.xxmassdeveloper.mpchartexample.custom;
22

33
import com.github.mikephil.charting.utils.ViewPortHandler;
4-
import com.github.mikephil.charting.utils.XValueFormatter;
4+
import com.github.mikephil.charting.formatter.XAxisValueFormatter;
55

66
/**
77
* Created by Philipp Jahoda on 14/09/15.
88
*/
9-
public class MyCustomXValueFormatter implements XValueFormatter {
9+
public class MyCustomXAxisValueFormatter implements XAxisValueFormatter {
1010

11-
public MyCustomXValueFormatter() {
11+
public MyCustomXAxisValueFormatter() {
1212
// maybe do something here or provide parameters in constructor
1313
}
1414

MPChartExample/src/com/xxmassdeveloper/mpchartexample/custom/MyFillFormatter.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import com.github.mikephil.charting.data.LineDataSet;
44
import com.github.mikephil.charting.interfaces.LineDataProvider;
5-
import com.github.mikephil.charting.utils.FillFormatter;
5+
import com.github.mikephil.charting.formatter.FillFormatter;
66

77
/**
88
* Created by Philipp Jahoda on 12/09/15.
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.xxmassdeveloper.mpchartexample.custom;
22

3-
import com.github.mikephil.charting.utils.ValueFormatter;
3+
import com.github.mikephil.charting.data.Entry;
4+
import com.github.mikephil.charting.formatter.ValueFormatter;
5+
import com.github.mikephil.charting.utils.ViewPortHandler;
46

57
import java.text.DecimalFormat;
68

@@ -11,10 +13,9 @@ public class MyValueFormatter implements ValueFormatter {
1113
public MyValueFormatter() {
1214
mFormat = new DecimalFormat("###,###,###,##0.0");
1315
}
14-
16+
1517
@Override
16-
public String getFormattedValue(float value) {
18+
public String getFormattedValue(float value, Entry entry, int dataSetIndex, ViewPortHandler viewPortHandler) {
1719
return mFormat.format(value) + " $";
1820
}
19-
2021
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.xxmassdeveloper.mpchartexample.custom;
2+
3+
import com.github.mikephil.charting.components.YAxis;
4+
import com.github.mikephil.charting.formatter.YAxisValueFormatter;
5+
6+
import java.text.DecimalFormat;
7+
8+
public class MyYAxisValueFormatter implements YAxisValueFormatter {
9+
10+
private DecimalFormat mFormat;
11+
12+
public MyYAxisValueFormatter() {
13+
mFormat = new DecimalFormat("###,###,###,##0.0");
14+
}
15+
16+
@Override
17+
public String getFormattedValue(float value, YAxis yAxis) {
18+
return mFormat.format(value) + " $";
19+
}
20+
}

MPChartExample/src/com/xxmassdeveloper/mpchartexample/listviewitems/PieChartItem.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import com.github.mikephil.charting.components.Legend.LegendPosition;
1313
import com.github.mikephil.charting.data.ChartData;
1414
import com.github.mikephil.charting.data.PieData;
15-
import com.github.mikephil.charting.utils.PercentFormatter;
15+
import com.github.mikephil.charting.formatter.PercentFormatter;
1616
import com.xxmassdeveloper.mpchartexample.R;
1717

1818
public class PieChartItem extends ChartItem {

MPChartLib/src/com/github/mikephil/charting/charts/BarLineChartBase.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -306,10 +306,10 @@ public void notifyDataSetChanged() {
306306

307307
calcMinMax();
308308

309-
if (mAxisLeft.needsDefaultFormatter())
310-
mAxisLeft.setValueFormatter(mDefaultFormatter);
311-
if (mAxisRight.needsDefaultFormatter())
312-
mAxisRight.setValueFormatter(mDefaultFormatter);
309+
// if (mAxisLeft.needsDefaultFormatter())
310+
// mAxisLeft.setValueFormatter(mDefaultFormatter);
311+
// if (mAxisRight.needsDefaultFormatter())
312+
// mAxisRight.setValueFormatter(mDefaultFormatter);
313313

314314
mAxisRendererLeft.computeAxis(mAxisLeft.mAxisMinimum, mAxisLeft.mAxisMaximum);
315315
mAxisRendererRight.computeAxis(mAxisRight.mAxisMinimum, mAxisRight.mAxisMaximum);

MPChartLib/src/com/github/mikephil/charting/charts/Chart.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@
4040
import com.github.mikephil.charting.listener.OnChartValueSelectedListener;
4141
import com.github.mikephil.charting.renderer.DataRenderer;
4242
import com.github.mikephil.charting.renderer.LegendRenderer;
43-
import com.github.mikephil.charting.utils.DefaultValueFormatter;
43+
import com.github.mikephil.charting.formatter.DefaultValueFormatter;
4444
import com.github.mikephil.charting.highlight.Highlight;
4545
import com.github.mikephil.charting.utils.Utils;
46-
import com.github.mikephil.charting.utils.ValueFormatter;
46+
import com.github.mikephil.charting.formatter.ValueFormatter;
4747
import com.github.mikephil.charting.utils.ViewPortHandler;
4848

4949
import java.io.File;

MPChartLib/src/com/github/mikephil/charting/charts/CombinedChart.java

-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import com.github.mikephil.charting.interfaces.LineDataProvider;
1919
import com.github.mikephil.charting.interfaces.ScatterDataProvider;
2020
import com.github.mikephil.charting.renderer.CombinedChartRenderer;
21-
import com.github.mikephil.charting.utils.FillFormatter;
2221

2322
/**
2423
* This chart class allows the combination of lines, bars, scatter and candle

MPChartLib/src/com/github/mikephil/charting/charts/LineChart.java

-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import com.github.mikephil.charting.data.LineData;
88
import com.github.mikephil.charting.interfaces.LineDataProvider;
99
import com.github.mikephil.charting.renderer.LineChartRenderer;
10-
import com.github.mikephil.charting.utils.FillFormatter;
1110

1211
/**
1312
* Chart that draws lines, surfaces, circles, ...

MPChartLib/src/com/github/mikephil/charting/charts/RadarChart.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -166,9 +166,9 @@ public void notifyDataSetChanged() {
166166

167167
calcMinMax();
168168

169-
if (mYAxis.needsDefaultFormatter()) {
170-
mYAxis.setValueFormatter(mDefaultFormatter);
171-
}
169+
// if (mYAxis.needsDefaultFormatter()) {
170+
// mYAxis.setValueFormatter(mDefaultFormatter);
171+
// }
172172

173173
mYAxisRenderer.computeAxis(mYAxis.mAxisMinimum, mYAxis.mAxisMaximum);
174174
mXAxisRenderer.computeAxis(mData.getXValAverageLength(), mData.getXVals());

MPChartLib/src/com/github/mikephil/charting/components/XAxis.java

+11-11
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11

22
package com.github.mikephil.charting.components;
33

4-
import com.github.mikephil.charting.utils.DefaultXValueFormatter;
5-
import com.github.mikephil.charting.utils.XValueFormatter;
4+
import com.github.mikephil.charting.formatter.DefaultXAxisAxisValueFormatter;
5+
import com.github.mikephil.charting.formatter.XAxisValueFormatter;
66

77
import java.util.ArrayList;
88
import java.util.List;
@@ -66,7 +66,7 @@ public class XAxis extends AxisBase {
6666
/**
6767
* Custom formatter for adjusting x-value strings
6868
*/
69-
protected XValueFormatter mXValueFormatter = new DefaultXValueFormatter();
69+
protected XAxisValueFormatter mXAxisValueFormatter = new DefaultXAxisAxisValueFormatter();
7070

7171
/** the position of the x-labels relative to the chart */
7272
private XAxisPosition mPosition = XAxisPosition.TOP;
@@ -101,7 +101,7 @@ public void setPosition(XAxisPosition pos) {
101101
* labels, default 4. This only applies if the number of labels that will be
102102
* skipped in between drawn axis labels is not custom set.
103103
*
104-
* @param space
104+
* @param spaceCharacters
105105
*/
106106
public void setSpaceBetweenLabels(int spaceCharacters) {
107107
mSpaceBetweenLabels = spaceCharacters;
@@ -191,25 +191,25 @@ public List<String> getValues() {
191191

192192

193193
/**
194-
* Sets a custom XValueFormatter for the data object that allows custom-formatting
194+
* Sets a custom XAxisValueFormatter for the data object that allows custom-formatting
195195
* of all x-values before rendering them. Provide null to reset back to the
196196
* default formatting.
197197
*
198198
* @param formatter
199199
*/
200-
public void setXValueFormatter(XValueFormatter formatter) {
200+
public void setValueFormatter(XAxisValueFormatter formatter) {
201201
if(formatter == null)
202-
mXValueFormatter = new DefaultXValueFormatter();
202+
mXAxisValueFormatter = new DefaultXAxisAxisValueFormatter();
203203
else
204-
mXValueFormatter = formatter;
204+
mXAxisValueFormatter = formatter;
205205
}
206206

207207
/**
208-
* Returns the custom XValueFormatter that is set for this data object.
208+
* Returns the custom XAxisValueFormatter that is set for this data object.
209209
* @return
210210
*/
211-
public XValueFormatter getXValueFormatter() {
212-
return mXValueFormatter;
211+
public XAxisValueFormatter getValueFormatter() {
212+
return mXAxisValueFormatter;
213213
}
214214

215215
@Override

MPChartLib/src/com/github/mikephil/charting/components/YAxis.java

+19-14
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22

33
import android.graphics.Paint;
44

5-
import com.github.mikephil.charting.utils.DefaultValueFormatter;
5+
import com.github.mikephil.charting.formatter.DefaultValueFormatter;
6+
import com.github.mikephil.charting.formatter.DefaultYAxisValueFormatter;
67
import com.github.mikephil.charting.utils.Utils;
7-
import com.github.mikephil.charting.utils.ValueFormatter;
8+
import com.github.mikephil.charting.formatter.YAxisValueFormatter;
89

910
/**
1011
* Class representing the y-axis labels settings and its entries. Only use the setter methods to modify it. Do not
@@ -17,7 +18,7 @@
1718
public class YAxis extends AxisBase {
1819

1920
/** custom formatter that is used instead of the auto-formatter if set */
20-
protected ValueFormatter mValueFormatter;
21+
protected YAxisValueFormatter mYAxisValueFormatter;
2122

2223
/** the actual array of entries */
2324
public float[] mEntries = new float[] {};
@@ -358,42 +359,46 @@ public String getFormattedLabel(int index) {
358359
if (index < 0 || index >= mEntries.length)
359360
return "";
360361
else
361-
return getValueFormatter().getFormattedValue(mEntries[index]);
362+
return getValueFormatter().getFormattedValue(mEntries[index], this);
362363
}
363364

364365
/**
365-
* Sets the formatter to be used for drawing the values inside the chart. If no formatter is set, the chart will
366+
* Sets the formatter to be used for formatting the axis labels. If no formatter is set, the chart will
366367
* automatically determine a reasonable formatting (concerning decimals) for all the values that are drawn inside
367368
* the chart. Use chart.getDefaultValueFormatter() to use the formatter calculated by the chart.
368369
*
369370
* @param f
370371
*/
371-
public void setValueFormatter(ValueFormatter f) {
372+
public void setValueFormatter(YAxisValueFormatter f) {
372373

373374
if (f == null)
374-
return;
375+
mYAxisValueFormatter = new DefaultYAxisValueFormatter(mDecimals);
375376
else
376-
mValueFormatter = f;
377+
mYAxisValueFormatter = f;
377378
}
378379

379380
/**
380-
* Returns the formatter used for drawing the values inside the chart.
381+
* Returns the formatter used for formatting the axis labels.
381382
*
382383
* @return
383384
*/
384-
public ValueFormatter getValueFormatter() {
385-
return mValueFormatter;
385+
public YAxisValueFormatter getValueFormatter() {
386+
387+
if(mYAxisValueFormatter == null)
388+
mYAxisValueFormatter = new DefaultYAxisValueFormatter(mDecimals);
389+
390+
return mYAxisValueFormatter;
386391
}
387392

388393
/**
389-
* If this component has no ValueFormatter or is only equipped with the default one (no custom set), return true.
394+
* If this component has no YAxisValueFormatter or is only equipped with the default one (no custom set), return true.
390395
*
391396
* @return
392397
*/
393398
public boolean needsDefaultFormatter() {
394-
if (mValueFormatter == null)
399+
if (mYAxisValueFormatter == null)
395400
return true;
396-
if (mValueFormatter instanceof DefaultValueFormatter)
401+
if (mYAxisValueFormatter instanceof DefaultValueFormatter)
397402
return true;
398403

399404
return false;

0 commit comments

Comments
 (0)