Skip to content

Commit 493da1d

Browse files
committed
Merge pull request PhilJay#1671 from danielgindi/legend-exploded
Exploded the Legend-Position enum to support more combinations
2 parents 48f1b6f + a1fa7ad commit 493da1d

File tree

5 files changed

+637
-386
lines changed

5 files changed

+637
-386
lines changed

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

Lines changed: 97 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@
1010
import android.graphics.Paint;
1111
import android.graphics.Paint.Style;
1212
import android.graphics.PointF;
13+
import android.graphics.RectF;
1314
import android.util.AttributeSet;
1415
import android.util.Log;
1516
import android.view.MotionEvent;
1617

18+
import com.github.mikephil.charting.components.Legend;
1719
import com.github.mikephil.charting.components.Legend.LegendPosition;
1820
import com.github.mikephil.charting.components.XAxis.XAxisPosition;
1921
import com.github.mikephil.charting.components.YAxis;
@@ -299,9 +301,13 @@ protected void prepareValuePxMatrix() {
299301
Log.i(LOG_TAG, "Preparing Value-Px Matrix, xmin: " + mXAxis.mAxisMinimum + ", xmax: "
300302
+ mXAxis.mAxisMaximum + ", xdelta: " + mXAxis.mAxisRange);
301303

302-
mRightAxisTransformer.prepareMatrixValuePx(mXAxis.mAxisMinimum, mXAxis.mAxisRange, mAxisRight.mAxisRange,
304+
mRightAxisTransformer.prepareMatrixValuePx(mXAxis.mAxisMinimum,
305+
mXAxis.mAxisRange,
306+
mAxisRight.mAxisRange,
303307
mAxisRight.mAxisMinimum);
304-
mLeftAxisTransformer.prepareMatrixValuePx(mXAxis.mAxisMinimum, mXAxis.mAxisRange, mAxisLeft.mAxisRange,
308+
mLeftAxisTransformer.prepareMatrixValuePx(mXAxis.mAxisMinimum,
309+
mXAxis.mAxisRange,
310+
mAxisLeft.mAxisRange,
305311
mAxisLeft.mAxisMinimum);
306312
}
307313

@@ -355,56 +361,103 @@ protected void calcMinMax() {
355361
.RIGHT));
356362
}
357363

