arjang27
arjang27

Reputation: 241

How to add Background to MPAndroidCharts xAxis labels?

I want to add background color to the x axis labels in a chart line and also to put y axis above the grid line as shown in the screenshot. Is it possible to achieve this without customising the library?

enter image description here

Upvotes: 4

Views: 2650

Answers (3)

Divya
Divya

Reputation: 184

I have done like this

LineDataSet set1 = new LineDataSet(x, "City");
set1.setColors(ColorTemplate.COLORFUL_COLORS);
set1.setCircleColor(Color.BLACK);
set1.setValueTextColor(Color.BLUE);
set1.setDrawFilled(true);
set1.setLineWidth(1.9f);
set1.setCircleRadius(4f);
LineData data = new LineData(y, set1);
lineChart.setData(data);

Upvotes: 0

g4gaj
g4gaj

Reputation: 85

It's possible, I was also working on changing the X-Axis label's background colour, there is no direct API to do this, But I have done like this.

  • Step1: first extend the XAxisRenderer class and override drawLabels() method.
  • Step2: In drawLabels() method add the below code, before drawLabel(c, label, x, pos, anchor, labelRotationAngleDegrees) method.
if (i == 0) {
    Paint rectPaint = new Paint();
    rectPaint.setColor(context.getResources().getColor(R.color.vitaskin_skin_history_background_color));
    rectPaint.setStyle(Paint.Style.FILL);
    c.drawRect(0, (int) mViewPortHandler.contentBottom(), mViewPortHandler.getChartWidth(), mViewPortHandler.getChartHeight(), rectPaint);
}

Upvotes: 0

arjang27
arjang27

Reputation: 241

I didnt want to mess with the Library so I had to extend few classes and override few methods to meet my requirement. What I did is as follows:

extend XAxis class and override:

private int mXAxisLabelBackgroundColor = Color.argb(200, 140, 234, 255);
/**
 * boolen used to enable or disable label background, default is enabled
 */
private boolean mEnableLabelBackground = false;


public CustomXAxis(){
    super();
}

public void setLabelBackgroundColor(int color){
    mXAxisLabelBackgroundColor = color;
}

public void setLabelBackgroundColor(String colorHex){
    mXAxisLabelBackgroundColor = Color.parseColor(colorHex);
}

public int getXAxisLabelBackgroundColor(){
    return mXAxisLabelBackgroundColor;
}

/**
 * Enable/disable X Axis label background, default is disabled.
 * @param enable
 */
public void setDrawLabelBackground(boolean enable){
    mEnableLabelBackground = enable;
}

/**
 *
 * @return boolean true if drawing label background is enabled otherwise false
 */
public boolean isDrawLabelBackgroundEnabled(){
    return mEnableLabelBackground;
}

Extend XAxisRenderer and override drawLabels(Canvas c, float pos). Draw a rectangle just before drawing labels.

        if (((CustomXAxis)mXAxis).isDrawLabelBackgroundEnabled()) {
           drawLabelBackground(c);
        }

Extend YAxis and just add an accessor and mutator to set the Yaxis label above or below the gridline.

private float mYLabelPosition = 0f;

public CustomYAxisRight(){
    super(AxisDependency.RIGHT);
}

/**
 * Sets the label position above or below the gridline.
 * <p>use negative number to set label position above gridline.</p>
 * @param position
 */
public void setYLabelPosition(float position){
    mYLabelPosition = position;
}

public float getYLabelPosition(){
    return mYLabelPosition;
}

Extend YAxisRenderer and override drawYLabels(Canvas c, float fixedPosition, float[] positions, float offset)

c.drawText(text, fixedPosition, positions[i * 2 + 1] + (offset+((CustomYAxisRight)mYAxis).getYLabelPosition()), mAxisLabelPaint);

I also wanted to show gridline for Y axis label 0 when the chart starts from 0.

Override renderGridlines(Canvas c) and draw a line before other lines are drawn.

if (mYAxis.isStartAtZeroEnabled()) {
        mTrans.pointValuesToPixel(position);
        gridLinePath.moveTo(mViewPortHandler.offsetLeft(), mViewPortHandler.contentBottom() - 1f);
        gridLinePath.lineTo(mViewPortHandler.contentRight(), mViewPortHandler.contentBottom() - 1f);
        // draw a path because lines don't support dashing on lower android versions
        c.drawPath(gridLinePath, mGridPaint);
        gridLinePath.reset();
    }

And finally extend LineChart and override:

    @Override
protected void init() {
    super.init();
    mAxisRight = new CustomYAxisRight();
    mXAxis = new CustomXAxis();
    mAxisRendererRight = new CustomYAxisRenderer(mViewPortHandler,mAxisRight,mRightAxisTransformer);
    mXAxisRenderer = new CustomXAxisRenderer(mViewPortHandler, mXAxis, mLeftAxisTransformer);
}

@Override
public void setViewPortOffsets(final float left, final float top, final float right, final float bottom) {
    mCustomViewPortEnabled = true;
    if (((CustomXAxis)mXAxis).isDrawLabelBackgroundEnabled()){
        if (bottom == 0){
            //we have to leave a space for the labels to draw
            _bottomOffset = 80f;
        }
    }
    post(new Runnable() {

        @Override
        public void run() {

            mViewPortHandler.restrainViewPort(left, top, right, _bottomOffset);
            prepareOffsetMatrix();
            prepareValuePxMatrix();
        }
    });
}

End result

Upvotes: 4

Related Questions