如何添加背景MPAndroidCharts x轴标签? [英] How to add Background to MPAndroidCharts xAxis labels?

查看:1175
本文介绍了如何添加背景MPAndroidCharts x轴标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

欲背景颜色添加到x轴标签以图表线也把网格线之上y轴上所示的屏幕截图。是否有可能不定制库实现这一目标?

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?

推荐答案

我没有想与图书馆一塌糊涂,所以我不得不延长几类,并覆盖一些方法来满足我的要求。我所做的是如下:

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:

拓展XAXIS类并覆盖:

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;
}

扩展XAxisRenderer并覆盖drawLabels(帆布C,浮动POS)。刚刚绘制标签之前绘制一个矩形。

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

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

y轴延伸,只是增加一个访问和mutator设置高于或网​​格线下方Y轴的标签。

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;
}

扩展YAxisRenderer并覆盖drawYLabels(帆布C,浮fixedPosition,浮动[]立场,浮偏移)

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);

我也想显示网格线为Y轴标签0当图表从0开始。

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

覆盖renderGridlines(帆布c)和画一条线被吸引其他行之前。

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();
        }
    });
}

这篇关于如何添加背景MPAndroidCharts x轴标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