Skip to content

Commit

Permalink
Merge pull request #1059 from danielgindi/features
Browse files Browse the repository at this point in the history
Some new features
  • Loading branch information
PhilJay committed Oct 1, 2015
2 parents beee126 + 5b31bb6 commit 10a392d
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ public abstract class BarLineChartBase<T extends BarLineScatterCandleBubbleData<

protected boolean mDrawBorders = false;

/** Sets the minimum offset (padding) around the chart, defaults to 10 */
protected float mMinOffset = 10.f;

/** the listener for user drawing on the chart */
protected OnDrawListener mDrawListener;

Expand Down Expand Up @@ -436,19 +439,26 @@ public void calculateOffsets() {
|| mLegend.getPosition() == LegendPosition.BELOW_CHART_RIGHT
|| mLegend.getPosition() == LegendPosition.BELOW_CHART_CENTER) {

float yOffset = mLegend.mTextHeightMax; // It's
// possible
// that we do
// not need
// this offset
// anymore as
// it is
// available
// through the
// extraOffsets
// 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());

}
}

Expand Down Expand Up @@ -488,10 +498,13 @@ public void calculateOffsets() {
offsetBottom += getExtraBottomOffset();
offsetLeft += getExtraLeftOffset();

float min = Utils.convertDpToPixel(10f);
float minOffset = Utils.convertDpToPixel(mMinOffset);

mViewPortHandler.restrainViewPort(Math.max(min, offsetLeft), Math.max(min, offsetTop),
Math.max(min, offsetRight), Math.max(min, offsetBottom));
mViewPortHandler.restrainViewPort(
Math.max(minOffset, offsetLeft),
Math.max(minOffset, offsetTop),
Math.max(minOffset, offsetRight),
Math.max(minOffset, offsetBottom));

if (mLogEnabled) {
Log.i(LOG_TAG, "offsetLeft: " + offsetLeft + ", offsetTop: " + offsetTop
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,23 @@ public void calculateOffsets() {
|| mLegend.getPosition() == LegendPosition.BELOW_CHART_RIGHT
|| mLegend.getPosition() == LegendPosition.BELOW_CHART_CENTER) {

float yOffset = mLegend.mTextHeightMax * 2.f; // It's possible that we do not need this offset anymore
// as it is available through the extraOffsets
// 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;

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());
}
}

Expand Down Expand Up @@ -119,10 +133,13 @@ public void calculateOffsets() {
offsetBottom += getExtraBottomOffset();
offsetLeft += getExtraLeftOffset();

float min = Utils.convertDpToPixel(10f);
float minOffset = Utils.convertDpToPixel(mMinOffset);

mViewPortHandler.restrainViewPort(Math.max(min, offsetLeft), Math.max(min, offsetTop), Math.max(min, offsetRight),
Math.max(min, offsetBottom));
mViewPortHandler.restrainViewPort(
Math.max(minOffset, offsetLeft),
Math.max(minOffset, offsetTop),
Math.max(minOffset, offsetRight),
Math.max(minOffset, offsetBottom));

if (mLogEnabled) {
Log.i(LOG_TAG, "offsetLeft: " + offsetLeft + ", offsetTop: " + offsetTop + ", offsetRight: " + offsetRight + ", offsetBottom: "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ public boolean isDrawCenterTextEnabled() {
}

@Override
protected float getRequiredBottomOffset() {
protected float getRequiredLegendOffset() {
return mLegendRenderer.getLabelPaint().getTextSize() * 2.f;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ public abstract class PieRadarChartBase<T extends ChartData<? extends DataSet<?
/** flag that indicates if rotation is enabled or not */
protected boolean mRotateEnabled = true;

/** Sets the minimum offset (padding) around the chart, defaults to 10 */
protected float mMinOffset = 10.f;

public PieRadarChartBase(Context context) {
super(context);
}
Expand Down Expand Up @@ -184,23 +187,38 @@ public void calculateOffsets() {
|| mLegend.getPosition() == LegendPosition.BELOW_CHART_RIGHT
|| mLegend.getPosition() == LegendPosition.BELOW_CHART_CENTER) {

float yOffset = getRequiredBottomOffset(); // It's possible that we do not need this offset anymore as it is available through the extraOffsets
// 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 = getRequiredLegendOffset();

legendBottom = 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 = getRequiredLegendOffset();

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

}

legendLeft += getRequiredBaseOffset();
legendRight += getRequiredBaseOffset();
legendTop += getRequiredBaseOffset();
}

float min = Utils.convertDpToPixel(10f);
float minOffset = Utils.convertDpToPixel(mMinOffset);

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

if (x.isEnabled() && x.isDrawLabelsEnabled()) {
min = Math.max(Utils.convertDpToPixel(10f), x.mLabelWidth);
minOffset = Math.max(minOffset, x.mLabelWidth);
}
}

Expand All @@ -209,10 +227,10 @@ public void calculateOffsets() {
legendBottom += getExtraBottomOffset();
legendLeft += getExtraLeftOffset();

float offsetLeft = Math.max(min, legendLeft);
float offsetTop = Math.max(min, legendTop);
float offsetRight = Math.max(min, legendRight);
float offsetBottom = Math.max(min, Math.max(getRequiredBaseOffset(), legendBottom));
float offsetLeft = Math.max(minOffset, legendLeft);
float offsetTop = Math.max(minOffset, legendTop);
float offsetRight = Math.max(minOffset, legendRight);
float offsetBottom = Math.max(minOffset, Math.max(getRequiredBaseOffset(), legendBottom));

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

Expand Down Expand Up @@ -383,11 +401,11 @@ public float getDiameter() {
public abstract float getRadius();

/**
* Returns the required bottom offset for the chart.
* Returns the required offset for the chart legend.
*
* @return
*/
protected abstract float getRequiredBottomOffset();
protected abstract float getRequiredLegendOffset();

/**
* Returns the base offset needed for the chart without calculating the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ public int getSkipWebLineCount() {
}

@Override
protected float getRequiredBottomOffset() {
protected float getRequiredLegendOffset() {
return mLegendRenderer.getLabelPaint().getTextSize() * 4.f;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public enum LegendPosition {
RIGHT_OF_CHART, RIGHT_OF_CHART_CENTER, RIGHT_OF_CHART_INSIDE,
LEFT_OF_CHART, LEFT_OF_CHART_CENTER, LEFT_OF_CHART_INSIDE,
BELOW_CHART_LEFT, BELOW_CHART_RIGHT, BELOW_CHART_CENTER,
ABOVE_CHART_LEFT, ABOVE_CHART_RIGHT, ABOVE_CHART_CENTER,
PIECHART_CENTER
}

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

} else if (mPosition == LegendPosition.BELOW_CHART_LEFT
|| mPosition == LegendPosition.BELOW_CHART_RIGHT
|| mPosition == LegendPosition.BELOW_CHART_CENTER) {
|| mPosition == LegendPosition.BELOW_CHART_CENTER
|| mPosition == LegendPosition.ABOVE_CHART_LEFT
|| mPosition == LegendPosition.ABOVE_CHART_RIGHT
|| mPosition == LegendPosition.ABOVE_CHART_CENTER) {

int labelCount = mLabels.length;
float labelLineHeight = Utils.getLineHeight(labelpaint);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,30 +204,42 @@ public void renderLegend(Canvas c) {
switch (legendPosition) {
case BELOW_CHART_LEFT:
case BELOW_CHART_RIGHT:
case BELOW_CHART_CENTER: {
case BELOW_CHART_CENTER:
case ABOVE_CHART_LEFT:
case ABOVE_CHART_RIGHT:
case ABOVE_CHART_CENTER: {
float contentWidth = mViewPortHandler.contentWidth();

float originPosX;

if (legendPosition == Legend.LegendPosition.BELOW_CHART_LEFT) {
if (legendPosition == Legend.LegendPosition.BELOW_CHART_LEFT
|| legendPosition == Legend.LegendPosition.ABOVE_CHART_LEFT) {
originPosX = mViewPortHandler.contentLeft() + xoffset;

if (direction == Legend.LegendDirection.RIGHT_TO_LEFT)
originPosX += mLegend.mNeededWidth;
} else if (legendPosition == Legend.LegendPosition.BELOW_CHART_RIGHT) {
} else if (legendPosition == Legend.LegendPosition.BELOW_CHART_RIGHT
|| legendPosition == Legend.LegendPosition.ABOVE_CHART_RIGHT) {
originPosX = mViewPortHandler.contentRight() - xoffset;

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

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

posX = originPosX;
posY = mViewPortHandler.getChartHeight() - yoffset - mLegend.mNeededHeight;

if (legendPosition == Legend.LegendPosition.ABOVE_CHART_LEFT ||
legendPosition == Legend.LegendPosition.ABOVE_CHART_RIGHT ||
legendPosition == Legend.LegendPosition.ABOVE_CHART_CENTER) {
posY = 0.f;
} else {
posY = mViewPortHandler.getChartHeight() - yoffset - mLegend.mNeededHeight;
}

int lineIndex = 0;

Expand Down

0 comments on commit 10a392d

Please sign in to comment.