Skip to content

Commit

Permalink
Merge pull request #1671 from danielgindi/legend-exploded
Browse files Browse the repository at this point in the history
Exploded the Legend-Position enum to support more combinations
  • Loading branch information
PhilJay committed Apr 11, 2016
2 parents 48f1b6f + a1fa7ad commit 493da1d
Show file tree
Hide file tree
Showing 5 changed files with 637 additions and 386 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.PointF;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;

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

mRightAxisTransformer.prepareMatrixValuePx(mXAxis.mAxisMinimum, mXAxis.mAxisRange, mAxisRight.mAxisRange,
mRightAxisTransformer.prepareMatrixValuePx(mXAxis.mAxisMinimum,
mXAxis.mAxisRange,
mAxisRight.mAxisRange,
mAxisRight.mAxisMinimum);
mLeftAxisTransformer.prepareMatrixValuePx(mXAxis.mAxisMinimum, mXAxis.mAxisRange, mAxisLeft.mAxisRange,
mLeftAxisTransformer.prepareMatrixValuePx(mXAxis.mAxisMinimum,
mXAxis.mAxisRange,
mAxisLeft.mAxisRange,
mAxisLeft.mAxisMinimum);
}

Expand Down Expand Up @@ -355,56 +361,103 @@ protected void calcMinMax() {
.RIGHT));
}

protected void calculateLegendOffsets(RectF offsets) {

offsets.left = 0.f;
offsets.right = 0.f;
offsets.top = 0.f;
offsets.bottom = 0.f;

// setup offsets for legend
if (mLegend != null && mLegend.isEnabled() && !mLegend.isDrawInsideEnabled()) {
switch (mLegend.getOrientation()) {
case VERTICAL:

switch (mLegend.getHorizontalAlignment()) {
case LEFT:
offsets.left += Math.min(mLegend.mNeededWidth,
mViewPortHandler.getChartWidth() * mLegend.getMaxSizePercent())
+ mLegend.getXOffset();
break;

case RIGHT:
offsets.right += Math.min(mLegend.mNeededWidth,
mViewPortHandler.getChartWidth() * mLegend.getMaxSizePercent())
+ mLegend.getXOffset();
break;

case CENTER:

switch (mLegend.getVerticalAlignment()) {
case TOP:
offsets.top += Math.min(mLegend.mNeededHeight,
mViewPortHandler.getChartHeight() * mLegend.getMaxSizePercent())
+ mLegend.getYOffset();

if (getXAxis().isEnabled() && getXAxis().isDrawLabelsEnabled())
offsets.top += getXAxis().mLabelRotatedHeight;
break;

case BOTTOM:
offsets.bottom += Math.min(mLegend.mNeededHeight,
mViewPortHandler.getChartHeight() * mLegend.getMaxSizePercent())
+ mLegend.getYOffset();

if (getXAxis().isEnabled() && getXAxis().isDrawLabelsEnabled())
offsets.bottom += getXAxis().mLabelRotatedHeight;
break;

default:
break;
}
}

break;

case HORIZONTAL:

switch (mLegend.getVerticalAlignment()) {
case TOP:
offsets.top += Math.min(mLegend.mNeededHeight,
mViewPortHandler.getChartHeight() * mLegend.getMaxSizePercent())
+ mLegend.getYOffset();

if (getXAxis().isEnabled() && getXAxis().isDrawLabelsEnabled())
offsets.top += getXAxis().mLabelRotatedHeight;
break;

case BOTTOM:
offsets.bottom += Math.min(mLegend.mNeededHeight,
mViewPortHandler.getChartHeight() * mLegend.getMaxSizePercent())
+ mLegend.getYOffset();

if (getXAxis().isEnabled() && getXAxis().isDrawLabelsEnabled())
offsets.bottom += getXAxis().mLabelRotatedHeight;
break;

default:
break;
}
break;
}
}
}

private RectF mOffsetsBuffer = new RectF();

