Skip to content

Commit c0d263c

Browse files
committed
Improvements concerning Pie- and RadarChart offset calculation. Improvements concerning Pie- and RadarChart touch listener.
1 parent 9c3bffd commit c0d263c

File tree

6 files changed

+230
-146
lines changed

6 files changed

+230
-146
lines changed

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

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1057,22 +1057,6 @@ public boolean onTouchEvent(MotionEvent event) {
10571057
*/
10581058
/** CODE BELOW THIS RELATED TO SCALING AND GESTURES */
10591059

1060-
/**
1061-
* disables intercept touchevents
1062-
*/
1063-
public void disableScroll() {
1064-
ViewParent parent = getParent();
1065-
parent.requestDisallowInterceptTouchEvent(true);
1066-
}
1067-
1068-
/**
1069-
* enables intercept touchevents
1070-
*/
1071-
public void enableScroll() {
1072-
ViewParent parent = getParent();
1073-
parent.requestDisallowInterceptTouchEvent(false);
1074-
}
1075-
10761060
/**
10771061
* Zooms in by 1.4f, x and y are the coordinates (in pixels) of the zoom
10781062
* center.

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import android.util.AttributeSet;
2323
import android.util.Log;
2424
import android.view.View;
25+
import android.view.ViewParent;
2526

2627
import com.github.mikephil.charting.data.BarData;
2728
import com.github.mikephil.charting.data.BarDataSet;
@@ -1737,6 +1738,22 @@ public Legend getLegend() {
17371738
public RectF getContentRect() {
17381739
return mContentRect;
17391740
}
1741+
1742+
/**
1743+
* disables intercept touchevents
1744+
*/
1745+
public void disableScroll() {
1746+
ViewParent parent = getParent();
1747+
parent.requestDisallowInterceptTouchEvent(true);
1748+
}
1749+
1750+
/**
1751+
* enables intercept touchevents
1752+
*/
1753+
public void enableScroll() {
1754+
ViewParent parent = getParent();
1755+
parent.requestDisallowInterceptTouchEvent(false);
1756+
}
17401757

17411758
/** paint for the grid lines (only line and barchart) */
17421759
public static final int PAINT_GRID = 3;

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

Lines changed: 10 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -173,77 +173,6 @@ protected void calcMinMax(boolean fixedValues) {
173173
calcAngles();
174174
}
175175

176-
@Override
177-
protected void calculateOffsets() {
178-
179-
float legendRight = 0f, legendBottom = 0f, legendTop = 0f;
180-
181-
if (mDrawLegend) {
182-
183-
if (mLegend == null)
184-
return;
185-
186-
if (mLegend.getPosition() == LegendPosition.RIGHT_OF_CHART_CENTER) {
187-
188-
// this is the space between the legend and the chart
189-
float spacing = Utils.convertDpToPixel(13f);
190-
191-
legendRight = mLegend.getMaximumEntryLength(mLegendLabelPaint)
192-
+ mLegend.getFormSize() + mLegend.getFormToTextSpace() + spacing;
193-
194-
mLegendLabelPaint.setTextAlign(Align.LEFT);
195-
// legendTop = mLegend.getFullHeight(mLegendLabelPaint);
196-
197-
} else if (mLegend.getPosition() == LegendPosition.RIGHT_OF_CHART) {
198-
199-
// this is the space between the legend and the chart
200-
float spacing = Utils.convertDpToPixel(13f);
201-
202-
float legendWidth = mLegend.getMaximumEntryLength(mLegendLabelPaint)
203-
+ mLegend.getFormSize() + mLegend.getFormToTextSpace() + spacing;
204-
205-
float legendHeight = mLegend.getFullHeight(mLegendLabelPaint) + mOffsetTop;
206-
207-
PointF bottomRight = new PointF(getWidth() - legendWidth, legendHeight);
208-
PointF reference = getPosition(getCenter(), getRadius(), 315);
209-
210-
float distLegend = distanceToCenter(bottomRight.x, bottomRight.y);
211-
float distReference = distanceToCenter(reference.x, reference.y);
212-
float min = Utils.convertDpToPixel(5f);
213-
214-
if (distLegend < distReference) {
215-
216-
float diff = distReference - distLegend;
217-
218-
legendRight = min + diff;
219-
legendTop = min + diff;
220-
}
221-
222-
mLegendLabelPaint.setTextAlign(Align.LEFT);
223-
224-
} else if (mLegend.getPosition() == LegendPosition.BELOW_CHART_LEFT
225-
|| mLegend.getPosition() == LegendPosition.BELOW_CHART_RIGHT
226-
|| mLegend.getPosition() == LegendPosition.BELOW_CHART_CENTER) {
227-
228-
legendBottom = mLegendLabelPaint.getTextSize() * 4f;
229-
}
230-
231-
mLegend.setOffsetBottom(legendBottom);
232-
mLegend.setOffsetRight(legendRight);
233-
}
234-
235-
float min = Utils.convertDpToPixel(11f);
236-
237-
mLegend.setOffsetLeft(min);
238-
239-
mOffsetLeft = min;
240-
mOffsetTop = Math.max(min, legendTop);
241-
mOffsetRight = Math.max(min, legendRight);
242-
mOffsetBottom = Math.max(min, legendBottom);
243-
244-
applyCalculatedOffsets();
245-
}
246-
247176
/**
248177
* calculates the needed angles for the chart slices
249178
*/
@@ -694,6 +623,16 @@ public boolean isDrawXValuesEnabled() {
694623
return mDrawXVals;
695624
}
696625

626+
@Override
627+
protected float getRequiredBottomOffset() {
628+
return mLegendLabelPaint.getTextSize() * 4f;
629+
}
630+
631+
@Override
632+
protected float getRequiredBaseOffset() {
633+
return 0;
634+
}
635+
697636
@Override
698637
public float getRadius() {
699638
if (mCircleBox == null)

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

Lines changed: 103 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,16 @@
44
import android.content.Context;
55
import android.graphics.Matrix;
66
import android.graphics.PointF;
7+
import android.graphics.Paint.Align;
78
import android.util.AttributeSet;
89
import android.view.MotionEvent;
910

1011
import com.github.mikephil.charting.data.ChartData;
1112
import com.github.mikephil.charting.data.DataSet;
1213
import com.github.mikephil.charting.data.Entry;
1314
import com.github.mikephil.charting.listener.PieRadarChartTouchListener;
15+
import com.github.mikephil.charting.utils.Utils;
16+
import com.github.mikephil.charting.utils.Legend.LegendPosition;
1417
import com.nineoldandroids.animation.ObjectAnimator;
1518

1619
/**
@@ -67,7 +70,7 @@ public void prepare() {
6770
calcMinMax(false);
6871

6972
prepareLegend();
70-
73+
7174
calculateOffsets();
7275
}
7376

@@ -76,6 +79,78 @@ public void notifyDataSetChanged() {
7679
prepare();
7780
}
7881

82+
@Override
83+
protected void calculateOffsets() {
84+
85+
float legendRight = 0f, legendBottom = 0f, legendTop = 0f;
86+
87+
if (mDrawLegend) {
88+
89+
if (mLegend == null)
90+
return;
91+
92+
if (mLegend.getPosition() == LegendPosition.RIGHT_OF_CHART_CENTER) {
93+
94+
// this is the space between the legend and the chart
95+
float spacing = Utils.convertDpToPixel(13f);
96+
97+
legendRight = getFullLegendWidth() + spacing;
98+
99+
mLegendLabelPaint.setTextAlign(Align.LEFT);
100+
// legendTop = mLegend.getFullHeight(mLegendLabelPaint);
101+
102+
} else if (mLegend.getPosition() == LegendPosition.RIGHT_OF_CHART) {
103+
104+
// this is the space between the legend and the chart
105+
float spacing = Utils.convertDpToPixel(13f);
106+
107+
float legendWidth = getFullLegendWidth() + spacing;
108+
109+
float legendHeight = mLegend.getFullHeight(mLegendLabelPaint) + mOffsetTop;
110+
111+
PointF bottomRight = new PointF(getWidth() - legendWidth, legendHeight);
112+
PointF reference = getPosition(getCenter(), getRadius(), 315);
113+
114+
float distLegend = distanceToCenter(bottomRight.x, bottomRight.y);
115+
float distReference = distanceToCenter(reference.x, reference.y);
116+
float min = Utils.convertDpToPixel(5f);
117+
118+
if (distLegend < distReference) {
119+
120+
float diff = distReference - distLegend;
121+
122+
legendRight = min + diff;
123+
legendTop = min + diff;
124+
}
125+
126+
mLegendLabelPaint.setTextAlign(Align.LEFT);
127+
128+
} else if (mLegend.getPosition() == LegendPosition.BELOW_CHART_LEFT
129+
|| mLegend.getPosition() == LegendPosition.BELOW_CHART_RIGHT
130+
|| mLegend.getPosition() == LegendPosition.BELOW_CHART_CENTER) {
131+
132+
legendBottom = getRequiredBottomOffset();
133+
}
134+
135+
legendRight += getRequiredBaseOffset();
136+
legendTop += getRequiredBaseOffset();
137+
138+
mLegend.setOffsetBottom(mLegendLabelPaint.getTextSize() * 4f);
139+
mLegend.setOffsetRight(legendRight);
140+
}
141+
142+
float min = Utils.convertDpToPixel(11f);
143+
144+
mLegend.setOffsetLeft(min);
145+
146+
mOffsetLeft = Math.max(min, getRequiredBaseOffset());
147+
mOffsetTop = Math.max(min, legendTop);
148+
mOffsetRight = Math.max(min, legendRight);
149+
mOffsetBottom = Math.max(min, legendBottom);
150+
151+
applyCalculatedOffsets();
152+
}
153+
79154
@Override
80155
protected void drawAdditional() {
81156
// TODO Auto-generated method stub
@@ -170,7 +245,7 @@ public float getAngleForPoint(float x, float y) {
170245

171246
return angle;
172247
}
173-
248+
174249
/**
175250
* Calculates the position around a center point, depending on the distance
176251
* from the center, and the angle of the position around the center.
@@ -199,7 +274,7 @@ protected PointF getPosition(PointF center, float dist, float angle) {
199274
public float distanceToCenter(float x, float y) {
200275

201276
PointF c = getCenterOffsets();
202-
277+
203278
float dist = 0f;
204279

205280
float xDist = 0f;
@@ -291,6 +366,31 @@ public float getDiameter() {
291366
*/
292367
public abstract float getRadius();
293368

369+
/**
370+
* Returns the required bottom offset for the chart.
371+
*
372+
* @return
373+
*/
374+
protected abstract float getRequiredBottomOffset();
375+
376+
/**
377+
* Returns the required right offset for the chart.
378+
*
379+
* @return
380+
*/
381+
private float getFullLegendWidth() {
382+
return mLegend.getMaximumEntryLength(mLegendLabelPaint)
383+
+ mLegend.getFormSize() + mLegend.getFormToTextSpace();
384+
}
385+
386+
/**
387+
* Returns the base offset needed for the chart without calculating the
388+
* legend size.
389+
*
390+
* @return
391+
*/
392+
protected abstract float getRequiredBaseOffset();
393+
294394
/**
295395
* set a new (e.g. custom) charttouchlistener NOTE: make sure to
296396
* setTouchEnabled(true); if you need touch gestures on the chart

0 commit comments

Comments
 (0)