364+
protected void calculateLegendOffsets(RectF offsets) {
365+
366+
offsets.left = 0.f;
367+
offsets.right = 0.f;
368+
offsets.top = 0.f;
369+
offsets.bottom = 0.f;
370+
371+
// setup offsets for legend
372+
if (mLegend != null && mLegend.isEnabled() && !mLegend.isDrawInsideEnabled()) {
373+
switch (mLegend.getOrientation()) {
374+
case VERTICAL:
375+
376+
switch (mLegend.getHorizontalAlignment()) {
377+
case LEFT:
378+
offsets.left += Math.min(mLegend.mNeededWidth,
379+
mViewPortHandler.getChartWidth() * mLegend.getMaxSizePercent())
380+
+ mLegend.getXOffset();
381+
break;
382+
383+
case RIGHT:
384+
offsets.right += Math.min(mLegend.mNeededWidth,
385+
mViewPortHandler.getChartWidth() * mLegend.getMaxSizePercent())
386+
+ mLegend.getXOffset();
387+
break;
388+
389+
case CENTER:
390+
391+
switch (mLegend.getVerticalAlignment()) {
392+
case TOP:
393+
offsets.top += Math.min(mLegend.mNeededHeight,
394+
mViewPortHandler.getChartHeight() * mLegend.getMaxSizePercent())
395+
+ mLegend.getYOffset();
396+
397+
if (getXAxis().isEnabled() && getXAxis().isDrawLabelsEnabled())
398+
offsets.top += getXAxis().mLabelRotatedHeight;
399+
break;
400+
401+
case BOTTOM:
402+
offsets.bottom += Math.min(mLegend.mNeededHeight,
403+
mViewPortHandler.getChartHeight() * mLegend.getMaxSizePercent())
404+
+ mLegend.getYOffset();
405+
406+
if (getXAxis().isEnabled() && getXAxis().isDrawLabelsEnabled())
407+
offsets.bottom += getXAxis().mLabelRotatedHeight;
408+
break;
409+
410+
default:
411+
break;
412+
}
413+
}
414+
415+
break;
416+
417+
case HORIZONTAL:
418+
419+
switch (mLegend.getVerticalAlignment()) {
420+
case TOP:
421+
offsets.top += Math.min(mLegend.mNeededHeight,
422+
mViewPortHandler.getChartHeight() * mLegend.getMaxSizePercent())
423+
+ mLegend.getYOffset();
424+
425+
if (getXAxis().isEnabled() && getXAxis().isDrawLabelsEnabled())
426+
offsets.top += getXAxis().mLabelRotatedHeight;
427+
break;
428+
429+
case BOTTOM:
430+
offsets.bottom += Math.min(mLegend.mNeededHeight,
431+
mViewPortHandler.getChartHeight() * mLegend.getMaxSizePercent())
432+
+ mLegend.getYOffset();
433+
434+
if (getXAxis().isEnabled() && getXAxis().isDrawLabelsEnabled())
435+
offsets.bottom += getXAxis().mLabelRotatedHeight;
436+
break;
437+
438+
default:
439+
break;
440+
}
441+
break;
442+
}
443+
}
444+
}
445+
446+
private RectF mOffsetsBuffer = new RectF();
447+
358448
@Override
359449
public void calculateOffsets() {
360450

361451
if (!mCustomViewPortEnabled) {
362452

363453
float offsetLeft = 0f, offsetRight = 0f, offsetTop = 0f, offsetBottom = 0f;
364454

365-
// setup offsets for legend
366-
if (mLegend != null && mLegend.isEnabled()) {
367-
368-
if (mLegend.getPosition() == LegendPosition.RIGHT_OF_CHART
369-
|| mLegend.getPosition() == LegendPosition.RIGHT_OF_CHART_CENTER) {
370-
371-
offsetRight += Math.min(mLegend.mNeededWidth, mViewPortHandler.getChartWidth()
372-
* mLegend.getMaxSizePercent())
373-
+ mLegend.getXOffset() * 2f;
374-
375-
} else if (mLegend.getPosition() == LegendPosition.LEFT_OF_CHART
376-
|| mLegend.getPosition() == LegendPosition.LEFT_OF_CHART_CENTER) {
377-
378-
offsetLeft += Math.min(mLegend.mNeededWidth, mViewPortHandler.getChartWidth()
379-
* mLegend.getMaxSizePercent())
380-
+ mLegend.getXOffset() * 2f;
455+
calculateLegendOffsets(mOffsetsBuffer);
381456

382-
} else if (mLegend.getPosition() == LegendPosition.BELOW_CHART_LEFT
383-
|| mLegend.getPosition() == LegendPosition.BELOW_CHART_RIGHT
384-
|| mLegend.getPosition() == LegendPosition.BELOW_CHART_CENTER) {
385-
386-
// It's possible that we do not need this offset anymore as it
387-
// is available through the extraOffsets, but changing it can mean
388-
// changing default visibility for existing apps.
389-
float yOffset = mLegend.mTextHeightMax;
390-
391-
offsetBottom += Math.min(mLegend.mNeededHeight + yOffset,
392-
mViewPortHandler.getChartHeight() * mLegend.getMaxSizePercent());
393-
394-
} else if (mLegend.getPosition() == LegendPosition.ABOVE_CHART_LEFT
395-
|| mLegend.getPosition() == LegendPosition.ABOVE_CHART_RIGHT
396-
|| mLegend.getPosition() == LegendPosition.ABOVE_CHART_CENTER) {
397-
398-
// It's possible that we do not need this offset anymore as it
399-
// is available through the extraOffsets, but changing it can mean
400-
// changing default visibility for existing apps.
401-
float yOffset = mLegend.mTextHeightMax;
402-
403-
offsetTop += Math.min(mLegend.mNeededHeight + yOffset,
404-
mViewPortHandler.getChartHeight() * mLegend.getMaxSizePercent());
405-
406-
}
407-
}
457+
offsetLeft += mOffsetsBuffer.left;
458+
offsetTop += mOffsetsBuffer.top;
459+
offsetRight += mOffsetsBuffer.right;
460+
offsetBottom += mOffsetsBuffer.bottom;
408461

409462
// offsets for y-labels
410463
if (mAxisLeft.needsOffset()) {

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

Lines changed: 7 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -56,48 +56,19 @@ protected void init() {
5656
mXAxisRenderer = new XAxisRendererHorizontalBarChart(mViewPortHandler, mXAxis, mLeftAxisTransformer, this);
5757
}
5858

59+
private RectF mOffsetsBuffer = new RectF();
60+
5961
@Override
6062
public void calculateOffsets() {
6163

6264
float offsetLeft = 0f, offsetRight = 0f, offsetTop = 0f, offsetBottom = 0f;
6365

64-
// setup offsets for legend
65-
if (mLegend != null && mLegend.isEnabled()) {
66-
67-
if (mLegend.getPosition() == LegendPosition.RIGHT_OF_CHART || mLegend.getPosition() == LegendPosition.RIGHT_OF_CHART_CENTER) {
68-
69-
offsetRight += Math.min(mLegend.mNeededWidth, mViewPortHandler.getChartWidth() * mLegend.getMaxSizePercent())
70-
+ mLegend.getXOffset() * 2f;
71-
72-
} else if (mLegend.getPosition() == LegendPosition.LEFT_OF_CHART
73-
|| mLegend.getPosition() == LegendPosition.LEFT_OF_CHART_CENTER) {
74-
75-
offsetLeft += Math.min(mLegend.mNeededWidth, mViewPortHandler.getChartWidth() * mLegend.getMaxSizePercent())
76-
+ mLegend.getXOffset() * 2f;
77-
78-
} else if (mLegend.getPosition() == LegendPosition.BELOW_CHART_LEFT
79-
|| mLegend.getPosition() == LegendPosition.BELOW_CHART_RIGHT
80-
|| mLegend.getPosition() == LegendPosition.BELOW_CHART_CENTER) {
66+
calculateLegendOffsets(mOffsetsBuffer);
8167

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;
86-
87-
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());
99-
}
100-
}
68+
offsetLeft += mOffsetsBuffer.left;
69+
offsetTop += mOffsetsBuffer.top;
70+
offsetRight += mOffsetsBuffer.right;
71+
offsetBottom += mOffsetsBuffer.bottom;
10172

10273
// offsets for y-labels
10374
if (mAxisLeft.needsOffset()) {

0 commit comments

Comments
 (0)