From c5c19d4f9197ce9348833b0aa70725fb7cc3d467 Mon Sep 17 00:00:00 2001 From: Daniel Cohen Gindi Date: Thu, 1 Oct 2015 16:13:40 +0300 Subject: [PATCH 1/2] Allow the minimum offset to be customized --- .../charting/charts/BarLineChartBase.java | 12 +++++++++--- .../charting/charts/HorizontalBarChart.java | 9 ++++++--- .../charting/charts/PieRadarChartBase.java | 15 +++++++++------ 3 files changed, 24 insertions(+), 12 deletions(-) diff --git a/MPChartLib/src/com/github/mikephil/charting/charts/BarLineChartBase.java b/MPChartLib/src/com/github/mikephil/charting/charts/BarLineChartBase.java index 1fc2ec2ea1..cf7fac5ef6 100644 --- a/MPChartLib/src/com/github/mikephil/charting/charts/BarLineChartBase.java +++ b/MPChartLib/src/com/github/mikephil/charting/charts/BarLineChartBase.java @@ -88,6 +88,9 @@ public abstract class BarLineChartBase Date: Thu, 1 Oct 2015 17:27:37 +0300 Subject: [PATCH 2/2] Add support for a legend above the chart --- .../charting/charts/BarLineChartBase.java | 27 ++++++++++++------- .../charting/charts/HorizontalBarChart.java | 18 +++++++++++-- .../mikephil/charting/charts/PieChart.java | 2 +- .../charting/charts/PieRadarChartBase.java | 21 ++++++++++++--- .../mikephil/charting/charts/RadarChart.java | 2 +- .../mikephil/charting/components/Legend.java | 6 ++++- .../charting/renderer/LegendRenderer.java | 22 +++++++++++---- 7 files changed, 75 insertions(+), 23 deletions(-) diff --git a/MPChartLib/src/com/github/mikephil/charting/charts/BarLineChartBase.java b/MPChartLib/src/com/github/mikephil/charting/charts/BarLineChartBase.java index cf7fac5ef6..70c54ca7e4 100644 --- a/MPChartLib/src/com/github/mikephil/charting/charts/BarLineChartBase.java +++ b/MPChartLib/src/com/github/mikephil/charting/charts/BarLineChartBase.java @@ -439,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()); + } } diff --git a/MPChartLib/src/com/github/mikephil/charting/charts/HorizontalBarChart.java b/MPChartLib/src/com/github/mikephil/charting/charts/HorizontalBarChart.java index 9e52b3150d..021a5b8257 100644 --- a/MPChartLib/src/com/github/mikephil/charting/charts/HorizontalBarChart.java +++ b/MPChartLib/src/com/github/mikephil/charting/charts/HorizontalBarChart.java @@ -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()); } } diff --git a/MPChartLib/src/com/github/mikephil/charting/charts/PieChart.java b/MPChartLib/src/com/github/mikephil/charting/charts/PieChart.java index 91511ed6c0..554d114236 100644 --- a/MPChartLib/src/com/github/mikephil/charting/charts/PieChart.java +++ b/MPChartLib/src/com/github/mikephil/charting/charts/PieChart.java @@ -420,7 +420,7 @@ public boolean isDrawCenterTextEnabled() { } @Override - protected float getRequiredBottomOffset() { + protected float getRequiredLegendOffset() { return mLegendRenderer.getLabelPaint().getTextSize() * 2.f; } diff --git a/MPChartLib/src/com/github/mikephil/charting/charts/PieRadarChartBase.java b/MPChartLib/src/com/github/mikephil/charting/charts/PieRadarChartBase.java index 925d32a4cd..1acc57aef9 100644 --- a/MPChartLib/src/com/github/mikephil/charting/charts/PieRadarChartBase.java +++ b/MPChartLib/src/com/github/mikephil/charting/charts/PieRadarChartBase.java @@ -187,9 +187,24 @@ 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(); @@ -386,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 diff --git a/MPChartLib/src/com/github/mikephil/charting/charts/RadarChart.java b/MPChartLib/src/com/github/mikephil/charting/charts/RadarChart.java index 961a0a0472..adcaabe635 100644 --- a/MPChartLib/src/com/github/mikephil/charting/charts/RadarChart.java +++ b/MPChartLib/src/com/github/mikephil/charting/charts/RadarChart.java @@ -374,7 +374,7 @@ public int getSkipWebLineCount() { } @Override - protected float getRequiredBottomOffset() { + protected float getRequiredLegendOffset() { return mLegendRenderer.getLabelPaint().getTextSize() * 4.f; } diff --git a/MPChartLib/src/com/github/mikephil/charting/components/Legend.java b/MPChartLib/src/com/github/mikephil/charting/components/Legend.java index 9ab2e724e8..9990abbe6b 100644 --- a/MPChartLib/src/com/github/mikephil/charting/components/Legend.java +++ b/MPChartLib/src/com/github/mikephil/charting/components/Legend.java @@ -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 } @@ -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); diff --git a/MPChartLib/src/com/github/mikephil/charting/renderer/LegendRenderer.java b/MPChartLib/src/com/github/mikephil/charting/renderer/LegendRenderer.java index d9dfb50fdd..9d87300347 100644 --- a/MPChartLib/src/com/github/mikephil/charting/renderer/LegendRenderer.java +++ b/MPChartLib/src/com/github/mikephil/charting/renderer/LegendRenderer.java @@ -204,22 +204,27 @@ 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(); @@ -227,7 +232,14 @@ public void renderLegend(Canvas c) { 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;