From 5a15715b25991e3d61d27d552f9eba45975d65e7 Mon Sep 17 00:00:00 2001 From: Philipp Jahoda Date: Sun, 20 Sep 2015 10:21:13 +0200 Subject: [PATCH] New formatters for YAxis and XAxis with greater customizeability. --- .../mpchartexample/BarChartActivity.java | 7 ++-- .../BarChartActivityMultiDataset.java | 2 +- .../mpchartexample/LineChartActivity1.java | 6 +-- .../mpchartexample/PieChartActivity.java | 2 +- .../mpchartexample/StackedBarActivity.java | 3 +- .../StackedBarActivityNegative.java | 15 +++++-- ....java => MyCustomXAxisValueFormatter.java} | 6 +-- .../custom/MyFillFormatter.java | 2 +- .../custom/MyValueFormatter.java | 9 +++-- .../custom/MyYAxisValueFormatter.java | 20 ++++++++++ .../listviewitems/PieChartItem.java | 2 +- .../charting/charts/BarLineChartBase.java | 8 ++-- .../mikephil/charting/charts/Chart.java | 4 +- .../charting/charts/CombinedChart.java | 1 - .../mikephil/charting/charts/LineChart.java | 1 - .../mikephil/charting/charts/RadarChart.java | 6 +-- .../mikephil/charting/components/XAxis.java | 22 +++++----- .../mikephil/charting/components/YAxis.java | 33 ++++++++------- .../mikephil/charting/data/ChartData.java | 2 +- .../mikephil/charting/data/DataSet.java | 4 +- .../mikephil/charting/data/LineDataSet.java | 5 +-- .../{utils => formatter}/ColorFormatter.java | 2 +- .../DefaultFillFormatter.java | 3 +- .../DefaultValueFormatter.java | 14 +++++-- .../DefaultXAxisAxisValueFormatter.java} | 6 ++- .../formatter/DefaultYAxisValueFormatter.java | 40 +++++++++++++++++++ .../{utils => formatter}/FillFormatter.java | 2 +- .../LargeValueFormatter.java | 19 +++++++-- .../charting/formatter/PercentFormatter.java | 35 ++++++++++++++++ .../charting/formatter/ValueFormatter.java | 28 +++++++++++++ .../XAxisValueFormatter.java} | 8 +++- .../formatter/YAxisValueFormatter.java | 22 ++++++++++ .../charting/interfaces/ChartInterface.java | 2 +- .../charting/interfaces/LineDataProvider.java | 2 - .../charting/renderer/BarChartRenderer.java | 33 +++++---------- .../renderer/BubbleChartRenderer.java | 14 +++---- .../renderer/CandleStickChartRenderer.java | 5 +-- .../charting/renderer/DataRenderer.java | 32 +++++++++++++++ .../renderer/HorizontalBarChartRenderer.java | 27 ++++++++----- .../charting/renderer/LineChartRenderer.java | 7 ++-- .../charting/renderer/PieChartRenderer.java | 22 +++++----- .../charting/renderer/RadarChartRenderer.java | 19 ++++----- .../renderer/ScatterChartRenderer.java | 7 ++-- .../charting/renderer/XAxisRenderer.java | 2 +- .../charting/utils/PercentFormatter.java | 24 ----------- .../charting/utils/ValueFormatter.java | 22 ---------- 46 files changed, 355 insertions(+), 202 deletions(-) rename MPChartExample/src/com/xxmassdeveloper/mpchartexample/custom/{MyCustomXValueFormatter.java => MyCustomXAxisValueFormatter.java} (81%) create mode 100644 MPChartExample/src/com/xxmassdeveloper/mpchartexample/custom/MyYAxisValueFormatter.java rename MPChartLib/src/com/github/mikephil/charting/{utils => formatter}/ColorFormatter.java (86%) rename MPChartLib/src/com/github/mikephil/charting/{utils => formatter}/DefaultFillFormatter.java (92%) rename MPChartLib/src/com/github/mikephil/charting/{utils => formatter}/DefaultValueFormatter.java (62%) rename MPChartLib/src/com/github/mikephil/charting/{utils/DefaultXValueFormatter.java => formatter/DefaultXAxisAxisValueFormatter.java} (65%) create mode 100644 MPChartLib/src/com/github/mikephil/charting/formatter/DefaultYAxisValueFormatter.java rename MPChartLib/src/com/github/mikephil/charting/{utils => formatter}/FillFormatter.java (93%) rename MPChartLib/src/com/github/mikephil/charting/{utils => formatter}/LargeValueFormatter.java (71%) create mode 100644 MPChartLib/src/com/github/mikephil/charting/formatter/PercentFormatter.java create mode 100644 MPChartLib/src/com/github/mikephil/charting/formatter/ValueFormatter.java rename MPChartLib/src/com/github/mikephil/charting/{utils/XValueFormatter.java => formatter/XAxisValueFormatter.java} (68%) create mode 100644 MPChartLib/src/com/github/mikephil/charting/formatter/YAxisValueFormatter.java delete mode 100644 MPChartLib/src/com/github/mikephil/charting/utils/PercentFormatter.java delete mode 100644 MPChartLib/src/com/github/mikephil/charting/utils/ValueFormatter.java diff --git a/MPChartExample/src/com/xxmassdeveloper/mpchartexample/BarChartActivity.java b/MPChartExample/src/com/xxmassdeveloper/mpchartexample/BarChartActivity.java index 1db0e0232..be899270d 100644 --- a/MPChartExample/src/com/xxmassdeveloper/mpchartexample/BarChartActivity.java +++ b/MPChartExample/src/com/xxmassdeveloper/mpchartexample/BarChartActivity.java @@ -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; @@ -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); @@ -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); diff --git a/MPChartExample/src/com/xxmassdeveloper/mpchartexample/BarChartActivityMultiDataset.java b/MPChartExample/src/com/xxmassdeveloper/mpchartexample/BarChartActivityMultiDataset.java index 652a314ff..398d00373 100644 --- a/MPChartExample/src/com/xxmassdeveloper/mpchartexample/BarChartActivityMultiDataset.java +++ b/MPChartExample/src/com/xxmassdeveloper/mpchartexample/BarChartActivityMultiDataset.java @@ -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; diff --git a/MPChartExample/src/com/xxmassdeveloper/mpchartexample/LineChartActivity1.java b/MPChartExample/src/com/xxmassdeveloper/mpchartexample/LineChartActivity1.java index 3a36aa09d..2831c8971 100644 --- a/MPChartExample/src/com/xxmassdeveloper/mpchartexample/LineChartActivity1.java +++ b/MPChartExample/src/com/xxmassdeveloper/mpchartexample/LineChartActivity1.java @@ -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"); @@ -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); diff --git a/MPChartExample/src/com/xxmassdeveloper/mpchartexample/PieChartActivity.java b/MPChartExample/src/com/xxmassdeveloper/mpchartexample/PieChartActivity.java index 613426259..468f71e79 100644 --- a/MPChartExample/src/com/xxmassdeveloper/mpchartexample/PieChartActivity.java +++ b/MPChartExample/src/com/xxmassdeveloper/mpchartexample/PieChartActivity.java @@ -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; diff --git a/MPChartExample/src/com/xxmassdeveloper/mpchartexample/StackedBarActivity.java b/MPChartExample/src/com/xxmassdeveloper/mpchartexample/StackedBarActivity.java index 4edc128f7..8ed71e49e 100644 --- a/MPChartExample/src/com/xxmassdeveloper/mpchartexample/StackedBarActivity.java +++ b/MPChartExample/src/com/xxmassdeveloper/mpchartexample/StackedBarActivity.java @@ -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 { @@ -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(); diff --git a/MPChartExample/src/com/xxmassdeveloper/mpchartexample/StackedBarActivityNegative.java b/MPChartExample/src/com/xxmassdeveloper/mpchartexample/StackedBarActivityNegative.java index c242a7f2f..89acfc268 100644 --- a/MPChartExample/src/com/xxmassdeveloper/mpchartexample/StackedBarActivityNegative.java +++ b/MPChartExample/src/com/xxmassdeveloper/mpchartexample/StackedBarActivityNegative.java @@ -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; @@ -211,7 +213,7 @@ public void onNothingSelected() { } - private class CustomFormatter implements ValueFormatter { + private class CustomFormatter implements ValueFormatter, YAxisValueFormatter { private DecimalFormat mFormat; @@ -219,8 +221,15 @@ 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"; } } diff --git a/MPChartExample/src/com/xxmassdeveloper/mpchartexample/custom/MyCustomXValueFormatter.java b/MPChartExample/src/com/xxmassdeveloper/mpchartexample/custom/MyCustomXAxisValueFormatter.java similarity index 81% rename from MPChartExample/src/com/xxmassdeveloper/mpchartexample/custom/MyCustomXValueFormatter.java rename to MPChartExample/src/com/xxmassdeveloper/mpchartexample/custom/MyCustomXAxisValueFormatter.java index 5762fe4e3..bdf918b1b 100644 --- a/MPChartExample/src/com/xxmassdeveloper/mpchartexample/custom/MyCustomXValueFormatter.java +++ b/MPChartExample/src/com/xxmassdeveloper/mpchartexample/custom/MyCustomXAxisValueFormatter.java @@ -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 } diff --git a/MPChartExample/src/com/xxmassdeveloper/mpchartexample/custom/MyFillFormatter.java b/MPChartExample/src/com/xxmassdeveloper/mpchartexample/custom/MyFillFormatter.java index 443e4a075..de96237cc 100644 --- a/MPChartExample/src/com/xxmassdeveloper/mpchartexample/custom/MyFillFormatter.java +++ b/MPChartExample/src/com/xxmassdeveloper/mpchartexample/custom/MyFillFormatter.java @@ -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. diff --git a/MPChartExample/src/com/xxmassdeveloper/mpchartexample/custom/MyValueFormatter.java b/MPChartExample/src/com/xxmassdeveloper/mpchartexample/custom/MyValueFormatter.java index c71b1ac1b..a374b60e7 100644 --- a/MPChartExample/src/com/xxmassdeveloper/mpchartexample/custom/MyValueFormatter.java +++ b/MPChartExample/src/com/xxmassdeveloper/mpchartexample/custom/MyValueFormatter.java @@ -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; @@ -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) + " $"; } - } diff --git a/MPChartExample/src/com/xxmassdeveloper/mpchartexample/custom/MyYAxisValueFormatter.java b/MPChartExample/src/com/xxmassdeveloper/mpchartexample/custom/MyYAxisValueFormatter.java new file mode 100644 index 000000000..cae82be1d --- /dev/null +++ b/MPChartExample/src/com/xxmassdeveloper/mpchartexample/custom/MyYAxisValueFormatter.java @@ -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) + " $"; + } +} diff --git a/MPChartExample/src/com/xxmassdeveloper/mpchartexample/listviewitems/PieChartItem.java b/MPChartExample/src/com/xxmassdeveloper/mpchartexample/listviewitems/PieChartItem.java index 82bd6d103..a8e02c325 100644 --- a/MPChartExample/src/com/xxmassdeveloper/mpchartexample/listviewitems/PieChartItem.java +++ b/MPChartExample/src/com/xxmassdeveloper/mpchartexample/listviewitems/PieChartItem.java @@ -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 { diff --git a/MPChartLib/src/com/github/mikephil/charting/charts/BarLineChartBase.java b/MPChartLib/src/com/github/mikephil/charting/charts/BarLineChartBase.java index 7ee2a07fd..1fc2ec2ea 100644 --- a/MPChartLib/src/com/github/mikephil/charting/charts/BarLineChartBase.java +++ b/MPChartLib/src/com/github/mikephil/charting/charts/BarLineChartBase.java @@ -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); diff --git a/MPChartLib/src/com/github/mikephil/charting/charts/Chart.java b/MPChartLib/src/com/github/mikephil/charting/charts/Chart.java index 7b61ead63..f2c86c516 100644 --- a/MPChartLib/src/com/github/mikephil/charting/charts/Chart.java +++ b/MPChartLib/src/com/github/mikephil/charting/charts/Chart.java @@ -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; diff --git a/MPChartLib/src/com/github/mikephil/charting/charts/CombinedChart.java b/MPChartLib/src/com/github/mikephil/charting/charts/CombinedChart.java index 8a82016d8..9c4be80cf 100644 --- a/MPChartLib/src/com/github/mikephil/charting/charts/CombinedChart.java +++ b/MPChartLib/src/com/github/mikephil/charting/charts/CombinedChart.java @@ -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 diff --git a/MPChartLib/src/com/github/mikephil/charting/charts/LineChart.java b/MPChartLib/src/com/github/mikephil/charting/charts/LineChart.java index d4f577b8a..e04859fe5 100644 --- a/MPChartLib/src/com/github/mikephil/charting/charts/LineChart.java +++ b/MPChartLib/src/com/github/mikephil/charting/charts/LineChart.java @@ -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, ... diff --git a/MPChartLib/src/com/github/mikephil/charting/charts/RadarChart.java b/MPChartLib/src/com/github/mikephil/charting/charts/RadarChart.java index c4873b6bf..961a0a047 100644 --- a/MPChartLib/src/com/github/mikephil/charting/charts/RadarChart.java +++ b/MPChartLib/src/com/github/mikephil/charting/charts/RadarChart.java @@ -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()); diff --git a/MPChartLib/src/com/github/mikephil/charting/components/XAxis.java b/MPChartLib/src/com/github/mikephil/charting/components/XAxis.java index 76e1cbacd..3db471710 100644 --- a/MPChartLib/src/com/github/mikephil/charting/components/XAxis.java +++ b/MPChartLib/src/com/github/mikephil/charting/components/XAxis.java @@ -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; @@ -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; @@ -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; @@ -191,25 +191,25 @@ public List 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 diff --git a/MPChartLib/src/com/github/mikephil/charting/components/YAxis.java b/MPChartLib/src/com/github/mikephil/charting/components/YAxis.java index 12a93559c..0c2365e4e 100644 --- a/MPChartLib/src/com/github/mikephil/charting/components/YAxis.java +++ b/MPChartLib/src/com/github/mikephil/charting/components/YAxis.java @@ -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 @@ -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[] {}; @@ -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; diff --git a/MPChartLib/src/com/github/mikephil/charting/data/ChartData.java b/MPChartLib/src/com/github/mikephil/charting/data/ChartData.java index c44388650..ff60bbfde 100644 --- a/MPChartLib/src/com/github/mikephil/charting/data/ChartData.java +++ b/MPChartLib/src/com/github/mikephil/charting/data/ChartData.java @@ -6,7 +6,7 @@ import com.github.mikephil.charting.components.YAxis.AxisDependency; import com.github.mikephil.charting.highlight.Highlight; -import com.github.mikephil.charting.utils.ValueFormatter; +import com.github.mikephil.charting.formatter.ValueFormatter; import java.util.ArrayList; import java.util.Arrays; diff --git a/MPChartLib/src/com/github/mikephil/charting/data/DataSet.java b/MPChartLib/src/com/github/mikephil/charting/data/DataSet.java index 3726b6d49..c3fff6caf 100644 --- a/MPChartLib/src/com/github/mikephil/charting/data/DataSet.java +++ b/MPChartLib/src/com/github/mikephil/charting/data/DataSet.java @@ -7,9 +7,9 @@ import com.github.mikephil.charting.components.YAxis.AxisDependency; import com.github.mikephil.charting.utils.ColorTemplate; -import com.github.mikephil.charting.utils.DefaultValueFormatter; +import com.github.mikephil.charting.formatter.DefaultValueFormatter; import com.github.mikephil.charting.utils.Utils; -import com.github.mikephil.charting.utils.ValueFormatter; +import com.github.mikephil.charting.formatter.ValueFormatter; import java.util.ArrayList; import java.util.List; diff --git a/MPChartLib/src/com/github/mikephil/charting/data/LineDataSet.java b/MPChartLib/src/com/github/mikephil/charting/data/LineDataSet.java index c0faad07c..fdbd15d6a 100644 --- a/MPChartLib/src/com/github/mikephil/charting/data/LineDataSet.java +++ b/MPChartLib/src/com/github/mikephil/charting/data/LineDataSet.java @@ -5,10 +5,9 @@ import android.graphics.Color; import android.graphics.DashPathEffect; -import com.github.mikephil.charting.charts.BarLineChartBase; import com.github.mikephil.charting.utils.ColorTemplate; -import com.github.mikephil.charting.utils.DefaultFillFormatter; -import com.github.mikephil.charting.utils.FillFormatter; +import com.github.mikephil.charting.formatter.DefaultFillFormatter; +import com.github.mikephil.charting.formatter.FillFormatter; import com.github.mikephil.charting.utils.Utils; import java.util.ArrayList; diff --git a/MPChartLib/src/com/github/mikephil/charting/utils/ColorFormatter.java b/MPChartLib/src/com/github/mikephil/charting/formatter/ColorFormatter.java similarity index 86% rename from MPChartLib/src/com/github/mikephil/charting/utils/ColorFormatter.java rename to MPChartLib/src/com/github/mikephil/charting/formatter/ColorFormatter.java index 81da6ae08..3a7d119e6 100644 --- a/MPChartLib/src/com/github/mikephil/charting/utils/ColorFormatter.java +++ b/MPChartLib/src/com/github/mikephil/charting/formatter/ColorFormatter.java @@ -1,4 +1,4 @@ -package com.github.mikephil.charting.utils; +package com.github.mikephil.charting.formatter; import com.github.mikephil.charting.data.Entry; diff --git a/MPChartLib/src/com/github/mikephil/charting/utils/DefaultFillFormatter.java b/MPChartLib/src/com/github/mikephil/charting/formatter/DefaultFillFormatter.java similarity index 92% rename from MPChartLib/src/com/github/mikephil/charting/utils/DefaultFillFormatter.java rename to MPChartLib/src/com/github/mikephil/charting/formatter/DefaultFillFormatter.java index c2ac68214..20ba8fdda 100644 --- a/MPChartLib/src/com/github/mikephil/charting/utils/DefaultFillFormatter.java +++ b/MPChartLib/src/com/github/mikephil/charting/formatter/DefaultFillFormatter.java @@ -1,7 +1,6 @@ -package com.github.mikephil.charting.utils; +package com.github.mikephil.charting.formatter; -import com.github.mikephil.charting.components.YAxis; import com.github.mikephil.charting.data.LineData; import com.github.mikephil.charting.data.LineDataSet; import com.github.mikephil.charting.interfaces.LineDataProvider; diff --git a/MPChartLib/src/com/github/mikephil/charting/utils/DefaultValueFormatter.java b/MPChartLib/src/com/github/mikephil/charting/formatter/DefaultValueFormatter.java similarity index 62% rename from MPChartLib/src/com/github/mikephil/charting/utils/DefaultValueFormatter.java rename to MPChartLib/src/com/github/mikephil/charting/formatter/DefaultValueFormatter.java index f63c77edc..bd66f51d0 100644 --- a/MPChartLib/src/com/github/mikephil/charting/utils/DefaultValueFormatter.java +++ b/MPChartLib/src/com/github/mikephil/charting/formatter/DefaultValueFormatter.java @@ -1,10 +1,13 @@ -package com.github.mikephil.charting.utils; +package com.github.mikephil.charting.formatter; + +import com.github.mikephil.charting.data.Entry; +import com.github.mikephil.charting.utils.ViewPortHandler; import java.text.DecimalFormat; /** - * Default formatter used for formatting values. Uses a DecimalFormat with + * Default formatter used for formatting values inside the chart. Uses a DecimalFormat with * pre-calculated number of digits (depending on max and min value). * * @author Philipp Jahoda @@ -33,8 +36,11 @@ public DefaultValueFormatter(int digits) { } @Override - public String getFormattedValue(float value) { - // avoid memory allocations here (for performance) + public String getFormattedValue(float value, Entry entry, int dataSetIndex, ViewPortHandler viewPortHandler) { + + // put more logic here ... + // avoid memory allocations here (for performance reasons) + return mFormat.format(value); } } diff --git a/MPChartLib/src/com/github/mikephil/charting/utils/DefaultXValueFormatter.java b/MPChartLib/src/com/github/mikephil/charting/formatter/DefaultXAxisAxisValueFormatter.java similarity index 65% rename from MPChartLib/src/com/github/mikephil/charting/utils/DefaultXValueFormatter.java rename to MPChartLib/src/com/github/mikephil/charting/formatter/DefaultXAxisAxisValueFormatter.java index e284b027a..4a30df6ff 100644 --- a/MPChartLib/src/com/github/mikephil/charting/utils/DefaultXValueFormatter.java +++ b/MPChartLib/src/com/github/mikephil/charting/formatter/DefaultXAxisAxisValueFormatter.java @@ -1,11 +1,13 @@ -package com.github.mikephil.charting.utils; +package com.github.mikephil.charting.formatter; + +import com.github.mikephil.charting.utils.ViewPortHandler; /** * Created by Philipp Jahoda on 14/09/15. * Default formatter class for adjusting x-values before drawing them. * This simply returns the original value unmodified. */ -public class DefaultXValueFormatter implements XValueFormatter { +public class DefaultXAxisAxisValueFormatter implements XAxisValueFormatter { @Override public String getXValue(String original, int index, ViewPortHandler viewPortHandler) { diff --git a/MPChartLib/src/com/github/mikephil/charting/formatter/DefaultYAxisValueFormatter.java b/MPChartLib/src/com/github/mikephil/charting/formatter/DefaultYAxisValueFormatter.java new file mode 100644 index 000000000..5b8cd0145 --- /dev/null +++ b/MPChartLib/src/com/github/mikephil/charting/formatter/DefaultYAxisValueFormatter.java @@ -0,0 +1,40 @@ +package com.github.mikephil.charting.formatter; + +import com.github.mikephil.charting.components.YAxis; + +import java.text.DecimalFormat; + +/** + * Created by Philipp Jahoda on 20/09/15. + * Default formatter used for formatting labels of the YAxis. Uses a DecimalFormat with + * pre-calculated number of digits (depending on max and min value). + */ +public class DefaultYAxisValueFormatter implements YAxisValueFormatter { + + /** decimalformat for formatting */ + private DecimalFormat mFormat; + + /** + * Constructor that specifies to how many digits the value should be + * formatted. + * + * @param digits + */ + public DefaultYAxisValueFormatter(int digits) { + + StringBuffer b = new StringBuffer(); + for (int i = 0; i < digits; i++) { + if (i == 0) + b.append("."); + b.append("0"); + } + + mFormat = new DecimalFormat("###,###,###,##0" + b.toString()); + } + + @Override + public String getFormattedValue(float value, YAxis yAxis) { + // avoid memory allocations here (for performance) + return mFormat.format(value); + } +} diff --git a/MPChartLib/src/com/github/mikephil/charting/utils/FillFormatter.java b/MPChartLib/src/com/github/mikephil/charting/formatter/FillFormatter.java similarity index 93% rename from MPChartLib/src/com/github/mikephil/charting/utils/FillFormatter.java rename to MPChartLib/src/com/github/mikephil/charting/formatter/FillFormatter.java index b8fe50bef..424874a19 100644 --- a/MPChartLib/src/com/github/mikephil/charting/utils/FillFormatter.java +++ b/MPChartLib/src/com/github/mikephil/charting/formatter/FillFormatter.java @@ -1,4 +1,4 @@ -package com.github.mikephil.charting.utils; +package com.github.mikephil.charting.formatter; import com.github.mikephil.charting.data.LineData; import com.github.mikephil.charting.data.LineDataSet; diff --git a/MPChartLib/src/com/github/mikephil/charting/utils/LargeValueFormatter.java b/MPChartLib/src/com/github/mikephil/charting/formatter/LargeValueFormatter.java similarity index 71% rename from MPChartLib/src/com/github/mikephil/charting/utils/LargeValueFormatter.java rename to MPChartLib/src/com/github/mikephil/charting/formatter/LargeValueFormatter.java index 119c67158..74b95b7d5 100644 --- a/MPChartLib/src/com/github/mikephil/charting/utils/LargeValueFormatter.java +++ b/MPChartLib/src/com/github/mikephil/charting/formatter/LargeValueFormatter.java @@ -1,5 +1,9 @@ -package com.github.mikephil.charting.utils; +package com.github.mikephil.charting.formatter; + +import com.github.mikephil.charting.components.YAxis; +import com.github.mikephil.charting.data.Entry; +import com.github.mikephil.charting.utils.ViewPortHandler; import java.text.DecimalFormat; @@ -13,7 +17,7 @@ * @author Philipp Jahoda * @author Oleksandr Tyshkovets */ -public class LargeValueFormatter implements ValueFormatter { +public class LargeValueFormatter implements ValueFormatter, YAxisValueFormatter { private static final String[] SUFFIX = new String[] { "", "k", "m", "b", "t" @@ -29,15 +33,22 @@ public LargeValueFormatter() { /** * Creates a formatter that appends a specified text to the result string - * @param text a text that will be appended + * @param appendix a text that will be appended */ public LargeValueFormatter(String appendix) { this(); mText = appendix; } + // ValueFormatter + @Override + public String getFormattedValue(float value, Entry entry, int dataSetIndex, ViewPortHandler viewPortHandler) { + return makePretty(value) + mText; + } + + // YAxisValueFormatter @Override - public String getFormattedValue(float value) { + public String getFormattedValue(float value, YAxis yAxis) { return makePretty(value) + mText; } diff --git a/MPChartLib/src/com/github/mikephil/charting/formatter/PercentFormatter.java b/MPChartLib/src/com/github/mikephil/charting/formatter/PercentFormatter.java new file mode 100644 index 000000000..01011ce59 --- /dev/null +++ b/MPChartLib/src/com/github/mikephil/charting/formatter/PercentFormatter.java @@ -0,0 +1,35 @@ + +package com.github.mikephil.charting.formatter; + +import com.github.mikephil.charting.components.YAxis; +import com.github.mikephil.charting.data.Entry; +import com.github.mikephil.charting.utils.ViewPortHandler; + +import java.text.DecimalFormat; + +/** + * This ValueFormatter is just for convenience and simply puts a "%" sign after + * each value. (Recommeded for PieChart) + * + * @author Philipp Jahoda + */ +public class PercentFormatter implements ValueFormatter, YAxisValueFormatter { + + protected DecimalFormat mFormat; + + public PercentFormatter() { + mFormat = new DecimalFormat("###,###,##0.0"); + } + + // ValueFormatter + @Override + public String getFormattedValue(float value, Entry entry, int dataSetIndex, ViewPortHandler viewPortHandler) { + return mFormat.format(value) + " %"; + } + + // YAxisValueFormatter + @Override + public String getFormattedValue(float value, YAxis yAxis) { + return mFormat.format(value) + " %"; + } +} diff --git a/MPChartLib/src/com/github/mikephil/charting/formatter/ValueFormatter.java b/MPChartLib/src/com/github/mikephil/charting/formatter/ValueFormatter.java new file mode 100644 index 000000000..1f056bf78 --- /dev/null +++ b/MPChartLib/src/com/github/mikephil/charting/formatter/ValueFormatter.java @@ -0,0 +1,28 @@ +package com.github.mikephil.charting.formatter; + +import com.github.mikephil.charting.data.Entry; +import com.github.mikephil.charting.utils.ViewPortHandler; + +/** + * Interface that allows custom formatting of all values inside the chart before they are + * being drawn to the screen. Simply create your own formatting class and let + * it implement ValueFormatter. Then override the getFormattedValue(...) method + * and return whatever you want. + * + * @author Philipp Jahoda + */ +public interface ValueFormatter { + + /** + * Called when a value (from labels inside the chart) is formatted + * before being drawn. For performance reasons, avoid excessive calculations + * and memory allocations inside this method. + * + * @param value the value to be formatted + * @param entry the entry the value belongs to - in e.g. BarChart, this is of class BarEntry + * @param dataSetIndex the index of the DataSet the entry in focus belongs to + * @param viewPortHandler provides information about the current chart state (scale, translation, ...) + * @return the formatted label ready for being drawn + */ + String getFormattedValue(float value, Entry entry, int dataSetIndex, ViewPortHandler viewPortHandler); +} diff --git a/MPChartLib/src/com/github/mikephil/charting/utils/XValueFormatter.java b/MPChartLib/src/com/github/mikephil/charting/formatter/XAxisValueFormatter.java similarity index 68% rename from MPChartLib/src/com/github/mikephil/charting/utils/XValueFormatter.java rename to MPChartLib/src/com/github/mikephil/charting/formatter/XAxisValueFormatter.java index 67d835cf5..1dcb72b04 100644 --- a/MPChartLib/src/com/github/mikephil/charting/utils/XValueFormatter.java +++ b/MPChartLib/src/com/github/mikephil/charting/formatter/XAxisValueFormatter.java @@ -1,4 +1,6 @@ -package com.github.mikephil.charting.utils; +package com.github.mikephil.charting.formatter; + +import com.github.mikephil.charting.utils.ViewPortHandler; /** * Created by Philipp Jahoda on 14/09/15. @@ -6,10 +8,12 @@ * * @author Philipp Jahoda */ -public interface XValueFormatter { +public interface XAxisValueFormatter { /** * Returns the customized label that is drawn on the x-axis. + * For performance reasons, avoid excessive calculations + * and memory allocations inside this method. * * @param original the original x-axis label to be drawn * @param index the x-index that is currently being drawn diff --git a/MPChartLib/src/com/github/mikephil/charting/formatter/YAxisValueFormatter.java b/MPChartLib/src/com/github/mikephil/charting/formatter/YAxisValueFormatter.java new file mode 100644 index 000000000..4bc6c3e98 --- /dev/null +++ b/MPChartLib/src/com/github/mikephil/charting/formatter/YAxisValueFormatter.java @@ -0,0 +1,22 @@ +package com.github.mikephil.charting.formatter; + +import com.github.mikephil.charting.components.YAxis; + +/** + * Created by Philipp Jahoda on 20/09/15. + * Custom formatter interface that allows formatting of + * YAxis labels before they are being drawn. + */ +public interface YAxisValueFormatter { + + /** + * Called when a value from the YAxis is formatted + * before being drawn. For performance reasons, avoid excessive calculations + * and memory allocations inside this method. + * + * @param value the YAxis value to be formatted + * @param yAxis the YAxis object the value belongs to + * @return + */ + String getFormattedValue(float value, YAxis yAxis); +} diff --git a/MPChartLib/src/com/github/mikephil/charting/interfaces/ChartInterface.java b/MPChartLib/src/com/github/mikephil/charting/interfaces/ChartInterface.java index 9c4d0628d..216e9a6ce 100644 --- a/MPChartLib/src/com/github/mikephil/charting/interfaces/ChartInterface.java +++ b/MPChartLib/src/com/github/mikephil/charting/interfaces/ChartInterface.java @@ -4,7 +4,7 @@ import android.graphics.RectF; import com.github.mikephil.charting.data.ChartData; -import com.github.mikephil.charting.utils.ValueFormatter; +import com.github.mikephil.charting.formatter.ValueFormatter; /** * Interface that provides everything there is to know about the dimensions, diff --git a/MPChartLib/src/com/github/mikephil/charting/interfaces/LineDataProvider.java b/MPChartLib/src/com/github/mikephil/charting/interfaces/LineDataProvider.java index 92d3d2b66..ef4866ef7 100644 --- a/MPChartLib/src/com/github/mikephil/charting/interfaces/LineDataProvider.java +++ b/MPChartLib/src/com/github/mikephil/charting/interfaces/LineDataProvider.java @@ -1,9 +1,7 @@ package com.github.mikephil.charting.interfaces; -import com.github.mikephil.charting.components.AxisBase; import com.github.mikephil.charting.components.YAxis; import com.github.mikephil.charting.data.LineData; -import com.github.mikephil.charting.utils.FillFormatter; public interface LineDataProvider extends BarLineScatterCandleBubbleDataProvider { diff --git a/MPChartLib/src/com/github/mikephil/charting/renderer/BarChartRenderer.java b/MPChartLib/src/com/github/mikephil/charting/renderer/BarChartRenderer.java index 5324ef112..1f55d17d8 100644 --- a/MPChartLib/src/com/github/mikephil/charting/renderer/BarChartRenderer.java +++ b/MPChartLib/src/com/github/mikephil/charting/renderer/BarChartRenderer.java @@ -17,7 +17,6 @@ import com.github.mikephil.charting.highlight.Highlight; import com.github.mikephil.charting.utils.Transformer; import com.github.mikephil.charting.utils.Utils; -import com.github.mikephil.charting.utils.ValueFormatter; import com.github.mikephil.charting.utils.ViewPortHandler; import java.util.List; @@ -206,8 +205,6 @@ public void drawValues(Canvas c) { negOffset = -negOffset - valueTextHeight; } - ValueFormatter formatter = dataSet.getValueFormatter(); - Transformer trans = mChart.getTransformer(dataSet.getAxisDependency()); List entries = dataSet.getYVals(); @@ -226,9 +223,10 @@ public void drawValues(Canvas c) { || !mViewPortHandler.isInBoundsLeft(valuePoints[j])) continue; - float val = entries.get(j / 2).getVal(); + BarEntry entry = entries.get(j / 2); + float val = entry.getVal(); - drawValue(c, formatter.getFormattedValue(val), valuePoints[j], + drawValue(c, dataSet.getValueFormatter(), val, entry, i, valuePoints[j], valuePoints[j + 1] + (val >= 0 ? posOffset : negOffset)); } @@ -237,9 +235,9 @@ public void drawValues(Canvas c) { for (int j = 0; j < (valuePoints.length - 1) * mAnimator.getPhaseX(); j += 2) { - BarEntry e = entries.get(j / 2); + BarEntry entry = entries.get(j / 2); - float[] vals = e.getVals(); + float[] vals = entry.getVals(); // we still draw stacked bars, but there is one // non-stacked @@ -253,8 +251,8 @@ public void drawValues(Canvas c) { || !mViewPortHandler.isInBoundsLeft(valuePoints[j])) continue; - drawValue(c, formatter.getFormattedValue(e.getVal()), valuePoints[j], - valuePoints[j + 1] + (e.getVal() >= 0 ? posOffset : negOffset)); + drawValue(c, dataSet.getValueFormatter(), entry.getVal(), entry, i, valuePoints[j], + valuePoints[j + 1] + (entry.getVal() >= 0 ? posOffset : negOffset)); // draw stack values } else { @@ -262,7 +260,7 @@ public void drawValues(Canvas c) { float[] transformed = new float[vals.length * 2]; float posY = 0f; - float negY = -e.getNegativeSum(); + float negY = -entry.getNegativeSum(); for (int k = 0, idx = 0; k < transformed.length; k += 2, idx++) { @@ -295,7 +293,7 @@ public void drawValues(Canvas c) { || !mViewPortHandler.isInBoundsLeft(x)) continue; - drawValue(c, formatter.getFormattedValue(vals[k / 2]), x, y); + drawValue(c, dataSet.getValueFormatter(), vals[k / 2], entry, i, x, y); } } } @@ -304,19 +302,6 @@ public void drawValues(Canvas c) { } } - /** - * Draws a value at the specified x and y position. - * - * @param value - * @param xPos - * @param yPos - * @formatter - */ - protected void drawValue(Canvas c, String value, float xPos, float yPos) { - c.drawText(value, xPos, yPos, - mValuePaint); - } - @Override public void drawHighlighted(Canvas c, Highlight[] indices) { diff --git a/MPChartLib/src/com/github/mikephil/charting/renderer/BubbleChartRenderer.java b/MPChartLib/src/com/github/mikephil/charting/renderer/BubbleChartRenderer.java index bf198eeb5..dae2f1a7d 100644 --- a/MPChartLib/src/com/github/mikephil/charting/renderer/BubbleChartRenderer.java +++ b/MPChartLib/src/com/github/mikephil/charting/renderer/BubbleChartRenderer.java @@ -131,7 +131,9 @@ public void drawValues(Canvas c) { float lineHeight = Utils.calcTextHeight(mValuePaint, "1"); - for (BubbleDataSet dataSet : dataSets) { + for (int i = 0; i < dataSets.size(); i++) { + + BubbleDataSet dataSet = dataSets.get(i); if (!dataSet.isDrawValuesEnabled() || dataSet.getEntryCount() == 0) continue; @@ -171,14 +173,10 @@ public void drawValues(Canvas c) { if ((!mViewPortHandler.isInBoundsLeft(x) || !mViewPortHandler.isInBoundsY(y))) continue; - final BubbleEntry entry = entries.get(j / 2 + minx); - - final float val = entry.getSize(); + BubbleEntry entry = entries.get(j / 2 + minx); - c.drawText(dataSet.getValueFormatter().getFormattedValue(val), - x, - y + (0.5f * lineHeight), - mValuePaint); + drawValue(c, dataSet.getValueFormatter(), entry.getSize(), entry, i, x, + y + (0.5f * lineHeight)); } } } diff --git a/MPChartLib/src/com/github/mikephil/charting/renderer/CandleStickChartRenderer.java b/MPChartLib/src/com/github/mikephil/charting/renderer/CandleStickChartRenderer.java index 67545ccae..460f98397 100644 --- a/MPChartLib/src/com/github/mikephil/charting/renderer/CandleStickChartRenderer.java +++ b/MPChartLib/src/com/github/mikephil/charting/renderer/CandleStickChartRenderer.java @@ -259,10 +259,9 @@ public void drawValues(Canvas c) { if (!mViewPortHandler.isInBoundsLeft(x) || !mViewPortHandler.isInBoundsY(y)) continue; - float val = entries.get(j / 2 + minx).getHigh(); + CandleEntry entry = entries.get(j / 2 + minx); - c.drawText(dataSet.getValueFormatter().getFormattedValue(val), x, y - yOffset, - mValuePaint); + drawValue(c, dataSet.getValueFormatter(), entry.getHigh(), entry, i, x, y - yOffset); } } } diff --git a/MPChartLib/src/com/github/mikephil/charting/renderer/DataRenderer.java b/MPChartLib/src/com/github/mikephil/charting/renderer/DataRenderer.java index 42a4fbaef..87daddac4 100644 --- a/MPChartLib/src/com/github/mikephil/charting/renderer/DataRenderer.java +++ b/MPChartLib/src/com/github/mikephil/charting/renderer/DataRenderer.java @@ -9,8 +9,10 @@ import com.github.mikephil.charting.animation.ChartAnimator; import com.github.mikephil.charting.data.DataSet; +import com.github.mikephil.charting.data.Entry; import com.github.mikephil.charting.highlight.Highlight; import com.github.mikephil.charting.utils.Utils; +import com.github.mikephil.charting.formatter.ValueFormatter; import com.github.mikephil.charting.utils.ViewPortHandler; /** @@ -112,10 +114,40 @@ protected void applyValueTextStyle(DataSet set) { */ public abstract void initBuffers(); + /** + * Draws the actual data in form of lines, bars, ... depending on Renderer subclass. + * + * @param c + */ public abstract void drawData(Canvas c); + /** + * Loops over all Entrys and draws their values. + * + * @param c + */ public abstract void drawValues(Canvas c); + /** + * Draws the value of the given entry by using the provided ValueFormatter. + * + * @param c canvas + * @param formatter formatter for custom value-formatting + * @param value the value to be drawn + * @param entry the entry the value belongs to + * @param dataSetIndex the index of the DataSet the drawn Entry belongs to + * @param x position + * @param y position + */ + public void drawValue(Canvas c, ValueFormatter formatter, float value, Entry entry, int dataSetIndex, float x, float y) { + c.drawText(formatter.getFormattedValue(value, entry, dataSetIndex, mViewPortHandler), x, y, mValuePaint); + } + + /** + * Draws any kind of additional information (e.g. line-circles). + * + * @param c + */ public abstract void drawExtras(Canvas c); /** diff --git a/MPChartLib/src/com/github/mikephil/charting/renderer/HorizontalBarChartRenderer.java b/MPChartLib/src/com/github/mikephil/charting/renderer/HorizontalBarChartRenderer.java index 25c902853..850464bea 100644 --- a/MPChartLib/src/com/github/mikephil/charting/renderer/HorizontalBarChartRenderer.java +++ b/MPChartLib/src/com/github/mikephil/charting/renderer/HorizontalBarChartRenderer.java @@ -13,7 +13,7 @@ import com.github.mikephil.charting.interfaces.BarDataProvider; import com.github.mikephil.charting.utils.Transformer; 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.util.List; @@ -138,11 +138,12 @@ public void drawValues(Canvas c) { if (!mViewPortHandler.isInBoundsBottom(valuePoints[j + 1])) continue; - float val = entries.get(j / 2).getVal(); - String valueText = formatter.getFormattedValue(val); + BarEntry e = entries.get(j / 2); + float val = e.getVal(); + String formattedValue = formatter.getFormattedValue(val, e, i, mViewPortHandler); // calculate the correct offset depending on the draw position of the value - float valueTextWidth = Utils.calcTextWidth(mValuePaint, valueText); + float valueTextWidth = Utils.calcTextWidth(mValuePaint, formattedValue); posOffset = (drawValueAboveBar ? valueOffsetPlus : -(valueTextWidth + valueOffsetPlus)); negOffset = (drawValueAboveBar ? -(valueTextWidth + valueOffsetPlus) : valueOffsetPlus); @@ -151,7 +152,7 @@ public void drawValues(Canvas c) { negOffset = -negOffset - valueTextWidth; } - drawValue(c, valueText, valuePoints[j] + (val >= 0 ? posOffset : negOffset), + drawValue(c, formattedValue, valuePoints[j] + (val >= 0 ? posOffset : negOffset), valuePoints[j + 1] + halfTextHeight); } @@ -179,10 +180,10 @@ public void drawValues(Canvas c) { continue; float val = e.getVal(); - String valueText = formatter.getFormattedValue(val); + String formattedValue = formatter.getFormattedValue(val, e, i, mViewPortHandler); // calculate the correct offset depending on the draw position of the value - float valueTextWidth = Utils.calcTextWidth(mValuePaint, valueText); + float valueTextWidth = Utils.calcTextWidth(mValuePaint, formattedValue); posOffset = (drawValueAboveBar ? valueOffsetPlus : -(valueTextWidth + valueOffsetPlus)); negOffset = (drawValueAboveBar ? -(valueTextWidth + valueOffsetPlus) : valueOffsetPlus); @@ -191,7 +192,7 @@ public void drawValues(Canvas c) { negOffset = -negOffset - valueTextWidth; } - drawValue(c, valueText, valuePoints[j] + drawValue(c, formattedValue, valuePoints[j] + (e.getVal() >= 0 ? posOffset : negOffset), valuePoints[j + 1] + halfTextHeight); @@ -223,10 +224,10 @@ public void drawValues(Canvas c) { for (int k = 0; k < transformed.length; k += 2) { float val = vals[k / 2]; - String valueText = formatter.getFormattedValue(val); + String formattedValue = formatter.getFormattedValue(val, e, i, mViewPortHandler); // calculate the correct offset depending on the draw position of the value - float valueTextWidth = Utils.calcTextWidth(mValuePaint, valueText); + float valueTextWidth = Utils.calcTextWidth(mValuePaint, formattedValue); posOffset = (drawValueAboveBar ? valueOffsetPlus : -(valueTextWidth + valueOffsetPlus)); negOffset = (drawValueAboveBar ? -(valueTextWidth + valueOffsetPlus) : valueOffsetPlus); @@ -248,7 +249,7 @@ public void drawValues(Canvas c) { if (!mViewPortHandler.isInBoundsBottom(y)) continue; - drawValue(c, valueText, x, y + halfTextHeight); + drawValue(c, formattedValue, x, y + halfTextHeight); } } } @@ -257,6 +258,10 @@ public void drawValues(Canvas c) { } } + protected void drawValue(Canvas c, String valueText, float x, float y) { + c.drawText(valueText, x, y, mValuePaint); + } + @Override protected void prepareBarHighlight(float x, float y1, float y2, float barspaceHalf, Transformer trans) { diff --git a/MPChartLib/src/com/github/mikephil/charting/renderer/LineChartRenderer.java b/MPChartLib/src/com/github/mikephil/charting/renderer/LineChartRenderer.java index 1d80c4392..186b925d3 100644 --- a/MPChartLib/src/com/github/mikephil/charting/renderer/LineChartRenderer.java +++ b/MPChartLib/src/com/github/mikephil/charting/renderer/LineChartRenderer.java @@ -444,11 +444,10 @@ public void drawValues(Canvas c) { if (!mViewPortHandler.isInBoundsLeft(x) || !mViewPortHandler.isInBoundsY(y)) continue; - float val = entries.get(j / 2 + minx).getVal(); + Entry entry = entries.get(j / 2 + minx); - c.drawText(dataSet.getValueFormatter().getFormattedValue(val), x, - y - valOffset, - mValuePaint); + drawValue(c, dataSet.getValueFormatter(), entry.getVal(), entry, i, x, + y - valOffset); } } } diff --git a/MPChartLib/src/com/github/mikephil/charting/renderer/PieChartRenderer.java b/MPChartLib/src/com/github/mikephil/charting/renderer/PieChartRenderer.java index 4ed06cccb..5766bd337 100644 --- a/MPChartLib/src/com/github/mikephil/charting/renderer/PieChartRenderer.java +++ b/MPChartLib/src/com/github/mikephil/charting/renderer/PieChartRenderer.java @@ -20,6 +20,7 @@ import com.github.mikephil.charting.data.PieDataSet; import com.github.mikephil.charting.highlight.Highlight; import com.github.mikephil.charting.utils.Utils; +import com.github.mikephil.charting.formatter.ValueFormatter; import com.github.mikephil.charting.utils.ViewPortHandler; import java.util.List; @@ -189,36 +190,39 @@ public void drawValues(Canvas c) { // apply the text-styling defined by the DataSet applyValueTextStyle(dataSet); + float lineHeight = Utils.calcTextHeight(mValuePaint, "Q") + + Utils.convertDpToPixel(4f); + List entries = dataSet.getYVals(); for (int j = 0, maxEntry = Math.min( (int) Math.ceil(entries.size() * mAnimator.getPhaseX()), entries.size()); j < maxEntry; j++) { + Entry entry = entries.get(j); + // offset needed to center the drawn text in the slice float offset = drawAngles[cnt] / 2; // calculate the text position float x = (float) (r * Math.cos(Math.toRadians((rotationAngle + absoluteAngles[cnt] - offset) - * mAnimator.getPhaseY())) + center.x); + * mAnimator.getPhaseY())) + center.x); float y = (float) (r * Math.sin(Math.toRadians((rotationAngle + absoluteAngles[cnt] - offset) * mAnimator.getPhaseY())) + center.y); - float value = mChart.isUsePercentValuesEnabled() ? entries.get(j).getVal() - / data.getYValueSum() * 100f : entries.get(j).getVal(); + float value = mChart.isUsePercentValuesEnabled() ? entry.getVal() + / data.getYValueSum() * 100f : entry.getVal(); - String val = dataSet.getValueFormatter().getFormattedValue(value); - - float lineHeight = Utils.calcTextHeight(mValuePaint, val) - + Utils.convertDpToPixel(4f); + ValueFormatter formatter = dataSet.getValueFormatter(); boolean drawYVals = dataSet.isDrawValuesEnabled(); // draw everything, depending on settings if (drawXVals && drawYVals) { - c.drawText(val, x, y, mValuePaint); + drawValue(c, formatter, value, entry, 0, x, y); + if (j < data.getXValCount()) c.drawText(data.getXVals().get(j), x, y + lineHeight, mValuePaint); @@ -228,7 +232,7 @@ public void drawValues(Canvas c) { c.drawText(data.getXVals().get(j), x, y + lineHeight / 2f, mValuePaint); } else if (!drawXVals && drawYVals) { - c.drawText(val, x, y + lineHeight / 2f, mValuePaint); + drawValue(c, formatter, value, entry, 0, x, y + lineHeight / 2f); } cnt++; diff --git a/MPChartLib/src/com/github/mikephil/charting/renderer/RadarChartRenderer.java b/MPChartLib/src/com/github/mikephil/charting/renderer/RadarChartRenderer.java index 57c68ce41..04a689372 100644 --- a/MPChartLib/src/com/github/mikephil/charting/renderer/RadarChartRenderer.java +++ b/MPChartLib/src/com/github/mikephil/charting/renderer/RadarChartRenderer.java @@ -22,11 +22,13 @@ public class RadarChartRenderer extends LineScatterCandleRadarRenderer { protected RadarChart mChart; - /** paint for drawing the web */ + /** + * paint for drawing the web + */ protected Paint mWebPaint; public RadarChartRenderer(RadarChart chart, ChartAnimator animator, - ViewPortHandler viewPortHandler) { + ViewPortHandler viewPortHandler) { super(animator, viewPortHandler); mChart = chart; @@ -46,9 +48,9 @@ public Paint getWebPaint() { @Override public void initBuffers() { // TODO Auto-generated method stub - + } - + @Override public void drawData(Canvas c) { @@ -141,13 +143,12 @@ public void drawValues(Canvas c) { for (int j = 0; j < entries.size(); j++) { - Entry e = entries.get(j); + Entry entry = entries.get(j); - PointF p = Utils.getPosition(center, (e.getVal() - mChart.getYChartMin()) * factor, + PointF p = Utils.getPosition(center, (entry.getVal() - mChart.getYChartMin()) * factor, sliceangle * j + mChart.getRotationAngle()); - c.drawText(dataSet.getValueFormatter().getFormattedValue(e.getVal()), - p.x, p.y - yoffset, mValuePaint); + drawValue(c, dataSet.getValueFormatter(), entry.getVal(), entry, i, p.x, p.y - yoffset); } } } @@ -235,7 +236,7 @@ public void drawHighlighted(Canvas c, Highlight[] indices) { PointF p = Utils.getPosition(center, y * factor, sliceangle * j + mChart.getRotationAngle()); - float[] pts = new float[] { + float[] pts = new float[]{ p.x, p.y }; diff --git a/MPChartLib/src/com/github/mikephil/charting/renderer/ScatterChartRenderer.java b/MPChartLib/src/com/github/mikephil/charting/renderer/ScatterChartRenderer.java index d23653163..a51c2aad9 100644 --- a/MPChartLib/src/com/github/mikephil/charting/renderer/ScatterChartRenderer.java +++ b/MPChartLib/src/com/github/mikephil/charting/renderer/ScatterChartRenderer.java @@ -233,11 +233,10 @@ public void drawValues(Canvas c) { || !mViewPortHandler.isInBoundsY(positions[j + 1]))) continue; - float val = entries.get(j / 2).getVal(); + Entry entry = entries.get(j / 2); - c.drawText(dataSet.getValueFormatter().getFormattedValue(val), positions[j], - positions[j + 1] - shapeSize, - mValuePaint); + drawValue(c, dataSet.getValueFormatter(), entry.getVal(), entry, i, positions[j], + positions[j + 1] - shapeSize); } } } diff --git a/MPChartLib/src/com/github/mikephil/charting/renderer/XAxisRenderer.java b/MPChartLib/src/com/github/mikephil/charting/renderer/XAxisRenderer.java index 1d1684a72..1f1c0d26f 100644 --- a/MPChartLib/src/com/github/mikephil/charting/renderer/XAxisRenderer.java +++ b/MPChartLib/src/com/github/mikephil/charting/renderer/XAxisRenderer.java @@ -156,7 +156,7 @@ protected void drawLabels(Canvas c, float pos) { } protected void drawLabel(Canvas c, String label, int xIndex, float x, float y) { - String formattedLabel = mXAxis.getXValueFormatter().getXValue(label, xIndex, mViewPortHandler); + String formattedLabel = mXAxis.getValueFormatter().getXValue(label, xIndex, mViewPortHandler); c.drawText(formattedLabel, x, y, mAxisLabelPaint); } diff --git a/MPChartLib/src/com/github/mikephil/charting/utils/PercentFormatter.java b/MPChartLib/src/com/github/mikephil/charting/utils/PercentFormatter.java deleted file mode 100644 index b69b3e9ba..000000000 --- a/MPChartLib/src/com/github/mikephil/charting/utils/PercentFormatter.java +++ /dev/null @@ -1,24 +0,0 @@ - -package com.github.mikephil.charting.utils; - -import java.text.DecimalFormat; - -/** - * This ValueFormatter is just for convenience and simply puts a "%" sign after - * each value. (Recommeded for PieChart) - * - * @author Philipp Jahoda - */ -public class PercentFormatter implements ValueFormatter { - - protected DecimalFormat mFormat; - - public PercentFormatter() { - mFormat = new DecimalFormat("###,###,##0.0"); - } - - @Override - public String getFormattedValue(float value) { - return mFormat.format(value) + " %"; - } -} diff --git a/MPChartLib/src/com/github/mikephil/charting/utils/ValueFormatter.java b/MPChartLib/src/com/github/mikephil/charting/utils/ValueFormatter.java deleted file mode 100644 index 70f19472a..000000000 --- a/MPChartLib/src/com/github/mikephil/charting/utils/ValueFormatter.java +++ /dev/null @@ -1,22 +0,0 @@ -package com.github.mikephil.charting.utils; - -/** - * Interface that allows custom formatting of all values and value-labels - * displayed inside the chart. Simply create your own formatting class and let - * it implement ValueFormatter. Then override the getFormattedLabel(...) method - * and return whatever you want. - * - * @author Philipp Jahoda - */ -public interface ValueFormatter { - - /** - * Called when a value (from labels, or inside the chart) is formatted - * before being drawn. For performance reasons, avoid excessive calculations - * and memory allocations inside this method. - * - * @param value the value to be formatted - * @return the formatted label ready for being drawn - */ - String getFormattedValue(float value); -}