MPAndroidChart柱状图——根据区间显示不同渐变色的柱状图

个人 专栏收录该内容
8 篇文章 0 订阅

本来可以用MPAndroidChart原来的柱状图就行了  设计给的柱状图是圆头而且还是带有渐变色的住状图。根据不同的区间显示的不同颜色。

效果图


布局文件是

<com.zyf.chart.MyBarChart
        android:id="@+id/chart"
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:layout_centerInParent="true" />

MyBarchart是继承Barchart实现的主要替换BarchartRenderer 翻译说是渲染器 应改就是专门负责画的东西。

package com.zyf.chart;

import android.content.Context;
import android.util.AttributeSet;

import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.highlight.BarHighlighter;

/**
 * 作者:小飞
 * 创建于 2018年4月14日 16:48:01
 */

public class MyBarChart extends BarChart {
    public MyBarChart(Context context) {
        super(context);
    }

    public MyBarChart(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MyBarChart(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    protected void init() {
        super.init();
        mRenderer = new MyBarChartRenderer(this, mAnimator, mViewPortHandler);

        setHighlighter(new BarHighlighter(this));

        getXAxis().setSpaceMin(0.5f);
        getXAxis().setSpaceMax(0.5f);
    }
}

MyBarChartRenderer是自己去实现柱状图的回话部分 复制原来的BarChartRenderer的代码 修改画笔属性

package com.zyf.chart;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Shader;

import com.github.mikephil.charting.animation.ChartAnimator;
import com.github.mikephil.charting.buffer.BarBuffer;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.highlight.Highlight;
import com.github.mikephil.charting.highlight.Range;
import com.github.mikephil.charting.interfaces.dataprovider.BarDataProvider;
import com.github.mikephil.charting.interfaces.datasets.IBarDataSet;
import com.github.mikephil.charting.renderer.BarChartRenderer;
import com.github.mikephil.charting.utils.Transformer;
import com.github.mikephil.charting.utils.Utils;
import com.github.mikephil.charting.utils.ViewPortHandler;

/**
 * 作者:小飞
 * 创建于 2018年4月14日
 */

public class MyBarChartRenderer extends BarChartRenderer {
    private RectF mBarShadowRectBuffer = new RectF();


    public MyBarChartRenderer(BarDataProvider chart, ChartAnimator animator, ViewPortHandler viewPortHandler) {
        super(chart, animator, viewPortHandler);
        this.mChart = chart;
        mHighlightPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mHighlightPaint.setColor(Color.rgb(0, 0, 0));
        mHighlightPaint.setAlpha(120);
        mShadowPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mShadowPaint.setStyle(Paint.Style.FILL);
        mBarBorderPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mBarBorderPaint.setStyle(Paint.Style.STROKE);
    }

    @Override
    protected void drawDataSet(Canvas c, IBarDataSet dataSet, int index) {

        Transformer trans = mChart.getTransformer(dataSet.getAxisDependency());

        mBarBorderPaint.setColor(dataSet.getBarBorderColor());
        mBarBorderPaint.setStrokeWidth(Utils.convertDpToPixel(dataSet.getBarBorderWidth()));

        final boolean drawBorder = dataSet.getBarBorderWidth() > 0.f;

        float phaseX = mAnimator.getPhaseX();
        float phaseY = mAnimator.getPhaseY();

        // draw the bar shadow before the values
        if (mChart.isDrawBarShadowEnabled()) {
            mShadowPaint.setColor(dataSet.getBarShadowColor());

            BarData barData = mChart.getBarData();

            final float barWidth = barData.getBarWidth();
            final float barWidthHalf = barWidth / 2.0f;
            float x;

            for (int i = 0, count = Math.min((int)(Math.ceil((float)(dataSet.getEntryCount()) * phaseX)), dataSet.getEntryCount());
                 i < count;
                 i++) {

                BarEntry e = dataSet.getEntryForIndex(i);

                x = e.getX();

                mBarShadowRectBuffer.left = x - barWidthHalf;
                mBarShadowRectBuffer.right = x + barWidthHalf;

                trans.rectValueToPixel(mBarShadowRectBuffer);

                if (!mViewPortHandler.isInBoundsLeft(mBarShadowRectBuffer.right)){
                    continue;
                }
                if (!mViewPortHandler.isInBoundsRight(mBarShadowRectBuffer.left)){
                    break;
                }
                mBarShadowRectBuffer.top = mViewPortHandler.contentTop();
                mBarShadowRectBuffer.bottom = mViewPortHandler.contentBottom();

                c.drawRect(mBarShadowRectBuffer, mShadowPaint);
            }
        }

        // initialize the buffer
        BarBuffer buffer = mBarBuffers[index];
        buffer.setPhases(phaseX, phaseY);
        buffer.setDataSet(index);
        buffer.setInverted(mChart.isInverted(dataSet.getAxisDependency()));
        buffer.setBarWidth(mChart.getBarData().getBarWidth());

        buffer.feed(dataSet);

        trans.pointValuesToPixel(buffer.buffer);

        final boolean isSingleColor = dataSet.getColors().size() == 1;

        if (isSingleColor) {
            mRenderPaint.setColor(dataSet.getColor());
        }

        for (int j = 0; j < buffer.size(); j += 4) {

            if (!mViewPortHandler.isInBoundsLeft(buffer.buffer[j + 2])){
                continue;
            }
            if (!mViewPortHandler.isInBoundsRight(buffer.buffer[j])) {
                break;
            }
            //isSingleColor是不是只有一个颜色  防止不需要渐变色的柱状图
            //下面是向画笔添加渐变的的不同的级别颜色不同
            if (!isSingleColor) {
                if (dataSet.getEntryForIndex(j/4).getY() > 140){
                    Shader shader = new LinearGradient(0, buffer.buffer[j + 1], 0, buffer.buffer[j + 3], Color.parseColor("#F37779"),
                            Color.parseColor("#DA1738"), Shader.TileMode.CLAMP);
                    mRenderPaint.setShader(shader);
                } else if(dataSet.getEntryForIndex(j/4).getY() < 30){
                    Shader shader = new LinearGradient(0, buffer.buffer[j + 1], 0, buffer.buffer[j + 3], Color.parseColor("#70AAFD"),
                            Color.parseColor("#0D70FC"), Shader.TileMode.CLAMP);
                    mRenderPaint.setShader(shader);
                } else {
                    Shader shader = new LinearGradient(0, buffer.buffer[j + 1], 0, buffer.buffer[j + 3], Color.parseColor("#71A4A7"),
                            Color.parseColor("#0E9355"), Shader.TileMode.CLAMP);
                    mRenderPaint.setShader(shader);
                }
            }else{
                Shader shader = new LinearGradient(0, buffer.buffer[j + 1], 0, buffer.buffer[j + 3], Color.parseColor("#70AAFD"),
                        Color.parseColor("#0D70FC"), Shader.TileMode.CLAMP);
                mRenderPaint.setShader(shader);
            }
            //修改画笔的属性 是否圆头
            mRenderPaint.setStrokeCap(Paint.Cap.ROUND);
            float wtch =  buffer.buffer[j + 2]-buffer.buffer[j];
            mRenderPaint.setStrokeWidth(wtch);
            c.drawLine(buffer.buffer[j]+wtch/2, buffer.buffer[j + 1], buffer.buffer[j + 2]-wtch/2,
                    buffer.buffer[j + 3], mRenderPaint);
            if (drawBorder) {
                c.drawLine(buffer.buffer[j]+wtch/2, buffer.buffer[j + 1], buffer.buffer[j + 2]-wtch/2,
                        buffer.buffer[j + 3], mRenderPaint);
            }
        }

    }
    /**
     * 手指点击的半透明灰色修改
     * */
    @Override
    public void drawHighlighted(Canvas c, Highlight[] indices) {
        BarData barData = mChart.getBarData();

        for (Highlight high : indices) {

            IBarDataSet set = barData.getDataSetByIndex(high.getDataSetIndex());

            if (set == null || !set.isHighlightEnabled())
                continue;

            BarEntry e = set.getEntryForXValue(high.getX(), high.getY());

            if (!isInBoundsX(e, set))
                continue;

            Transformer trans = mChart.getTransformer(set.getAxisDependency());

            mHighlightPaint.setColor(set.getHighLightColor());
            mHighlightPaint.setAlpha(set.getHighLightAlpha());

            boolean isStack = (high.getStackIndex() >= 0  && e.isStacked()) ? true : false;

            final float y1;
            final float y2;

            if (isStack) {

                if(mChart.isHighlightFullBarEnabled()) {

                    y1 = e.getPositiveSum();
                    y2 = -e.getNegativeSum();

                } else {

                    Range range = e.getRanges()[high.getStackIndex()];

                    y1 = range.from;
                    y2 = range.to;
                }

            } else {
                y1 = e.getY();
                y2 = 0.f;
            }

            prepareBarHighlight(e.getX(), y1, y2, barData.getBarWidth() / 2f, trans);

            setHighlightDrawPos(high, mBarRect);
           // c.drawRect(mBarRect, mHighlightPaint);  原父类方法   
            //修改画笔的属性 是否圆头  根据mBarRect的矩形区域算出线的宽度和中心点画线
            mHighlightPaint.setStrokeCap(Paint.Cap.ROUND);
            mHighlightPaint.setStrokeWidth(mBarRect.right-mBarRect.left);
            c.drawLine(mBarRect.centerX(),mBarRect.top,mBarRect.centerX(),mBarRect.bottom,mHighlightPaint);
        }
    }
}



  • 0
    点赞
  • 4
    评论
  • 6
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

©️2021 CSDN 皮肤主题: 创作都市 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值