Skip to content

Commit

Permalink
New ValueFormatter
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
almic committed Nov 7, 2018
1 parent 608d9e2 commit e5b6619
Show file tree
Hide file tree
Showing 47 changed files with 408 additions and 338 deletions.
6 changes: 4 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
bin/
gen/
generated/
docs/
finalOutput/

build.xml
Expand All @@ -23,6 +22,8 @@ local.properties
# Eclipse project files
.classpath
.project
.settings/
.vscode/

# Proguard folder generated by Eclipse
proguard/
Expand All @@ -31,7 +32,8 @@ proguard/
*.iml
*.ipr
*.iws
.idea/
/.idea/*
!/.idea/runConfigurations

.directory

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

package com.xxmassdeveloper.mpchartexample;

import android.Manifest;
Expand Down Expand Up @@ -28,15 +27,15 @@
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.formatter.IAxisValueFormatter;
import com.github.mikephil.charting.formatter.ValueFormatter;
import com.github.mikephil.charting.highlight.Highlight;
import com.github.mikephil.charting.interfaces.datasets.IBarDataSet;
import com.github.mikephil.charting.interfaces.datasets.IDataSet;
import com.github.mikephil.charting.listener.OnChartValueSelectedListener;
import com.github.mikephil.charting.model.GradientColor;
import com.github.mikephil.charting.utils.MPPointF;
import com.xxmassdeveloper.mpchartexample.custom.DayAxisValueFormatter;
import com.xxmassdeveloper.mpchartexample.custom.MyAxisValueFormatter;
import com.xxmassdeveloper.mpchartexample.custom.MyValueFormatter;
import com.xxmassdeveloper.mpchartexample.custom.XYMarkerView;
import com.xxmassdeveloper.mpchartexample.notimportant.DemoBase;

Expand Down Expand Up @@ -86,7 +85,7 @@ protected void onCreate(Bundle savedInstanceState) {
chart.setDrawGridBackground(false);
// chart.setDrawYLabels(false);

IAxisValueFormatter xAxisFormatter = new DayAxisValueFormatter(chart);
ValueFormatter xAxisFormatter = new DayAxisValueFormatter(chart);

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

IAxisValueFormatter custom = new MyAxisValueFormatter();
ValueFormatter custom = new MyValueFormatter("$");

YAxis leftAxis = chart.getAxisLeft();
leftAxis.setTypeface(tfLight);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

package com.xxmassdeveloper.mpchartexample;

import android.Manifest;
Expand All @@ -17,16 +16,15 @@
import android.widget.TextView;

import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.components.AxisBase;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.formatter.IAxisValueFormatter;
import com.github.mikephil.charting.formatter.LargeValueFormatter;
import com.github.mikephil.charting.formatter.ValueFormatter;
import com.github.mikephil.charting.highlight.Highlight;
import com.github.mikephil.charting.interfaces.datasets.IBarDataSet;
import com.github.mikephil.charting.listener.OnChartValueSelectedListener;
Expand Down Expand Up @@ -100,9 +98,9 @@ protected void onCreate(Bundle savedInstanceState) {
xAxis.setTypeface(tfLight);
xAxis.setGranularity(1f);
xAxis.setCenterAxisLabels(true);
xAxis.setValueFormatter(new IAxisValueFormatter() {
xAxis.setValueFormatter(new ValueFormatter() {
@Override
public String getFormattedValue(float value, AxisBase axis) {
public String getFormattedValue(float value) {
return String.valueOf((int) value);
}
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

package com.xxmassdeveloper.mpchartexample;

import android.content.Intent;
Expand All @@ -10,17 +9,13 @@
import android.view.WindowManager;

import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.components.AxisBase;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.XAxis.XAxisPosition;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.formatter.IAxisValueFormatter;
import com.github.mikephil.charting.formatter.IValueFormatter;
import com.github.mikephil.charting.utils.ViewPortHandler;
import com.github.mikephil.charting.formatter.ValueFormatter;
import com.xxmassdeveloper.mpchartexample.notimportant.DemoBase;

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

xAxis.setValueFormatter(new IAxisValueFormatter() {
xAxis.setValueFormatter(new ValueFormatter() {
@Override
public String getFormattedValue(float value, AxisBase axis) {
public String getFormattedValue(float value) {
return data.get(Math.min(Math.max((int) value, 0), data.size()-1)).xAxisValue;
}
});
Expand Down Expand Up @@ -135,7 +130,7 @@ private void setData(List<Data> dataList) {
BarData data = new BarData(set);
data.setValueTextSize(13f);
data.setValueTypeface(tfRegular);
data.setValueFormatter(new ValueFormatter());
data.setValueFormatter(new Formatter());
data.setBarWidth(0.8f);

chart.setData(data);
Expand All @@ -159,17 +154,17 @@ private class Data {
}
}

private class ValueFormatter implements IValueFormatter
private class Formatter extends ValueFormatter
{

private final DecimalFormat mFormat;

ValueFormatter() {
Formatter() {
mFormat = new DecimalFormat("######.0");
}

@Override
public String getFormattedValue(float value, Entry entry, int dataSetIndex, ViewPortHandler viewPortHandler) {
public String getFormattedValue(float value) {
return mFormat.format(value);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

package com.xxmassdeveloper.mpchartexample;

import android.content.Intent;
Expand All @@ -11,7 +10,6 @@

import com.github.mikephil.charting.charts.CombinedChart;
import com.github.mikephil.charting.charts.CombinedChart.DrawOrder;
import com.github.mikephil.charting.components.AxisBase;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.XAxis.XAxisPosition;
Expand All @@ -31,7 +29,7 @@
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.data.ScatterData;
import com.github.mikephil.charting.data.ScatterDataSet;
import com.github.mikephil.charting.formatter.IAxisValueFormatter;
import com.github.mikephil.charting.formatter.ValueFormatter;
import com.github.mikephil.charting.interfaces.datasets.IDataSet;
import com.github.mikephil.charting.utils.ColorTemplate;
import com.xxmassdeveloper.mpchartexample.notimportant.DemoBase;
Expand Down Expand Up @@ -83,9 +81,9 @@ protected void onCreate(Bundle savedInstanceState) {
xAxis.setPosition(XAxisPosition.BOTH_SIDED);
xAxis.setAxisMinimum(0f);
xAxis.setGranularity(1f);
xAxis.setValueFormatter(new IAxisValueFormatter() {
xAxis.setValueFormatter(new ValueFormatter() {
@Override
public String getFormattedValue(float value, AxisBase axis) {
public String getFormattedValue(float value) {
return months[(int) value % months.length];
}
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

package com.xxmassdeveloper.mpchartexample;

import android.Manifest;
Expand All @@ -16,15 +15,14 @@
import android.widget.TextView;

import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.AxisBase;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.components.YAxis.AxisDependency;
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.IAxisValueFormatter;
import com.github.mikephil.charting.formatter.ValueFormatter;
import com.github.mikephil.charting.interfaces.datasets.ILineDataSet;
import com.github.mikephil.charting.utils.ColorTemplate;
import com.xxmassdeveloper.mpchartexample.notimportant.DemoBase;
Expand Down Expand Up @@ -92,12 +90,12 @@ protected void onCreate(Bundle savedInstanceState) {
xAxis.setTextColor(Color.rgb(255, 192, 56));
xAxis.setCenterAxisLabels(true);
xAxis.setGranularity(1f); // one hour
xAxis.setValueFormatter(new IAxisValueFormatter() {
xAxis.setValueFormatter(new ValueFormatter() {

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

@Override
public String getFormattedValue(float value, AxisBase axis) {
public String getFormattedValue(float value) {

long millis = TimeUnit.HOURS.toMillis((long) value);
return mFormat.format(new Date(millis));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ private void setData(int count, float range) {
//dataSet.setSelectionShift(0f);

PieData data = new PieData(dataSet);
data.setValueFormatter(new PercentFormatter());
data.setValueFormatter(new PercentFormatter(chart));
data.setValueTextSize(11f);
data.setValueTextColor(Color.WHITE);
data.setValueTypeface(tfLight);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

package com.xxmassdeveloper.mpchartexample;

import android.Manifest;
Expand All @@ -14,15 +13,14 @@

import com.github.mikephil.charting.animation.Easing;
import com.github.mikephil.charting.charts.RadarChart;
import com.github.mikephil.charting.components.AxisBase;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.MarkerView;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.RadarData;
import com.github.mikephil.charting.data.RadarDataSet;
import com.github.mikephil.charting.data.RadarEntry;
import com.github.mikephil.charting.formatter.IAxisValueFormatter;
import com.github.mikephil.charting.formatter.ValueFormatter;
import com.github.mikephil.charting.interfaces.datasets.IDataSet;
import com.github.mikephil.charting.interfaces.datasets.IRadarDataSet;
import com.xxmassdeveloper.mpchartexample.custom.RadarMarkerView;
Expand Down Expand Up @@ -69,12 +67,12 @@ protected void onCreate(Bundle savedInstanceState) {
xAxis.setTextSize(9f);
xAxis.setYOffset(0f);
xAxis.setXOffset(0f);
xAxis.setValueFormatter(new IAxisValueFormatter() {
xAxis.setValueFormatter(new ValueFormatter() {

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

@Override
public String getFormattedValue(float value, AxisBase axis) {
public String getFormattedValue(float value) {
return mActivities[(int) value % mActivities.length];
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.formatter.StackedValueFormatter;
import com.github.mikephil.charting.highlight.Highlight;
import com.github.mikephil.charting.interfaces.datasets.IBarDataSet;
import com.github.mikephil.charting.listener.OnChartValueSelectedListener;
import com.github.mikephil.charting.utils.ColorTemplate;
import com.xxmassdeveloper.mpchartexample.custom.MyAxisValueFormatter;
import com.xxmassdeveloper.mpchartexample.custom.MyValueFormatter;
import com.xxmassdeveloper.mpchartexample.notimportant.DemoBase;

Expand Down Expand Up @@ -78,7 +78,7 @@ protected void onCreate(Bundle savedInstanceState) {

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

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

BarData data = new BarData(dataSets);
data.setValueFormatter(new MyValueFormatter());
data.setValueFormatter(new StackedValueFormatter(false, "", 1));
data.setValueTextColor(Color.WHITE);

chart.setData(data);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

package com.xxmassdeveloper.mpchartexample;

import android.Manifest;
Expand All @@ -14,7 +13,6 @@
import android.view.WindowManager;

import com.github.mikephil.charting.charts.HorizontalBarChart;
import com.github.mikephil.charting.components.AxisBase;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.XAxis.XAxisPosition;
Expand All @@ -23,12 +21,10 @@
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.formatter.IValueFormatter;
import com.github.mikephil.charting.formatter.IAxisValueFormatter;
import com.github.mikephil.charting.formatter.ValueFormatter;
import com.github.mikephil.charting.highlight.Highlight;
import com.github.mikephil.charting.interfaces.datasets.IBarDataSet;
import com.github.mikephil.charting.listener.OnChartValueSelectedListener;
import com.github.mikephil.charting.utils.ViewPortHandler;
import com.xxmassdeveloper.mpchartexample.notimportant.DemoBase;

import java.text.DecimalFormat;
Expand Down Expand Up @@ -80,12 +76,12 @@ protected void onCreate(Bundle savedInstanceState) {
xAxis.setCenterAxisLabels(true);
xAxis.setLabelCount(12);
xAxis.setGranularity(10f);
xAxis.setValueFormatter(new IAxisValueFormatter() {
xAxis.setValueFormatter(new ValueFormatter() {

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

@Override
public String getFormattedValue(float value, AxisBase axis) {
public String getFormattedValue(float value) {
return format.format(value) + "-" + format.format(value + 10);
}
});
Expand Down Expand Up @@ -242,23 +238,16 @@ public void onNothingSelected() {
Log.i("NOTING SELECTED", "");
}

private class CustomFormatter implements IValueFormatter, IAxisValueFormatter {
private class CustomFormatter extends ValueFormatter {

private final DecimalFormat mFormat;

CustomFormatter() {
mFormat = new DecimalFormat("###");
}

// data
@Override
public String getFormattedValue(float value, Entry entry, int dataSetIndex, ViewPortHandler viewPortHandler) {
return mFormat.format(Math.abs(value)) + "m";
}

// YAxis
@Override
public String getFormattedValue(float value, AxisBase axis) {
public String getFormattedValue(float value) {
return mFormat.format(Math.abs(value)) + "m";
}
}
Expand Down
Loading

1 comment on commit e5b6619

@theblackwidower
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"To convert existing code, just use 'extends' instead of 'implements' and change the names to 'ValueFormatter'"? Yeah--Not that simple if you're using a lambda expression. Just thought I should point out somewhere that this change of yours gave me a bit of a headache.

Please sign in to comment.