MPAndroidChart实现蛛网图设置背景色

469 阅读1分钟

效果图:

image.png

上菜:

1、RadarChartRenderer中drawWeb(Canvas c)设置如下大菜:

    protected Path mDrawWebBg = new Path();
    protected void drawWeb(Canvas c) {
        Path surface = mDrawWebBg;
        surface.reset();
        boolean hasMovedToPoint = false;
        for (int i = 0; i < maxEntryCount; i += xIncrements) {
            if (mChart.isWebBackgroundEnable()) {
                //绘制背景的路径
                if (!hasMovedToPoint) {
                    surface.moveTo(p.x, p.y);
                    hasMovedToPoint = true;
                } else {
                    surface.lineTo(p.x, p.y);
                }
            }
        }
        if (mChart.isWebBackgroundEnable()) {
            //填充背景
            drawFilledPath(c, surface, mChart.getWebBackgroundColor(), mChart.getWebBackgroundFillAlpha());
        }
        surface.close();

2、RadarChart中叫上服务员:

    /**
     * 蛛网背景色默认关闭设置
     */
    private boolean mWebBackgroundEnable = false;
    /**
     * 蛛网背景色,默认
     */
    private int mWebBackgroundColor = -1;
    /**
     * 不透明度 (0-255)
     */
    private int mWebBackgroundFillAlpha = 180;
    
    /**
     * 设置蛛网背景色与透明度
     *
     * @param color
     * @param fillAlpha
     */
    public void setWebBackgroundColor(boolean isOpen, int color, int fillAlpha) {
        this.mWebBackgroundEnable = isOpen;
        this.mWebBackgroundColor = color;
        this.mWebBackgroundFillAlpha = fillAlpha;
    }

    public int getWebBackgroundColor() {
        return mWebBackgroundColor;
    }

    public int getWebBackgroundFillAlpha() {
        return mWebBackgroundFillAlpha;
    }

    public boolean isWebBackgroundEnable() {
        return mWebBackgroundEnable;
    }

3、该你喊服务员了:

        chart.setWebBackgroundColor(true, Color.rgb(255, 189, 0), 180);

餐厅地址:

github.com/hzl512/MPAn…

喜欢就给个star呗~