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

Make the ChartXAxisRenderer more flexible: now possible to overwrite drawing the line or label of the ChartLimitLine #1065

Closed
wants to merge 3 commits into from
Closed
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
223 changes: 122 additions & 101 deletions MPChartLib/src/com/github/mikephil/charting/renderer/XAxisRenderer.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

package com.github.mikephil.charting.renderer;

import android.graphics.Canvas;
Expand Down Expand Up @@ -110,15 +109,78 @@ public void renderAxisLine(Canvas c) {
}
}

@Override
public void renderGridLines(Canvas c) {

if (!mXAxis.isDrawGridLinesEnabled() || !mXAxis.isEnabled())
return;

// pre alloc
float[] position = new float[]{
0f, 0f
};

mGridPaint.setColor(mXAxis.getGridColor());
mGridPaint.setStrokeWidth(mXAxis.getGridLineWidth());
mGridPaint.setPathEffect(mXAxis.getGridDashPathEffect());

Path gridLinePath = new Path();

for (int i = mMinX; i <= mMaxX; i += mXAxis.mAxisLabelModulus) {

position[0] = i;
mTrans.pointValuesToPixel(position);

if (position[0] >= mViewPortHandler.offsetLeft()
&& position[0] <= mViewPortHandler.getChartWidth()) {

gridLinePath.moveTo(position[0], mViewPortHandler.contentBottom());
gridLinePath.lineTo(position[0], mViewPortHandler.contentTop());

// draw a path because lines don't support dashing on lower android versions
c.drawPath(gridLinePath, mGridPaint);
}

gridLinePath.reset();
}
}

/**
* Draws the LimitLines associated with this axis to the screen.
*
* @param c
*/
@Override
public void renderLimitLines(Canvas c) {

List<LimitLine> limitLines = mXAxis.getLimitLines();

if (limitLines == null || limitLines.size() <= 0)
return;

float[] pts = new float[4];
Path limitLinePath = new Path();

for (int i = 0; i < limitLines.size(); i++) {

LimitLine l = limitLines.get(i);

renderOneLimitLine(c, pts, limitLinePath, l);
renderLimitLineLabel(c, pts, l, 0f, 0f);

limitLinePath.reset();
}
}

