From 70e2c3899ba54527720c8d616484313237efb702 Mon Sep 17 00:00:00 2001 From: Philipp Jahoda Date: Sun, 11 Oct 2015 16:34:50 +0200 Subject: [PATCH 01/31] Fix SpannableString PieChart center text issues (#993). --- .../xxmassdeveloper/mpchartexample/PieChartActivity.java | 8 ++++---- .../mikephil/charting/renderer/PieChartRenderer.java | 6 +++--- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/MPChartExample/src/com/xxmassdeveloper/mpchartexample/PieChartActivity.java b/MPChartExample/src/com/xxmassdeveloper/mpchartexample/PieChartActivity.java index 8392f186d..93f2b3e5e 100644 --- a/MPChartExample/src/com/xxmassdeveloper/mpchartexample/PieChartActivity.java +++ b/MPChartExample/src/com/xxmassdeveloper/mpchartexample/PieChartActivity.java @@ -240,10 +240,10 @@ private SpannableString generateCenterSpannableText() { SpannableString s = new SpannableString("MPAndroidChart developed by Philipp Jahoda"); s.setSpan(new RelativeSizeSpan(1.7f), 0, 14, 0); -// s.setSpan(new StyleSpan(Typeface.NORMAL), 14, s.length() - 15, 0); -// s.setSpan(new ForegroundColorSpan(Color.GRAY), 14, s.length() - 15, 0); -// s.setSpan(new RelativeSizeSpan(.8f), 14, s.length() - 15, 0); -// s.setSpan(new StyleSpan(Typeface.ITALIC), s.length() - 14, s.length(), 0); + s.setSpan(new StyleSpan(Typeface.NORMAL), 14, s.length() - 15, 0); + s.setSpan(new ForegroundColorSpan(Color.GRAY), 14, s.length() - 15, 0); + s.setSpan(new RelativeSizeSpan(.8f), 14, s.length() - 15, 0); + s.setSpan(new StyleSpan(Typeface.ITALIC), s.length() - 14, s.length(), 0); return s; } diff --git a/MPChartLib/src/com/github/mikephil/charting/renderer/PieChartRenderer.java b/MPChartLib/src/com/github/mikephil/charting/renderer/PieChartRenderer.java index 9dfdb556e..46884a018 100644 --- a/MPChartLib/src/com/github/mikephil/charting/renderer/PieChartRenderer.java +++ b/MPChartLib/src/com/github/mikephil/charting/renderer/PieChartRenderer.java @@ -74,7 +74,7 @@ public PieChartRenderer(PieChart chart, ChartAnimator animator, mCenterTextPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG); mCenterTextPaint.setColor(Color.BLACK); mCenterTextPaint.setTextSize(Utils.convertDpToPixel(12f)); - mCenterTextPaint.setTextAlign(Align.CENTER); + //mCenterTextPaint.setTextAlign(Align.CENTER); mValuePaint.setTextSize(Utils.convertDpToPixel(13f)); mValuePaint.setColor(Color.WHITE); @@ -327,7 +327,7 @@ protected void drawCenterText(Canvas c) { mCenterTextLayout = new StaticLayout(centerText, 0, centerText.length(), mCenterTextPaint, (int) Math.max(Math.ceil(width), 1.f), - Layout.Alignment.ALIGN_NORMAL, 1.f, 0.f, false); + Layout.Alignment.ALIGN_CENTER, 1.f, 0.f, false); } // I wish we could make an ellipse clipping path on Android to clip to the hole... @@ -337,7 +337,7 @@ protected void drawCenterText(Canvas c) { float layoutHeight = mCenterTextLayout.getHeight(); c.save(); - c.translate(boundingRect.centerX(), boundingRect.top + (boundingRect.height() - layoutHeight) / 2.f); + c.translate(boundingRect.left, boundingRect.top + (boundingRect.height() - layoutHeight) / 2.f); mCenterTextLayout.draw(c); c.restore(); From 280cb5c648bcb615067957c7855cf4b63e31f6b0 Mon Sep 17 00:00:00 2001 From: Philipp Jahoda Date: Sun, 11 Oct 2015 16:47:55 +0200 Subject: [PATCH 02/31] Improve example --- .../CubicLineChartActivity.java | 30 ++++++++++++++----- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/MPChartExample/src/com/xxmassdeveloper/mpchartexample/CubicLineChartActivity.java b/MPChartExample/src/com/xxmassdeveloper/mpchartexample/CubicLineChartActivity.java index 6b663c95e..22fb3409c 100644 --- a/MPChartExample/src/com/xxmassdeveloper/mpchartexample/CubicLineChartActivity.java +++ b/MPChartExample/src/com/xxmassdeveloper/mpchartexample/CubicLineChartActivity.java @@ -21,6 +21,8 @@ import com.github.mikephil.charting.data.LineDataSet; import com.github.mikephil.charting.data.filter.Approximator; import com.github.mikephil.charting.data.filter.Approximator.ApproximatorType; +import com.github.mikephil.charting.formatter.FillFormatter; +import com.github.mikephil.charting.interfaces.LineDataProvider; import com.github.mikephil.charting.utils.ColorTemplate; import com.xxmassdeveloper.mpchartexample.notimportant.DemoBase; @@ -54,7 +56,8 @@ protected void onCreate(Bundle savedInstanceState) { mSeekBarX.setOnSeekBarChangeListener(this); mChart = (LineChart) findViewById(R.id.chart1); - // if enabled, the chart will always start at zero on the y-axis + mChart.setViewPortOffsets(0, 20, 0, 0); + mChart.setBackgroundColor(Color.rgb(104, 241, 175)); // no description text mChart.setDescription(""); @@ -77,13 +80,16 @@ protected void onCreate(Bundle savedInstanceState) { tf = Typeface.createFromAsset(getAssets(), "OpenSans-Regular.ttf"); XAxis x = mChart.getXAxis(); - x.setTypeface(tf); x.setEnabled(false); YAxis y = mChart.getAxisLeft(); y.setTypeface(tf); - y.setLabelCount(5, false); - y.setEnabled(false); + y.setLabelCount(6, false); + y.setStartAtZero(false); + y.setTextColor(Color.WHITE); + y.setPosition(YAxis.YAxisLabelPosition.INSIDE_CHART); + y.setDrawGridLines(false); + y.setAxisLineColor(Color.WHITE); mChart.getAxisRight().setEnabled(false); @@ -270,12 +276,20 @@ private void setData(int count, float range) { set1.setCubicIntensity(0.2f); //set1.setDrawFilled(true); set1.setDrawCircles(false); - set1.setLineWidth(2f); - set1.setCircleSize(5f); + set1.setLineWidth(1.8f); + set1.setCircleSize(4f); + set1.setCircleColor(Color.WHITE); set1.setHighLightColor(Color.rgb(244, 117, 117)); - set1.setColor(Color.rgb(104, 241, 175)); - set1.setFillColor(ColorTemplate.getHoloBlue()); + set1.setColor(Color.WHITE); + set1.setFillColor(Color.WHITE); + set1.setFillAlpha(100); set1.setDrawHorizontalHighlightIndicator(false); + set1.setFillFormatter(new FillFormatter() { + @Override + public float getFillLinePosition(LineDataSet dataSet, LineDataProvider dataProvider) { + return -10; + } + }); // create a data object with the datasets LineData data = new LineData(xVals, set1); From 1eae8e050ec6efc77c94084e3bcf5b654f5bc283 Mon Sep 17 00:00:00 2001 From: Philipp Jahoda Date: Sun, 11 Oct 2015 19:23:16 +0200 Subject: [PATCH 03/31] Improvements on piechart. --- .../mpchartexample/PieChartActivity.java | 11 +++++----- .../fragments/PieChartFrag.java | 17 ++++++++++++--- .../fragments/SimpleFragment.java | 2 +- .../listviewitems/PieChartItem.java | 21 +++++++++++++++++-- .../charting/renderer/PieChartRenderer.java | 16 +++++++------- 5 files changed, 48 insertions(+), 19 deletions(-) diff --git a/MPChartExample/src/com/xxmassdeveloper/mpchartexample/PieChartActivity.java b/MPChartExample/src/com/xxmassdeveloper/mpchartexample/PieChartActivity.java index 93f2b3e5e..a5ff3955d 100644 --- a/MPChartExample/src/com/xxmassdeveloper/mpchartexample/PieChartActivity.java +++ b/MPChartExample/src/com/xxmassdeveloper/mpchartexample/PieChartActivity.java @@ -94,7 +94,7 @@ protected void onCreate(Bundle savedInstanceState) { setData(3, 100); - mChart.animateY(1500, Easing.EasingOption.EaseInOutQuad); + mChart.animateY(1400, Easing.EasingOption.EaseInOutQuad); // mChart.spin(2000, 0, 360); Legend l = mChart.getLegend(); @@ -153,15 +153,15 @@ public boolean onOptionsItemSelected(MenuItem item) { mChart.invalidate(); break; case R.id.animateX: { - mChart.animateX(1800); + mChart.animateX(1400); break; } case R.id.animateY: { - mChart.animateY(1800); + mChart.animateY(1400); break; } case R.id.animateXY: { - mChart.animateXY(1800, 1800); + mChart.animateXY(1400, 1400); break; } } @@ -196,7 +196,7 @@ private void setData(int count, float range) { xVals.add(mParties[i % mParties.length]); PieDataSet dataSet = new PieDataSet(yVals1, "Election Results"); - dataSet.setSliceSpace(3f); + dataSet.setSliceSpace(2f); dataSet.setSelectionShift(5f); // add a lot of colors @@ -244,6 +244,7 @@ private SpannableString generateCenterSpannableText() { s.setSpan(new ForegroundColorSpan(Color.GRAY), 14, s.length() - 15, 0); s.setSpan(new RelativeSizeSpan(.8f), 14, s.length() - 15, 0); s.setSpan(new StyleSpan(Typeface.ITALIC), s.length() - 14, s.length(), 0); + s.setSpan(new ForegroundColorSpan(ColorTemplate.getHoloBlue()), s.length() - 14, s.length(), 0); return s; } diff --git a/MPChartExample/src/com/xxmassdeveloper/mpchartexample/fragments/PieChartFrag.java b/MPChartExample/src/com/xxmassdeveloper/mpchartexample/fragments/PieChartFrag.java index b06ab32b8..ae7ca9f19 100644 --- a/MPChartExample/src/com/xxmassdeveloper/mpchartexample/fragments/PieChartFrag.java +++ b/MPChartExample/src/com/xxmassdeveloper/mpchartexample/fragments/PieChartFrag.java @@ -1,7 +1,11 @@ package com.xxmassdeveloper.mpchartexample.fragments; +import android.graphics.Color; import android.graphics.Typeface; import android.os.Bundle; import android.support.v4.app.Fragment; +import android.text.SpannableString; +import android.text.style.ForegroundColorSpan; +import android.text.style.RelativeSizeSpan; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -30,12 +34,12 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa Typeface tf = Typeface.createFromAsset(getActivity().getAssets(), "OpenSans-Light.ttf"); mChart.setCenterTextTypeface(tf); - mChart.setCenterText("Revenues"); - mChart.setCenterTextSize(22f); + mChart.setCenterText(generateCenterText()); + mChart.setCenterTextSize(10f); mChart.setCenterTextTypeface(tf); // radius of the center hole in percent of maximum radius - mChart.setHoleRadius(45f); + mChart.setHoleRadius(45f); mChart.setTransparentCircleRadius(50f); Legend l = mChart.getLegend(); @@ -45,4 +49,11 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa return v; } + + private SpannableString generateCenterText() { + SpannableString s = new SpannableString("Revenues\nQuarters 2015"); + s.setSpan(new RelativeSizeSpan(2f), 0, 8, 0); + s.setSpan(new ForegroundColorSpan(Color.GRAY), 8, s.length(), 0); + return s; + } } diff --git a/MPChartExample/src/com/xxmassdeveloper/mpchartexample/fragments/SimpleFragment.java b/MPChartExample/src/com/xxmassdeveloper/mpchartexample/fragments/SimpleFragment.java index ce9a49b71..386f699fe 100644 --- a/MPChartExample/src/com/xxmassdeveloper/mpchartexample/fragments/SimpleFragment.java +++ b/MPChartExample/src/com/xxmassdeveloper/mpchartexample/fragments/SimpleFragment.java @@ -113,7 +113,7 @@ protected PieData generatePieData() { entries1.add(new Entry((float) (Math.random() * 60) + 40, i)); } - PieDataSet ds1 = new PieDataSet(entries1, "Quarterly Revenues 2014"); + PieDataSet ds1 = new PieDataSet(entries1, "Quarterly Revenues 2015"); ds1.setColors(ColorTemplate.VORDIPLOM_COLORS); ds1.setSliceSpace(2f); ds1.setValueTextColor(Color.WHITE); diff --git a/MPChartExample/src/com/xxmassdeveloper/mpchartexample/listviewitems/PieChartItem.java b/MPChartExample/src/com/xxmassdeveloper/mpchartexample/listviewitems/PieChartItem.java index f65c02e50..fb09474b5 100644 --- a/MPChartExample/src/com/xxmassdeveloper/mpchartexample/listviewitems/PieChartItem.java +++ b/MPChartExample/src/com/xxmassdeveloper/mpchartexample/listviewitems/PieChartItem.java @@ -4,6 +4,9 @@ import android.content.Context; import android.graphics.Color; import android.graphics.Typeface; +import android.text.SpannableString; +import android.text.style.ForegroundColorSpan; +import android.text.style.RelativeSizeSpan; import android.view.LayoutInflater; import android.view.View; @@ -13,16 +16,19 @@ import com.github.mikephil.charting.data.ChartData; import com.github.mikephil.charting.data.PieData; import com.github.mikephil.charting.formatter.PercentFormatter; +import com.github.mikephil.charting.utils.ColorTemplate; import com.xxmassdeveloper.mpchartexample.R; public class PieChartItem extends ChartItem { private Typeface mTf; + private SpannableString mCenterText; public PieChartItem(ChartData cd, Context c) { super(cd); mTf = Typeface.createFromAsset(c.getAssets(), "OpenSans-Regular.ttf"); + mCenterText = generateCenterText(); } @Override @@ -53,9 +59,9 @@ public View getView(int position, View convertView, Context c) { holder.chart.setDescription(""); holder.chart.setHoleRadius(52f); holder.chart.setTransparentCircleRadius(57f); - holder.chart.setCenterText("MPChart\nAndroid"); + holder.chart.setCenterText(mCenterText); holder.chart.setCenterTextTypeface(mTf); - holder.chart.setCenterTextSize(18f); + holder.chart.setCenterTextSize(9f); holder.chart.setUsePercentValues(true); holder.chart.setExtraOffsets(5, 10, 50, 10); @@ -78,6 +84,17 @@ public View getView(int position, View convertView, Context c) { return convertView; } + private SpannableString generateCenterText() { + SpannableString s = new SpannableString("MPAndroidChart\ncreated by\nPhilipp Jahoda"); + s.setSpan(new RelativeSizeSpan(1.6f), 0, 14, 0); + s.setSpan(new ForegroundColorSpan(ColorTemplate.VORDIPLOM_COLORS[0]), 0, 14, 0); + s.setSpan(new RelativeSizeSpan(.9f), 14, 25, 0); + s.setSpan(new ForegroundColorSpan(Color.GRAY), 14, 25, 0); + s.setSpan(new RelativeSizeSpan(1.4f), 25, s.length(), 0); + s.setSpan(new ForegroundColorSpan(ColorTemplate.getHoloBlue()), 25, s.length(), 0); + return s; + } + private static class ViewHolder { PieChart chart; } diff --git a/MPChartLib/src/com/github/mikephil/charting/renderer/PieChartRenderer.java b/MPChartLib/src/com/github/mikephil/charting/renderer/PieChartRenderer.java index 46884a018..2b4fdb488 100644 --- a/MPChartLib/src/com/github/mikephil/charting/renderer/PieChartRenderer.java +++ b/MPChartLib/src/com/github/mikephil/charting/renderer/PieChartRenderer.java @@ -69,7 +69,7 @@ public PieChartRenderer(PieChart chart, ChartAnimator animator, mTransparentCirclePaint = new Paint(Paint.ANTI_ALIAS_FLAG); mTransparentCirclePaint.setColor(Color.WHITE); mTransparentCirclePaint.setStyle(Style.FILL); - mTransparentCirclePaint.setAlpha(100); + mTransparentCirclePaint.setAlpha(105); mCenterTextPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG); mCenterTextPaint.setColor(Color.BLACK); @@ -266,19 +266,19 @@ protected void drawHole(Canvas c) { PointF center = mChart.getCenterCircleBox(); - if (transparentCircleRadius > holeRadius && mAnimator.getPhaseX() >= 1f - && mAnimator.getPhaseY() >= 1f) { + // only draw the circle if it can be seen (not covered by the hole) + if (transparentCircleRadius > holeRadius) { - //int color = mTransparentCirclePaint.getColor(); - - // make transparent - //mTransparentCirclePaint.setColor(color & 0x60FFFFFF); + // get original alpha + int alpha = mTransparentCirclePaint.getAlpha(); + mTransparentCirclePaint.setAlpha((int) ((float) alpha * mAnimator.getPhaseX() * mAnimator.getPhaseY())); // draw the transparent-circle mBitmapCanvas.drawCircle(center.x, center.y, radius / 100 * transparentCircleRadius, mTransparentCirclePaint); - //mTransparentCirclePaint.setColor(color); + // reset alpha + mTransparentCirclePaint.setAlpha(alpha); } // draw the hole-circle From e3a7b0969b97f4014122a6dce275c20928f14088 Mon Sep 17 00:00:00 2001 From: Philipp Jahoda Date: Sun, 11 Oct 2015 19:24:06 +0200 Subject: [PATCH 04/31] Update version. --- MPChartExample/AndroidManifest.xml | 4 ++-- MPChartExample/build.gradle | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/MPChartExample/AndroidManifest.xml b/MPChartExample/AndroidManifest.xml index 18a347ea1..639694e26 100644 --- a/MPChartExample/AndroidManifest.xml +++ b/MPChartExample/AndroidManifest.xml @@ -1,8 +1,8 @@ + android:versionCode="45" + android:versionName="2.1.5" > Date: Sun, 11 Oct 2015 19:39:01 +0200 Subject: [PATCH 05/31] Update README.md --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 710618822..f17b4585a 100644 --- a/README.md +++ b/README.md @@ -145,7 +145,7 @@ repositories { } dependencies { - compile 'com.github.PhilJay:MPAndroidChart:v2.1.4' + compile 'com.github.PhilJay:MPAndroidChart:v2.1.5' } ``` @@ -160,7 +160,7 @@ dependencies { com.github.PhilJay MPAndroidChart - v2.1.4 + v2.1.5 ``` @@ -180,7 +180,7 @@ dependencies { Documentation ======= -For a **detailed documentation**, please have a look at the [**Wiki**](https://github.com/PhilJay/MPAndroidChart/wiki). +For a **detailed documentation**, please have a look at the [**Wiki**](https://github.com/PhilJay/MPAndroidChart/wiki) or the [javadocs](https://jitpack.io/com/github/PhilJay/MPAndroidChart/-SNAPSHOT/javadoc/). Furthermore, you can also rely on the [**MPChartExample**](https://github.com/PhilJay/MPAndroidChart/tree/master/MPChartExample) folder and check out the example code in that project. The corresponding application to the example project is also [**available in the Google PlayStore**](https://play.google.com/store/apps/details?id=com.xxmassdeveloper.mpchartexample). From a2ecebca0caaba0c2262f7a154e05a3734fc0d77 Mon Sep 17 00:00:00 2001 From: Philipp Jahoda Date: Sun, 11 Oct 2015 23:13:40 +0200 Subject: [PATCH 06/31] add javadoc link --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index f17b4585a..324132ec7 100644 --- a/README.md +++ b/README.md @@ -57,7 +57,7 @@ Questions & Issues If you are having questions or problems, you should: - Make sure you are using the latest version of the library. Check the [**release-section**](https://github.com/PhilJay/MPAndroidChart/releases). - - Study the [**Documentation-Wiki**](https://github.com/PhilJay/MPAndroidChart/wiki) or the [javadocs](https://jitpack.io/com/github/PhilJay/MPAndroidChart/-SNAPSHOT/javadoc/) + - Study the [**Documentation-Wiki**](https://github.com/PhilJay/MPAndroidChart/wiki) or the [javadocs](https://jitpack.io/com/github/PhilJay/MPAndroidChart/v2.1.5/javadoc/) - Search or open questions on [**stackoverflow**](https://stackoverflow.com/search?q=mpandroidchart) with the `mpandroidchart` tag - Search [**known issues**](https://github.com/PhilJay/MPAndroidChart/issues) for your problem (open and closed) - Create new issues (please **search known issues before**, do not create duplicate issues) @@ -180,7 +180,7 @@ dependencies { Documentation ======= -For a **detailed documentation**, please have a look at the [**Wiki**](https://github.com/PhilJay/MPAndroidChart/wiki) or the [javadocs](https://jitpack.io/com/github/PhilJay/MPAndroidChart/-SNAPSHOT/javadoc/). +For a **detailed documentation**, please have a look at the [**Wiki**](https://github.com/PhilJay/MPAndroidChart/wiki) or the [javadocs](https://jitpack.io/com/github/PhilJay/MPAndroidChart/v2.1.5/javadoc/). Furthermore, you can also rely on the [**MPChartExample**](https://github.com/PhilJay/MPAndroidChart/tree/master/MPChartExample) folder and check out the example code in that project. The corresponding application to the example project is also [**available in the Google PlayStore**](https://play.google.com/store/apps/details?id=com.xxmassdeveloper.mpchartexample). From a7898f191a1ad3a8ae91f8b48cf577892980c061 Mon Sep 17 00:00:00 2001 From: Daniel Cohen Gindi Date: Tue, 13 Oct 2015 20:56:34 +0300 Subject: [PATCH 07/31] Allow getting/settings minOffset of the charts, instead of zeroing them out totally --- .../charting/charts/BarLineChartBase.java | 10 ++++++++++ .../charting/charts/PieRadarChartBase.java | 17 +++++++++++++++-- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/MPChartLib/src/com/github/mikephil/charting/charts/BarLineChartBase.java b/MPChartLib/src/com/github/mikephil/charting/charts/BarLineChartBase.java index 713ed0fc6..3a89c2aad 100644 --- a/MPChartLib/src/com/github/mikephil/charting/charts/BarLineChartBase.java +++ b/MPChartLib/src/com/github/mikephil/charting/charts/BarLineChartBase.java @@ -1131,6 +1131,16 @@ public void setBorderColor(int color) { mBorderPaint.setColor(color); } + /** Gets the minimum offset (padding) around the chart, defaults to 10.f */ + public float getMinOffset() { + return mMinOffset; + } + + /** Sets the minimum offset (padding) around the chart, defaults to 10.f */ + public void setMinOffset(float minOffset) { + mMinOffset = minOffset; + } + /** * Returns the Highlight object (contains x-index and DataSet index) of the * selected value at the given touch point inside the Line-, Scatter-, or diff --git a/MPChartLib/src/com/github/mikephil/charting/charts/PieRadarChartBase.java b/MPChartLib/src/com/github/mikephil/charting/charts/PieRadarChartBase.java index c5ff63c7e..881ea65f9 100644 --- a/MPChartLib/src/com/github/mikephil/charting/charts/PieRadarChartBase.java +++ b/MPChartLib/src/com/github/mikephil/charting/charts/PieRadarChartBase.java @@ -42,6 +42,9 @@ public abstract class PieRadarChartBase Date: Tue, 13 Oct 2015 20:15:18 +0200 Subject: [PATCH 08/31] Minor changes --- .../com/xxmassdeveloper/mpchartexample/PieChartActivity.java | 4 ++-- settings.gradle | 2 ++ 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/MPChartExample/src/com/xxmassdeveloper/mpchartexample/PieChartActivity.java b/MPChartExample/src/com/xxmassdeveloper/mpchartexample/PieChartActivity.java index a5ff3955d..5874f8268 100644 --- a/MPChartExample/src/com/xxmassdeveloper/mpchartexample/PieChartActivity.java +++ b/MPChartExample/src/com/xxmassdeveloper/mpchartexample/PieChartActivity.java @@ -76,11 +76,11 @@ protected void onCreate(Bundle savedInstanceState) { mChart.setTransparentCircleColor(Color.WHITE); mChart.setTransparentCircleAlpha(110); - + mChart.setHoleRadius(58f); mChart.setTransparentCircleRadius(61f); - mChart.setDrawCenterText(true); + mChart.setDrawCenterText(true); mChart.setRotationAngle(0); // enable rotation of the chart by touch diff --git a/settings.gradle b/settings.gradle index 0d770eb38..6d793f8d1 100644 --- a/settings.gradle +++ b/settings.gradle @@ -1,3 +1,5 @@ include 'MPChartLib' +//include 'MPAndroidChart-Realm' include 'MPChartExample' + From 47b78b4308bacdd6e4e85b4e2fbfcc3c7adbeaf4 Mon Sep 17 00:00:00 2001 From: Christopher Roberts Date: Thu, 15 Oct 2015 00:45:11 -0400 Subject: [PATCH 09/31] Make Entry class Parcelable. As an Entry object may contain arbitrary data (in the form of a single object), I took the route of checking if that object is also parcelable and parceling it if so. If the object is not parcelable, then an Exception is thrown. --- .../github/mikephil/charting/data/Entry.java | 45 ++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/MPChartLib/src/com/github/mikephil/charting/data/Entry.java b/MPChartLib/src/com/github/mikephil/charting/data/Entry.java index e57852ea3..35052fbcb 100644 --- a/MPChartLib/src/com/github/mikephil/charting/data/Entry.java +++ b/MPChartLib/src/com/github/mikephil/charting/data/Entry.java @@ -1,13 +1,17 @@ package com.github.mikephil.charting.data; +import android.os.Parcel; +import android.os.ParcelFormatException; +import android.os.Parcelable; + /** * Class representing one entry in the chart. Might contain multiple values. * Might only contain a single value depending on the used constructor. * * @author Philipp Jahoda */ -public class Entry { +public class Entry implements Parcelable { /** the actual value */ private float mVal = 0f; @@ -142,4 +146,43 @@ public boolean equalTo(Entry e) { public String toString() { return "Entry, xIndex: " + mXIndex + " val (sum): " + getVal(); } + + @Override + public int describeContents() { + return 0; + } + + @Override + public void writeToParcel(Parcel dest, int flags) { + dest.writeFloat(this.mVal); + dest.writeInt(this.mXIndex); + if (mData != null) { + if (mData instanceof Parcelable) { + dest.writeInt(1); + dest.writeParcelable((Parcelable) this.mData, flags); + } else { + throw new ParcelFormatException("Cannot parcel an Entry with non-parcelable data"); + } + } else { + dest.writeInt(0); + } + } + + protected Entry(Parcel in) { + this.mVal = in.readFloat(); + this.mXIndex = in.readInt(); + if (in.readInt() == 1) { + this.mData = in.readParcelable(Object.class.getClassLoader()); + } + } + + public static final Parcelable.Creator CREATOR = new Parcelable.Creator() { + public Entry createFromParcel(Parcel source) { + return new Entry(source); + } + + public Entry[] newArray(int size) { + return new Entry[size]; + } + }; } From c91507525b73ad7af4e8f3148c6d1d2c7ace9b55 Mon Sep 17 00:00:00 2001 From: Philipp Jahoda Date: Thu, 15 Oct 2015 16:24:39 +0200 Subject: [PATCH 10/31] Remove chart.setHighlightEnabled(...) method because it led to confusions when no data was set (issue #1110). --- .../mpchartexample/AnotherBarActivity.java | 10 ++--- .../mpchartexample/BarChartActivity.java | 14 ++----- .../BarChartActivityMultiDataset.java | 9 ++-- .../mpchartexample/BarChartActivitySinus.java | 9 ++-- .../mpchartexample/BubbleChartActivity.java | 10 ++--- .../CandleStickChartActivity.java | 9 ++-- .../CubicLineChartActivity.java | 12 ++---- .../mpchartexample/DrawChartActivity.java | 17 ++------ .../HorizontalBarChartActivity.java | 9 ++-- .../InvertedLineChartActivity.java | 16 ++----- .../mpchartexample/LineChartActivity1.java | 12 ++---- .../mpchartexample/LineChartActivity2.java | 12 ++---- .../MultiLineChartActivity.java | 12 ++---- .../mpchartexample/PerformanceLineChart.java | 7 ---- .../mpchartexample/RadarChartActivitry.java | 9 ++-- .../RealtimeLineChartActivity.java | 3 -- .../mpchartexample/ScatterChartActivity.java | 10 ++--- .../mpchartexample/StackedBarActivity.java | 9 ++-- .../StackedBarActivityNegative.java | 9 ++-- .../fragments/BarChartFrag.java | 2 - .../fragments/ComplexityFragment.java | 6 +-- .../fragments/ScatterChartFrag.java | 1 - .../fragments/SineCosineFragment.java | 7 +--- .../mikephil/charting/charts/Chart.java | 42 +++++++++---------- 24 files changed, 92 insertions(+), 164 deletions(-) diff --git a/MPChartExample/src/com/xxmassdeveloper/mpchartexample/AnotherBarActivity.java b/MPChartExample/src/com/xxmassdeveloper/mpchartexample/AnotherBarActivity.java index 2fe372309..0cf9982ef 100644 --- a/MPChartExample/src/com/xxmassdeveloper/mpchartexample/AnotherBarActivity.java +++ b/MPChartExample/src/com/xxmassdeveloper/mpchartexample/AnotherBarActivity.java @@ -104,11 +104,11 @@ public boolean onOptionsItemSelected(MenuItem item) { break; } case R.id.actionToggleHighlight: { - if (mChart.isHighlightEnabled()) - mChart.setHighlightEnabled(false); - else - mChart.setHighlightEnabled(true); - mChart.invalidate(); + + if(mChart.getData() != null) { + mChart.getData().setHighlightEnabled(!mChart.getData().isHighlightEnabled()); + mChart.invalidate(); + } break; } case R.id.actionTogglePinch: { diff --git a/MPChartExample/src/com/xxmassdeveloper/mpchartexample/BarChartActivity.java b/MPChartExample/src/com/xxmassdeveloper/mpchartexample/BarChartActivity.java index be899270d..9ade72efb 100644 --- a/MPChartExample/src/com/xxmassdeveloper/mpchartexample/BarChartActivity.java +++ b/MPChartExample/src/com/xxmassdeveloper/mpchartexample/BarChartActivity.java @@ -76,11 +76,6 @@ protected void onCreate(Bundle savedInstanceState) { // scaling can now only be done on x- and y-axis separately mChart.setPinchZoom(false); - // draw shadows for each bar that show the maximum value - // mChart.setDrawBarShadow(true); - - // mChart.setDrawXLabels(false); - mChart.setDrawGridBackground(false); // mChart.setDrawYLabels(false); @@ -149,11 +144,10 @@ public boolean onOptionsItemSelected(MenuItem item) { break; } case R.id.actionToggleHighlight: { - if (mChart.isHighlightEnabled()) - mChart.setHighlightEnabled(false); - else - mChart.setHighlightEnabled(true); - mChart.invalidate(); + if(mChart.getData() != null) { + mChart.getData().setHighlightEnabled(!mChart.getData().isHighlightEnabled()); + mChart.invalidate(); + } break; } case R.id.actionTogglePinch: { diff --git a/MPChartExample/src/com/xxmassdeveloper/mpchartexample/BarChartActivityMultiDataset.java b/MPChartExample/src/com/xxmassdeveloper/mpchartexample/BarChartActivityMultiDataset.java index 398d00373..910e4839e 100644 --- a/MPChartExample/src/com/xxmassdeveloper/mpchartexample/BarChartActivityMultiDataset.java +++ b/MPChartExample/src/com/xxmassdeveloper/mpchartexample/BarChartActivityMultiDataset.java @@ -135,11 +135,10 @@ public boolean onOptionsItemSelected(MenuItem item) { break; } case R.id.actionToggleHighlight: { - if (mChart.isHighlightEnabled()) - mChart.setHighlightEnabled(false); - else - mChart.setHighlightEnabled(true); - mChart.invalidate(); + if(mChart.getData() != null) { + mChart.getData().setHighlightEnabled(!mChart.getData().isHighlightEnabled()); + mChart.invalidate(); + } break; } case R.id.actionToggleHighlightArrow: { diff --git a/MPChartExample/src/com/xxmassdeveloper/mpchartexample/BarChartActivitySinus.java b/MPChartExample/src/com/xxmassdeveloper/mpchartexample/BarChartActivitySinus.java index 5e53f2b14..1a29182e8 100644 --- a/MPChartExample/src/com/xxmassdeveloper/mpchartexample/BarChartActivitySinus.java +++ b/MPChartExample/src/com/xxmassdeveloper/mpchartexample/BarChartActivitySinus.java @@ -130,11 +130,10 @@ public boolean onOptionsItemSelected(MenuItem item) { break; } case R.id.actionToggleHighlight: { - if (mChart.isHighlightEnabled()) - mChart.setHighlightEnabled(false); - else - mChart.setHighlightEnabled(true); - mChart.invalidate(); + if(mChart.getData() != null) { + mChart.getData().setHighlightEnabled(!mChart.getData().isHighlightEnabled()); + mChart.invalidate(); + } break; } case R.id.actionTogglePinch: { diff --git a/MPChartExample/src/com/xxmassdeveloper/mpchartexample/BubbleChartActivity.java b/MPChartExample/src/com/xxmassdeveloper/mpchartexample/BubbleChartActivity.java index 4a1a84d95..690bc6528 100644 --- a/MPChartExample/src/com/xxmassdeveloper/mpchartexample/BubbleChartActivity.java +++ b/MPChartExample/src/com/xxmassdeveloper/mpchartexample/BubbleChartActivity.java @@ -66,7 +66,6 @@ protected void onCreate(Bundle savedInstanceState) { mChart.setDrawGridBackground(false); mChart.setTouchEnabled(true); - mChart.setHighlightEnabled(true); // enable scaling and dragging mChart.setDragEnabled(true); @@ -116,11 +115,10 @@ public boolean onOptionsItemSelected(MenuItem item) { break; } case R.id.actionToggleHighlight: { - if (mChart.isHighlightEnabled()) - mChart.setHighlightEnabled(false); - else - mChart.setHighlightEnabled(true); - mChart.invalidate(); + if(mChart.getData() != null) { + mChart.getData().setHighlightEnabled(!mChart.getData().isHighlightEnabled()); + mChart.invalidate(); + } break; } case R.id.actionTogglePinch: { diff --git a/MPChartExample/src/com/xxmassdeveloper/mpchartexample/CandleStickChartActivity.java b/MPChartExample/src/com/xxmassdeveloper/mpchartexample/CandleStickChartActivity.java index 384983f42..5ea63dab9 100644 --- a/MPChartExample/src/com/xxmassdeveloper/mpchartexample/CandleStickChartActivity.java +++ b/MPChartExample/src/com/xxmassdeveloper/mpchartexample/CandleStickChartActivity.java @@ -101,11 +101,10 @@ public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.actionToggleHighlight: { - if (mChart.isHighlightEnabled()) - mChart.setHighlightEnabled(false); - else - mChart.setHighlightEnabled(true); - mChart.invalidate(); + if(mChart.getData() != null) { + mChart.getData().setHighlightEnabled(!mChart.getData().isHighlightEnabled()); + mChart.invalidate(); + } break; } case R.id.actionTogglePinch: { diff --git a/MPChartExample/src/com/xxmassdeveloper/mpchartexample/CubicLineChartActivity.java b/MPChartExample/src/com/xxmassdeveloper/mpchartexample/CubicLineChartActivity.java index 22fb3409c..1d476c9c2 100644 --- a/MPChartExample/src/com/xxmassdeveloper/mpchartexample/CubicLineChartActivity.java +++ b/MPChartExample/src/com/xxmassdeveloper/mpchartexample/CubicLineChartActivity.java @@ -62,9 +62,6 @@ protected void onCreate(Bundle savedInstanceState) { // no description text mChart.setDescription(""); - // enable value highlighting - mChart.setHighlightEnabled(true); - // enable touch gestures mChart.setTouchEnabled(true); @@ -122,11 +119,10 @@ public boolean onOptionsItemSelected(MenuItem item) { break; } case R.id.actionToggleHighlight: { - if (mChart.isHighlightEnabled()) - mChart.setHighlightEnabled(false); - else - mChart.setHighlightEnabled(true); - mChart.invalidate(); + if(mChart.getData() != null) { + mChart.getData().setHighlightEnabled(!mChart.getData().isHighlightEnabled()); + mChart.invalidate(); + } break; } case R.id.actionToggleFilled: { diff --git a/MPChartExample/src/com/xxmassdeveloper/mpchartexample/DrawChartActivity.java b/MPChartExample/src/com/xxmassdeveloper/mpchartexample/DrawChartActivity.java index 62b9d2386..7e66ecbb0 100644 --- a/MPChartExample/src/com/xxmassdeveloper/mpchartexample/DrawChartActivity.java +++ b/MPChartExample/src/com/xxmassdeveloper/mpchartexample/DrawChartActivity.java @@ -47,14 +47,6 @@ protected void onCreate(Bundle savedInstanceState) { mChart.setOnChartValueSelectedListener(this); mChart.setOnDrawListener(this); - // enable drawing with the finger - // mChart.setDrawingEnabled(true); - - // mChart.setLineWidth(5f); - // mChart.setCircleSize(5f); - - mChart.setHighlightEnabled(true); - // if disabled, drawn datasets with the finger will not be automatically // finished // mChart.setAutoFinish(true); @@ -119,11 +111,10 @@ public boolean onOptionsItemSelected(MenuItem item) { break; } case R.id.actionToggleHighlight: { - if (mChart.isHighlightEnabled()) - mChart.setHighlightEnabled(false); - else - mChart.setHighlightEnabled(true); - mChart.invalidate(); + if(mChart.getData() != null) { + mChart.getData().setHighlightEnabled(!mChart.getData().isHighlightEnabled()); + mChart.invalidate(); + } break; } case R.id.actionToggleStartzero: { diff --git a/MPChartExample/src/com/xxmassdeveloper/mpchartexample/HorizontalBarChartActivity.java b/MPChartExample/src/com/xxmassdeveloper/mpchartexample/HorizontalBarChartActivity.java index 4df4497da..83ea2be4e 100644 --- a/MPChartExample/src/com/xxmassdeveloper/mpchartexample/HorizontalBarChartActivity.java +++ b/MPChartExample/src/com/xxmassdeveloper/mpchartexample/HorizontalBarChartActivity.java @@ -141,11 +141,10 @@ public boolean onOptionsItemSelected(MenuItem item) { break; } case R.id.actionToggleHighlight: { - if (mChart.isHighlightEnabled()) - mChart.setHighlightEnabled(false); - else - mChart.setHighlightEnabled(true); - mChart.invalidate(); + if(mChart.getData() != null) { + mChart.getData().setHighlightEnabled(!mChart.getData().isHighlightEnabled()); + mChart.invalidate(); + } break; } case R.id.actionTogglePinch: { diff --git a/MPChartExample/src/com/xxmassdeveloper/mpchartexample/InvertedLineChartActivity.java b/MPChartExample/src/com/xxmassdeveloper/mpchartexample/InvertedLineChartActivity.java index c4ee0ae5d..e943222ea 100644 --- a/MPChartExample/src/com/xxmassdeveloper/mpchartexample/InvertedLineChartActivity.java +++ b/MPChartExample/src/com/xxmassdeveloper/mpchartexample/InvertedLineChartActivity.java @@ -62,9 +62,6 @@ protected void onCreate(Bundle savedInstanceState) { // no description text mChart.setDescription(""); - // enable value highlighting - mChart.setHighlightEnabled(true); - // enable touch gestures mChart.setTouchEnabled(true); @@ -84,10 +81,6 @@ protected void onCreate(Bundle savedInstanceState) { // set the marker to the chart mChart.setMarkerView(mv); - - // enable/disable highlight indicators (the lines that indicate the - // highlighted Entry) - mChart.setHighlightEnabled(false); XAxis xl = mChart.getXAxis(); xl.setAvoidFirstLastClipping(true); @@ -136,11 +129,10 @@ public boolean onOptionsItemSelected(MenuItem item) { break; } case R.id.actionToggleHighlight: { - if (mChart.isHighlightEnabled()) - mChart.setHighlightEnabled(false); - else - mChart.setHighlightEnabled(true); - mChart.invalidate(); + if(mChart.getData() != null) { + mChart.getData().setHighlightEnabled(!mChart.getData().isHighlightEnabled()); + mChart.invalidate(); + } break; } case R.id.actionToggleFilled: { diff --git a/MPChartExample/src/com/xxmassdeveloper/mpchartexample/LineChartActivity1.java b/MPChartExample/src/com/xxmassdeveloper/mpchartexample/LineChartActivity1.java index 24551f0fc..a7ee98a6b 100644 --- a/MPChartExample/src/com/xxmassdeveloper/mpchartexample/LineChartActivity1.java +++ b/MPChartExample/src/com/xxmassdeveloper/mpchartexample/LineChartActivity1.java @@ -72,9 +72,6 @@ protected void onCreate(Bundle savedInstanceState) { mChart.setDescription(""); mChart.setNoDataTextDescription("You need to provide data for the chart."); - // enable value highlighting - mChart.setHighlightEnabled(true); - // enable touch gestures mChart.setTouchEnabled(true); @@ -186,11 +183,10 @@ public boolean onOptionsItemSelected(MenuItem item) { break; } case R.id.actionToggleHighlight: { - if (mChart.isHighlightEnabled()) - mChart.setHighlightEnabled(false); - else - mChart.setHighlightEnabled(true); - mChart.invalidate(); + if(mChart.getData() != null) { + mChart.getData().setHighlightEnabled(!mChart.getData().isHighlightEnabled()); + mChart.invalidate(); + } break; } case R.id.actionToggleFilled: { diff --git a/MPChartExample/src/com/xxmassdeveloper/mpchartexample/LineChartActivity2.java b/MPChartExample/src/com/xxmassdeveloper/mpchartexample/LineChartActivity2.java index 6cce635b4..fa2681e9e 100644 --- a/MPChartExample/src/com/xxmassdeveloper/mpchartexample/LineChartActivity2.java +++ b/MPChartExample/src/com/xxmassdeveloper/mpchartexample/LineChartActivity2.java @@ -66,9 +66,6 @@ protected void onCreate(Bundle savedInstanceState) { mChart.setDescription(""); mChart.setNoDataTextDescription("You need to provide data for the chart."); - // enable value highlighting - mChart.setHighlightEnabled(true); - // enable touch gestures mChart.setTouchEnabled(true); @@ -146,11 +143,10 @@ public boolean onOptionsItemSelected(MenuItem item) { break; } case R.id.actionToggleHighlight: { - if (mChart.isHighlightEnabled()) - mChart.setHighlightEnabled(false); - else - mChart.setHighlightEnabled(true); - mChart.invalidate(); + if(mChart.getData() != null) { + mChart.getData().setHighlightEnabled(!mChart.getData().isHighlightEnabled()); + mChart.invalidate(); + } break; } case R.id.actionToggleFilled: { diff --git a/MPChartExample/src/com/xxmassdeveloper/mpchartexample/MultiLineChartActivity.java b/MPChartExample/src/com/xxmassdeveloper/mpchartexample/MultiLineChartActivity.java index 81d1505fc..d6bd2e3aa 100644 --- a/MPChartExample/src/com/xxmassdeveloper/mpchartexample/MultiLineChartActivity.java +++ b/MPChartExample/src/com/xxmassdeveloper/mpchartexample/MultiLineChartActivity.java @@ -63,9 +63,6 @@ protected void onCreate(Bundle savedInstanceState) { mChart.getXAxis().setDrawAxisLine(false); mChart.getXAxis().setDrawGridLines(false); - // enable value highlighting - mChart.setHighlightEnabled(true); - // enable touch gestures mChart.setTouchEnabled(true); @@ -115,11 +112,10 @@ public boolean onOptionsItemSelected(MenuItem item) { break; } case R.id.actionToggleHighlight: { - if (mChart.isHighlightEnabled()) - mChart.setHighlightEnabled(false); - else - mChart.setHighlightEnabled(true); - mChart.invalidate(); + if(mChart.getData() != null) { + mChart.getData().setHighlightEnabled(!mChart.getData().isHighlightEnabled()); + mChart.invalidate(); + } break; } case R.id.actionToggleFilled: { diff --git a/MPChartExample/src/com/xxmassdeveloper/mpchartexample/PerformanceLineChart.java b/MPChartExample/src/com/xxmassdeveloper/mpchartexample/PerformanceLineChart.java index fa8a393d0..9a195cb4b 100644 --- a/MPChartExample/src/com/xxmassdeveloper/mpchartexample/PerformanceLineChart.java +++ b/MPChartExample/src/com/xxmassdeveloper/mpchartexample/PerformanceLineChart.java @@ -45,9 +45,6 @@ protected void onCreate(Bundle savedInstanceState) { mChart.setDescription(""); mChart.setNoDataTextDescription("You need to provide data for the chart."); - // disable value highlighting - mChart.setHighlightEnabled(false); - // enable touch gestures mChart.setTouchEnabled(true); @@ -57,10 +54,6 @@ protected void onCreate(Bundle savedInstanceState) { // if disabled, scaling can be done on x- and y-axis separately mChart.setPinchZoom(false); - - // enable/disable highlight indicators (the lines that indicate the - // highlighted Entry) - mChart.setHighlightEnabled(false); mChart.getAxisLeft().setDrawGridLines(false); mChart.getAxisRight().setEnabled(false); diff --git a/MPChartExample/src/com/xxmassdeveloper/mpchartexample/RadarChartActivitry.java b/MPChartExample/src/com/xxmassdeveloper/mpchartexample/RadarChartActivitry.java index 966072376..143bdaf6e 100644 --- a/MPChartExample/src/com/xxmassdeveloper/mpchartexample/RadarChartActivitry.java +++ b/MPChartExample/src/com/xxmassdeveloper/mpchartexample/RadarChartActivitry.java @@ -90,11 +90,10 @@ public boolean onOptionsItemSelected(MenuItem item) { break; } case R.id.actionToggleHighlight: { - if (mChart.isHighlightEnabled()) - mChart.setHighlightEnabled(false); - else - mChart.setHighlightEnabled(true); - mChart.invalidate(); + if(mChart.getData() != null) { + mChart.getData().setHighlightEnabled(!mChart.getData().isHighlightEnabled()); + mChart.invalidate(); + } break; } case R.id.actionToggleRotate: { diff --git a/MPChartExample/src/com/xxmassdeveloper/mpchartexample/RealtimeLineChartActivity.java b/MPChartExample/src/com/xxmassdeveloper/mpchartexample/RealtimeLineChartActivity.java index 286bd4182..afa181cd0 100644 --- a/MPChartExample/src/com/xxmassdeveloper/mpchartexample/RealtimeLineChartActivity.java +++ b/MPChartExample/src/com/xxmassdeveloper/mpchartexample/RealtimeLineChartActivity.java @@ -43,9 +43,6 @@ protected void onCreate(Bundle savedInstanceState) { mChart.setDescription(""); mChart.setNoDataTextDescription("You need to provide data for the chart."); - // enable value highlighting - mChart.setHighlightEnabled(true); - // enable touch gestures mChart.setTouchEnabled(true); diff --git a/MPChartExample/src/com/xxmassdeveloper/mpchartexample/ScatterChartActivity.java b/MPChartExample/src/com/xxmassdeveloper/mpchartexample/ScatterChartActivity.java index 989ed1b80..c33a77ce1 100644 --- a/MPChartExample/src/com/xxmassdeveloper/mpchartexample/ScatterChartActivity.java +++ b/MPChartExample/src/com/xxmassdeveloper/mpchartexample/ScatterChartActivity.java @@ -65,7 +65,6 @@ protected void onCreate(Bundle savedInstanceState) { mChart.setDrawGridBackground(false); mChart.setTouchEnabled(true); - mChart.setHighlightEnabled(true); // enable scaling and dragging mChart.setDragEnabled(true); @@ -109,11 +108,10 @@ public boolean onOptionsItemSelected(MenuItem item) { break; } case R.id.actionToggleHighlight: { - if (mChart.isHighlightEnabled()) - mChart.setHighlightEnabled(false); - else - mChart.setHighlightEnabled(true); - mChart.invalidate(); + if(mChart.getData() != null) { + mChart.getData().setHighlightEnabled(!mChart.getData().isHighlightEnabled()); + mChart.invalidate(); + } break; } case R.id.actionTogglePinch: { diff --git a/MPChartExample/src/com/xxmassdeveloper/mpchartexample/StackedBarActivity.java b/MPChartExample/src/com/xxmassdeveloper/mpchartexample/StackedBarActivity.java index 8ed71e49e..a62e55ecd 100644 --- a/MPChartExample/src/com/xxmassdeveloper/mpchartexample/StackedBarActivity.java +++ b/MPChartExample/src/com/xxmassdeveloper/mpchartexample/StackedBarActivity.java @@ -112,11 +112,10 @@ public boolean onOptionsItemSelected(MenuItem item) { break; } case R.id.actionToggleHighlight: { - if (mChart.isHighlightEnabled()) - mChart.setHighlightEnabled(false); - else - mChart.setHighlightEnabled(true); - mChart.invalidate(); + if(mChart.getData() != null) { + mChart.getData().setHighlightEnabled(!mChart.getData().isHighlightEnabled()); + mChart.invalidate(); + } break; } case R.id.actionTogglePinch: { diff --git a/MPChartExample/src/com/xxmassdeveloper/mpchartexample/StackedBarActivityNegative.java b/MPChartExample/src/com/xxmassdeveloper/mpchartexample/StackedBarActivityNegative.java index 89acfc268..3fa58437c 100644 --- a/MPChartExample/src/com/xxmassdeveloper/mpchartexample/StackedBarActivityNegative.java +++ b/MPChartExample/src/com/xxmassdeveloper/mpchartexample/StackedBarActivityNegative.java @@ -126,11 +126,10 @@ public boolean onOptionsItemSelected(MenuItem item) { break; } case R.id.actionToggleHighlight: { - if (mChart.isHighlightEnabled()) - mChart.setHighlightEnabled(false); - else - mChart.setHighlightEnabled(true); - mChart.invalidate(); + if(mChart.getData() != null) { + mChart.getData().setHighlightEnabled(!mChart.getData().isHighlightEnabled()); + mChart.invalidate(); + } break; } case R.id.actionTogglePinch: { diff --git a/MPChartExample/src/com/xxmassdeveloper/mpchartexample/fragments/BarChartFrag.java b/MPChartExample/src/com/xxmassdeveloper/mpchartexample/fragments/BarChartFrag.java index c33802674..98e368a4d 100644 --- a/MPChartExample/src/com/xxmassdeveloper/mpchartexample/fragments/BarChartFrag.java +++ b/MPChartExample/src/com/xxmassdeveloper/mpchartexample/fragments/BarChartFrag.java @@ -39,8 +39,6 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa MyMarkerView mv = new MyMarkerView(getActivity(), R.layout.custom_marker_view); mChart.setMarkerView(mv); - - mChart.setHighlightEnabled(false); mChart.setDrawGridBackground(false); mChart.setDrawBarShadow(false); diff --git a/MPChartExample/src/com/xxmassdeveloper/mpchartexample/fragments/ComplexityFragment.java b/MPChartExample/src/com/xxmassdeveloper/mpchartexample/fragments/ComplexityFragment.java index c4fdd7580..98b904b2b 100644 --- a/MPChartExample/src/com/xxmassdeveloper/mpchartexample/fragments/ComplexityFragment.java +++ b/MPChartExample/src/com/xxmassdeveloper/mpchartexample/fragments/ComplexityFragment.java @@ -28,16 +28,12 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa mChart = (LineChart) v.findViewById(R.id.lineChart1); mChart.setDescription(""); - - mChart.setHighlightEnabled(false); + mChart.setDrawGridBackground(false); mChart.setData(getComplexity()); mChart.animateX(3000); -// mChart.setScaleMinima(3f, 3f); -// mChart.centerViewPort(300, 0); - Typeface tf = Typeface.createFromAsset(getActivity().getAssets(),"OpenSans-Light.ttf"); Legend l = mChart.getLegend(); diff --git a/MPChartExample/src/com/xxmassdeveloper/mpchartexample/fragments/ScatterChartFrag.java b/MPChartExample/src/com/xxmassdeveloper/mpchartexample/fragments/ScatterChartFrag.java index 3e0786955..a93038c1e 100644 --- a/MPChartExample/src/com/xxmassdeveloper/mpchartexample/fragments/ScatterChartFrag.java +++ b/MPChartExample/src/com/xxmassdeveloper/mpchartexample/fragments/ScatterChartFrag.java @@ -36,7 +36,6 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa mChart.setMarkerView(mv); - mChart.setHighlightEnabled(false); mChart.setDrawGridBackground(false); mChart.setData(generateScatterData(6, 10000, 200)); diff --git a/MPChartExample/src/com/xxmassdeveloper/mpchartexample/fragments/SineCosineFragment.java b/MPChartExample/src/com/xxmassdeveloper/mpchartexample/fragments/SineCosineFragment.java index ec783286c..b2e2cda03 100644 --- a/MPChartExample/src/com/xxmassdeveloper/mpchartexample/fragments/SineCosineFragment.java +++ b/MPChartExample/src/com/xxmassdeveloper/mpchartexample/fragments/SineCosineFragment.java @@ -28,17 +28,12 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa mChart = (LineChart) v.findViewById(R.id.lineChart1); mChart.setDescription(""); -// mChart.setCircleSize(5f); - - mChart.setHighlightEnabled(false); + mChart.setDrawGridBackground(false); mChart.setData(generateLineData()); mChart.animateX(3000); -// mChart.setScaleMinima(3f, 3f); -// mChart.centerViewPort(300, 0); - Typeface tf = Typeface.createFromAsset(getActivity().getAssets(),"OpenSans-Light.ttf"); Legend l = mChart.getLegend(); diff --git a/MPChartLib/src/com/github/mikephil/charting/charts/Chart.java b/MPChartLib/src/com/github/mikephil/charting/charts/Chart.java index 5191d8c67..6b202b398 100644 --- a/MPChartLib/src/com/github/mikephil/charting/charts/Chart.java +++ b/MPChartLib/src/com/github/mikephil/charting/charts/Chart.java @@ -875,27 +875,27 @@ public void setOnChartGestureListener(OnChartGestureListener l) { public OnChartGestureListener getOnChartGestureListener() { return mGestureListener; } - - /** - * If set to true, value highlighting is enabled for all underlying data of - * the chart which means that all values can be highlighted programmatically - * or by touch gesture. - * - * @param enabled - */ - public void setHighlightEnabled(boolean enabled) { - if (mData != null) - mData.setHighlightEnabled(enabled); - } - - /** - * Returns true if highlighting of values is enabled, false if not - * - * @return - */ - public boolean isHighlightEnabled() { - return mData == null ? true : mData.isHighlightEnabled(); - } +// +// /** +// * If set to true, value highlighting is enabled for all underlying data of +// * the chart which means that all values can be highlighted programmatically +// * or by touch gesture. +// * +// * @param enabled +// */ +// public void setHighlightEnabled(boolean enabled) { +// if (mData != null) +// mData.setHighlightEnabled(enabled); +// } +// +// /** +// * Returns true if highlighting of values is enabled, false if not +// * +// * @return +// */ +// public boolean isHighlightEnabled() { +// return mData == null ? true : mData.isHighlightEnabled(); +// } /** * returns the current y-max value across all DataSets From 1fdb1827204db3398f1c4adf259ebc7f2add3ece Mon Sep 17 00:00:00 2001 From: Philipp Jahoda Date: Thu, 15 Oct 2015 18:13:32 +0200 Subject: [PATCH 11/31] Fix #924 --- .../src/com/github/mikephil/charting/charts/Chart.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/MPChartLib/src/com/github/mikephil/charting/charts/Chart.java b/MPChartLib/src/com/github/mikephil/charting/charts/Chart.java index 6b202b398..bf350dbb7 100644 --- a/MPChartLib/src/com/github/mikephil/charting/charts/Chart.java +++ b/MPChartLib/src/com/github/mikephil/charting/charts/Chart.java @@ -479,8 +479,11 @@ public void highlightValues(Highlight[] highs) { // set the indices to highlight mIndicesToHighlight = highs; - if(highs == null || highs.length == 0) + if(highs == null || highs.length <= 0 || highs[0] == null) { mChartTouchListener.setLastHighlighted(null); + } else { + mChartTouchListener.setLastHighlighted(highs[0]); + } // redraw the chart invalidate(); From 18e3b07b9d3e28e9324146a74633cf42a8a1a326 Mon Sep 17 00:00:00 2001 From: Philipp Jahoda Date: Thu, 15 Oct 2015 18:16:03 +0200 Subject: [PATCH 12/31] Add new highlighting method. --- .../mikephil/charting/charts/Chart.java | 201 ++++++++++++------ 1 file changed, 135 insertions(+), 66 deletions(-) diff --git a/MPChartLib/src/com/github/mikephil/charting/charts/Chart.java b/MPChartLib/src/com/github/mikephil/charting/charts/Chart.java index bf350dbb7..074976aa9 100644 --- a/MPChartLib/src/com/github/mikephil/charting/charts/Chart.java +++ b/MPChartLib/src/com/github/mikephil/charting/charts/Chart.java @@ -65,7 +65,9 @@ public abstract class Chart mJobs = new ArrayList(); /** * Adds a job to be executed after the chart-view is setup (after * onSizeChanged(...) is called). - * + * * @param job */ public void addJob(Runnable job) { @@ -1525,7 +1594,7 @@ public void clearAllJobs() { /** * Returns all jobs that are scheduled to be executed after * onSizeChanged(...). - * + * * @return */ public ArrayList getJobs() { @@ -1580,7 +1649,7 @@ protected void onSizeChanged(int w, int h, int oldw, int oldh) { /** * Setting this to true will set the layer-type HARDWARE for the view, false * will set layer-type SOFTWARE. - * + * * @param enabled */ public void setHardwareAccelerationEnabled(boolean enabled) { From 5cbc6b7245d6ff37972c9468daca2cda4620df51 Mon Sep 17 00:00:00 2001 From: Philipp Jahoda Date: Fri, 16 Oct 2015 12:05:49 +0200 Subject: [PATCH 13/31] Allow disabling tap-highlight while still being able to highlight programatically or via drag. --- .../mpchartexample/LineChartActivity1.java | 3 ++- .../mpchartexample/PieChartActivity.java | 1 + .../com/github/mikephil/charting/charts/Chart.java | 13 +++++++++++++ .../listener/BarLineChartTouchListener.java | 8 ++++++-- .../listener/PieRadarChartTouchListener.java | 4 ++++ 5 files changed, 26 insertions(+), 3 deletions(-) diff --git a/MPChartExample/src/com/xxmassdeveloper/mpchartexample/LineChartActivity1.java b/MPChartExample/src/com/xxmassdeveloper/mpchartexample/LineChartActivity1.java index a7ee98a6b..c75ed1d96 100644 --- a/MPChartExample/src/com/xxmassdeveloper/mpchartexample/LineChartActivity1.java +++ b/MPChartExample/src/com/xxmassdeveloper/mpchartexample/LineChartActivity1.java @@ -370,8 +370,9 @@ public void onChartGestureStart(MotionEvent me, ChartTouchListener.ChartGesture public void onChartGestureEnd(MotionEvent me, ChartTouchListener.ChartGesture lastPerformedGesture) { Log.i("Gesture", "END, lastGesture: " + lastPerformedGesture); + // un-highlight values after the gesture is finished and no single-tap if(lastPerformedGesture != ChartTouchListener.ChartGesture.SINGLE_TAP) - mChart.highlightValues(null); + mChart.highlightValues(null); // or highlightTouch(null) for callback to onNothingSelected(...) } @Override diff --git a/MPChartExample/src/com/xxmassdeveloper/mpchartexample/PieChartActivity.java b/MPChartExample/src/com/xxmassdeveloper/mpchartexample/PieChartActivity.java index 5874f8268..69fd7d950 100644 --- a/MPChartExample/src/com/xxmassdeveloper/mpchartexample/PieChartActivity.java +++ b/MPChartExample/src/com/xxmassdeveloper/mpchartexample/PieChartActivity.java @@ -85,6 +85,7 @@ protected void onCreate(Bundle savedInstanceState) { mChart.setRotationAngle(0); // enable rotation of the chart by touch mChart.setRotationEnabled(true); + mChart.setHighLightPerTapEnabled(false); // mChart.setUnit(" €"); // mChart.setDrawUnitsInChart(true); diff --git a/MPChartLib/src/com/github/mikephil/charting/charts/Chart.java b/MPChartLib/src/com/github/mikephil/charting/charts/Chart.java index 074976aa9..7638636fd 100644 --- a/MPChartLib/src/com/github/mikephil/charting/charts/Chart.java +++ b/MPChartLib/src/com/github/mikephil/charting/charts/Chart.java @@ -76,6 +76,11 @@ public abstract class Chart Date: Fri, 16 Oct 2015 12:06:24 +0200 Subject: [PATCH 14/31] Remove unused variable --- .../src/com/github/mikephil/charting/charts/Chart.java | 5 ----- 1 file changed, 5 deletions(-) diff --git a/MPChartLib/src/com/github/mikephil/charting/charts/Chart.java b/MPChartLib/src/com/github/mikephil/charting/charts/Chart.java index 7638636fd..17034de5c 100644 --- a/MPChartLib/src/com/github/mikephil/charting/charts/Chart.java +++ b/MPChartLib/src/com/github/mikephil/charting/charts/Chart.java @@ -121,11 +121,6 @@ public abstract class Chart Date: Fri, 16 Oct 2015 12:07:12 +0200 Subject: [PATCH 15/31] Add docs --- .../src/com/github/mikephil/charting/data/ChartData.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/MPChartLib/src/com/github/mikephil/charting/data/ChartData.java b/MPChartLib/src/com/github/mikephil/charting/data/ChartData.java index ff60bbfde..d61534788 100644 --- a/MPChartLib/src/com/github/mikephil/charting/data/ChartData.java +++ b/MPChartLib/src/com/github/mikephil/charting/data/ChartData.java @@ -963,7 +963,8 @@ public void setDrawValues(boolean enabled) { /** * Enables / disables highlighting values for all DataSets this data object - * contains. + * contains. If set to true, this means that values can + * be highlighted programmatically or by touch gesture. */ public void setHighlightEnabled(boolean enabled) { for (DataSet set : mDataSets) { From 2e475b8d7dd81570959f5d13ca7808505d6d825a Mon Sep 17 00:00:00 2001 From: Philipp Jahoda Date: Fri, 16 Oct 2015 14:30:48 +0200 Subject: [PATCH 16/31] Improve touch-listener code style --- .../StackedBarActivityNegative.java | 2 +- .../listener/BarLineChartTouchListener.java | 22 +++---------------- .../charting/listener/ChartTouchListener.java | 17 ++++++++++++++ .../listener/PieRadarChartTouchListener.java | 11 +--------- 4 files changed, 22 insertions(+), 30 deletions(-) diff --git a/MPChartExample/src/com/xxmassdeveloper/mpchartexample/StackedBarActivityNegative.java b/MPChartExample/src/com/xxmassdeveloper/mpchartexample/StackedBarActivityNegative.java index 3fa58437c..45a672ba1 100644 --- a/MPChartExample/src/com/xxmassdeveloper/mpchartexample/StackedBarActivityNegative.java +++ b/MPChartExample/src/com/xxmassdeveloper/mpchartexample/StackedBarActivityNegative.java @@ -209,7 +209,7 @@ public void onValueSelected(Entry e, int dataSetIndex, Highlight h) { @Override public void onNothingSelected() { // TODO Auto-generated method stub - + Log.i("NOTING SELECTED", ""); } private class CustomFormatter implements ValueFormatter, YAxisValueFormatter { diff --git a/MPChartLib/src/com/github/mikephil/charting/listener/BarLineChartTouchListener.java b/MPChartLib/src/com/github/mikephil/charting/listener/BarLineChartTouchListener.java index 0a74bec20..4430cc619 100644 --- a/MPChartLib/src/com/github/mikephil/charting/listener/BarLineChartTouchListener.java +++ b/MPChartLib/src/com/github/mikephil/charting/listener/BarLineChartTouchListener.java @@ -372,24 +372,6 @@ private void performZoom(MotionEvent event) { } } - /** - * Perform a highlight operation. - * - * @param e - */ - private void performHighlight(MotionEvent e) { - - Highlight h = mChart.getHighlightByTouchPoint(e.getX(), e.getY()); - - if (h == null || h.equalTo(mLastHighlighted)) { - mChart.highlightTouch(null); - mLastHighlighted = null; - } else { - mLastHighlighted = h; - mChart.highlightTouch(h); - } - } - /** * Highlights upon dragging, generates callbacks for the selection-listener. * @@ -554,7 +536,9 @@ public boolean onSingleTapUp(MotionEvent e) { return false; } - performHighlight(e); + + Highlight h = mChart.getHighlightByTouchPoint(e.getX(), e.getY()); + performHighlight(h, e); return super.onSingleTapUp(e); } diff --git a/MPChartLib/src/com/github/mikephil/charting/listener/ChartTouchListener.java b/MPChartLib/src/com/github/mikephil/charting/listener/ChartTouchListener.java index f5aadcf06..eef16c846 100644 --- a/MPChartLib/src/com/github/mikephil/charting/listener/ChartTouchListener.java +++ b/MPChartLib/src/com/github/mikephil/charting/listener/ChartTouchListener.java @@ -109,6 +109,23 @@ public ChartGesture getLastGesture() { return mLastGesture; } + + /** + * Perform a highlight operation. + * + * @param e + */ + protected void performHighlight(Highlight h, MotionEvent e) { + + if (h == null || h.equalTo(mLastHighlighted)) { + mChart.highlightTouch(null); + mLastHighlighted = null; + } else { + mLastHighlighted = h; + mChart.highlightTouch(h); + } + } + /** * returns the distance between two points * diff --git a/MPChartLib/src/com/github/mikephil/charting/listener/PieRadarChartTouchListener.java b/MPChartLib/src/com/github/mikephil/charting/listener/PieRadarChartTouchListener.java index 1657d1a1a..50c61a419 100644 --- a/MPChartLib/src/com/github/mikephil/charting/listener/PieRadarChartTouchListener.java +++ b/MPChartLib/src/com/github/mikephil/charting/listener/PieRadarChartTouchListener.java @@ -201,16 +201,7 @@ public boolean onSingleTapUp(MotionEvent e) { mLastHighlighted = null; } else { Highlight h = new Highlight(index, dataSetIndex); - - if (h.equalTo(mLastHighlighted)) { - - mChart.highlightTouch(null); - mLastHighlighted = null; - } else { - - mChart.highlightTouch(h); - mLastHighlighted = h; - } + performHighlight(h, e); } } } From 5190f8198c23d100b77bc95ca61ef651808812f7 Mon Sep 17 00:00:00 2001 From: Philipp Jahoda Date: Fri, 16 Oct 2015 17:05:15 +0200 Subject: [PATCH 17/31] Code cleanup, documentation. --- .../mpchartexample/PieChartActivity.java | 2 +- .../github/mikephil/charting/charts/Chart.java | 15 +++++++++++++-- .../listener/BarLineChartTouchListener.java | 2 +- .../listener/PieRadarChartTouchListener.java | 2 +- 4 files changed, 16 insertions(+), 5 deletions(-) diff --git a/MPChartExample/src/com/xxmassdeveloper/mpchartexample/PieChartActivity.java b/MPChartExample/src/com/xxmassdeveloper/mpchartexample/PieChartActivity.java index 69fd7d950..72a84b937 100644 --- a/MPChartExample/src/com/xxmassdeveloper/mpchartexample/PieChartActivity.java +++ b/MPChartExample/src/com/xxmassdeveloper/mpchartexample/PieChartActivity.java @@ -85,7 +85,7 @@ protected void onCreate(Bundle savedInstanceState) { mChart.setRotationAngle(0); // enable rotation of the chart by touch mChart.setRotationEnabled(true); - mChart.setHighLightPerTapEnabled(false); + mChart.setHighlightPerTapEnabled(true); // mChart.setUnit(" €"); // mChart.setDrawUnitsInChart(true); diff --git a/MPChartLib/src/com/github/mikephil/charting/charts/Chart.java b/MPChartLib/src/com/github/mikephil/charting/charts/Chart.java index 17034de5c..a5eec8893 100644 --- a/MPChartLib/src/com/github/mikephil/charting/charts/Chart.java +++ b/MPChartLib/src/com/github/mikephil/charting/charts/Chart.java @@ -495,11 +495,22 @@ public Highlight[] getHighlighted() { return mIndicesToHighlight; } - public boolean isHighLightPerTapEnabled() { + /** + * Returns true if values can be highlighted via tap gesture, false if not. + * + * @return + */ + public boolean isHighlightPerTapEnabled() { return mHighLightPerTapEnabled; } - public void setHighLightPerTapEnabled(boolean enabled) { + /** + * Set this to false to prevent values from being highlighted by tap gesture. + * Values can still be highlighted via drag or programmatically. Default: true + * + * @param enabled + */ + public void setHighlightPerTapEnabled(boolean enabled) { mHighLightPerTapEnabled = enabled; } diff --git a/MPChartLib/src/com/github/mikephil/charting/listener/BarLineChartTouchListener.java b/MPChartLib/src/com/github/mikephil/charting/listener/BarLineChartTouchListener.java index 4430cc619..8a6f3d4a8 100644 --- a/MPChartLib/src/com/github/mikephil/charting/listener/BarLineChartTouchListener.java +++ b/MPChartLib/src/com/github/mikephil/charting/listener/BarLineChartTouchListener.java @@ -532,7 +532,7 @@ public boolean onSingleTapUp(MotionEvent e) { l.onChartSingleTapped(e); } - if(!mChart.isHighLightPerTapEnabled()) { + if(!mChart.isHighlightPerTapEnabled()) { return false; } diff --git a/MPChartLib/src/com/github/mikephil/charting/listener/PieRadarChartTouchListener.java b/MPChartLib/src/com/github/mikephil/charting/listener/PieRadarChartTouchListener.java index 50c61a419..9163c823d 100644 --- a/MPChartLib/src/com/github/mikephil/charting/listener/PieRadarChartTouchListener.java +++ b/MPChartLib/src/com/github/mikephil/charting/listener/PieRadarChartTouchListener.java @@ -147,7 +147,7 @@ public boolean onSingleTapUp(MotionEvent e) { l.onChartSingleTapped(e); } - if(!mChart.isHighLightPerTapEnabled()) { + if(!mChart.isHighlightPerTapEnabled()) { return false; } From 8065d17016b3d0f3e2406e27705233d7980d1a7c Mon Sep 17 00:00:00 2001 From: Philipp Jahoda Date: Mon, 19 Oct 2015 14:20:30 +0200 Subject: [PATCH 18/31] Critical fix in radarchart (issue #1121) --- .../src/com/github/mikephil/charting/charts/RadarChart.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/MPChartLib/src/com/github/mikephil/charting/charts/RadarChart.java b/MPChartLib/src/com/github/mikephil/charting/charts/RadarChart.java index 7de9699de..c619cad9a 100644 --- a/MPChartLib/src/com/github/mikephil/charting/charts/RadarChart.java +++ b/MPChartLib/src/com/github/mikephil/charting/charts/RadarChart.java @@ -60,7 +60,7 @@ public class RadarChart extends PieRadarChartBase { /** * modulus that determines how many labels and web-lines are skipped before the next is drawn */ - private int mSkipWebLineCount = 1; + private int mSkipWebLineCount = 0; /** * the object reprsenting the y-axis labels From 5f5950b4414fc6294f333870deda0341ea0dd9fc Mon Sep 17 00:00:00 2001 From: Philipp Jahoda Date: Fri, 23 Oct 2015 13:58:43 +0200 Subject: [PATCH 19/31] Fix issue #1136 --- .../mpchartexample/custom/MyMarkerView.java | 4 +- .../custom/StackedBarsMarkerView.java | 4 +- .../charting/components/MarkerView.java | 127 +++++++++--------- 3 files changed, 67 insertions(+), 68 deletions(-) diff --git a/MPChartExample/src/com/xxmassdeveloper/mpchartexample/custom/MyMarkerView.java b/MPChartExample/src/com/xxmassdeveloper/mpchartexample/custom/MyMarkerView.java index 836e7befb..4198d14f1 100644 --- a/MPChartExample/src/com/xxmassdeveloper/mpchartexample/custom/MyMarkerView.java +++ b/MPChartExample/src/com/xxmassdeveloper/mpchartexample/custom/MyMarkerView.java @@ -43,13 +43,13 @@ public void refreshContent(Entry e, Highlight highlight) { } @Override - public int getXOffset() { + public int getXOffset(float xpos) { // this will center the marker-view horizontally return -(getWidth() / 2); } @Override - public int getYOffset() { + public int getYOffset(float ypos) { // this will cause the marker-view to be above the selected value return -getHeight(); } diff --git a/MPChartExample/src/com/xxmassdeveloper/mpchartexample/custom/StackedBarsMarkerView.java b/MPChartExample/src/com/xxmassdeveloper/mpchartexample/custom/StackedBarsMarkerView.java index 3b3b5e186..9be2f30ac 100644 --- a/MPChartExample/src/com/xxmassdeveloper/mpchartexample/custom/StackedBarsMarkerView.java +++ b/MPChartExample/src/com/xxmassdeveloper/mpchartexample/custom/StackedBarsMarkerView.java @@ -50,13 +50,13 @@ public void refreshContent(Entry e, Highlight highlight) { } @Override - public int getXOffset() { + public int getXOffset(float xpos) { // this will center the marker-view horizontally return -(getWidth() / 2); } @Override - public int getYOffset() { + public int getYOffset(float ypos) { // this will cause the marker-view to be above the selected value return -getHeight(); } diff --git a/MPChartLib/src/com/github/mikephil/charting/components/MarkerView.java b/MPChartLib/src/com/github/mikephil/charting/components/MarkerView.java index 7a65b7e23..523376c78 100644 --- a/MPChartLib/src/com/github/mikephil/charting/components/MarkerView.java +++ b/MPChartLib/src/com/github/mikephil/charting/components/MarkerView.java @@ -17,78 +17,77 @@ */ public abstract class MarkerView extends RelativeLayout { - /** - * Constructor. Sets up the MarkerView with a custom layout resource. - * - * @param context - * @param layoutResource - * the layout resource to use for the MarkerView - */ - public MarkerView(Context context, int layoutResource) { - super(context); - setupLayoutResource(layoutResource); - } + /** + * Constructor. Sets up the MarkerView with a custom layout resource. + * + * @param context + * @param layoutResource the layout resource to use for the MarkerView + */ + public MarkerView(Context context, int layoutResource) { + super(context); + setupLayoutResource(layoutResource); + } - /** - * Sets the layout resource for a custom MarkerView. - * - * @param layoutResource - */ - private void setupLayoutResource(int layoutResource) { + /** + * Sets the layout resource for a custom MarkerView. + * + * @param layoutResource + */ + private void setupLayoutResource(int layoutResource) { - View inflated = LayoutInflater.from(getContext()).inflate(layoutResource, this); + View inflated = LayoutInflater.from(getContext()).inflate(layoutResource, this); - inflated.setLayoutParams(new LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT)); - inflated.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)); + inflated.setLayoutParams(new LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT)); + inflated.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)); - // measure(getWidth(), getHeight()); - inflated.layout(0, 0, inflated.getMeasuredWidth(), inflated.getMeasuredHeight()); - } + // measure(getWidth(), getHeight()); + inflated.layout(0, 0, inflated.getMeasuredWidth(), inflated.getMeasuredHeight()); + } - /** - * Draws the MarkerView on the given position on the screen with the given Canvas object. - * - * @param canvas - * @param posx - * @param posy - */ - public void draw(Canvas canvas, float posx, float posy) { + /** + * Draws the MarkerView on the given position on the screen with the given Canvas object. + * + * @param canvas + * @param posx + * @param posy + */ + public void draw(Canvas canvas, float posx, float posy) { - // take offsets into consideration - posx += getXOffset(); - posy += getYOffset(); + // take offsets into consideration + posx += getXOffset(posx); + posy += getYOffset(posy); - // translate to the correct position and draw - canvas.translate(posx, posy); - draw(canvas); - canvas.translate(-posx, -posy); - } + // translate to the correct position and draw + canvas.translate(posx, posy); + draw(canvas); + canvas.translate(-posx, -posy); + } - /** - * This method enables a specified custom MarkerView to update it's content everytime the MarkerView is redrawn. - * - * @param e - * The Entry the MarkerView belongs to. This can also be any subclass of Entry, like BarEntry or - * CandleEntry, simply cast it at runtime. - * @param highlight - * the highlight object contains information about the highlighted value such as it's dataset-index, the - * selected range or stack-index (only stacked bar entries). - */ - public abstract void refreshContent(Entry e, Highlight highlight); + /** + * This method enables a specified custom MarkerView to update it's content everytime the MarkerView is redrawn. + * + * @param e The Entry the MarkerView belongs to. This can also be any subclass of Entry, like BarEntry or + * CandleEntry, simply cast it at runtime. + * @param highlight the highlight object contains information about the highlighted value such as it's dataset-index, the + * selected range or stack-index (only stacked bar entries). + */ + public abstract void refreshContent(Entry e, Highlight highlight); - /** - * Use this to return the desired offset you wish the MarkerView to have on the x-axis. By returning -(getWidth() / - * 2) you will center the MarkerView horizontally. - * - * @return - */ - public abstract int getXOffset(); + /** + * Use this to return the desired offset you wish the MarkerView to have on the x-axis. By returning -(getWidth() / + * 2) you will center the MarkerView horizontally. + * + * @param xpos the position on the x-axis in pixels where the marker is drawn + * @return + */ + public abstract int getXOffset(float xpos); - /** - * Use this to return the desired position offset you wish the MarkerView to have on the y-axis. By returning - * -getHeight() you will cause the MarkerView to be above the selected value. - * - * @return - */ - public abstract int getYOffset(); + /** + * Use this to return the desired position offset you wish the MarkerView to have on the y-axis. By returning + * -getHeight() you will cause the MarkerView to be above the selected value. + * + * @param ypos the position on the y-axis in pixels where the marker is drawn + * @return + */ + public abstract int getYOffset(float ypos); } From 58845ebd7d4c88db0cfec572eb6b1f9c0fd9590c Mon Sep 17 00:00:00 2001 From: Daniel Cohen Gindi Date: Fri, 23 Oct 2015 15:02:46 +0300 Subject: [PATCH 20/31] Fixed a potential crash in CombinedChart with lineData and only one x-value Deployed same deltaX fix as in LineChart --- .../com/github/mikephil/charting/charts/CombinedChart.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/MPChartLib/src/com/github/mikephil/charting/charts/CombinedChart.java b/MPChartLib/src/com/github/mikephil/charting/charts/CombinedChart.java index 9c4be80cf..f74fee2f1 100644 --- a/MPChartLib/src/com/github/mikephil/charting/charts/CombinedChart.java +++ b/MPChartLib/src/com/github/mikephil/charting/charts/CombinedChart.java @@ -102,6 +102,10 @@ protected void calcMinMax() { } mDeltaX = Math.abs(mXChartMax - mXChartMin); + + if (mDeltaX == 0.f && getLineData() != null && getLineData().getYValCount() > 0) { + mDeltaX = 1.f; + } } @Override From 25336b88dc7783ba839d76b5ff656fe107d60c32 Mon Sep 17 00:00:00 2001 From: Daniel Cohen Gindi Date: Fri, 23 Oct 2015 15:05:10 +0300 Subject: [PATCH 21/31] Fixed potential crash when line data is empty --- .../github/mikephil/charting/renderer/LineChartRenderer.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/MPChartLib/src/com/github/mikephil/charting/renderer/LineChartRenderer.java b/MPChartLib/src/com/github/mikephil/charting/renderer/LineChartRenderer.java index 147229cb8..725fea540 100644 --- a/MPChartLib/src/com/github/mikephil/charting/renderer/LineChartRenderer.java +++ b/MPChartLib/src/com/github/mikephil/charting/renderer/LineChartRenderer.java @@ -485,7 +485,8 @@ protected void drawCircles(Canvas c) { LineDataSet dataSet = dataSets.get(i); - if (!dataSet.isVisible() || !dataSet.isDrawCirclesEnabled()) + if (!dataSet.isVisible() || !dataSet.isDrawCirclesEnabled() || + dataSet.getEntryCount() == 0) continue; mCirclePaintInner.setColor(dataSet.getCircleHoleColor()); From 9a3d2ad1caff9ae6c0da8f28037ac49330b74cb6 Mon Sep 17 00:00:00 2001 From: Daniel Cohen Gindi Date: Fri, 23 Oct 2015 15:12:36 +0300 Subject: [PATCH 22/31] Moved highlightTouch logic to highlightValue & deprecated highlightTouch --- .../mikephil/charting/charts/Chart.java | 21 ++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/MPChartLib/src/com/github/mikephil/charting/charts/Chart.java b/MPChartLib/src/com/github/mikephil/charting/charts/Chart.java index a5eec8893..5745b32df 100644 --- a/MPChartLib/src/com/github/mikephil/charting/charts/Chart.java +++ b/MPChartLib/src/com/github/mikephil/charting/charts/Chart.java @@ -572,14 +572,12 @@ public void highlightValue(int xIndex, int dataSetIndex) { /** * Highlights the values represented by the provided Highlight object + * This DOES NOT generate a callback to the OnChartValueSelectedListener. * * @param highlight contains information about which entry should be highlighted */ public void highlightValue(Highlight highlight) { - if (highlight == null) - highlightValues(null); - else - highlightValues(new Highlight[]{highlight}); + highlightValue(highlight); } /** @@ -587,9 +585,10 @@ public void highlightValue(Highlight highlight) { * highlightValues(...), this generates a callback to the * OnChartValueSelectedListener. * - * @param high + * @param high - the highlight object + * @param callListener - call the listener */ - public void highlightTouch(Highlight high) { + public void highlightValue(Highlight high, boolean callListener) { Entry e = null; @@ -612,7 +611,7 @@ public void highlightTouch(Highlight high) { } } - if (mSelectionListener != null) { + if (callListener && mSelectionListener != null) { if (!valuesToHighlight()) mSelectionListener.onNothingSelected(); @@ -625,6 +624,14 @@ public void highlightTouch(Highlight high) { invalidate(); } + /** + * Deprecated. Calls highlightValue(high, true) + */ + @Deprecated + public void highlightTouch(Highlight high) { + highlightValue(high, true); + } + /** * Set a new (e.g. custom) ChartTouchListener NOTE: make sure to * setTouchEnabled(true); if you need touch gestures on the chart From 0298122d206fa65fa1d4c61586ede1043f616a01 Mon Sep 17 00:00:00 2001 From: Daniel Cohen Gindi Date: Fri, 23 Oct 2015 15:16:14 +0300 Subject: [PATCH 23/31] Fixed the avoidFirstLastClipping feature --- .../charting/renderer/XAxisRendererBarChart.java | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/MPChartLib/src/com/github/mikephil/charting/renderer/XAxisRendererBarChart.java b/MPChartLib/src/com/github/mikephil/charting/renderer/XAxisRendererBarChart.java index 1a1d304be..a9d0c07ec 100644 --- a/MPChartLib/src/com/github/mikephil/charting/renderer/XAxisRendererBarChart.java +++ b/MPChartLib/src/com/github/mikephil/charting/renderer/XAxisRendererBarChart.java @@ -60,15 +60,16 @@ protected void drawLabels(Canvas c, float pos) { if (i == mXAxis.getValues().size() - 1) { float width = Utils.calcTextWidth(mAxisLabelPaint, label); - if (width > mViewPortHandler.offsetRight() * 2 - && position[0] + width > mViewPortHandler.getChartWidth()) - position[0] -= width / 2; + if (position[0] + width / 2.f > mViewPortHandler.contentRight()) + position[0] = mViewPortHandler.contentRight() - (width / 2.f); // avoid clipping of the first } else if (i == 0) { float width = Utils.calcTextWidth(mAxisLabelPaint, label); - position[0] += width / 2; + + if (position[0] - width / 2.f < mViewPortHandler.contentLeft()) + position[0] = mViewPortHandler.contentLeft() + (width / 2.f); } } From 548381bcf5dd58c6cfd17ef2249468a52504dca1 Mon Sep 17 00:00:00 2001 From: Daniel Cohen Gindi Date: Fri, 23 Oct 2015 16:49:12 +0300 Subject: [PATCH 24/31] Fix missing argument causing an overflow loop --- MPChartLib/src/com/github/mikephil/charting/charts/Chart.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/MPChartLib/src/com/github/mikephil/charting/charts/Chart.java b/MPChartLib/src/com/github/mikephil/charting/charts/Chart.java index 5745b32df..f79aa065d 100644 --- a/MPChartLib/src/com/github/mikephil/charting/charts/Chart.java +++ b/MPChartLib/src/com/github/mikephil/charting/charts/Chart.java @@ -577,7 +577,7 @@ public void highlightValue(int xIndex, int dataSetIndex) { * @param highlight contains information about which entry should be highlighted */ public void highlightValue(Highlight highlight) { - highlightValue(highlight); + highlightValue(highlight, false); } /** From 8490f80d38a7dccd7e02ab012ebbef6aa02e7e18 Mon Sep 17 00:00:00 2001 From: Daniel Cohen Gindi Date: Tue, 27 Oct 2015 22:30:57 +0200 Subject: [PATCH 25/31] Implemented support for rotated labels on the x-axis --- .../charting/charts/BarLineChartBase.java | 10 +- .../charting/charts/HorizontalBarChart.java | 5 +- .../charting/charts/PieRadarChartBase.java | 2 +- .../mikephil/charting/charts/RadarChart.java | 4 +- .../mikephil/charting/components/XAxis.java | 44 ++++++++- .../charting/renderer/XAxisRenderer.java | 62 +++++++++---- .../renderer/XAxisRendererBarChart.java | 7 +- .../XAxisRendererHorizontalBarChart.java | 55 +++++++---- .../renderer/XAxisRendererRadarChart.java | 8 +- .../github/mikephil/charting/utils/Utils.java | 92 +++++++++++++++++++ 10 files changed, 235 insertions(+), 54 deletions(-) diff --git a/MPChartLib/src/com/github/mikephil/charting/charts/BarLineChartBase.java b/MPChartLib/src/com/github/mikephil/charting/charts/BarLineChartBase.java index 3a89c2aad..a528c282f 100644 --- a/MPChartLib/src/com/github/mikephil/charting/charts/BarLineChartBase.java +++ b/MPChartLib/src/com/github/mikephil/charting/charts/BarLineChartBase.java @@ -489,7 +489,7 @@ public void calculateOffsets() { if (mXAxis.isEnabled() && mXAxis.isDrawLabelsEnabled()) { - float xlabelheight = mXAxis.mLabelHeight * 2f; + float xlabelheight = mXAxis.mLabelRotatedHeight + mXAxis.getYOffset(); // offsets for x-labels if (mXAxis.getPosition() == XAxisPosition.BOTTOM) { @@ -545,14 +545,16 @@ protected void calcModulus() { mViewPortHandler.getMatrixTouch().getValues(values); mXAxis.mAxisLabelModulus = (int) Math - .ceil((mData.getXValCount() * mXAxis.mLabelWidth) + .ceil((mData.getXValCount() * mXAxis.mLabelRotatedWidth) / (mViewPortHandler.contentWidth() * values[Matrix.MSCALE_X])); } if (mLogEnabled) - Log.i(LOG_TAG, "X-Axis modulus: " + mXAxis.mAxisLabelModulus + ", x-axis label width: " - + mXAxis.mLabelWidth + ", content width: " + mViewPortHandler.contentWidth()); + Log.i(LOG_TAG, "X-Axis modulus: " + mXAxis.mAxisLabelModulus + + ", x-axis label width: " + mXAxis.mLabelWidth + + ", x-axis label rotated width: " + mXAxis.mLabelRotatedWidth + + ", content width: " + mViewPortHandler.contentWidth()); if (mXAxis.mAxisLabelModulus < 1) mXAxis.mAxisLabelModulus = 1; diff --git a/MPChartLib/src/com/github/mikephil/charting/charts/HorizontalBarChart.java b/MPChartLib/src/com/github/mikephil/charting/charts/HorizontalBarChart.java index 021a5b825..9e3b0f1ac 100644 --- a/MPChartLib/src/com/github/mikephil/charting/charts/HorizontalBarChart.java +++ b/MPChartLib/src/com/github/mikephil/charting/charts/HorizontalBarChart.java @@ -108,7 +108,7 @@ public void calculateOffsets() { offsetBottom += mAxisRight.getRequiredHeightSpace(mAxisRendererRight.getPaintAxisLabels()); } - float xlabelwidth = mXAxis.mLabelWidth; + float xlabelwidth = mXAxis.mLabelRotatedWidth; if (mXAxis.isEnabled()) { @@ -162,7 +162,8 @@ protected void calcModulus() { float[] values = new float[9]; mViewPortHandler.getMatrixTouch().getValues(values); - mXAxis.mAxisLabelModulus = (int) Math.ceil((mData.getXValCount() * mXAxis.mLabelHeight) + mXAxis.mAxisLabelModulus = + (int) Math.ceil((mData.getXValCount() * mXAxis.mLabelRotatedHeight) / (mViewPortHandler.contentHeight() * values[Matrix.MSCALE_Y])); if (mXAxis.mAxisLabelModulus < 1) diff --git a/MPChartLib/src/com/github/mikephil/charting/charts/PieRadarChartBase.java b/MPChartLib/src/com/github/mikephil/charting/charts/PieRadarChartBase.java index 881ea65f9..3abbc7d81 100644 --- a/MPChartLib/src/com/github/mikephil/charting/charts/PieRadarChartBase.java +++ b/MPChartLib/src/com/github/mikephil/charting/charts/PieRadarChartBase.java @@ -218,7 +218,7 @@ public void calculateOffsets() { XAxis x = ((RadarChart) this).getXAxis(); if (x.isEnabled() && x.isDrawLabelsEnabled()) { - minOffset = Math.max(minOffset, x.mLabelWidth); + minOffset = Math.max(minOffset, x.mLabelRotatedWidth); } } diff --git a/MPChartLib/src/com/github/mikephil/charting/charts/RadarChart.java b/MPChartLib/src/com/github/mikephil/charting/charts/RadarChart.java index c619cad9a..6a92830c9 100644 --- a/MPChartLib/src/com/github/mikephil/charting/charts/RadarChart.java +++ b/MPChartLib/src/com/github/mikephil/charting/charts/RadarChart.java @@ -377,7 +377,9 @@ protected float getRequiredLegendOffset() { @Override protected float getRequiredBaseOffset() { - return mXAxis.isEnabled() && mXAxis.isDrawLabelsEnabled() ? mXAxis.mLabelWidth : Utils.convertDpToPixel(10f); + return mXAxis.isEnabled() && mXAxis.isDrawLabelsEnabled() ? + mXAxis.mLabelRotatedWidth : + Utils.convertDpToPixel(10f); } @Override diff --git a/MPChartLib/src/com/github/mikephil/charting/components/XAxis.java b/MPChartLib/src/com/github/mikephil/charting/components/XAxis.java index 40172a599..0578967cb 100644 --- a/MPChartLib/src/com/github/mikephil/charting/components/XAxis.java +++ b/MPChartLib/src/com/github/mikephil/charting/components/XAxis.java @@ -3,6 +3,7 @@ import com.github.mikephil.charting.formatter.DefaultXAxisValueFormatter; import com.github.mikephil.charting.formatter.XAxisValueFormatter; +import com.github.mikephil.charting.utils.Utils; import java.util.ArrayList; import java.util.List; @@ -20,17 +21,34 @@ public class XAxis extends AxisBase { protected List mValues = new ArrayList(); /** - * width of the x-axis labels in pixels - this is calculated by the - * calcTextWidth() method of the utils + * width of the x-axis labels in pixels - this is automatically + * calculated by the computeAxis() methods in the renderers */ public int mLabelWidth = 1; /** - * height of the x-axis labels in pixels - this is calculated by the - * calcTextHeight() method of the utils + * height of the x-axis labels in pixels - this is automatically + * calculated by the computeAxis() methods in the renderers */ public int mLabelHeight = 1; + /** + * width of the (rotated) x-axis labels in pixels - this is automatically + * calculated by the computeAxis() methods in the renderers + */ + public int mLabelRotatedWidth = 1; + + /** + * height of the (rotated) x-axis labels in pixels - this is automatically + * calculated by the computeAxis() methods in the renderers + */ + public int mLabelRotatedHeight = 1; + + /** + * This is the angle for drawing the X axis labels (in degrees) + */ + protected float mLabelRotationAngle = 0.f; + /** * the space that should be left out (in characters) between the x-axis * labels @@ -78,6 +96,8 @@ public enum XAxisPosition { public XAxis() { super(); + + mYOffset = Utils.convertDpToPixel(4.f); } /** @@ -96,6 +116,22 @@ public void setPosition(XAxisPosition pos) { mPosition = pos; } + /** + * returns the angle for drawing the X axis labels (in degrees) + */ + public float getLabelRotationAngle() { + return mLabelRotationAngle; + } + + /** + * sets the angle for drawing the X axis labels (in degrees) + * + * @param angle the angle in degrees + */ + public void setLabelRotationAngle(float angle) { + mLabelRotationAngle = angle; + } + /** * Sets the space (in characters) that should be left out between the x-axis * labels, default 4. This only applies if the number of labels that will be diff --git a/MPChartLib/src/com/github/mikephil/charting/renderer/XAxisRenderer.java b/MPChartLib/src/com/github/mikephil/charting/renderer/XAxisRenderer.java index 7eb6fb5ad..51109ca23 100644 --- a/MPChartLib/src/com/github/mikephil/charting/renderer/XAxisRenderer.java +++ b/MPChartLib/src/com/github/mikephil/charting/renderer/XAxisRenderer.java @@ -6,10 +6,13 @@ import android.graphics.Paint; import android.graphics.Paint.Align; import android.graphics.Path; +import android.graphics.PointF; +import android.util.Size; import com.github.mikephil.charting.components.LimitLine; import com.github.mikephil.charting.components.XAxis; import com.github.mikephil.charting.components.XAxis.XAxisPosition; +import com.github.mikephil.charting.utils.FSize; import com.github.mikephil.charting.utils.Transformer; import com.github.mikephil.charting.utils.Utils; import com.github.mikephil.charting.utils.ViewPortHandler; @@ -35,17 +38,30 @@ public void computeAxis(float xValAverageLength, List xValues) { mAxisLabelPaint.setTypeface(mXAxis.getTypeface()); mAxisLabelPaint.setTextSize(mXAxis.getTextSize()); - StringBuffer a = new StringBuffer(); + StringBuilder widthText = new StringBuilder(); - int max = (int) Math.round(xValAverageLength + int max = Math.round(xValAverageLength + mXAxis.getSpaceBetweenLabels()); for (int i = 0; i < max; i++) { - a.append('h'); + widthText.append('h'); } - mXAxis.mLabelWidth = Utils.calcTextWidth(mAxisLabelPaint, a.toString()); - mXAxis.mLabelHeight = Utils.calcTextHeight(mAxisLabelPaint, "Q"); + final FSize labelSize = Utils.calcTextSize(mAxisLabelPaint, widthText.toString()); + + final float labelWidth = labelSize.width; + final float labelHeight = Utils.calcTextHeight(mAxisLabelPaint, "Q"); + + final FSize labelRotatedSize = Utils.getSizeOfRotatedRectangleByDegrees( + labelWidth, + labelHeight, + mXAxis.getLabelRotationAngle()); + + mXAxis.mLabelWidth = Math.round(labelWidth); + mXAxis.mLabelHeight = Math.round(labelHeight); + mXAxis.mLabelRotatedWidth = Math.round(labelRotatedSize.width); + mXAxis.mLabelRotatedHeight = Math.round(labelRotatedSize.height); + mXAxis.setValues(xValues); } @@ -55,7 +71,7 @@ public void renderAxisLabels(Canvas c) { if (!mXAxis.isEnabled() || !mXAxis.isDrawLabelsEnabled()) return; - float yoffset = Utils.convertDpToPixel(4f); + float yoffset = mXAxis.getYOffset(); mAxisLabelPaint.setTypeface(mXAxis.getTypeface()); mAxisLabelPaint.setTextSize(mXAxis.getTextSize()); @@ -63,24 +79,30 @@ public void renderAxisLabels(Canvas c) { if (mXAxis.getPosition() == XAxisPosition.TOP) { - drawLabels(c, mViewPortHandler.offsetTop() - yoffset); + drawLabels(c, mViewPortHandler.contentTop() - yoffset, + new PointF(0.5f, 1.0f)); - } else if (mXAxis.getPosition() == XAxisPosition.BOTTOM) { + } else if (mXAxis.getPosition() == XAxisPosition.TOP_INSIDE) { - drawLabels(c, mViewPortHandler.contentBottom() + mXAxis.mLabelHeight + yoffset * 1.5f); + drawLabels(c, mViewPortHandler.contentTop() + yoffset + mXAxis.mLabelRotatedHeight, + new PointF(0.5f, 1.0f)); - } else if (mXAxis.getPosition() == XAxisPosition.BOTTOM_INSIDE) { + } else if (mXAxis.getPosition() == XAxisPosition.BOTTOM) { - drawLabels(c, mViewPortHandler.contentBottom() - yoffset); + drawLabels(c, mViewPortHandler.contentBottom() + yoffset, + new PointF(0.5f, 0.0f)); - } else if (mXAxis.getPosition() == XAxisPosition.TOP_INSIDE) { + } else if (mXAxis.getPosition() == XAxisPosition.BOTTOM_INSIDE) { - drawLabels(c, mViewPortHandler.offsetTop() + yoffset + mXAxis.mLabelHeight); + drawLabels(c, mViewPortHandler.contentBottom() - yoffset - mXAxis.mLabelRotatedHeight, + new PointF(0.5f, 0.0f)); } else { // BOTH SIDED - drawLabels(c, mViewPortHandler.offsetTop() - yoffset); - drawLabels(c, mViewPortHandler.contentBottom() + mXAxis.mLabelHeight + yoffset * 1.6f); + drawLabels(c, mViewPortHandler.contentTop() - yoffset, + new PointF(0.5f, 1.0f)); + drawLabels(c, mViewPortHandler.contentBottom() + yoffset, + new PointF(0.5f, 0.0f)); } } @@ -115,7 +137,9 @@ public void renderAxisLine(Canvas c) { * * @param pos */ - protected void drawLabels(Canvas c, float pos) { + protected void drawLabels(Canvas c, float pos, PointF anchor) { + + final float labelRotationAngleDegrees = mXAxis.getLabelRotationAngle(); // pre allocate to save performance (dont allocate in loop) float[] position = new float[] { @@ -150,14 +174,14 @@ protected void drawLabels(Canvas c, float pos) { } } - drawLabel(c, label, i, position[0], pos); + drawLabel(c, label, i, position[0], pos, anchor, labelRotationAngleDegrees); } } } - protected void drawLabel(Canvas c, String label, int xIndex, float x, float y) { + protected void drawLabel(Canvas c, String label, int xIndex, float x, float y, PointF anchor, float angleDegrees) { String formattedLabel = mXAxis.getValueFormatter().getXValue(label, xIndex, mViewPortHandler); - c.drawText(formattedLabel, x, y, mAxisLabelPaint); + Utils.drawText(c, formattedLabel, x, y, mAxisLabelPaint, anchor, angleDegrees); } @Override diff --git a/MPChartLib/src/com/github/mikephil/charting/renderer/XAxisRendererBarChart.java b/MPChartLib/src/com/github/mikephil/charting/renderer/XAxisRendererBarChart.java index a9d0c07ec..c869bec35 100644 --- a/MPChartLib/src/com/github/mikephil/charting/renderer/XAxisRendererBarChart.java +++ b/MPChartLib/src/com/github/mikephil/charting/renderer/XAxisRendererBarChart.java @@ -2,6 +2,7 @@ package com.github.mikephil.charting.renderer; import android.graphics.Canvas; +import android.graphics.PointF; import com.github.mikephil.charting.charts.BarChart; import com.github.mikephil.charting.components.XAxis; @@ -27,7 +28,9 @@ public XAxisRendererBarChart(ViewPortHandler viewPortHandler, XAxis xAxis, Trans * @param pos */ @Override - protected void drawLabels(Canvas c, float pos) { + protected void drawLabels(Canvas c, float pos, PointF anchor) { + + final float labelRotationAngleDegrees = mXAxis.getLabelRotationAngle(); // pre allocate to save performance (dont allocate in loop) float[] position = new float[] { @@ -73,7 +76,7 @@ protected void drawLabels(Canvas c, float pos) { } } - drawLabel(c, label, i, position[0], pos); + drawLabel(c, label, i, position[0], pos, anchor, labelRotationAngleDegrees); } } } diff --git a/MPChartLib/src/com/github/mikephil/charting/renderer/XAxisRendererHorizontalBarChart.java b/MPChartLib/src/com/github/mikephil/charting/renderer/XAxisRendererHorizontalBarChart.java index fc64c70a7..e86226352 100644 --- a/MPChartLib/src/com/github/mikephil/charting/renderer/XAxisRendererHorizontalBarChart.java +++ b/MPChartLib/src/com/github/mikephil/charting/renderer/XAxisRendererHorizontalBarChart.java @@ -5,12 +5,14 @@ import android.graphics.Paint; import android.graphics.Paint.Align; import android.graphics.Path; +import android.graphics.PointF; import com.github.mikephil.charting.charts.BarChart; import com.github.mikephil.charting.components.LimitLine; import com.github.mikephil.charting.components.XAxis; import com.github.mikephil.charting.components.XAxis.XAxisPosition; import com.github.mikephil.charting.data.BarData; +import com.github.mikephil.charting.utils.FSize; import com.github.mikephil.charting.utils.Transformer; import com.github.mikephil.charting.utils.Utils; import com.github.mikephil.charting.utils.ViewPortHandler; @@ -32,8 +34,21 @@ public void computeAxis(float xValAverageLength, List xValues) { mXAxis.setValues(xValues); String longest = mXAxis.getLongestLabel(); - mXAxis.mLabelWidth = (int) (Utils.calcTextWidth(mAxisLabelPaint, longest) + mXAxis.getXOffset() * 3.5f); - mXAxis.mLabelHeight = Utils.calcTextHeight(mAxisLabelPaint, longest); + + final FSize labelSize = Utils.calcTextSize(mAxisLabelPaint, longest); + + final float labelWidth = (int)(labelSize.width + mXAxis.getXOffset() * 3.5f); + final float labelHeight = labelSize.height; + + final FSize labelRotatedSize = Utils.getSizeOfRotatedRectangleByDegrees( + labelSize.width, + labelHeight, + mXAxis.getLabelRotationAngle()); + + mXAxis.mLabelWidth = Math.round(labelWidth); + mXAxis.mLabelHeight = Math.round(labelHeight); + mXAxis.mLabelRotatedWidth = (int)(labelRotatedSize.width + mXAxis.getXOffset() * 3.5f); + mXAxis.mLabelRotatedHeight = Math.round(labelRotatedSize.height); } @Override @@ -50,30 +65,30 @@ public void renderAxisLabels(Canvas c) { if (mXAxis.getPosition() == XAxisPosition.TOP) { - mAxisLabelPaint.setTextAlign(Align.LEFT); - drawLabels(c, mViewPortHandler.contentRight() + xoffset); + drawLabels(c, mViewPortHandler.contentRight() + xoffset, + new PointF(0.0f, 0.5f)); - } else if (mXAxis.getPosition() == XAxisPosition.BOTTOM) { + } else if (mXAxis.getPosition() == XAxisPosition.TOP_INSIDE) { - mAxisLabelPaint.setTextAlign(Align.RIGHT); - drawLabels(c, mViewPortHandler.contentLeft() - xoffset); + drawLabels(c, mViewPortHandler.contentRight() - xoffset, + new PointF(1.0f, 0.5f)); - } else if (mXAxis.getPosition() == XAxisPosition.BOTTOM_INSIDE) { + } else if (mXAxis.getPosition() == XAxisPosition.BOTTOM) { - mAxisLabelPaint.setTextAlign(Align.LEFT); - drawLabels(c, mViewPortHandler.contentLeft() + xoffset); + drawLabels(c, mViewPortHandler.contentLeft() - xoffset, + new PointF(1.0f, 0.5f)); - } else if (mXAxis.getPosition() == XAxisPosition.TOP_INSIDE) { + } else if (mXAxis.getPosition() == XAxisPosition.BOTTOM_INSIDE) { - mAxisLabelPaint.setTextAlign(Align.RIGHT); - drawLabels(c, mViewPortHandler.contentRight() - xoffset); + drawLabels(c, mViewPortHandler.contentLeft() + xoffset, + new PointF(0.0f, 0.5f)); } else { // BOTH SIDED - mAxisLabelPaint.setTextAlign(Align.RIGHT); - drawLabels(c, mViewPortHandler.contentLeft() - xoffset); - mAxisLabelPaint.setTextAlign(Align.LEFT); - drawLabels(c, mViewPortHandler.contentRight() + xoffset); + drawLabels(c, mViewPortHandler.contentRight() + xoffset, + new PointF(0.0f, 0.5f)); + drawLabels(c, mViewPortHandler.contentLeft() - xoffset, + new PointF(1.0f, 0.5f)); } } @@ -83,7 +98,9 @@ public void renderAxisLabels(Canvas c) { * @param pos */ @Override - protected void drawLabels(Canvas c, float pos) { + protected void drawLabels(Canvas c, float pos, PointF anchor) { + + final float labelRotationAngleDegrees = mXAxis.getLabelRotationAngle(); // pre allocate to save performance (dont allocate in loop) float[] position = new float[] { @@ -108,7 +125,7 @@ protected void drawLabels(Canvas c, float pos) { if (mViewPortHandler.isInBoundsY(position[1])) { String label = mXAxis.getValues().get(i); - drawLabel(c, label, i, pos, position[1] + mXAxis.mLabelHeight / 2f); + drawLabel(c, label, i, pos, position[1], anchor, labelRotationAngleDegrees); } } } diff --git a/MPChartLib/src/com/github/mikephil/charting/renderer/XAxisRendererRadarChart.java b/MPChartLib/src/com/github/mikephil/charting/renderer/XAxisRendererRadarChart.java index b5a9ebc59..50a2fc7dd 100644 --- a/MPChartLib/src/com/github/mikephil/charting/renderer/XAxisRendererRadarChart.java +++ b/MPChartLib/src/com/github/mikephil/charting/renderer/XAxisRendererRadarChart.java @@ -25,6 +25,9 @@ public void renderAxisLabels(Canvas c) { if (!mXAxis.isEnabled() || !mXAxis.isDrawLabelsEnabled()) return; + final float labelRotationAngleDegrees = mXAxis.getLabelRotationAngle(); + final PointF drawLabelAnchor = new PointF(0.5f, 0.0f); + mAxisLabelPaint.setTypeface(mXAxis.getTypeface()); mAxisLabelPaint.setTextSize(mXAxis.getTextSize()); mAxisLabelPaint.setColor(mXAxis.getTextColor()); @@ -44,9 +47,10 @@ public void renderAxisLabels(Canvas c) { float angle = (sliceangle * i + mChart.getRotationAngle()) % 360f; PointF p = Utils.getPosition(center, mChart.getYRange() * factor - + mXAxis.mLabelWidth / 2f, angle); + + mXAxis.mLabelRotatedWidth / 2f, angle); - drawLabel(c, label, i, p.x, p.y + mXAxis.mLabelHeight / 2f); + drawLabel(c, label, i, p.x, p.y - mXAxis.mLabelRotatedHeight / 2.f, + drawLabelAnchor, labelRotationAngleDegrees); } } diff --git a/MPChartLib/src/com/github/mikephil/charting/utils/Utils.java b/MPChartLib/src/com/github/mikephil/charting/utils/Utils.java index 8f86e36d7..df3aa1ec3 100644 --- a/MPChartLib/src/com/github/mikephil/charting/utils/Utils.java +++ b/MPChartLib/src/com/github/mikephil/charting/utils/Utils.java @@ -4,6 +4,7 @@ import android.annotation.SuppressLint; import android.content.Context; import android.content.res.Resources; +import android.graphics.Canvas; import android.graphics.Paint; import android.graphics.PointF; import android.graphics.Rect; @@ -32,6 +33,8 @@ public abstract class Utils { private static DisplayMetrics mMetrics; private static int mMinimumFlingVelocity = 50; private static int mMaximumFlingVelocity = 8000; + public final static double DEG2RAD = (Math.PI / 180.0); + public final static float FDEG2RAD = ((float)Math.PI / 180.f); /** * initialize method, called inside the Chart.init() method. @@ -498,4 +501,93 @@ public static float getNormalizedAngle(float angle) { return angle % 360.f; } + + private static Rect mDrawTextRectBuffer = new Rect(); + + public static void drawText(Canvas c, String text, float x, float y, Paint paint, PointF anchor, float angleDegrees) { + + float drawOffsetX = 0.f; + float drawOffsetY = 0.f; + + paint.getTextBounds(text, 0, text.length(), mDrawTextRectBuffer); + + // Android sometimes has pre-padding + drawOffsetX -= mDrawTextRectBuffer.left; + + // Android sets the top = - (lineheight), and we want to normalize it to the center + drawOffsetY -= mDrawTextRectBuffer.top; + + // To have a consistent point of reference, we always draw left-aligned + Paint.Align originalTextAlign = paint.getTextAlign(); + paint.setTextAlign(Paint.Align.LEFT); + + if (angleDegrees != 0.f) { + + // Move the text drawing rect in a way that it always rotates around its center + drawOffsetX -= mDrawTextRectBuffer.width() * 0.5f; + drawOffsetY -= mDrawTextRectBuffer.height() * 0.5f; + + float translateX = x; + float translateY = y; + + // Move the "outer" rect relative to the anchor, assuming its centered + if (anchor.x != 0.5f || anchor.y != 0.5f) { + final FSize rotatedSize = getSizeOfRotatedRectangleByDegrees( + mDrawTextRectBuffer.width(), + mDrawTextRectBuffer.height(), + angleDegrees); + + translateX -= rotatedSize.width * (anchor.x - 0.5f); + translateY -= rotatedSize.height * (anchor.y - 0.5f); + } + + c.save(); + c.translate(translateX, translateY); + c.rotate(angleDegrees); + + c.drawText(text, drawOffsetX, drawOffsetY, paint); + + c.restore(); + } + else { + if (anchor.x != 0.f || anchor.y != 0.f) { + + drawOffsetX -= mDrawTextRectBuffer.width() * anchor.x; + drawOffsetY -= mDrawTextRectBuffer.height() * anchor.y; + } + + drawOffsetX += x; + drawOffsetY += y; + + c.drawText(text, drawOffsetX, drawOffsetY, paint); + } + + paint.setTextAlign(originalTextAlign); + } + + public static FSize getSizeOfRotatedRectangleByDegrees(FSize rectangleSize, float degrees) + { + final float radians = degrees * FDEG2RAD; + return getSizeOfRotatedRectangleByRadians(rectangleSize.width, rectangleSize.height, radians); + } + + public static FSize getSizeOfRotatedRectangleByRadians(FSize rectangleSize, float radians) + { + return getSizeOfRotatedRectangleByRadians(rectangleSize.width, rectangleSize.height, radians); + } + + public static FSize getSizeOfRotatedRectangleByDegrees(float rectangleWidth, float rectangleHeight, float degrees) + { + final float radians = degrees * FDEG2RAD; + return getSizeOfRotatedRectangleByRadians(rectangleWidth, rectangleHeight, radians); + } + + public static FSize getSizeOfRotatedRectangleByRadians(float rectangleWidth, float rectangleHeight, float radians) + { + return new FSize( + Math.abs(rectangleWidth * (float)Math.cos(radians)) + Math.abs(rectangleHeight * (float)Math.sin(radians)), + Math.abs(rectangleWidth * (float)Math.sin(radians)) + Math.abs(rectangleHeight * (float)Math.cos(radians)) + ); + } + } From dd3bd71027e349e90771d96fdb2d462b074be45e Mon Sep 17 00:00:00 2001 From: Philipp Jahoda Date: Mon, 2 Nov 2015 14:52:10 +0100 Subject: [PATCH 26/31] Fix #1168 --- .../com/github/mikephil/charting/highlight/BarHighlighter.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/MPChartLib/src/com/github/mikephil/charting/highlight/BarHighlighter.java b/MPChartLib/src/com/github/mikephil/charting/highlight/BarHighlighter.java index 07e476acd..a85f48868 100644 --- a/MPChartLib/src/com/github/mikephil/charting/highlight/BarHighlighter.java +++ b/MPChartLib/src/com/github/mikephil/charting/highlight/BarHighlighter.java @@ -131,7 +131,7 @@ protected int getClosestStackIndex(Range[] ranges, float value) { stackIndex++; } - int length = ranges.length - 1; + int length = Math.max(ranges.length - 1, 0); return (value > ranges[length].to) ? length : 0; // From af0c5acffbf0dbee912703241df3d8b0f09f85c0 Mon Sep 17 00:00:00 2001 From: Philipp Jahoda Date: Mon, 2 Nov 2015 20:31:37 +0100 Subject: [PATCH 27/31] Update version --- MPChartExample/AndroidManifest.xml | 4 ++-- MPChartExample/build.gradle | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/MPChartExample/AndroidManifest.xml b/MPChartExample/AndroidManifest.xml index 639694e26..34125b028 100644 --- a/MPChartExample/AndroidManifest.xml +++ b/MPChartExample/AndroidManifest.xml @@ -1,8 +1,8 @@ + android:versionCode="46" + android:versionName="2.1.6" > Date: Mon, 2 Nov 2015 20:33:56 +0100 Subject: [PATCH 28/31] Update README.md --- README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 324132ec7..6c0f46ed5 100644 --- a/README.md +++ b/README.md @@ -57,7 +57,7 @@ Questions & Issues If you are having questions or problems, you should: - Make sure you are using the latest version of the library. Check the [**release-section**](https://github.com/PhilJay/MPAndroidChart/releases). - - Study the [**Documentation-Wiki**](https://github.com/PhilJay/MPAndroidChart/wiki) or the [javadocs](https://jitpack.io/com/github/PhilJay/MPAndroidChart/v2.1.5/javadoc/) + - Study the [**Documentation-Wiki**](https://github.com/PhilJay/MPAndroidChart/wiki) or the [javadocs](https://jitpack.io/com/github/PhilJay/MPAndroidChart/v2.1.6/javadoc/) - Search or open questions on [**stackoverflow**](https://stackoverflow.com/search?q=mpandroidchart) with the `mpandroidchart` tag - Search [**known issues**](https://github.com/PhilJay/MPAndroidChart/issues) for your problem (open and closed) - Create new issues (please **search known issues before**, do not create duplicate issues) @@ -145,7 +145,7 @@ repositories { } dependencies { - compile 'com.github.PhilJay:MPAndroidChart:v2.1.5' + compile 'com.github.PhilJay:MPAndroidChart:v2.1.6' } ``` @@ -160,7 +160,7 @@ dependencies { com.github.PhilJay MPAndroidChart - v2.1.5 + v2.1.6 ``` @@ -180,7 +180,7 @@ dependencies { Documentation ======= -For a **detailed documentation**, please have a look at the [**Wiki**](https://github.com/PhilJay/MPAndroidChart/wiki) or the [javadocs](https://jitpack.io/com/github/PhilJay/MPAndroidChart/v2.1.5/javadoc/). +For a **detailed documentation**, please have a look at the [**Wiki**](https://github.com/PhilJay/MPAndroidChart/wiki) or the [javadocs](https://jitpack.io/com/github/PhilJay/MPAndroidChart/v2.1.6/javadoc/). Furthermore, you can also rely on the [**MPChartExample**](https://github.com/PhilJay/MPAndroidChart/tree/master/MPChartExample) folder and check out the example code in that project. The corresponding application to the example project is also [**available in the Google PlayStore**](https://play.google.com/store/apps/details?id=com.xxmassdeveloper.mpchartexample). From b2f4e26e67997786e145b62cb991685b5504af4f Mon Sep 17 00:00:00 2001 From: Philipp Jahoda Date: Wed, 4 Nov 2015 19:04:03 +0100 Subject: [PATCH 29/31] Change default minoffset to 15 (instead of 10) --- .../github/mikephil/charting/charts/BarLineChartBase.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/MPChartLib/src/com/github/mikephil/charting/charts/BarLineChartBase.java b/MPChartLib/src/com/github/mikephil/charting/charts/BarLineChartBase.java index a528c282f..a9f8039c2 100644 --- a/MPChartLib/src/com/github/mikephil/charting/charts/BarLineChartBase.java +++ b/MPChartLib/src/com/github/mikephil/charting/charts/BarLineChartBase.java @@ -103,9 +103,9 @@ public abstract class BarLineChartBase Date: Wed, 4 Nov 2015 13:04:11 -0700 Subject: [PATCH 30/31] Added ability to set image format and subdirectory when saving to gallery --- .../mikephil/charting/charts/Chart.java | 61 ++++++++++++++----- 1 file changed, 46 insertions(+), 15 deletions(-) diff --git a/MPChartLib/src/com/github/mikephil/charting/charts/Chart.java b/MPChartLib/src/com/github/mikephil/charting/charts/Chart.java index f79aa065d..aebe81166 100644 --- a/MPChartLib/src/com/github/mikephil/charting/charts/Chart.java +++ b/MPChartLib/src/com/github/mikephil/charting/charts/Chart.java @@ -1532,16 +1532,18 @@ public boolean saveToPath(String title, String pathOnSD) { } /** - * Saves the current state of the chart to the gallery as a JPEG image. The - * filename and compression can be set. 0 == maximum compression, 100 = low + * Saves the current state of the chart to the gallery as an image type. The + * compression must be set for JPEG only. 0 == maximum compression, 100 = low * compression (high quality). NOTE: Needs permission WRITE_EXTERNAL_STORAGE * * @param fileName e.g. "my_image" - * @param quality e.g. 50, min = 0, max = 100 - * @return returns true if saving was successfull, false if not + * @param subFolderPath e.g. "ChartPics" + * @param fileDescription e.g. "Chart details" + * @param format e.g. Bitmap.CompressFormat.PNG + * @param quality e.g. 50, min = 0, max = 100 + * @return returns true if saving was successful, false if not */ - public boolean saveToGallery(String fileName, int quality) { - + public boolean saveToGallery(String fileName, String subFolderPath, String fileDescription, Bitmap.CompressFormat format, int quality) { // restrain quality if (quality < 0 || quality > 100) quality = 50; @@ -1549,23 +1551,40 @@ public boolean saveToGallery(String fileName, int quality) { long currentTime = System.currentTimeMillis(); File extBaseDir = Environment.getExternalStorageDirectory(); - File file = new File(extBaseDir.getAbsolutePath() + "/DCIM"); + File file = new File(extBaseDir.getAbsolutePath() + "/DCIM/" + subFolderPath); if (!file.exists()) { if (!file.mkdirs()) { return false; } } + String mimeType = ""; + switch (format) { + case PNG: + mimeType = "image/png"; + if (!fileName.endsWith(".png")) + fileName += ".png"; + break; + case WEBP: + mimeType = "image/webp"; + if (!fileName.endsWith(".webp")) + fileName += ".webp"; + break; + case JPEG: + default: + mimeType = "image/jpeg"; + if (!(fileName.endsWith(".jpg") || fileName.endsWith(".jpeg"))) + fileName += ".jpg"; + break; + } + String filePath = file.getAbsolutePath() + "/" + fileName; FileOutputStream out = null; try { out = new FileOutputStream(filePath); Bitmap b = getChartBitmap(); - - b.compress(Bitmap.CompressFormat.JPEG, quality, out); // control - // the jpeg - // quality + b.compress(format, quality, out); out.flush(); out.close(); @@ -1584,14 +1603,26 @@ public boolean saveToGallery(String fileName, int quality) { values.put(Images.Media.TITLE, fileName); values.put(Images.Media.DISPLAY_NAME, fileName); values.put(Images.Media.DATE_ADDED, currentTime); - values.put(Images.Media.MIME_TYPE, "image/jpeg"); - values.put(Images.Media.DESCRIPTION, "MPAndroidChart-Library Save"); + values.put(Images.Media.MIME_TYPE, mimeType); + values.put(Images.Media.DESCRIPTION, fileDescription); values.put(Images.Media.ORIENTATION, 0); values.put(Images.Media.DATA, filePath); values.put(Images.Media.SIZE, size); - return getContext().getContentResolver().insert(Images.Media.EXTERNAL_CONTENT_URI, values) == null - ? false : true; + return getContext().getContentResolver().insert(Images.Media.EXTERNAL_CONTENT_URI, values) != null; + } + + /** + * Saves the current state of the chart to the gallery as a JPEG image. The + * filename and compression can be set. 0 == maximum compression, 100 = low + * compression (high quality). NOTE: Needs permission WRITE_EXTERNAL_STORAGE + * + * @param fileName e.g. "my_image" + * @param quality e.g. 50, min = 0, max = 100 + * @return returns true if saving was successful, false if not + */ + public boolean saveToGallery(String fileName, int quality) { + return saveToGallery(fileName, "", "MPAndroidChart-Library Save", Bitmap.CompressFormat.JPEG, quality); } /** From b495a7d9ebc278addb2a81695825f1b06202f36a Mon Sep 17 00:00:00 2001 From: Philipp Jahoda Date: Thu, 5 Nov 2015 10:52:33 +0100 Subject: [PATCH 31/31] Fix #1168 --- .../github/mikephil/charting/highlight/BarHighlighter.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/MPChartLib/src/com/github/mikephil/charting/highlight/BarHighlighter.java b/MPChartLib/src/com/github/mikephil/charting/highlight/BarHighlighter.java index a85f48868..7d001dfe2 100644 --- a/MPChartLib/src/com/github/mikephil/charting/highlight/BarHighlighter.java +++ b/MPChartLib/src/com/github/mikephil/charting/highlight/BarHighlighter.java @@ -119,7 +119,7 @@ protected Highlight getStackedHighlight(Highlight old, BarDataSet set, int xInde */ protected int getClosestStackIndex(Range[] ranges, float value) { - if (ranges == null) + if (ranges == null || ranges.length == 0) return 0; int stackIndex = 0; @@ -188,7 +188,7 @@ protected Range[] getRanges(BarEntry entry) { float[] values = entry.getVals(); - if (values == null) + if (values == null || values.length == 0) return null; float negRemain = -entry.getNegativeSum();