Skip to content

Commit

Permalink
Revert: e5b6619 - bring back polymorphism to value formatters
Browse files Browse the repository at this point in the history
If anyone does not know how to add a space or change the format in anyway,
please learn how to subclass.
  • Loading branch information
danielgindi committed Sep 21, 2020
1 parent 8e4dccf commit bc0be2c
Show file tree
Hide file tree
Showing 47 changed files with 340 additions and 418 deletions.
6 changes: 2 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
bin/
gen/
generated/
docs/
finalOutput/
projectFilesBackup/

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

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

.directory

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

package com.xxmassdeveloper.mpchartexample;

import android.Manifest;
Expand Down Expand Up @@ -27,15 +28,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.ValueFormatter;
import com.github.mikephil.charting.formatter.IAxisValueFormatter;
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.utils.Fill;
import com.github.mikephil.charting.utils.MPPointF;
import com.xxmassdeveloper.mpchartexample.custom.DayAxisValueFormatter;
import com.xxmassdeveloper.mpchartexample.custom.MyValueFormatter;
import com.xxmassdeveloper.mpchartexample.custom.MyAxisValueFormatter;
import com.xxmassdeveloper.mpchartexample.custom.XYMarkerView;
import com.xxmassdeveloper.mpchartexample.notimportant.DemoBase;

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

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

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

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

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

package com.xxmassdeveloper.mpchartexample;

import android.Manifest;
Expand All @@ -16,15 +17,16 @@
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 @@ -98,9 +100,9 @@ protected void onCreate(Bundle savedInstanceState) {
xAxis.setTypeface(tfLight);
xAxis.setGranularity(1f);
xAxis.setCenterAxisLabels(true);
xAxis.setValueFormatter(new ValueFormatter() {
xAxis.setValueFormatter(new IAxisValueFormatter() {
@Override
public String getFormattedValue(float value) {
public String getFormattedValue(float value, AxisBase axis) {
return String.valueOf((int) value);
}
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

package com.xxmassdeveloper.mpchartexample;

import android.content.Intent;
Expand All @@ -9,13 +10,17 @@
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.formatter.ValueFormatter;
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.xxmassdeveloper.mpchartexample.notimportant.DemoBase;

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

chart.setData(data);

This comment has been minimized.

Copy link
@Mattmbugua

Mattmbugua Feb 1, 2021

This doesnt work..what could be the problem?

This comment has been minimized.

Copy link
@LanceBao0313

LanceBao0313 Nov 8, 2022

Same for me, I solved this by changing everything back to the old version.

Expand All @@ -154,17 +159,17 @@ private class Data {
}
}

private class Formatter extends ValueFormatter
private class ValueFormatter implements IValueFormatter
{

private final DecimalFormat mFormat;

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

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

package com.xxmassdeveloper.mpchartexample;

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

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 @@ -29,7 +31,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.ValueFormatter;
import com.github.mikephil.charting.formatter.IAxisValueFormatter;
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 @@ -81,9 +83,9 @@ protected void onCreate(Bundle savedInstanceState) {
xAxis.setPosition(XAxisPosition.BOTH_SIDED);
xAxis.setAxisMinimum(0f);
xAxis.setGranularity(1f);
xAxis.setValueFormatter(new ValueFormatter() {
xAxis.setValueFormatter(new IAxisValueFormatter() {
@Override
public String getFormattedValue(float value) {
public String getFormattedValue(float value, AxisBase axis) {
return months[(int) value % months.length];
}
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

package com.xxmassdeveloper.mpchartexample;

import android.Manifest;
Expand All @@ -15,14 +16,15 @@
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.ValueFormatter;
import com.github.mikephil.charting.formatter.IAxisValueFormatter;
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 @@ -90,12 +92,12 @@ protected void onCreate(Bundle savedInstanceState) {
xAxis.setTextColor(Color.rgb(255, 192, 56));
xAxis.setCenterAxisLabels(true);
xAxis.setGranularity(1f); // one hour
xAxis.setValueFormatter(new ValueFormatter() {
xAxis.setValueFormatter(new IAxisValueFormatter() {

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

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

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(chart));
data.setValueFormatter(new PercentFormatter());
data.setValueTextSize(11f);
data.setValueTextColor(Color.WHITE);
data.setValueTypeface(tfLight);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

package com.xxmassdeveloper.mpchartexample;

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

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.ValueFormatter;
import com.github.mikephil.charting.formatter.IAxisValueFormatter;
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 @@ -67,12 +69,12 @@ protected void onCreate(Bundle savedInstanceState) {
xAxis.setTextSize(9f);
xAxis.setYOffset(0f);
xAxis.setXOffset(0f);
xAxis.setValueFormatter(new ValueFormatter() {
xAxis.setValueFormatter(new IAxisValueFormatter() {

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

@Override
public String getFormattedValue(float value) {
public String getFormattedValue(float value, AxisBase axis) {
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 MyValueFormatter("K"));
leftAxis.setValueFormatter(new MyAxisValueFormatter());
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 StackedValueFormatter(false, "", 1));
data.setValueFormatter(new MyValueFormatter());
data.setValueTextColor(Color.WHITE);

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

package com.xxmassdeveloper.mpchartexample;

import android.Manifest;
Expand All @@ -13,6 +14,7 @@
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 @@ -21,10 +23,12 @@
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.ValueFormatter;
import com.github.mikephil.charting.formatter.IValueFormatter;
import com.github.mikephil.charting.formatter.IAxisValueFormatter;
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 @@ -76,12 +80,12 @@ protected void onCreate(Bundle savedInstanceState) {
xAxis.setCenterAxisLabels(true);
xAxis.setLabelCount(12);
xAxis.setGranularity(10f);
xAxis.setValueFormatter(new ValueFormatter() {
xAxis.setValueFormatter(new IAxisValueFormatter() {

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

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

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

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) {
public String getFormattedValue(float value, AxisBase axis) {
return mFormat.format(Math.abs(value)) + "m";
}
}
Expand Down
Loading

0 comments on commit bc0be2c

Please sign in to comment.