美文网首页
自定义雷达图

自定义雷达图

作者: 小婷婷tt | 来源:发表于2019-05-27 16:27 被阅读0次

雷达图修改自网络https://github.com/jutao/AndroidNode/blob/master/2017/2017.3

效果图如下:

1

使用方法举例:

List datas =new ArrayList<>();

datas.add(new RadarView.ItemInfo("11",0));

datas.add(new RadarView.ItemInfo("22",0));

datas.add(new RadarView.ItemInfo("33",0));

datas.add(new RadarView.ItemInfo("44",0));

datas.add(new RadarView.ItemInfo("55",0));

viewHolder.myRadarview.setMaxValue(5);

viewHolder.myRadarview.setDatas(datas);

if (mTopList.size() !=0) {

viewHolder.myRadarview.updateDatas(mTopList);

}

以下为自定义RadarView

public class RadarView extends View {

private ContextmContext;

private static final StringTAG ="RadarView";

private Listdatas;

private float angle;

private float radius;

private float maxRadius;//网格最大半径

    private int centerX;//中心X

    private int centerY;//中心Y

    private float maxValue =100;//数据最大值

    private PaintmainPaint;//雷达区画笔

    private PaintvaluePaint;//数据区画笔

    private PainttextPaint;//文本画笔

    private PaintcirclePaint;//圆形画笔

    private int mainColor = Color.parseColor("#FD8BBA");

private int circleColor = Color.parseColor("#33ffffff");

private int defaultColor = Color.WHITE;

public void setDatas(List datas) {

this.datas = datas;

angle = (float) (Math.PI *2 / datas.size());

postInvalidate();

}

public void updateDatas(List datas) {

this.datas = datas;

angle = (float) (Math.PI *2 / datas.size());

postInvalidate();

}

/**

* 设置雷达图非白色的颜色

* main_color 雷达框框颜色

* circle_color 文字后面的圆形背景颜色

* default_color 雷达框内绘制区和绘制区圆点颜色

*/

    public void setBgColor(int main_color,int circle_color,int default_color) {

this.mainColor = main_color;

this.circleColor = circle_color;

this.defaultColor = default_color;

mainPaint.setColor(mainColor);

valuePaint.setColor(defaultColor);

circlePaint.setColor(circleColor);

}

public static class ItemInfo {

private Stringtitle;

private int value;

public ItemInfo(String title,int value) {

this.title = title;

this.value = value;

}

}

public RadarView(Context context) {

this(context,null,0);

}

public RadarView(Context context, AttributeSet attrs) {

this(context, attrs,0);

}

public RadarView(Context context, AttributeSet attrs,int defStyleAttr) {

super(context, attrs, defStyleAttr);

mContext = context;

init();

}

private void init() {

mainPaint =new Paint();//雷达区画笔

        mainPaint.setAntiAlias(true);

mainPaint.setStrokeWidth(ViewUtils.dip2px(mContext,1));

mainPaint.setColor(mainColor);

mainPaint.setStyle(Paint.Style.STROKE);

valuePaint =new Paint();//数据区画笔

        valuePaint.setAntiAlias(true);

valuePaint.setColor(defaultColor);

valuePaint.setStyle(Paint.Style.FILL);

textPaint =new Paint();//文本画笔

        textPaint.setAntiAlias(true);

textPaint.setColor(Color.WHITE);

datas =new ArrayList<>();

angle = (float) (Math.PI *2 /datas.size());//每个角的弧度

        circlePaint =new Paint();//圆形画笔

        circlePaint.setAntiAlias(true);

circlePaint.setStyle(Paint.Style.FILL);

circlePaint.setColor(circleColor);

}

@Override

    protected void onSizeChanged(int w,int h,int oldw,int oldh) {

radius = Math.min(h, w) /2 *0.34f;//雷达图半径

        maxRadius =radius / (datas.size() -1) *datas.size();

//中心坐标

        centerX = w /2;

centerY = h /2;

postInvalidate();

super.onSizeChanged(w, h, oldw, oldh);

}

@Override

    protected void onDraw(Canvas canvas) {

drawLines(canvas);

drawText(canvas);

drawRegion(canvas);

}

@Override

    protected void onMeasure(int widthMeasureSpec,int heightMeasureSpec) {

super.onMeasure(widthMeasureSpec, heightMeasureSpec);

int size =0;

int width = getMeasuredWidth();

int height = getMeasuredHeight();

if (width > height) {

size = height;

}else {

size = width;

}

setMeasuredDimension(size, size);

//        setMeasuredDimension(ViewUtils.getScreenWidth(mContext) / 375 * 267,ViewUtils.getScreenWidth(mContext) / 375 * 267);

    }

/**

* 绘制直线

*/

