Skip to content

Commit 10a392d

Browse files
committedOct 1, 2015
Merge pull request #1059 from danielgindi/features
Some new features
2 parents beee126 + 5b31bb6 commit 10a392d

File tree

7 files changed

+99
-35
lines changed

7 files changed

+99
-35
lines changed
 

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

+26-13
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,9 @@ public abstract class BarLineChartBase<T extends BarLineScatterCandleBubbleData<
8888

8989
protected boolean mDrawBorders = false;
9090

91+
/** Sets the minimum offset (padding) around the chart, defaults to 10 */
92+
protected float mMinOffset = 10.f;
93+
9194
/** the listener for user drawing on the chart */
9295
protected OnDrawListener mDrawListener;
9396

@@ -436,19 +439,26 @@ public void calculateOffsets() {
436439
|| mLegend.getPosition() == LegendPosition.BELOW_CHART_RIGHT
437440
|| mLegend.getPosition() == LegendPosition.BELOW_CHART_CENTER) {
438441

439-
float yOffset = mLegend.mTextHeightMax; // It's
440-
// possible
441-
// that we do
442-
// not need
443-
// this offset
444-
// anymore as
445-
// it is
446-
// available
447-
// through the
448-
// extraOffsets
442+
// It's possible that we do not need this offset anymore as it
443+
// is available through the extraOffsets, but changing it can mean
444+
// changing default visibility for existing apps.
445+
float yOffset = mLegend.mTextHeightMax;
446+
449447
offsetBottom += Math.min(mLegend.mNeededHeight + yOffset,
450448
mViewPortHandler.getChartHeight() * mLegend.getMaxSizePercent());
451449

450+
} else if (mLegend.getPosition() == LegendPosition.ABOVE_CHART_LEFT
451+
|| mLegend.getPosition() == LegendPosition.ABOVE_CHART_RIGHT
452+
|| mLegend.getPosition() == LegendPosition.ABOVE_CHART_CENTER) {
453+
454+
// It's possible that we do not need this offset anymore as it
455+
// is available through the extraOffsets, but changing it can mean
456+
// changing default visibility for existing apps.
457+
float yOffset = mLegend.mTextHeightMax;
458+
459+
offsetTop += Math.min(mLegend.mNeededHeight + yOffset,
460+
mViewPortHandler.getChartHeight() * mLegend.getMaxSizePercent());
461+
452462
}
453463
}
454464

