java 向背景板加标签_如何在MPAndroidCharts xAxis标签中添加背景?

我不想弄乱图书馆所以我不得不扩展几个类并覆盖几个方法来满足我的要求 . 我做的是如下:

扩展XAxis类并覆盖:

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(Canvas c,float pos) . 在绘制标签之前绘制一个矩形 .

if (((CustomXAxis)mXAxis).isDrawLabelBackgroundEnabled()) {

drawLabelBackground(c);

}

扩展YAxis并添加一个访问器和增变器,以在网格线上方或下方设置Yaxis标签 .

private float mYLabelPosition = 0f;

public CustomYAxisRight(){

super(AxisDependency.RIGHT);

}

/**

* Sets the label position above or below the gridline.

*

use negative number to set label position above gridline.

* @param position

*/

public void setYLabelPosition(float position){

mYLabelPosition = position;

}

public float getYLabelPosition(){

return mYLabelPosition;

}

扩展YAxisRenderer并覆盖drawYLabels(Canvas c,float fixedPosition,float [] position,float offset)

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

当图表从0开始时,我还想显示Y轴标签0的网格线 .

覆盖renderGridlines(Canvas c)并在绘制其他线之前绘制一条线 .

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

}

最后扩展LineChart并覆盖:

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

}

});

}

XnooT.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值