Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Display highlighted entry values on the axes #5249

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
*/
Expand Down Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand All @@ -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);
}
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());

Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
Loading