@@ -488,10 +498,13 @@ public void calculateOffsets() {
488498
offsetBottom += getExtraBottomOffset();
489499
offsetLeft += getExtraLeftOffset();
490500

491-
float min = Utils.convertDpToPixel(10f);
501+
float minOffset = Utils.convertDpToPixel(mMinOffset);
492502

493-
mViewPortHandler.restrainViewPort(Math.max(min, offsetLeft), Math.max(min, offsetTop),
494-
Math.max(min, offsetRight), Math.max(min, offsetBottom));
503+
mViewPortHandler.restrainViewPort(
504+
Math.max(minOffset, offsetLeft),
505+
Math.max(minOffset, offsetTop),
506+
Math.max(minOffset, offsetRight),
507+
Math.max(minOffset, offsetBottom));
495508

496509
if (mLogEnabled) {
497510
Log.i(LOG_TAG, "offsetLeft: " + offsetLeft + ", offsetTop: " + offsetTop

‎MPChartLib/src/com/github/mikephil/charting/charts/HorizontalBarChart.java

+22-5
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,23 @@ public void calculateOffsets() {
7979
|| mLegend.getPosition() == LegendPosition.BELOW_CHART_RIGHT
8080
|| mLegend.getPosition() == LegendPosition.BELOW_CHART_CENTER) {
8181

82-
float yOffset = mLegend.mTextHeightMax * 2.f; // It's possible that we do not need this offset anymore
83-
// as it is available through the extraOffsets
82+
// It's possible that we do not need this offset anymore as it
83+
// is available through the extraOffsets, but changing it can mean
84+
// changing default visibility for existing apps.
85+
float yOffset = mLegend.mTextHeightMax * 2.f;
86+
8487
offsetBottom += Math.min(mLegend.mNeededHeight + yOffset, mViewPortHandler.getChartHeight() * mLegend.getMaxSizePercent());
88+
89+
} else if (mLegend.getPosition() == LegendPosition.ABOVE_CHART_LEFT
90+
|| mLegend.getPosition() == LegendPosition.ABOVE_CHART_RIGHT
91+
|| mLegend.getPosition() == LegendPosition.ABOVE_CHART_CENTER) {
92+
93+
// It's possible that we do not need this offset anymore as it
94+
// is available through the extraOffsets, but changing it can mean
95+
// changing default visibility for existing apps.
96+
float yOffset = mLegend.mTextHeightMax * 2.f;
97+
98+
offsetTop += Math.min(mLegend.mNeededHeight + yOffset, mViewPortHandler.getChartHeight() * mLegend.getMaxSizePercent());
8599
}
86100
}
87101

@@ -119,10 +133,13 @@ public void calculateOffsets() {
119133
offsetBottom += getExtraBottomOffset();
120134
offsetLeft += getExtraLeftOffset();
121135

122-
float min = Utils.convertDpToPixel(10f);
136+
float minOffset = Utils.convertDpToPixel(mMinOffset);
123137

124-
mViewPortHandler.restrainViewPort(Math.max(min, offsetLeft), Math.max(min, offsetTop), Math.max(min, offsetRight),
125-
Math.max(min, offsetBottom));
138+
mViewPortHandler.restrainViewPort(
139+
Math.max(minOffset, offsetLeft),
140+
Math.max(minOffset, offsetTop),
141+
Math.max(minOffset, offsetRight),
142+
Math.max(minOffset, offsetBottom));
126143

127144
if (mLogEnabled) {
128145
Log.i(LOG_TAG, "offsetLeft: " + offsetLeft + ", offsetTop: " + offsetTop + ", offsetRight: " + offsetRight + ", offsetBottom: "

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,7 @@ public boolean isDrawCenterTextEnabled() {
420420
}
421421

422422
@Override
423-
protected float getRequiredBottomOffset() {
423+
protected float getRequiredLegendOffset() {
424424
return mLegendRenderer.getLabelPaint().getTextSize() * 2.f;
425425
}
426426

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

+27-9
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ public abstract class PieRadarChartBase<T extends ChartData<? extends DataSet<?
4242
/** flag that indicates if rotation is enabled or not */
4343
protected boolean mRotateEnabled = true;
4444

45+
/** Sets the minimum offset (padding) around the chart, defaults to 10 */
46+
protected float mMinOffset = 10.f;
47+
4548
public PieRadarChartBase(Context context) {
4649
super(context);
4750
}
@@ -184,23 +187,38 @@ public void calculateOffsets() {
184187
|| mLegend.getPosition() == LegendPosition.BELOW_CHART_RIGHT
185188
|| mLegend.getPosition() == LegendPosition.BELOW_CHART_CENTER) {
186189

187-
float yOffset = getRequiredBottomOffset(); // It's possible that we do not need this offset anymore as it is available through the extraOffsets
190+
// It's possible that we do not need this offset anymore as it
191+
// is available through the extraOffsets, but changing it can mean
192+
// changing default visibility for existing apps.
193+
float yOffset = getRequiredLegendOffset();
194+
188195
legendBottom = Math.min(mLegend.mNeededHeight + yOffset, mViewPortHandler.getChartHeight() * mLegend.getMaxSizePercent());
189196

197+
} else if (mLegend.getPosition() == LegendPosition.ABOVE_CHART_LEFT
198+
|| mLegend.getPosition() == LegendPosition.ABOVE_CHART_RIGHT
199+
|| mLegend.getPosition() == LegendPosition.ABOVE_CHART_CENTER) {
200+
201+
// It's possible that we do not need this offset anymore as it
202+
// is available through the extraOffsets, but changing it can mean
203+
// changing default visibility for existing apps.
204+
float yOffset = getRequiredLegendOffset();
205+
206+
legendTop = Math.min(mLegend.mNeededHeight + yOffset, mViewPortHandler.getChartHeight() * mLegend.getMaxSizePercent());
207+
190208
}
191209

192210
legendLeft += getRequiredBaseOffset();
193211
legendRight += getRequiredBaseOffset();
194212
legendTop += getRequiredBaseOffset();
195213
}
196214

197-
float min = Utils.convertDpToPixel(10f);
215+
float minOffset = Utils.convertDpToPixel(mMinOffset);
198216

199217
if (this instanceof RadarChart) {
200218
XAxis x = ((RadarChart) this).getXAxis();
201219

202220
if (x.isEnabled() && x.isDrawLabelsEnabled()) {
203-
min = Math.max(Utils.convertDpToPixel(10f), x.mLabelWidth);
221+
minOffset = Math.max(minOffset, x.mLabelWidth);
204222
}
205223
}
206224

@@ -209,10 +227,10 @@ public void calculateOffsets() {
209227
legendBottom += getExtraBottomOffset();
210228
legendLeft += getExtraLeftOffset();
211229

212-
float offsetLeft = Math.max(min, legendLeft);
213-
float offsetTop = Math.max(min, legendTop);
214-
float offsetRight = Math.max(min, legendRight);
215-
float offsetBottom = Math.max(min, Math.max(getRequiredBaseOffset(), legendBottom));
230+
float offsetLeft = Math.max(minOffset, legendLeft);
231+
float offsetTop = Math.max(minOffset, legendTop);
232+
float offsetRight = Math.max(minOffset, legendRight);
233+
float offsetBottom = Math.max(minOffset, Math.max(getRequiredBaseOffset(), legendBottom));
216234

217235
mViewPortHandler.restrainViewPort(offsetLeft, offsetTop, offsetRight, offsetBottom);
218236

@@ -383,11 +401,11 @@ public float getDiameter() {
383401
public abstract float getRadius();
384402

385403
/**
386-
* Returns the required bottom offset for the chart.
404+
* Returns the required offset for the chart legend.
387405
*
388406
* @return
389407
*/
390-
protected abstract float getRequiredBottomOffset();
408+
protected abstract float getRequiredLegendOffset();
391409

392410
/**
393411
* Returns the base offset needed for the chart without calculating the

‎MPChartLib/src/com/github/mikephil/charting/charts/RadarChart.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ public int getSkipWebLineCount() {
371371
}
372372

373373
@Override
374-
protected float getRequiredBottomOffset() {
374+
protected float getRequiredLegendOffset() {
375375
return mLegendRenderer.getLabelPaint().getTextSize() * 4.f;
376376
}
377377

‎MPChartLib/src/com/github/mikephil/charting/components/Legend.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public enum LegendPosition {
2424
RIGHT_OF_CHART, RIGHT_OF_CHART_CENTER, RIGHT_OF_CHART_INSIDE,
2525
LEFT_OF_CHART, LEFT_OF_CHART_CENTER, LEFT_OF_CHART_INSIDE,
2626
BELOW_CHART_LEFT, BELOW_CHART_RIGHT, BELOW_CHART_CENTER,
27+
ABOVE_CHART_LEFT, ABOVE_CHART_RIGHT, ABOVE_CHART_CENTER,
2728
PIECHART_CENTER
2829
}
2930

@@ -651,7 +652,10 @@ public void calculateDimensions(Paint labelpaint, ViewPortHandler viewPortHandle
651652

652653
} else if (mPosition == LegendPosition.BELOW_CHART_LEFT
653654
|| mPosition == LegendPosition.BELOW_CHART_RIGHT
654-
|| mPosition == LegendPosition.BELOW_CHART_CENTER) {
655+
|| mPosition == LegendPosition.BELOW_CHART_CENTER
656+
|| mPosition == LegendPosition.ABOVE_CHART_LEFT
657+
|| mPosition == LegendPosition.ABOVE_CHART_RIGHT
658+
|| mPosition == LegendPosition.ABOVE_CHART_CENTER) {
655659

656660
int labelCount = mLabels.length;
657661
float labelLineHeight = Utils.getLineHeight(labelpaint);

‎MPChartLib/src/com/github/mikephil/charting/renderer/LegendRenderer.java

+17-5
Original file line numberDiff line numberDiff line change
@@ -204,30 +204,42 @@ public void renderLegend(Canvas c) {
204204
switch (legendPosition) {
205205
case BELOW_CHART_LEFT:
206206
case BELOW_CHART_RIGHT:
207-
case BELOW_CHART_CENTER: {
207+
case BELOW_CHART_CENTER:
208+
case ABOVE_CHART_LEFT:
209+
case ABOVE_CHART_RIGHT:
210+
case ABOVE_CHART_CENTER: {
208211
float contentWidth = mViewPortHandler.contentWidth();
209212

210213
float originPosX;
211214

212-
if (legendPosition == Legend.LegendPosition.BELOW_CHART_LEFT) {
215+
if (legendPosition == Legend.LegendPosition.BELOW_CHART_LEFT
216+
|| legendPosition == Legend.LegendPosition.ABOVE_CHART_LEFT) {
213217
originPosX = mViewPortHandler.contentLeft() + xoffset;
214218

215219
if (direction == Legend.LegendDirection.RIGHT_TO_LEFT)
216220
originPosX += mLegend.mNeededWidth;
217-
} else if (legendPosition == Legend.LegendPosition.BELOW_CHART_RIGHT) {
221+
} else if (legendPosition == Legend.LegendPosition.BELOW_CHART_RIGHT
222+
|| legendPosition == Legend.LegendPosition.ABOVE_CHART_RIGHT) {
218223
originPosX = mViewPortHandler.contentRight() - xoffset;
219224

220225
if (direction == Legend.LegendDirection.LEFT_TO_RIGHT)
221226
originPosX -= mLegend.mNeededWidth;
222-
} else // if (legendPosition == Legend.LegendPosition.BELOW_CHART_CENTER)
227+
} else // BELOW_CHART_CENTER || ABOVE_CHART_CENTER
223228
originPosX = mViewPortHandler.contentLeft() + contentWidth / 2.f;
224229

225230
FSize[] calculatedLineSizes = mLegend.getCalculatedLineSizes();
226231
FSize[] calculatedLabelSizes = mLegend.getCalculatedLabelSizes();
227232
Boolean[] calculatedLabelBreakPoints = mLegend.getCalculatedLabelBreakPoints();
228233

229234
posX = originPosX;
230-
posY = mViewPortHandler.getChartHeight() - yoffset - mLegend.mNeededHeight;
235+
236+
if (legendPosition == Legend.LegendPosition.ABOVE_CHART_LEFT ||
237+
legendPosition == Legend.LegendPosition.ABOVE_CHART_RIGHT ||
238+
legendPosition == Legend.LegendPosition.ABOVE_CHART_CENTER) {
239+
posY = 0.f;
240+
} else {
241+
posY = mViewPortHandler.getChartHeight() - yoffset - mLegend.mNeededHeight;
242+
}
231243

232244
int lineIndex = 0;
233245

0 commit comments

Comments
 (0)
Please sign in to comment.