/**
* draws the x-labels on the specified y-position
*
*
* @param pos
*/
protected void drawLabels(Canvas c, float pos) {

// pre allocate to save performance (dont allocate in loop)
float[] position = new float[] {
float[] position = new float[]{
0f, 0f
};

Expand Down Expand Up @@ -160,118 +222,77 @@ protected void drawLabel(Canvas c, String label, int xIndex, float x, float y) {
c.drawText(formattedLabel, x, y, mAxisLabelPaint);
}

@Override
public void renderGridLines(Canvas c) {

if (!mXAxis.isDrawGridLinesEnabled() || !mXAxis.isEnabled())
return;

// pre alloc
float[] position = new float[] {
0f, 0f
};

mGridPaint.setColor(mXAxis.getGridColor());
mGridPaint.setStrokeWidth(mXAxis.getGridLineWidth());
mGridPaint.setPathEffect(mXAxis.getGridDashPathEffect());

Path gridLinePath = new Path();

for (int i = mMinX; i <= mMaxX; i += mXAxis.mAxisLabelModulus) {
/**
* Draws one limit line
*
* @param c
* @param pts
* @param limitLinePath
* @param l
*/
protected void renderOneLimitLine(Canvas c, float[] pts, Path limitLinePath, LimitLine l) {
pts[0] = l.getLimit();
pts[2] = l.getLimit();

position[0] = i;
mTrans.pointValuesToPixel(position);
mTrans.pointValuesToPixel(pts);

if (position[0] >= mViewPortHandler.offsetLeft()
&& position[0] <= mViewPortHandler.getChartWidth()) {
pts[1] = mViewPortHandler.contentTop();
pts[3] = mViewPortHandler.contentBottom();

gridLinePath.moveTo(position[0], mViewPortHandler.contentBottom());
gridLinePath.lineTo(position[0], mViewPortHandler.contentTop());
limitLinePath.moveTo(pts[0], pts[1]);
limitLinePath.lineTo(pts[2], pts[3]);

// draw a path because lines don't support dashing on lower android versions
c.drawPath(gridLinePath, mGridPaint);
}
mLimitLinePaint.setStyle(Paint.Style.STROKE);
mLimitLinePaint.setColor(l.getLineColor());
mLimitLinePaint.setStrokeWidth(l.getLineWidth());
mLimitLinePaint.setPathEffect(l.getDashPathEffect());

gridLinePath.reset();
}
c.drawPath(limitLinePath, mLimitLinePaint);
}

/**
* Draws the LimitLines associated with this axis to the screen.
*
* @param c
*/
@Override
public void renderLimitLines(Canvas c) {

List<LimitLine> limitLines = mXAxis.getLimitLines();

if (limitLines == null || limitLines.size() <= 0)
return;

float[] pts = new float[4];
Path limitLinePath = new Path();

for (int i = 0; i < limitLines.size(); i++) {

LimitLine l = limitLines.get(i);

pts[0] = l.getLimit();
pts[2] = l.getLimit();

mTrans.pointValuesToPixel(pts);

pts[1] = mViewPortHandler.contentTop();
pts[3] = mViewPortHandler.contentBottom();

limitLinePath.moveTo(pts[0], pts[1]);
limitLinePath.lineTo(pts[2], pts[3]);

mLimitLinePaint.setStyle(Paint.Style.STROKE);
mLimitLinePaint.setColor(l.getLineColor());
mLimitLinePaint.setStrokeWidth(l.getLineWidth());
mLimitLinePaint.setPathEffect(l.getDashPathEffect());

c.drawPath(limitLinePath, mLimitLinePaint);
limitLinePath.reset();

String label = l.getLabel();
/**
* Renders the labels for one limit line
* @param c
* @param pts
* @param l
*/
protected void renderLimitLineLabel(Canvas c, float[] pts, LimitLine l, float xOffsetFromLine, float yOffsetFromAxis) {
String label = l.getLabel();

// if drawing the limit-value label is enabled
if (label != null && !label.equals("")) {
// if drawing the limit-value label is enabled
if (label != null && !label.equals("")) {

mLimitLinePaint.setStyle(l.getTextStyle());
mLimitLinePaint.setPathEffect(null);
mLimitLinePaint.setColor(l.getTextColor());
mLimitLinePaint.setStrokeWidth(0.5f);
mLimitLinePaint.setTextSize(l.getTextSize());
mLimitLinePaint.setStyle(l.getTextStyle());
mLimitLinePaint.setPathEffect(null);
mLimitLinePaint.setColor(l.getTextColor());
mLimitLinePaint.setStrokeWidth(0.5f);
mLimitLinePaint.setTextSize(l.getTextSize());

float xOffset = l.getLineWidth();
float add = Utils.convertDpToPixel(4f);
float yOffset = add / 2f;
float xOffset = l.getLineWidth() + xOffsetFromLine;
float add = Utils.convertDpToPixel(4f);
float yOffset = add / 2f + yOffsetFromAxis;

final LimitLine.LimitLabelPosition position = l.getLabelPosition();
final LimitLine.LimitLabelPosition position = l.getLabelPosition();

if (position == LimitLine.LimitLabelPosition.RIGHT_TOP) {
if (position == LimitLine.LimitLabelPosition.RIGHT_TOP) {

final float labelLineHeight = Utils.calcTextHeight(mLimitLinePaint, label);
mLimitLinePaint.setTextAlign(Align.LEFT);
c.drawText(label, pts[0] + xOffset, mViewPortHandler.contentTop() + yOffset + labelLineHeight, mLimitLinePaint);
} else if (position == LimitLine.LimitLabelPosition.RIGHT_BOTTOM) {
final float labelLineHeight = Utils.calcTextHeight(mLimitLinePaint, label);
mLimitLinePaint.setTextAlign(Align.LEFT);
c.drawText(label, pts[0] + xOffset, mViewPortHandler.contentTop() + yOffset + labelLineHeight, mLimitLinePaint);
} else if (position == LimitLine.LimitLabelPosition.RIGHT_BOTTOM) {

mLimitLinePaint.setTextAlign(Align.LEFT);
c.drawText(label, pts[0] + xOffset, mViewPortHandler.contentBottom() - add, mLimitLinePaint);
} else if (position == LimitLine.LimitLabelPosition.LEFT_TOP) {
mLimitLinePaint.setTextAlign(Align.LEFT);
c.drawText(label, pts[0] + xOffset, mViewPortHandler.contentBottom() - yOffset, mLimitLinePaint);
} else if (position == LimitLine.LimitLabelPosition.LEFT_TOP) {

mLimitLinePaint.setTextAlign(Align.RIGHT);
final float labelLineHeight = Utils.calcTextHeight(mLimitLinePaint, label);
c.drawText(label, pts[0] - xOffset, mViewPortHandler.contentTop() + yOffset + labelLineHeight, mLimitLinePaint);
} else {
mLimitLinePaint.setTextAlign(Align.RIGHT);
final float labelLineHeight = Utils.calcTextHeight(mLimitLinePaint, label);
c.drawText(label, pts[0] - xOffset, mViewPortHandler.contentTop() + yOffset + labelLineHeight, mLimitLinePaint);
} else {

mLimitLinePaint.setTextAlign(Align.RIGHT);
c.drawText(label, pts[0] - xOffset, mViewPortHandler.contentBottom() - yOffset, mLimitLinePaint);
}
}
}
}
mLimitLinePaint.setTextAlign(Align.RIGHT);
c.drawText(label, pts[0] - xOffset, mViewPortHandler.contentBottom() - yOffset, mLimitLinePaint);
}
}
}
}