雷达图的绘制(绘制圆的拓展)

using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.UI;
/// <summary>
/// 雷达图 And 圆
/// 提示:
/// 1.如果要挂载在image上,要删除image组件
/// 2.如果要挂载在空对象上,要加上CanvasRender组件
/// </summary>
public class ReadMap : MaskableGraphic
{
public Color outColor = Color.cyan;
public Color inColor = Color.red;
//这是雷达图的数据
public float[] arr;
// Start is called before the first frame update
/// <summary>
/// 重写绘制放法
/// </summary>
/// <param name="vh"></param>
protected override void OnPopulateMesh(VertexHelper vh)
{
//首先要清理脏数据
vh.Clear();
//计算分的角度个数
int num = arr.Length;
if(num >= 3)
{
//获取对象的宽高,用对象较短的一边作为雷达图的最大值(即半径)
Rect rect = this.rectTransform.rect;
float r = rect.width < rect.height ? rect.width / 2 : rect.height / 2;
//Linp语句求最大值和半径做比 求出他的比值
float p = r / arr.Max();//(max是Linp语句)
//计算每个角度大小
float ang = 2 * Mathf.PI / num;
//添加中心点
vh.AddVert(Vector3.zero, outColor, new Vector2(0.5f, 0.5f));
for (int i = 0; i < num; i++)
{
//计算坐标(*arr【i】是为了控制每个值得大小 *p是为了让他以比例的方式显示,不至于超出太多)
float x = Mathf.Sin(i * ang) * arr[i] * p;
float y = Mathf.Cos(i * ang) * arr[i] * p;
//计算uv坐标
float uvx = (x + r) / (r + r);
float uvy = (y + r) / (r + r);
//添加顶点
vh.AddVert(new Vector3(x, y, 0), inColor, new Vector2(uvx, uvy));
//绘制圆(雷达图)
if (i == 0)
{
//首尾相连
vh.AddTriangle(0, num, 1);
}
else
{
vh.AddTriangle(0, i, i + 1);
}
}
}
}
}

 

posted @ 2023-02-14 14:39  old_Host  阅读(90)  评论(0编辑  收藏  举报