自定义view柱状图

2019-08-26  本文已影响0人  涂涂家的小七呀

记录一个简单的柱状图view,很简陋,数据可自定义

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;

import com.chuangdu.kotlin.R;
import com.chuangdu.kotlin.entity.HistogramDataElement;
import com.chuangdu.kotlin.entity.HistogramDatas;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by TU on 2019/8/26.
 * 柱状图
 */
public class CustHistogram extends View {
    private HistogramDatas histogramDatas;
    private String[] x_Group = {"1", "2", "3", "4", "5"};//x轴
    private String[] y_Group = {"10", "20", "30", "40", "50"};//y轴
    int[] BarHeight = {100, 200, 300, 400, 500};//相对的柱状高度
    public final static int[] platterTable = new int[]{R.color.colorAccent};
    private Paint myPaint;
    private int width;
    private int height;

    public CustHistogram(Context context) {
        super(context);
    }

    public CustHistogram(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CustHistogram(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        WindowManager wm = (WindowManager) this.getContext().getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics dm = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(dm);
        width = dm.widthPixels;
        height = dm.heightPixels - 760;
        Log.e("", width + "+ width");
        Log.e("height = ", height + "");
        //画背景
        myPaint = new Paint();
        myPaint.setStrokeWidth(2);
        myPaint.setColor(Color.WHITE);
        myPaint.setStrokeWidth(0);
    }

    public void setHistogramDatas(HistogramDatas histogramDatas) {
        this.histogramDatas = histogramDatas;
    }

    @Override
    public void draw(Canvas canvas) {
        int xOffset = (int) (width * 0.1);
        int yOffset = (int) (height * 0.1);

        myPaint.setColor(getResources().getColor(R.color.colorAccent));
        myPaint.setStrokeWidth(2);

        if (histogramDatas == null) {
            getMockUphistogramDatas();
        }
        int xPadding = 10;
        if (histogramDatas != null) {
            //添加柱状图数据
            int count = histogramDatas.getHistogramCount();
            int xUnit = (width - 2 - xOffset) / count;
            myPaint.setTextSize(40);
            String[] histogramDatasNames = histogramDatas.getHistogramKeys();
            for (int i = 0; i < x_Group.length; i++) {
                Log.e("星星数量", x_Group[i]);
                List<HistogramDataElement> itemList = histogramDatas.getItems(histogramDatasNames[i]);
                int barWidth = (int) ((xUnit / Math.pow(itemList.size(), 2))) / 2;
                int startPos = xOffset - 30 + xPadding + xUnit * i;
                canvas.drawText(x_Group[i], startPos - barWidth / 3 + barWidth / 4, (height - yOffset) / 2 + 180, myPaint);
            }

            // Y Axis markers
            float min = 0, max = 0;
            for (int i = 0; i < histogramDatasNames.length; i++) {
                List<HistogramDataElement> itemList = histogramDatas.getItems(histogramDatasNames[i]);
                if (itemList != null && itemList.size() > 0) {
                    for (HistogramDataElement item : itemList) {
                        if (item.getValue() > max) {
                            max = item.getValue();
                        }
                        if (item.getValue() < min) {
                            min = item.getValue();
                        }
                    }
                }
            }
            //X轴
            myPaint.setColor(getResources().getColor(R.color.colorPrimary));
            canvas.drawLine(0, (height - yOffset) / 2 + 130, width, (height - yOffset) / 2 + 130, myPaint);//xOffset+barWid
            myPaint.setColor(getResources().getColor(R.color.colorAccent));

            myPaint.setStyle(Paint.Style.FILL);
            myPaint.setStrokeWidth(0);
            int maxItem = 0;
            for (int i = 0; i < histogramDatasNames.length; i++) {
                List<HistogramDataElement> itemList = histogramDatas.getItems(histogramDatasNames[i]);
                int barWidth = (int) ((xUnit / Math.pow(itemList.size(), 2))) / 2;
                int startPos = xOffset - 30 + xPadding + xUnit * i;
                int index = 0;
                int interval = barWidth / 2;
                if (itemList.size() > maxItem) {
                    maxItem = itemList.size();
                }
                int barHeight = BarHeight[i];
                myPaint.setColor(getResources().getColor(R.color.colorPrimaryDark));
                myPaint.setTextSize(40);
                canvas.drawText(y_Group[i], startPos + barWidth * index + interval * index - barWidth / 3 + barWidth / 4, (height - yOffset - barHeight) / 2 + 125, myPaint);
                myPaint.setColor(getResources().getColor(R.color.colorPrimary));
                canvas.drawRect(startPos + barWidth * index + interval * index - barWidth / 4, (height + 65 - yOffset - barHeight) / 2 + 100, startPos + barWidth * index + interval * index + barWidth - barWidth / 4, (height + 65 - yOffset) / 2 + 100, myPaint);
                index++;
            }

        }
        super.draw(canvas);
    }

   /**
     * 模拟数据
     */
    public void getMockUphistogramDatas() {
        histogramDatas = new HistogramDatas();
        for (int i = 1; i <= 5; i++) {
            List<HistogramDataElement> itemListOne = new ArrayList<>();
            itemListOne.add(new HistogramDataElement("", i * 10.0f, platterTable[0]));
            histogramDatas.addSeries(i , itemListOne);
        }
    }
}

代码直接cv就可以运行,测试数据已经写好了,基本的实体类就不贴了,根据自己需求扩展吧。

上一篇下一篇

猜你喜欢

热点阅读