Skip to content

Commit 6033862

Browse files
committed
Fixed issue concerning vertical clipping in the PieChart legend.
1 parent 0f3f5e0 commit 6033862

6 files changed

Lines changed: 58 additions & 19 deletions

File tree

134 KB
Binary file not shown.

MPChartExample/src/com/xxmassdeveloper/mpchartexample/PieChartActivity.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ protected void onCreate(Bundle savedInstanceState) {
7373
mSeekBarY.setProgress(100);
7474

7575
Legend l = mChart.getLegend();
76-
l.setPosition(LegendPosition.RIGHT_OF_CHART);
76+
l.setPosition(LegendPosition.BELOW_CHART_LEFT);
7777
l.setYEntrySpace(3f);
7878
}
7979

134 KB
Binary file not shown.

MPChartLib/src/com/github/mikephil/charting/charts/Chart.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -661,6 +661,8 @@ protected void drawLegend() {
661661

662662
if (tf != null)
663663
mLegendLabelPaint.setTypeface(tf);
664+
665+
mLegendLabelPaint.setTextSize(mLegend.getTextSize());
664666

665667
float formSize = mLegend.getFormSize();
666668

@@ -671,7 +673,7 @@ protected void drawLegend() {
671673
float xEntrySpace = mLegend.getXEntrySpace() + formSize;
672674
float yEntrySpace = mLegend.getYEntrySpace() + formSize;
673675

674-
float textSize = mLegendLabelPaint.getTextSize();
676+
float textSize = mLegend.getTextSize();
675677

676678
// the amount of pixels the text needs to be set down to be on the same
677679
// height as the form

MPChartLib/src/com/github/mikephil/charting/charts/PieChart.java

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import com.github.mikephil.charting.data.PieDataSet;
2222
import com.github.mikephil.charting.listener.PieChartTouchListener;
2323
import com.github.mikephil.charting.utils.Legend.LegendPosition;
24+
import com.github.mikephil.charting.utils.XLabels.XLabelPosition;
2425
import com.github.mikephil.charting.utils.Utils;
2526

2627
import java.text.DecimalFormat;
@@ -56,7 +57,7 @@ public class PieChart extends Chart {
5657
* this value is null, the default is "Total Value\n + getYValueSum()"
5758
*/
5859
private String mCenterText = null;
59-
60+
6061
/**
6162
* indicates the size of the hole in the center of the piechart, default:
6263
* radius / 2
@@ -133,9 +134,10 @@ protected void init() {
133134
// for the piechart, drawing values is enabled
134135
mDrawYValues = true;
135136
}
136-
137+
137138
/**
138139
* Sets a PieData object as a model for the PieChart.
140+
*
139141
* @param data
140142
*/
141143
public void setData(PieData data) {
@@ -199,18 +201,29 @@ public void notifyDataSetChanged() {
199201
@Override
200202
protected void calculateOffsets() {
201203

202-
if (mDrawLegend) {
203-
if (mLegend.getPosition() == LegendPosition.RIGHT_OF_CHART) {
204+
if (mLegend == null)
205+
return;
204206

205-
mLegendLabelPaint.setTextAlign(Align.LEFT);
206-
mOffsetTop = (int) (mLegendLabelPaint.getTextSize() * 3.5f);
207+
// setup offsets for legend
208+
if (mLegend.getPosition() == LegendPosition.RIGHT_OF_CHART) {
207209

208-
} else if (mLegend.getPosition() == LegendPosition.BELOW_CHART_LEFT
209-
|| mLegend.getPosition() == LegendPosition.BELOW_CHART_RIGHT) {
210-
mOffsetBottom = (int) (mLegendLabelPaint.getTextSize() * 3.5f);
211-
}
210+
mLegend.setOffsetRight(mLegend.getMaximumEntryLength(mLegendLabelPaint));
211+
mLegendLabelPaint.setTextAlign(Align.LEFT);
212+
213+
} else if (mLegend.getPosition() == LegendPosition.BELOW_CHART_LEFT
214+
|| mLegend.getPosition() == LegendPosition.BELOW_CHART_RIGHT) {
215+
216+
mLegend.setOffsetBottom(mLegendLabelPaint.getTextSize() * 3.5f);
212217
}
213218

219+
if (mDrawLegend) {
220+
221+
mOffsetBottom = Math.max(mOffsetBottom, mLegend.getOffsetBottom());
222+
}
223+
224+
mLegend.setOffsetTop(mOffsetTop);
225+
mLegend.setOffsetLeft(mOffsetLeft);
226+
214227
prepareContentRect();
215228

216229
float scaleX = (float) ((getWidth() - mOffsetLeft - mOffsetRight) / mDeltaX);
@@ -378,9 +391,10 @@ protected void drawHighlights() {
378391

379392
float shiftangle = (float) Math.toRadians(angle + sliceDegrees / 2f);
380393

381-
PieDataSet set = (PieDataSet) mCurrentData.getDataSetByIndex(mIndicesToHightlight[i]
382-
.getDataSetIndex());
383-
394+
PieDataSet set = (PieDataSet) mCurrentData
395+
.getDataSetByIndex(mIndicesToHightlight[i]
396+
.getDataSetIndex());
397+
384398
float shift = set.getSelectionShift();
385399
float xShift = shift * (float) Math.cos(shiftangle);
386400
float yShift = shift * (float) Math.sin(shiftangle);
@@ -432,7 +446,8 @@ protected void drawData() {
432446
}
433447

434448
/**
435-
* draws the hole in the center of the chart and the transparent circle / hole
449+
* draws the hole in the center of the chart and the transparent circle /
450+
* hole
436451
*/
437452
private void drawHole() {
438453

@@ -934,7 +949,8 @@ public void setPaint(Paint p, int which) {
934949
@Override
935950
public Paint getPaint(int which) {
936951
Paint p = super.getPaint(which);
937-
if(p != null) return p;
952+
if (p != null)
953+
return p;
938954

939955
switch (which) {
940956
case PAINT_HOLE:

MPChartLib/src/com/github/mikephil/charting/utils/Legend.java

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ public enum LegendForm {
4242

4343
/** the typeface used for the legend labels */
4444
private Typeface mTypeface = null;
45+
46+
/** the text size of the legend labels */
47+
private float mTextSize = 9f;
4548

4649
/** the size of the legend forms/shapes */
4750
private float mFormSize = 8f;
@@ -66,10 +69,11 @@ public enum LegendForm {
6669
/** default constructor */
6770
public Legend() {
6871

69-
mFormSize = Utils.convertDpToPixel(mFormSize);
72+
mFormSize = Utils.convertDpToPixel(8f);
7073
mXEntrySpace = Utils.convertDpToPixel(5f);
7174
mYEntrySpace = Utils.convertDpToPixel(3f);
72-
mFormToTextSpace = Utils.convertDpToPixel(mFormToTextSpace);
75+
mFormToTextSpace = Utils.convertDpToPixel(5f);
76+
mTextSize = Utils.convertDpToPixel(9f);
7377
}
7478

7579
/**
@@ -367,6 +371,7 @@ public void apply(Legend l) {
367371
mXEntrySpace = l.mXEntrySpace;
368372
mYEntrySpace = l.mYEntrySpace;
369373
mFormToTextSpace = l.mFormToTextSpace;
374+
mTextSize = l.mTextSize;
370375
}
371376

372377
/**
@@ -440,4 +445,20 @@ public void setOffsetTop(float off) {
440445
public void setOffsetLeft(float off) {
441446
mLegendOffsetLeft = off;
442447
}
448+
449+
/**
450+
* sets the text size of the legend labels, default 9f
451+
* @param size
452+
*/
453+
public void setTextSize(float size) {
454+
mTextSize = Utils.convertDpToPixel(size);
455+
}
456+
457+
/**
458+
* returns the text size of the legend labels
459+
* @return
460+
*/
461+
public float getTextSize() {
462+
return mTextSize;
463+
}
443464
}

0 commit comments

Comments
 (0)