Skip to content

Commit

Permalink
New formatters for YAxis and XAxis with greater customizeability.
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilJay committed Sep 20, 2015
1 parent 94dae2e commit 5a15715
Show file tree
Hide file tree
Showing 46 changed files with 355 additions and 202 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@
import com.github.mikephil.charting.data.filter.Approximator.ApproximatorType;
import com.github.mikephil.charting.listener.OnChartValueSelectedListener;
import com.github.mikephil.charting.highlight.Highlight;
import com.github.mikephil.charting.utils.ValueFormatter;
import com.xxmassdeveloper.mpchartexample.custom.MyValueFormatter;
import com.github.mikephil.charting.formatter.YAxisValueFormatter;
import com.xxmassdeveloper.mpchartexample.custom.MyYAxisValueFormatter;
import com.xxmassdeveloper.mpchartexample.notimportant.DemoBase;

import java.util.ArrayList;
Expand Down Expand Up @@ -92,7 +92,7 @@ protected void onCreate(Bundle savedInstanceState) {
xAxis.setDrawGridLines(false);
xAxis.setSpaceBetweenLabels(2);

ValueFormatter custom = new MyValueFormatter();
YAxisValueFormatter custom = new MyYAxisValueFormatter();

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

BarData data = new BarData(xVals, dataSets);
// data.setValueFormatter(new MyValueFormatter());
data.setValueTextSize(10f);
data.setValueTypeface(mTf);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.listener.OnChartValueSelectedListener;
import com.github.mikephil.charting.highlight.Highlight;
import com.github.mikephil.charting.utils.LargeValueFormatter;
import com.github.mikephil.charting.formatter.LargeValueFormatter;
import com.xxmassdeveloper.mpchartexample.custom.MyMarkerView;
import com.xxmassdeveloper.mpchartexample.notimportant.DemoBase;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ protected void onCreate(Bundle savedInstanceState) {
llXAxis.setTextSize(10f);

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

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

mChart.getAxisRight().setEnabled(false);

mChart.getViewPortHandler().setMaximumScaleY(2f);
mChart.getViewPortHandler().setMaximumScaleX(2f);
//mChart.getViewPortHandler().setMaximumScaleY(2f);
//mChart.getViewPortHandler().setMaximumScaleX(2f);

// add data
setData(45, 100);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import com.github.mikephil.charting.listener.OnChartValueSelectedListener;
import com.github.mikephil.charting.utils.ColorTemplate;
import com.github.mikephil.charting.highlight.Highlight;
import com.github.mikephil.charting.utils.PercentFormatter;
import com.github.mikephil.charting.formatter.PercentFormatter;
import com.xxmassdeveloper.mpchartexample.notimportant.DemoBase;

import java.util.ArrayList;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import com.github.mikephil.charting.listener.OnChartValueSelectedListener;
import com.github.mikephil.charting.utils.ColorTemplate;
import com.xxmassdeveloper.mpchartexample.custom.MyValueFormatter;
import com.xxmassdeveloper.mpchartexample.custom.MyYAxisValueFormatter;
import com.xxmassdeveloper.mpchartexample.notimportant.DemoBase;

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

// change the position of the y-labels
YAxis yLabels = mChart.getAxisLeft();
yLabels.setValueFormatter(new MyValueFormatter());
yLabels.setValueFormatter(new MyYAxisValueFormatter());
mChart.getAxisRight().setEnabled(false);

XAxis xLabels = mChart.getXAxis();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@
import com.github.mikephil.charting.data.filter.Approximator.ApproximatorType;
import com.github.mikephil.charting.listener.OnChartValueSelectedListener;
import com.github.mikephil.charting.highlight.Highlight;
import com.github.mikephil.charting.utils.ValueFormatter;
import com.github.mikephil.charting.formatter.ValueFormatter;
import com.github.mikephil.charting.utils.ViewPortHandler;
import com.github.mikephil.charting.formatter.YAxisValueFormatter;
import com.xxmassdeveloper.mpchartexample.notimportant.DemoBase;

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

}

private class CustomFormatter implements ValueFormatter {
private class CustomFormatter implements ValueFormatter, YAxisValueFormatter {

private DecimalFormat mFormat;

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

// data
@Override
public String getFormattedValue(float value) {
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, YAxis yAxis) {
return mFormat.format(Math.abs(value)) + "m";
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package com.xxmassdeveloper.mpchartexample.custom;

import com.github.mikephil.charting.utils.ViewPortHandler;
import com.github.mikephil.charting.utils.XValueFormatter;
import com.github.mikephil.charting.formatter.XAxisValueFormatter;

/**
* Created by Philipp Jahoda on 14/09/15.
*/
public class MyCustomXValueFormatter implements XValueFormatter {
public class MyCustomXAxisValueFormatter implements XAxisValueFormatter {

public MyCustomXValueFormatter() {
public MyCustomXAxisValueFormatter() {
// maybe do something here or provide parameters in constructor
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.interfaces.LineDataProvider;
import com.github.mikephil.charting.utils.FillFormatter;
import com.github.mikephil.charting.formatter.FillFormatter;

/**
* Created by Philipp Jahoda on 12/09/15.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.xxmassdeveloper.mpchartexample.custom;

import com.github.mikephil.charting.utils.ValueFormatter;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.formatter.ValueFormatter;
import com.github.mikephil.charting.utils.ViewPortHandler;

import java.text.DecimalFormat;

Expand All @@ -11,10 +13,9 @@ public class MyValueFormatter implements ValueFormatter {
public MyValueFormatter() {
mFormat = new DecimalFormat("###,###,###,##0.0");
}

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

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.xxmassdeveloper.mpchartexample.custom;

import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.formatter.YAxisValueFormatter;

import java.text.DecimalFormat;

public class MyYAxisValueFormatter implements YAxisValueFormatter {

private DecimalFormat mFormat;

public MyYAxisValueFormatter() {
mFormat = new DecimalFormat("###,###,###,##0.0");
}

@Override
public String getFormattedValue(float value, YAxis yAxis) {
return mFormat.format(value) + " $";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import com.github.mikephil.charting.components.Legend.LegendPosition;
import com.github.mikephil.charting.data.ChartData;
import com.github.mikephil.charting.data.PieData;
import com.github.mikephil.charting.utils.PercentFormatter;
import com.github.mikephil.charting.formatter.PercentFormatter;
import com.xxmassdeveloper.mpchartexample.R;

public class PieChartItem extends ChartItem {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -306,10 +306,10 @@ public void notifyDataSetChanged() {

calcMinMax();

if (mAxisLeft.needsDefaultFormatter())
mAxisLeft.setValueFormatter(mDefaultFormatter);
if (mAxisRight.needsDefaultFormatter())
mAxisRight.setValueFormatter(mDefaultFormatter);
// if (mAxisLeft.needsDefaultFormatter())
// mAxisLeft.setValueFormatter(mDefaultFormatter);
// if (mAxisRight.needsDefaultFormatter())
// mAxisRight.setValueFormatter(mDefaultFormatter);

mAxisRendererLeft.computeAxis(mAxisLeft.mAxisMinimum, mAxisLeft.mAxisMaximum);
mAxisRendererRight.computeAxis(mAxisRight.mAxisMinimum, mAxisRight.mAxisMaximum);
Expand Down
4 changes: 2 additions & 2 deletions MPChartLib/src/com/github/mikephil/charting/charts/Chart.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@
import com.github.mikephil.charting.listener.OnChartValueSelectedListener;
import com.github.mikephil.charting.renderer.DataRenderer;
import com.github.mikephil.charting.renderer.LegendRenderer;
import com.github.mikephil.charting.utils.DefaultValueFormatter;
import com.github.mikephil.charting.formatter.DefaultValueFormatter;
import com.github.mikephil.charting.highlight.Highlight;
import com.github.mikephil.charting.utils.Utils;
import com.github.mikephil.charting.utils.ValueFormatter;
import com.github.mikephil.charting.formatter.ValueFormatter;
import com.github.mikephil.charting.utils.ViewPortHandler;

import java.io.File;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import com.github.mikephil.charting.interfaces.LineDataProvider;
import com.github.mikephil.charting.interfaces.ScatterDataProvider;
import com.github.mikephil.charting.renderer.CombinedChartRenderer;
import com.github.mikephil.charting.utils.FillFormatter;

/**
* This chart class allows the combination of lines, bars, scatter and candle
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.interfaces.LineDataProvider;
import com.github.mikephil.charting.renderer.LineChartRenderer;
import com.github.mikephil.charting.utils.FillFormatter;

/**
* Chart that draws lines, surfaces, circles, ...
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,9 @@ public void notifyDataSetChanged() {

calcMinMax();

if (mYAxis.needsDefaultFormatter()) {
mYAxis.setValueFormatter(mDefaultFormatter);
}
// if (mYAxis.needsDefaultFormatter()) {
// mYAxis.setValueFormatter(mDefaultFormatter);
// }

mYAxisRenderer.computeAxis(mYAxis.mAxisMinimum, mYAxis.mAxisMaximum);
mXAxisRenderer.computeAxis(mData.getXValAverageLength(), mData.getXVals());
Expand Down
22 changes: 11 additions & 11 deletions MPChartLib/src/com/github/mikephil/charting/components/XAxis.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@

package com.github.mikephil.charting.components;

import com.github.mikephil.charting.utils.DefaultXValueFormatter;
import com.github.mikephil.charting.utils.XValueFormatter;
import com.github.mikephil.charting.formatter.DefaultXAxisAxisValueFormatter;
import com.github.mikephil.charting.formatter.XAxisValueFormatter;

import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -66,7 +66,7 @@ public class XAxis extends AxisBase {
/**
* Custom formatter for adjusting x-value strings
*/
protected XValueFormatter mXValueFormatter = new DefaultXValueFormatter();
protected XAxisValueFormatter mXAxisValueFormatter = new DefaultXAxisAxisValueFormatter();

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


/**
* Sets a custom XValueFormatter for the data object that allows custom-formatting
* Sets a custom XAxisValueFormatter for the data object that allows custom-formatting
* of all x-values before rendering them. Provide null to reset back to the
* default formatting.
*
* @param formatter
*/
public void setXValueFormatter(XValueFormatter formatter) {
public void setValueFormatter(XAxisValueFormatter formatter) {
if(formatter == null)
mXValueFormatter = new DefaultXValueFormatter();
mXAxisValueFormatter = new DefaultXAxisAxisValueFormatter();
else
mXValueFormatter = formatter;
mXAxisValueFormatter = formatter;
}

/**
* Returns the custom XValueFormatter that is set for this data object.
* Returns the custom XAxisValueFormatter that is set for this data object.
* @return
*/
public XValueFormatter getXValueFormatter() {
return mXValueFormatter;
public XAxisValueFormatter getValueFormatter() {
return mXAxisValueFormatter;
}

@Override
Expand Down
33 changes: 19 additions & 14 deletions MPChartLib/src/com/github/mikephil/charting/components/YAxis.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

import android.graphics.Paint;

import com.github.mikephil.charting.utils.DefaultValueFormatter;
import com.github.mikephil.charting.formatter.DefaultValueFormatter;
import com.github.mikephil.charting.formatter.DefaultYAxisValueFormatter;
import com.github.mikephil.charting.utils.Utils;
import com.github.mikephil.charting.utils.ValueFormatter;
import com.github.mikephil.charting.formatter.YAxisValueFormatter;

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

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

/** the actual array of entries */
public float[] mEntries = new float[] {};
Expand Down Expand Up @@ -358,42 +359,46 @@ public String getFormattedLabel(int index) {
if (index < 0 || index >= mEntries.length)
return "";
else
return getValueFormatter().getFormattedValue(mEntries[index]);
return getValueFormatter().getFormattedValue(mEntries[index], this);
}

/**
* Sets the formatter to be used for drawing the values inside the chart. If no formatter is set, the chart will
* Sets the formatter to be used for formatting the axis labels. If no formatter is set, the chart will
* automatically determine a reasonable formatting (concerning decimals) for all the values that are drawn inside
* the chart. Use chart.getDefaultValueFormatter() to use the formatter calculated by the chart.
*
* @param f
*/
public void setValueFormatter(ValueFormatter f) {
public void setValueFormatter(YAxisValueFormatter f) {

if (f == null)
return;
mYAxisValueFormatter = new DefaultYAxisValueFormatter(mDecimals);
else
mValueFormatter = f;
mYAxisValueFormatter = f;
}

/**
* Returns the formatter used for drawing the values inside the chart.
* Returns the formatter used for formatting the axis labels.
*
* @return
*/
public ValueFormatter getValueFormatter() {
return mValueFormatter;
public YAxisValueFormatter getValueFormatter() {

if(mYAxisValueFormatter == null)
mYAxisValueFormatter = new DefaultYAxisValueFormatter(mDecimals);

return mYAxisValueFormatter;
}

/**
* If this component has no ValueFormatter or is only equipped with the default one (no custom set), return true.
* If this component has no YAxisValueFormatter or is only equipped with the default one (no custom set), return true.
*
* @return
*/
public boolean needsDefaultFormatter() {
if (mValueFormatter == null)
if (mYAxisValueFormatter == null)
return true;
if (mValueFormatter instanceof DefaultValueFormatter)
if (mYAxisValueFormatter instanceof DefaultValueFormatter)
return true;

return false;
Expand Down
Loading

0 comments on commit 5a15715

Please sign in to comment.