如何将背景添加到MPAndroidCharts xAxis标签?

如何将背景添加到MPAndroidCharts xAxis标签?,第1张

概述我想将背景添加到图表线的x轴标签中,并将y轴放置在网格线上方,如屏幕截图所示.是否可以在不定制库的情况下实现这一目标?解决方法:我不想弄乱Library,所以我不得不扩展几个类并重写一些方法来满足我的要求.我所做的如下:扩展XAxis类并重写:privateintmXAxisLabelBackgroundColo

我想将背景色添加到图表线的x轴标签中,并将y轴放置在网格线上方,如屏幕截图所示.是否可以在不定制库的情况下实现这一目标?

解决方法:

我不想弄乱library,所以我不得不扩展几个类并重写一些方法来满足我的要求.我所做的如下:

扩展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. * <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,float fixedposition,float []位置,float偏移量)

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并覆盖:

    @OverrIDeprotected voID init() {    super.init();    mAxisRight = new CustomYAxisRight();    mXAxis = new CustomXAxis();    mAxisRendererRight = new CustomYAxisRenderer(mVIEwPortHandler,mAxisRight,mRightAxistransformer);    mXAxisRenderer = new CustomXAxisRenderer(mVIEwPortHandler, mXAxis, mleftAxistransformer);}@OverrIDepublic 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 xAxis标签?全部内容,希望文章能够帮你解决如何将背景添加到MPAndroidCharts xAxis标签?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1079993.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-27
下一篇 2022-05-27

发表评论

登录后才能评论

评论列表(0条)

保存