Reputation: 49
I am using MPAndroidChart library to draw LineChart with two YAxis to show two graphs for different values. I just want to show LimitLine between these two graphs. Is it possible if yes then how?Sample Image Attached.
Upvotes: 0
Views: 698
Reputation: 179
Yes it's possible.You should extend XAxisRenderer class and override the necessary methods.
public class CustomXAxisRenderer extends XAxisRenderer {
private float[] mLimitLineSegmentsBuffer = new float[4];
private Path mLimitLinePath = new Path();
private float topOffset;
private float bottomOffset;
public CustomXAxisRenderer(ViewPortHandler viewPortHandler, XAxis xAxis, Transformer trans) {
super(viewPortHandler, xAxis, trans);
}
@Override
public void renderLimitLines(Canvas c) {
List<LimitLine> limitLines = mXAxis.getLimitLines();
if (limitLines == null || limitLines.size() <= 0)
return;
float[] position = mRenderLimitLinesBuffer;
position[0] = 0;
position[1] = 0;
for (int i = 0; i < limitLines.size(); i++) {
LimitLine l = limitLines.get(i);
if (!l.isEnabled())
continue;
int clipRestoreCount = c.save();
mLimitLineClippingRect.set(mViewPortHandler.getContentRect());
mLimitLineClippingRect.inset(-l.getLineWidth(), 0.f);
c.clipRect(mLimitLineClippingRect);
position[0] = l.getLimit();
position[1] = 0.f;
mTrans.pointValuesToPixel(position);
//Here you can count your offsets
// topOffset = ... ;
// bottomOffset = ...;
renderLimitLineLine(c, l, position);
renderLimitLineLabel(c, l, position, yOffset);
c.restoreToCount(clipRestoreCount);
}
}
@Override
public void renderLimitLineLine(Canvas c, LimitLine limitLine, float[] position) {
mLimitLineSegmentsBuffer[0] = position[0];
// Here, the offsets will be applied
mLimitLineSegmentsBuffer[1] = mViewPortHandler.contentTop() + topOffset;
mLimitLineSegmentsBuffer[2] = position[0];
mLimitLineSegmentsBuffer[3] = mViewPortHandler.contentBottom() - bottomOffset;
mLimitLinePath.reset();
mLimitLinePath.moveTo(mLimitLineSegmentsBuffer[0], mLimitLineSegmentsBuffer[1]);
mLimitLinePath.lineTo(mLimitLineSegmentsBuffer[2], mLimitLineSegmentsBuffer[3]);
mLimitLinePaint.setStyle(Paint.Style.STROKE);
mLimitLinePaint.setColor(limitLine.getLineColor());
mLimitLinePaint.setStrokeWidth(limitLine.getLineWidth());
mLimitLinePaint.setPathEffect(limitLine.getDashPathEffect());
c.drawPath(mLimitLinePath, mLimitLinePaint);
}
Then you must set new renderer:
mGraph.setXAxisRenderer(new CustomXAxisRenderer(mGraph.getViewPortHandler()
, mGraph.getXAxis(),
mGraph.getTransformer(YAxis.AxisDependency.LEFT)));
Upvotes: 1