03.MPAndroidChart堆叠柱状图自定义显示MarkView

先上一张效果图

03.MPAndroidChart堆叠柱状图自定义显示MarkView_第1张图片

/**
 * 堆叠柱状图的mark_view
 */
@SuppressLint("ViewConstructor")
public class BarChartMarkView extends MarkerView {

    private TextView mDateTv;
    private BarData mBarData;
    private RecyclerView mMarkRv;
    private RelativeLayout mMarkRl;
    private List mDateList;
    private int[] mLineColorArr = {0xff45D6E1, 0xff675AAD, 0xffFB80D8, 0xff0e7e71, 0xff0066cc, 0xffFF8040, 0xffDD5246, 0xffF2350D};

    public BarChartMarkView(Context context, List dateList, BarData barData) {
        super(context, R.layout.multi_mark_view);
        mMarkRl = findViewById(R.id.mark_rl);
        mDateTv = findViewById(R.id.date_tv);
        mMarkRv = findViewById(R.id.mark_rv);
        mMarkRv.setLayoutManager(new LinearLayoutManager(context));
        this.mDateList = dateList;
        this.mBarData = barData;
    }

    private List setMarkData(BarEntry entry) {
        List markBeanList = new ArrayList<>();
        BarDataSet barDataSet = (BarDataSet) mBarData.getDataSetByIndex(0);
        for (int i = 0; i < barDataSet.getStackLabels().length; i++) {
            MarkBean bean = new MarkBean();
            bean.setName(barDataSet.getStackLabels()[i]);
            try {
                if (i < entry.getYVals().length) {
                    bean.setValue(entry.getYVals()[i] + "");
                } else {
                    break;
                }
            } catch (IndexOutOfBoundsException e) {
                LogUtil.e("-->" + e);
            }
            markBeanList.add(bean);
        }
        return markBeanList;
    }

    // runs every time the MarkerView is redrawn, can be used to update the
    // content (user-interface)
    @Override
    public void refreshContent(Entry entry, Highlight highlight) {
        try {
            mMarkRv.setAdapter(new MarkViewAdapter(setMarkData((BarEntry) entry)));
            mDateTv.setText(mDateList.get((int) entry.getX()));
        } catch (Exception e) {
            LogUtil.e("-->" + e);
        }
        super.refreshContent(entry, highlight);
    }

    @Override
    public MPPointF getOffset() {
        return new MPPointF(-(getWidth() / 2), -getHeight());
    }

    public void setMarkColor(int color) {
        int strokeWidth = 5;// 3dp 边框宽度
        int roundRadius = 15;// 8dp 圆角半径
        GradientDrawable gradientDrawable = new GradientDrawable();
//        gradientDrawable.setColor(Color.parseColor("#eeeeee"));
        gradientDrawable.setCornerRadius(roundRadius);
        gradientDrawable.setStroke(strokeWidth, color);
        mMarkRl.setBackground(gradientDrawable);
    }

    class MarkViewAdapter extends BaseQuickAdapter {

        MarkViewAdapter(@Nullable List data) {
            super(R.layout.adapter_multi_mark_view_item, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, MarkBean bean) {
            helper.setText(R.id.name_tv, bean.getName())
                    .setBackgroundColor(R.id.legend_bg, mLineColorArr[helper.getAdapterPosition()])
                    .setTextColor(R.id.name_tv, mLineColorArr[helper.getAdapterPosition()])
                    .setTextColor(R.id.value_tv, mLineColorArr[helper.getAdapterPosition()])
                    .setText(R.id.value_tv, bean.getValue());
        }
    }

    class MarkBean {
        private String name;
        private String value;

        private String getName() {
            return name;
        }

        private void setName(String name) {
            this.name = name;
        }

        private String getValue() {
            return value;
        }

        void setValue(String value) {
            this.value = value;
        }
    }

}

就是由原来简单的布局换成了一个RecyclerView去加载一个带有颜色的列表,看上去就好看了

你可能感兴趣的:(03.MPAndroidChart堆叠柱状图自定义显示MarkView)