电商_自定义柱形图

布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".activity.RectActivity">

    <com.example.myapplication.view.RectView
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</RelativeLayout>

Activity

package com.example.myapplication.activity;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import com.example.myapplication.R;

//自定义柱形图
public class RectActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rect);
    }
}

自定义View

package com.example.myapplication.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;


public class RectView extends View{
    private Paint mPaintTitle;

    private String[] mTitle={
            "管理成本","劳务成本","销售成本","资产盘亏"
    };

    private String[] mYTitle={
            "30.00%","25.00%","20.00%","15.00%","10.00%","5.00%","0.00%"
    };


    private float[] mPrice={
      27.64f,28.17f,21.48f,22.70f
    };
    private Paint mPaintRect;

    public RectView(Context context) {
        super(context);
        init(context);
    }

    public RectView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    //初始化
    private void init(Context context) {
        setBackgroundColor(Color.parseColor("#222222"));

        mPaintTitle=new Paint();
        mPaintTitle.setTextSize(56);
        mPaintTitle.setColor(Color.WHITE);
        mPaintTitle.setAntiAlias(true);
        mPaintTitle.setStrokeWidth(5);
        mPaintTitle.setTextAlign(Paint.Align.CENTER);

        mPaintRect=new Paint();
        mPaintRect.setColor(Color.WHITE);
        mPaintRect.setAntiAlias(true);
        mPaintRect.setStrokeWidth(100);

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        canvasTitle(canvas);

        canvasXY(canvas);

        canvasRect(canvas);
    }

    //绘制柱形图
    private void canvasRect(Canvas canvas) {
        float width=(getRight()-marginLeft*2)/4;//获取每一份的X宽度

        float height=(getBottom()-marginLeft*2);//获取整个Y轴的一个高度
        float heightY=height/30;//每一份所占的一个高度


        for (int a=0;a<mPrice.length;a++){
            float h=mPrice[a]*heightY;//通过返回的数据,计算这个数据所占的高度
            //以线的形式绘制柱形图
            canvas.drawLine(marginLeft+(width/2+width*a),getBottom()-h-marginLeft,marginLeft+(width/2+width*a),getBottom()-marginLeft,mPaintRect);
            //以矩形的形式绘制
//            Rect rect=new Rect();
        }

    }

    private float marginLeft=100;
    //绘制XY轴
    private void canvasXY(Canvas canvas) {

        //绘制X轴
        canvas.drawLine(marginLeft,getBottom()-marginLeft,
                getRight()-marginLeft,getBottom()-marginLeft,mPaintTitle);

        //绘制Y轴坐标
        canvas.drawLine(marginLeft,marginLeft,marginLeft,getBottom()-marginLeft,mPaintTitle);

        //绘制X轴的份数

        float width=(getRight()-marginLeft*2)/4;//获取每一份的X宽度

        mPaintTitle.setTextSize(26);
        for (int a=0;a<5;a++){
            //绘制X轴刻度值
            canvas.drawLine(marginLeft+(width*a),getBottom()-marginLeft,marginLeft+(width*a),getBottom()-marginLeft+20,mPaintTitle);
            //绘制X轴文字
            if(a<4){
                canvas.drawText(mTitle[a],marginLeft+(width/2+width*a),getBottom()-marginLeft/2,mPaintTitle);
            }
        }

        //绘制Y轴刻度值
        float height=(getBottom()-marginLeft*2)/6;//获取每一份的Y高度
        for (int a=0;a<7;a++){
            canvas.drawLine(marginLeft-20,marginLeft+(height*a),marginLeft,marginLeft+(height*a),mPaintTitle);

            //绘制Y轴文字
            canvas.drawText(mYTitle[a],marginLeft/2,marginLeft+height*a+10,mPaintTitle);
        }






    }

    //绘制标题
    private void canvasTitle(Canvas canvas) {
        mPaintTitle.setTextSize(56);
        canvas.drawText("成本费用占比",getRight()/2,80,mPaintTitle);
    }
}

效果图
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_43797842/article/details/88708481