diff --git a/MPChartExample/src/main/java/com/xxmassdeveloper/mpchartexample/LineChartTime.java b/MPChartExample/src/main/java/com/xxmassdeveloper/mpchartexample/LineChartTime.java index 212b90ff8..6c88b75cd 100644 --- a/MPChartExample/src/main/java/com/xxmassdeveloper/mpchartexample/LineChartTime.java +++ b/MPChartExample/src/main/java/com/xxmassdeveloper/mpchartexample/LineChartTime.java @@ -92,6 +92,7 @@ protected void onCreate(Bundle savedInstanceState) { xAxis.setTextColor(Color.rgb(255, 192, 56)); xAxis.setCenterAxisLabels(true); xAxis.setGranularity(1f); // one hour + xAxis.setDrawHighlightLabelsEnabled(true); xAxis.setValueFormatter(new IAxisValueFormatter() { private final SimpleDateFormat mFormat = new SimpleDateFormat("dd MMM HH:mm", Locale.ENGLISH); @@ -114,6 +115,7 @@ public String getFormattedValue(float value, AxisBase axis) { leftAxis.setAxisMaximum(170f); leftAxis.setYOffset(-9f); leftAxis.setTextColor(Color.rgb(255, 192, 56)); + leftAxis.setDrawHighlightLabelsEnabled(true); YAxis rightAxis = chart.getAxisRight(); rightAxis.setEnabled(false); diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/charts/BarLineChartBase.java b/MPChartLib/src/main/java/com/github/mikephil/charting/charts/BarLineChartBase.java index 0926dff24..0da7536c7 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/charts/BarLineChartBase.java +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/charts/BarLineChartBase.java @@ -268,9 +268,9 @@ protected void onDraw(Canvas canvas) { if (mAxisRight.isEnabled() && !mAxisRight.isDrawLimitLinesBehindDataEnabled()) mAxisRendererRight.renderLimitLines(canvas); - mXAxisRenderer.renderAxisLabels(canvas); - mAxisRendererLeft.renderAxisLabels(canvas); - mAxisRendererRight.renderAxisLabels(canvas); + mXAxisRenderer.renderAxisLabels(canvas, mIndicesToHighlight); + mAxisRendererLeft.renderAxisLabels(canvas, mIndicesToHighlight); + mAxisRendererRight.renderAxisLabels(canvas, mIndicesToHighlight); if (isClipValuesToContentEnabled()) { clipRestoreCount = canvas.save(); diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/charts/RadarChart.java b/MPChartLib/src/main/java/com/github/mikephil/charting/charts/RadarChart.java index 8c0885395..2be76ebda 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/charts/RadarChart.java +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/charts/RadarChart.java @@ -133,7 +133,7 @@ protected void onDraw(Canvas canvas) { if (mXAxis.isEnabled()) mXAxisRenderer.computeAxis(mXAxis.mAxisMinimum, mXAxis.mAxisMaximum, false); - mXAxisRenderer.renderAxisLabels(canvas); + mXAxisRenderer.renderAxisLabels(canvas, mIndicesToHighlight); if (mDrawWeb) mRenderer.drawExtras(canvas); @@ -149,7 +149,7 @@ protected void onDraw(Canvas canvas) { if (mYAxis.isEnabled() && !mYAxis.isDrawLimitLinesBehindDataEnabled()) mYAxisRenderer.renderLimitLines(canvas); - mYAxisRenderer.renderAxisLabels(canvas); + mYAxisRenderer.renderAxisLabels(canvas, mIndicesToHighlight); mRenderer.drawValues(canvas); diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/components/AxisBase.java b/MPChartLib/src/main/java/com/github/mikephil/charting/components/AxisBase.java index c90b4fc9b..e26e9ce0d 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/components/AxisBase.java +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/components/AxisBase.java @@ -3,6 +3,7 @@ import android.graphics.Color; import android.graphics.DashPathEffect; +import android.graphics.RectF; import android.util.Log; import com.github.mikephil.charting.formatter.DefaultAxisValueFormatter; @@ -185,6 +186,26 @@ public void setAxisMaxLabels(int labels) { mAxisMaxLabels = labels; } + /** + * If true, the highlight value label is rendered in front of the axis labels + */ + protected boolean mDrawHighlightLabelsEnabled = false; + + /** + * Color for the x-label highlight text. + */ + protected int mHighlightTextColor = Color.BLACK; + + /** + * Paint for the box surrounding the highlight label. + */ + protected int mHighlightFillColor = Color.WHITE; + + /** + * Additional padding for the highlight box + */ + protected RectF mHighlightFillPadding = new RectF(10f, 10f, 10f, 10f); + /** * default constructor */ @@ -813,4 +834,76 @@ public void setSpaceMax(float mSpaceMax) { this.mSpaceMax = mSpaceMax; } + + /** + * @return true if drawing highlight labels in front of the axis labels is enabled + */ + public boolean isDrawHighlightLabelsEnabled() { + return mDrawHighlightLabelsEnabled; + } + + /** + * Set to true if drawing highlight labels in front of the axis labels should be enabled + * + * @param enabled + */ + public void setDrawHighlightLabelsEnabled(boolean enabled) { + mDrawHighlightLabelsEnabled = enabled; + } + + /** + * @return highlight label text color + */ + public int getHighlightTextColor() { return mHighlightTextColor; } + + /** + * Set highlight label text color + * + * @param color + */ + public void setHighlightTextColor(int color) { + mHighlightTextColor = color; + } + + /** + * @return highlight label fill color + */ + public int getHighlightFillColor() { return mHighlightFillColor; } + + /** + * Sets highlight label fill color + * + * @param color + */ + public void setHighlightFillColor(int color) { + mHighlightFillColor = color; + } + + /** + * @return highlight label fill padding + */ + public RectF getHighlightFillPadding() { return mHighlightFillPadding; } + + /** + * Sets the highlight label fill padding. + * The fill rectangle is restricted to stay within ViewPortHandler content bounds + * for axis of INSIDE_CHART labelPosition type, + * and outside of the bounds for OUTSIDE_CHART axis. + * @param left + * @param top + * @param right + * @param bottom + */ + public void setHighlightFillPadding(float left, float top, float right, float bottom) { + mHighlightFillPadding = new RectF(left, top, right, bottom); + } + + /** + * Sets the highlight label fill padding. + * + * @param padding + */ + public void setHighlightFillPadding(RectF padding) { + mHighlightFillPadding = padding; + } } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/AxisRenderer.java b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/AxisRenderer.java index 72ea2d17c..eb89422ab 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/AxisRenderer.java +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/AxisRenderer.java @@ -7,6 +7,7 @@ import android.graphics.Paint.Style; import com.github.mikephil.charting.components.AxisBase; +import com.github.mikephil.charting.highlight.Highlight; import com.github.mikephil.charting.utils.MPPointD; import com.github.mikephil.charting.utils.Transformer; import com.github.mikephil.charting.utils.Utils; @@ -268,7 +269,7 @@ else if (last == first && n == 0) { * * @param c */ - public abstract void renderAxisLabels(Canvas c); + public abstract void renderAxisLabels(Canvas c, Highlight[] indicesToHighlight); /** * Draws the grid lines belonging to the axis. diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/XAxisRenderer.java b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/XAxisRenderer.java index 8adb56c73..96f4548a2 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/XAxisRenderer.java +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/XAxisRenderer.java @@ -11,6 +11,7 @@ import com.github.mikephil.charting.components.LimitLine; import com.github.mikephil.charting.components.XAxis; import com.github.mikephil.charting.components.XAxis.XAxisPosition; +import com.github.mikephil.charting.highlight.Highlight; import com.github.mikephil.charting.utils.FSize; import com.github.mikephil.charting.utils.MPPointD; import com.github.mikephil.charting.utils.MPPointF; @@ -102,9 +103,9 @@ protected void computeSize() { } @Override - public void renderAxisLabels(Canvas c) { + public void renderAxisLabels(Canvas c, Highlight[] indicesToHighlight) { - if (!mXAxis.isEnabled() || !mXAxis.isDrawLabelsEnabled()) + if (!mXAxis.isEnabled() || (!mXAxis.isDrawLabelsEnabled() && !mXAxis.isDrawHighlightLabelsEnabled())) return; float yoffset = mXAxis.getYOffset(); @@ -113,34 +114,47 @@ public void renderAxisLabels(Canvas c) { mAxisLabelPaint.setTextSize(mXAxis.getTextSize()); mAxisLabelPaint.setColor(mXAxis.getTextColor()); + float pos; MPPointF pointF = MPPointF.getInstance(0,0); if (mXAxis.getPosition() == XAxisPosition.TOP) { pointF.x = 0.5f; pointF.y = 1.0f; - drawLabels(c, mViewPortHandler.contentTop() - yoffset, pointF); + pos = mViewPortHandler.contentTop() - yoffset; + drawLabels(c, pos, pointF); + drawHighlightLabels(c, pos, pointF, indicesToHighlight); } else if (mXAxis.getPosition() == XAxisPosition.TOP_INSIDE) { pointF.x = 0.5f; pointF.y = 1.0f; - drawLabels(c, mViewPortHandler.contentTop() + yoffset + mXAxis.mLabelRotatedHeight, pointF); + pos = mViewPortHandler.contentTop() + yoffset + mXAxis.mLabelRotatedHeight; + drawLabels(c, pos, pointF); + drawHighlightLabels(c, pos, pointF, indicesToHighlight); } else if (mXAxis.getPosition() == XAxisPosition.BOTTOM) { pointF.x = 0.5f; pointF.y = 0.0f; - drawLabels(c, mViewPortHandler.contentBottom() + yoffset, pointF); + pos = mViewPortHandler.contentBottom() + yoffset; + drawLabels(c, pos, pointF); + drawHighlightLabels(c, pos, pointF, indicesToHighlight); } else if (mXAxis.getPosition() == XAxisPosition.BOTTOM_INSIDE) { pointF.x = 0.5f; pointF.y = 0.0f; - drawLabels(c, mViewPortHandler.contentBottom() - yoffset - mXAxis.mLabelRotatedHeight, pointF); + pos = mViewPortHandler.contentBottom() - yoffset - mXAxis.mLabelRotatedHeight; + drawLabels(c, pos, pointF); + drawHighlightLabels(c, pos, pointF, indicesToHighlight); } else { // BOTH SIDED pointF.x = 0.5f; pointF.y = 1.0f; - drawLabels(c, mViewPortHandler.contentTop() - yoffset, pointF); + pos = mViewPortHandler.contentTop() - yoffset; + drawLabels(c, pos, pointF); + drawHighlightLabels(c, pos, pointF, indicesToHighlight); pointF.x = 0.5f; pointF.y = 0.0f; - drawLabels(c, mViewPortHandler.contentBottom() + yoffset, pointF); + pos = mViewPortHandler.contentBottom() + yoffset; + drawLabels(c, pos, pointF); + drawHighlightLabels(c, pos, pointF, indicesToHighlight); } MPPointF.recycleInstance(pointF); } @@ -179,6 +193,9 @@ public void renderAxisLine(Canvas c) { */ protected void drawLabels(Canvas c, float pos, MPPointF anchor) { + if (!mXAxis.isDrawLabelsEnabled()) + return; + final float labelRotationAngleDegrees = mXAxis.getLabelRotationAngle(); boolean centeringEnabled = mXAxis.isCenterAxisLabelsEnabled(); @@ -398,4 +415,88 @@ public void renderLimitLineLabel(Canvas c, LimitLine limitLine, float[] position } } } + + /** + * Draws highlight labels + * + * @param c + * @param yPos + * @param anchor + */ + protected void drawHighlightLabels(Canvas c, float yPos, MPPointF anchor, Highlight[] indicesToHighlight) { + + if (!mXAxis.isDrawHighlightLabelsEnabled() || indicesToHighlight == null) + return; + + for (Highlight high : indicesToHighlight) { + + float xPos = high.getDrawX(); + + if (!mViewPortHandler.isInBoundsX(xPos)) + continue; + + String formattedLabel = mXAxis.getValueFormatter().getFormattedValue(high.getX(), mXAxis); + + // draw a rectangle + Paint paint = new Paint(mAxisLabelPaint); + paint.setColor(mXAxis.getHighlightFillColor()); + paint.setStyle(Paint.Style.FILL); + paint.setTextAlign(Paint.Align.LEFT); + + // off: x, y, translateX, translateY + float angleDegrees = mXAxis.getLabelRotationAngle(); + RectF offsets = Utils.computeXAxisOffsets(formattedLabel, xPos, yPos, paint, anchor, angleDegrees); + FSize size = Utils.calcTextSize(paint, formattedLabel); + + RectF padding = mXAxis.getHighlightFillPadding(); + int textColor = mXAxis.getHighlightTextColor(); + + // Resolve ambiguity of XAxisPosition + XAxisPosition xAxisPosition = mXAxis.getPosition(); + XAxisPosition axisPosition = xAxisPosition == XAxisPosition.TOP || xAxisPosition == XAxisPosition.TOP_INSIDE + || (xAxisPosition == XAxisPosition.BOTH_SIDED && yPos == (mViewPortHandler.contentTop() - mXAxis.getYOffset())) + ? XAxisPosition.TOP : XAxisPosition.BOTTOM; + boolean labelPositionInsideChart = xAxisPosition == XAxisPosition.TOP_INSIDE + || xAxisPosition == XAxisPosition.BOTTOM_INSIDE; + + // rectangle location + float left, right, top, bottom; + left = offsets.left - padding.left; + right = offsets.left + size.width + padding.right; + top = offsets.top - size.height - padding.top; + bottom = offsets.top + padding.bottom; + if (axisPosition == XAxisPosition.TOP) { + if (labelPositionInsideChart) + top = Math.max(top, mViewPortHandler.contentTop()); + else + bottom = Math.min(bottom, mViewPortHandler.contentTop()); + } else { + if (labelPositionInsideChart) + bottom = Math.min(bottom, mViewPortHandler.contentBottom()); + else + top = Math.max(top, mViewPortHandler.contentBottom()); + } + + if (angleDegrees != 0f) { + c.save(); + c.translate(offsets.right, offsets.bottom); + c.rotate(angleDegrees); + + c.drawRect(left, top, right, bottom, paint); + + // draw the label in the rectangle + paint.setColor(textColor); + c.drawText(formattedLabel, offsets.left, offsets.top, paint); + + c.restore(); + } else { + c.drawRect(left, top, right, bottom, paint); + + paint.setColor(textColor); + c.drawText(formattedLabel, offsets.left, offsets.top, paint); + } + + FSize.recycleInstance(size); + } + } } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/XAxisRendererHorizontalBarChart.java b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/XAxisRendererHorizontalBarChart.java index 86047cf1b..0fed5d0ad 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/XAxisRendererHorizontalBarChart.java +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/XAxisRendererHorizontalBarChart.java @@ -11,6 +11,7 @@ import com.github.mikephil.charting.components.LimitLine; import com.github.mikephil.charting.components.XAxis; import com.github.mikephil.charting.components.XAxis.XAxisPosition; +import com.github.mikephil.charting.highlight.Highlight; import com.github.mikephil.charting.utils.FSize; import com.github.mikephil.charting.utils.MPPointF; import com.github.mikephil.charting.utils.MPPointD; @@ -57,10 +58,10 @@ public void computeAxis(float min, float max, boolean inverted) { computeAxisValues(min, max); } - + @Override protected void computeSize() { - + mAxisLabelPaint.setTypeface(mXAxis.getTypeface()); mAxisLabelPaint.setTextSize(mXAxis.getTextSize()); @@ -85,7 +86,7 @@ protected void computeSize() { } @Override - public void renderAxisLabels(Canvas c) { + public void renderAxisLabels(Canvas c, Highlight[] indicesToHighlight) { if (!mXAxis.isEnabled() || !mXAxis.isDrawLabelsEnabled()) return; diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/XAxisRendererRadarChart.java b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/XAxisRendererRadarChart.java index 956e8c7d5..7e147898d 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/XAxisRendererRadarChart.java +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/XAxisRendererRadarChart.java @@ -2,10 +2,10 @@ package com.github.mikephil.charting.renderer; import android.graphics.Canvas; -import android.graphics.PointF; import com.github.mikephil.charting.charts.RadarChart; import com.github.mikephil.charting.components.XAxis; +import com.github.mikephil.charting.highlight.Highlight; import com.github.mikephil.charting.utils.MPPointF; import com.github.mikephil.charting.utils.Utils; import com.github.mikephil.charting.utils.ViewPortHandler; @@ -21,7 +21,7 @@ public XAxisRendererRadarChart(ViewPortHandler viewPortHandler, XAxis xAxis, Rad } @Override - public void renderAxisLabels(Canvas c) { + public void renderAxisLabels(Canvas c, Highlight[] indicesToHighlight) { if (!mXAxis.isEnabled() || !mXAxis.isDrawLabelsEnabled()) return; diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/YAxisRenderer.java b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/YAxisRenderer.java index 53cca7ee0..127e0c71a 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/YAxisRenderer.java +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/YAxisRenderer.java @@ -11,6 +11,8 @@ import com.github.mikephil.charting.components.YAxis; import com.github.mikephil.charting.components.YAxis.AxisDependency; import com.github.mikephil.charting.components.YAxis.YAxisLabelPosition; +import com.github.mikephil.charting.highlight.Highlight; +import com.github.mikephil.charting.utils.FSize; import com.github.mikephil.charting.utils.MPPointD; import com.github.mikephil.charting.utils.Transformer; import com.github.mikephil.charting.utils.Utils; @@ -45,9 +47,9 @@ public YAxisRenderer(ViewPortHandler viewPortHandler, YAxis yAxis, Transformer t * draws the y-axis labels to the screen */ @Override - public void renderAxisLabels(Canvas c) { + public void renderAxisLabels(Canvas c, Highlight[] indicesToHighlight) { - if (!mYAxis.isEnabled() || !mYAxis.isDrawLabelsEnabled()) + if (!mYAxis.isEnabled() || (!mYAxis.isDrawLabelsEnabled() && !mYAxis.isDrawHighlightLabelsEnabled())) return; float[] positions = getTransformedPositions(); @@ -86,6 +88,7 @@ public void renderAxisLabels(Canvas c) { } drawYLabels(c, xPos, positions, yoffset); + drawHighlightLabels(c, xPos, yoffset, indicesToHighlight); } @Override @@ -114,6 +117,9 @@ public void renderAxisLine(Canvas c) { */ protected void drawYLabels(Canvas c, float fixedPosition, float[] positions, float offset) { + if (!mYAxis.isDrawLabelsEnabled()) + return; + final int from = mYAxis.isDrawBottomYLabelEntryEnabled() ? 0 : 1; final int to = mYAxis.isDrawTopYLabelEntryEnabled() ? mYAxis.mEntryCount @@ -349,4 +355,68 @@ public void renderLimitLines(Canvas c) { c.restoreToCount(clipRestoreCount); } } + + /** + * Draws highlight labels + * + * @param c + * @param xPos + * @param yOffset + */ + protected void drawHighlightLabels(Canvas c, float xPos, float yOffset, Highlight[] indicesToHighlight) { + + if (!mYAxis.isDrawHighlightLabelsEnabled() || indicesToHighlight == null) + return; + + for (Highlight high : indicesToHighlight) { + + float yPos = high.getDrawY(); + + if (!mViewPortHandler.isInBoundsY(yPos)) + continue; + + String formattedLabel = mYAxis.getValueFormatter().getFormattedValue(high.getY(), mYAxis); + yPos += + yOffset; + + // draw a rectangle + Paint paint = new Paint(mAxisLabelPaint); + paint.setColor(mYAxis.getHighlightFillColor()); + paint.setStyle(Paint.Style.FILL); + + FSize size = Utils.calcTextSize(paint, formattedLabel); + + float left, right, top, bottom; + RectF padding = mYAxis.getHighlightFillPadding(); + + YAxisLabelPosition labelPosition = mYAxis.getLabelPosition(); + + if (mYAxis.getAxisDependency() == AxisDependency.LEFT) { + if (labelPosition == YAxisLabelPosition.OUTSIDE_CHART) { + left = xPos - size.width - padding.left; + right = Math.min(xPos + padding.right, mViewPortHandler.contentLeft()); + } else { + left = Math.max(xPos - padding.left, mViewPortHandler.contentLeft()); + right = xPos + size.width + padding.right; + } + } else { + if (labelPosition == YAxisLabelPosition.OUTSIDE_CHART) { + left = Math.max(xPos - padding.left, mViewPortHandler.contentRight()); + right = xPos + size.width + padding.right; + } else { + left = xPos - size.width - padding.left; + right = Math.min(xPos + padding.right, mViewPortHandler.contentRight()); + } + } + top = yPos - size.height - padding.top; + bottom = yPos + padding.bottom; + + c.drawRect(left, top, right, bottom, paint); + + // draw text in the rectangle + paint.setColor(mYAxis.getHighlightTextColor()); + c.drawText(formattedLabel, xPos, yPos, paint); + + FSize.recycleInstance(size); + } + } } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/YAxisRendererHorizontalBarChart.java b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/YAxisRendererHorizontalBarChart.java index fedf8054a..053c7d008 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/YAxisRendererHorizontalBarChart.java +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/YAxisRendererHorizontalBarChart.java @@ -11,6 +11,7 @@ import com.github.mikephil.charting.components.YAxis; import com.github.mikephil.charting.components.YAxis.AxisDependency; import com.github.mikephil.charting.components.YAxis.YAxisLabelPosition; +import com.github.mikephil.charting.highlight.Highlight; import com.github.mikephil.charting.utils.MPPointD; import com.github.mikephil.charting.utils.Transformer; import com.github.mikephil.charting.utils.Utils; @@ -64,7 +65,7 @@ public void computeAxis(float yMin, float yMax, boolean inverted) { * draws the y-axis labels to the screen */ @Override - public void renderAxisLabels(Canvas c) { + public void renderAxisLabels(Canvas c, Highlight[] indicesToHighlight) { if (!mYAxis.isEnabled() || !mYAxis.isDrawLabelsEnabled()) return; diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/YAxisRendererRadarChart.java b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/YAxisRendererRadarChart.java index f7b1ad9e8..5f96f2be5 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/YAxisRendererRadarChart.java +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/YAxisRendererRadarChart.java @@ -7,6 +7,7 @@ import com.github.mikephil.charting.charts.RadarChart; import com.github.mikephil.charting.components.LimitLine; import com.github.mikephil.charting.components.YAxis; +import com.github.mikephil.charting.highlight.Highlight; import com.github.mikephil.charting.utils.MPPointF; import com.github.mikephil.charting.utils.Utils; import com.github.mikephil.charting.utils.ViewPortHandler; @@ -145,7 +146,7 @@ protected void computeAxisValues(float min, float max) { } @Override - public void renderAxisLabels(Canvas c) { + public void renderAxisLabels(Canvas c, Highlight[] indicesToHighlight) { if (!mYAxis.isEnabled() || !mYAxis.isDrawLabelsEnabled()) return; diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/utils/Utils.java b/MPChartLib/src/main/java/com/github/mikephil/charting/utils/Utils.java index c30267391..be5976f03 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/utils/Utils.java +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/utils/Utils.java @@ -7,7 +7,7 @@ import android.graphics.Canvas; import android.graphics.Paint; import android.graphics.Rect; -import android.graphics.drawable.BitmapDrawable; +import android.graphics.RectF; import android.graphics.drawable.Drawable; import android.os.Build; import android.text.Layout; @@ -15,7 +15,6 @@ import android.text.TextPaint; import android.util.DisplayMetrics; import android.util.Log; -import android.util.SizeF; import android.view.MotionEvent; import android.view.VelocityTracker; import android.view.View; @@ -557,8 +556,43 @@ public static void drawXAxisValue(Canvas c, String text, float x, float y, Paint paint, MPPointF anchor, float angleDegrees) { + Paint.Align originalAlign = paint.getTextAlign(); + paint.setTextAlign(Paint.Align.LEFT); + + // offsets: offsetX, offsetY, translateX, translateY + RectF offsets = computeXAxisOffsets(text, x, y, paint, anchor, angleDegrees); + + if (angleDegrees != 0f) { + c.save(); + c.translate(offsets.right, offsets.bottom); + c.rotate(angleDegrees); + + c.drawText(text, offsets.left, offsets.top, paint); + + c.restore(); + } else { + c.drawText(text, offsets.left, offsets.top, paint); + } + + paint.setTextAlign(originalAlign); + } + + /** + * Computes offset and tranlation for an axis label. + * + * @param text axis label + * @param x x position in pixels + * @param y y position in pixels + * @param paint paint to draw with + * @param anchor point to rotate around + * @param angleDegrees rotations angle + * @return RectF: offsetX, offsetY, translateX, translateY + */ + public static RectF computeXAxisOffsets(String text, float x, float y, Paint paint, MPPointF anchor, float angleDegrees) { float drawOffsetX = 0.f; float drawOffsetY = 0.f; + float translateX = 0.f; + float translateY = 0.f; final float lineHeight = paint.getFontMetrics(mFontMetricsBuffer); paint.getTextBounds(text, 0, text.length(), mDrawTextRectBuffer); @@ -572,8 +606,8 @@ public static void drawXAxisValue(Canvas c, String text, float x, float y, drawOffsetY += -mFontMetricsBuffer.ascent; // To have a consistent point of reference, we always draw left-aligned - Paint.Align originalTextAlign = paint.getTextAlign(); - paint.setTextAlign(Paint.Align.LEFT); + Paint p = new Paint(paint); + p.setTextAlign(Paint.Align.LEFT); if (angleDegrees != 0.f) { @@ -581,8 +615,8 @@ public static void drawXAxisValue(Canvas c, String text, float x, float y, drawOffsetX -= mDrawTextRectBuffer.width() * 0.5f; drawOffsetY -= lineHeight * 0.5f; - float translateX = x; - float translateY = y; + translateX = x; + translateY = y; // Move the "outer" rect relative to the anchor, assuming its centered if (anchor.x != 0.5f || anchor.y != 0.5f) { @@ -596,13 +630,7 @@ public static void drawXAxisValue(Canvas c, String text, float x, float y, FSize.recycleInstance(rotatedSize); } - c.save(); - c.translate(translateX, translateY); - c.rotate(angleDegrees); - c.drawText(text, drawOffsetX, drawOffsetY, paint); - - c.restore(); } else { if (anchor.x != 0.f || anchor.y != 0.f) { @@ -613,10 +641,9 @@ public static void drawXAxisValue(Canvas c, String text, float x, float y, drawOffsetX += x; drawOffsetY += y; - c.drawText(text, drawOffsetX, drawOffsetY, paint); } - paint.setTextAlign(originalTextAlign); + return new RectF(drawOffsetX, drawOffsetY, translateX, translateY); } public static void drawMultilineText(Canvas c, StaticLayout textLayout,