@Override
public void calculateOffsets() {

if (!mCustomViewPortEnabled) {

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

// setup offsets for legend
if (mLegend != null && mLegend.isEnabled()) {

if (mLegend.getPosition() == LegendPosition.RIGHT_OF_CHART
|| mLegend.getPosition() == LegendPosition.RIGHT_OF_CHART_CENTER) {

offsetRight += Math.min(mLegend.mNeededWidth, mViewPortHandler.getChartWidth()
* mLegend.getMaxSizePercent())
+ mLegend.getXOffset() * 2f;

} else if (mLegend.getPosition() == LegendPosition.LEFT_OF_CHART
|| mLegend.getPosition() == LegendPosition.LEFT_OF_CHART_CENTER) {

offsetLeft += Math.min(mLegend.mNeededWidth, mViewPortHandler.getChartWidth()
* mLegend.getMaxSizePercent())
+ mLegend.getXOffset() * 2f;
calculateLegendOffsets(mOffsetsBuffer);

} else if (mLegend.getPosition() == LegendPosition.BELOW_CHART_LEFT
|| mLegend.getPosition() == LegendPosition.BELOW_CHART_RIGHT
|| mLegend.getPosition() == LegendPosition.BELOW_CHART_CENTER) {

// It's possible that we do not need this offset anymore as it
// is available through the extraOffsets, but changing it can mean
// changing default visibility for existing apps.
float yOffset = mLegend.mTextHeightMax;

offsetBottom += Math.min(mLegend.mNeededHeight + yOffset,
mViewPortHandler.getChartHeight() * mLegend.getMaxSizePercent());

} else if (mLegend.getPosition() == LegendPosition.ABOVE_CHART_LEFT
|| mLegend.getPosition() == LegendPosition.ABOVE_CHART_RIGHT
|| mLegend.getPosition() == LegendPosition.ABOVE_CHART_CENTER) {

// It's possible that we do not need this offset anymore as it
// is available through the extraOffsets, but changing it can mean
// changing default visibility for existing apps.
float yOffset = mLegend.mTextHeightMax;

offsetTop += Math.min(mLegend.mNeededHeight + yOffset,
mViewPortHandler.getChartHeight() * mLegend.getMaxSizePercent());

}
}
offsetLeft += mOffsetsBuffer.left;
offsetTop += mOffsetsBuffer.top;
offsetRight += mOffsetsBuffer.right;
offsetBottom += mOffsetsBuffer.bottom;

// offsets for y-labels
if (mAxisLeft.needsOffset()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,48 +56,19 @@ protected void init() {
mXAxisRenderer = new XAxisRendererHorizontalBarChart(mViewPortHandler, mXAxis, mLeftAxisTransformer, this);
}

private RectF mOffsetsBuffer = new RectF();

@Override
public void calculateOffsets() {

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

// setup offsets for legend
if (mLegend != null && mLegend.isEnabled()) {

if (mLegend.getPosition() == LegendPosition.RIGHT_OF_CHART || mLegend.getPosition() == LegendPosition.RIGHT_OF_CHART_CENTER) {

offsetRight += Math.min(mLegend.mNeededWidth, mViewPortHandler.getChartWidth() * mLegend.getMaxSizePercent())
+ mLegend.getXOffset() * 2f;

} else if (mLegend.getPosition() == LegendPosition.LEFT_OF_CHART
|| mLegend.getPosition() == LegendPosition.LEFT_OF_CHART_CENTER) {

offsetLeft += Math.min(mLegend.mNeededWidth, mViewPortHandler.getChartWidth() * mLegend.getMaxSizePercent())
+ mLegend.getXOffset() * 2f;

} else if (mLegend.getPosition() == LegendPosition.BELOW_CHART_LEFT
|| mLegend.getPosition() == LegendPosition.BELOW_CHART_RIGHT
|| mLegend.getPosition() == LegendPosition.BELOW_CHART_CENTER) {
calculateLegendOffsets(mOffsetsBuffer);

// It's possible that we do not need this offset anymore as it
// is available through the extraOffsets, but changing it can mean
// changing default visibility for existing apps.
float yOffset = mLegend.mTextHeightMax;

offsetBottom += Math.min(mLegend.mNeededHeight + yOffset, mViewPortHandler.getChartHeight() * mLegend.getMaxSizePercent());

} else if (mLegend.getPosition() == LegendPosition.ABOVE_CHART_LEFT
|| mLegend.getPosition() == LegendPosition.ABOVE_CHART_RIGHT
|| mLegend.getPosition() == LegendPosition.ABOVE_CHART_CENTER) {

// It's possible that we do not need this offset anymore as it
// is available through the extraOffsets, but changing it can mean
// changing default visibility for existing apps.
float yOffset = mLegend.mTextHeightMax * 2.f;

offsetTop += Math.min(mLegend.mNeededHeight + yOffset, mViewPortHandler.getChartHeight() * mLegend.getMaxSizePercent());
}
}
offsetLeft += mOffsetsBuffer.left;
offsetTop += mOffsetsBuffer.top;
offsetRight += mOffsetsBuffer.right;
offsetBottom += mOffsetsBuffer.bottom;

// offsets for y-labels
if (mAxisLeft.needsOffset()) {
Expand Down
Loading

0 comments on commit 493da1d

Please sign in to comment.