    private void drawLines(Canvas canvas) {

Path path =new Path();

Path pathOut =new Path();

for (int i =0; i

path.reset();

path.moveTo(centerX,centerY);

float x = (float) (centerX +maxRadius * Math.sin(angle * i));

float y = (float) (centerY -maxRadius * Math.cos(angle * i));

path.lineTo(x, y);

if (i ==0)

pathOut.moveTo(x, y);

else

                pathOut.lineTo(x, y);

canvas.drawPath(path,mainPaint);//画出中间分割线

        }

//画出最外层多边形

        pathOut.close();

canvas.drawPath(pathOut,mainPaint);

}

/**

* 绘制文字

* 其实四个象限,是以(centerX, centerY)为圆点的象限

*

    * @param canvas

    */

    private void drawText(Canvas canvas) {

for (int i =0; i

float rx = (float) (centerX +maxRadius *1.77 * Math.sin(angle * i));

float ry = (float) (centerY -maxRadius *1.77 * Math.cos(angle * i));

drawCircle(canvas, rx, ry);//画出文字外边圈

            textPaint.setTextAlign(Paint.Align.CENTER);

textPaint.setTextSize(ViewUtils.sp2px(getContext(),18));

canvas.drawText(String.valueOf(datas.get(i ).value), rx, ry -9,textPaint);

textPaint.setTextSize(ViewUtils.sp2px(getContext(),12));

canvas.drawText(String.valueOf(datas.get(i ).title), rx, ry +40,textPaint);

}

}

/*

*  绘制文字外的圆

*/

    private void drawCircle(Canvas canvas,float x,float y) {

canvas.drawCircle(x, y, ViewUtils.dip2px(mContext,31.5f),circlePaint);

}

/**

* 绘制中间有效区域

*

    * @param canvas

    */

    private void drawRegion(Canvas canvas) {

Path path =new Path();

for (int i =0; i

double percent =datas.get(i).value /maxValue;//计算该数值与最大值比例

//小圆点所在位置距离圆心的距离

            float x = (float) (centerX +maxRadius * Math.sin(angle * i) * percent);

float y = (float) (centerY -maxRadius * Math.cos(angle * i) * percent);

if (i ==0) {

path.moveTo(x, y);

}else {

path.lineTo(x, y);

}

valuePaint.setStyle(Paint.Style.FILL);

canvas.drawCircle(x, y, ViewUtils.dip2px(mContext,2),valuePaint);

}

path.close();

canvas.save();

valuePaint.setColor(defaultColor);

valuePaint.setStyle(Paint.Style.FILL);

valuePaint.setAlpha(90);

canvas.drawPath(path,valuePaint);

canvas.save();

valuePaint.setStrokeWidth(ViewUtils.dip2px(mContext,1));

valuePaint.setColor(defaultColor);

valuePaint.setStyle(Paint.Style.STROKE);

canvas.drawPath(path,valuePaint);

}

public float getMaxValue() {

return maxValue;

}

//设置最大数值

    public void setMaxValue(float maxValue) {

this.maxValue = maxValue;

}

//设置蜘蛛网颜色

    public void setMainPaintColor(int color) {

mainPaint.setColor(color);

}

//设置标题颜色

    public void setTextPaintColor(int color) {

textPaint.setColor(color);

}

//设置覆盖局域颜色

    public void setValuePaintColor(int color) {

valuePaint.setColor(color);

}

}

相关文章

  • 雷达图

    let option = {/* 标题 /title: {text: '自定义雷达图'},/ 说明图 /legen...

  • 自定义多边形统计图(雷达图)

    自定义雷达图 根据项目需求在图中画多条线 attrs文件 Point类

  • 自定义雷达图

    雷达图修改自网络https://github.com/jutao/AndroidNode/blob/master/...

  • Flutter雷达图package

    本文主要用Flutter实现雷达统计图,支持自定义颜色、形状、背景图等效果。大家可在github查看代码:http...

  • 自定义雷达图(CustomRadarCharView)

    模仿掌盟能力值分析效果 自定义雷达图(CustomRadarCharView) 引言: 今天是我电校庆日,甚是开心...

  • iOS自定义雷达图

    因为工作需要雷达图,有很多开源的也很好,但是代码太多了,太杂,只需要雷达图,所以就自己定义了 可设置多个边如图 使...

  • ECharts雷达图自定义

    最终效果和示例之间主要需要修改的两个大的地方: 不同颜色的圆弧,指标的值所处的范围是0到1(0%-100%);50...

  • R语言可视化(二十一):雷达图绘制

    21. 雷达图绘制 清除当前环境中的变量 设置工作目录 使用fmsb包绘制雷达图 使用ggradar包绘制雷达图

  • R实战| 雷达图(Radar Chart)

    R实战| 雷达图(Radar Chart) 雷达图(radar chart),又称蜘蛛网图(spider plot...

  • 数据雷达图

    最近公司项目,需要用到雷达图的效果,echarts的雷达图不太符合公司的需求,所以自己用canvas写了个雷达图的...

网友评论

      本文标题:自定义雷达图

      本文链接:https://www.haomeiwen.com/subject/pvgzbqtx.html