雷达图(Radar Chart) 是以从同一点开始的轴上表示的三个或更多个定量变量的二维图表的形式显示多变量数据的图形方法。轴的相对位置和角度通常是无信息的。 雷达图也称为网络图,蜘蛛图,星图,蜘蛛网图,不规则多边形,极坐标图或Kiviat图。它相当于平行坐标图,轴径向排列。
雷达图可以直观地对多维数据集目标对象的性能、优势及关键特征进行展示。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.Linq;
public class leidatu : MaskableGraphic
{
public int[] arr;
public float m = 1;
public override Texture mainTexture
{
get
{
if (material != null && material.mainTexture != null)
{
return material.mainTexture;
}
return s_WhiteTexture;
}
}
protected override void OnPopulateMesh(VertexHelper vh)
{
vh.Clear();
int n = arr.Length;
if (n < 3)
{
base.OnPopulateMesh(vh);
}
float w = rectTransform.sizeDelta.x;
float h = rectTransform.sizeDelta.y;
float max = arr.Max();
float p = w < h ? ((w / 2) / max) : ((h / 2) / max);
float ang = (Mathf.PI * 2) / n;
vh.AddVert(Vector3.up, Color.red, new Vector2(0.5f, 0.5f));
for (int i = 0; i < n; i++)
{
float x = Mathf.Sin(i * ang) * arr[i] * p;
float y = Mathf.Cos(i * ang) * arr[i] * p;
float ux = (x + w / 2) / w;
float uy = (y + h / 2) / h;
vh.AddVert(new Vector3(x, y, 0), Color.blue, new Vector2(ux, uy));
if (i == 0)
{
vh.AddTriangle(0, n, 1);
}
else
{
vh.AddTriangle(0, i, i + 1);
}
}
//描边
int next = arr.Length + 1;
for (int i = 0; i < n; i++)
{
//内顶点
float x0 = Mathf.Sin(i * ang) * (arr[i] * p - m);
float y0 = Mathf.Cos(i * ang) * (arr[i] * p - m);
float ux0 = (x0 + w / 2) / w;
float uy0 = (y0 + h / 2) / h;
vh.AddVert(new Vector3(x0, y0, 0), Color.white, new Vector2(ux0, uy0));
float x = Mathf.Sin(i * ang) * arr[i] * p;
float y = Mathf.Cos(i * ang) * arr[i] * p;
float ux = (x + w / 2) / w;
float uy = (y + h / 2) / h;
vh.AddVert(new Vector3(x, y, 0), Color.white, new Vector2(ux, uy));
if (i == arr.Length-1)
{
vh.AddTriangle(i * 2 + next, i * 2 + 1 + next, 1 + next);
vh.AddTriangle(i * 2 + next,1 + next, next);
}
else
{
vh.AddTriangle(i * 2 + next, i * 2 + 1 + next, (i + 1) * 2 + 1 + next);
vh.AddTriangle(i * 2 + next, (i + 1) * 2 + 1 + next, (i + 1) * 2 + next);
}
}
}
}